2017-04-24 31 views
3

我想在Python中使用bioformats讀取顯微鏡圖像(.lsm,.czi,.lif ,你將其命名),打印出元數據並顯示圖像。 ome = bf.OMEXML(md)給我一個錯誤(下面)。我想這是關於存儲在md內的信息。它不喜歡md中的信息不全是ASCII。但是,我如何克服這個問題呢? 這是我寫的:Bioformats-Python錯誤:'ascii'編解碼器無法編碼字符u' xb5'當使用OMEXML()

import Tkinter as Tk, tkFileDialog 
import os 
import javabridge as jv 
import bioformats as bf 
import matplotlib.pyplot as plt 
import numpy as np 

jv.start_vm(class_path=bf.JARS, max_heap_size='12G') 

用戶選擇文件中numpy的陣列

raw_data = [] 
    for z in range(iome.Pixels.get_SizeZ()): 
    raw_image = reader.read(z=z, series=0, rescale=False) 
    raw_data.append(raw_image) 
raw_data = np.array(raw_data) 

展上展出了元數據與

#hiding root alllows file diaglog GUI to be shown without any other GUI elements 
root = Tk.Tk() 
root.withdraw() 
file_full_path = tkFileDialog.askopenfilename() 
filepath, filename = os.path.split(file_full_path) 
os.chdir(os.path.dirname(file_full_path)) 

print('opening: %s' %filename) 
reader = bf.ImageReader(file_full_path) 
md = bf.get_omexml_metadata(file_full_path) 
ome = bf.OMEXML(md) 

認沽圖像工作

iome = ome.image(0) # e.g. first image 
print(iome.get_Name()) 
print(iome.Pixels.get_SizeX()) 
print(iome.Pixels.get_SizeY()) 

這裏的e RROR我得到:

--------------------------------------------------------------------------- 
UnicodeEncodeError      Traceback (most recent call last) 
<ipython-input-22-a22c1dbbdd1e> in <module>() 
    11 reader = bf.ImageReader(file_full_path) 
    12 md = bf.get_omexml_metadata(file_full_path) 
---> 13 ome = bf.OMEXML(md) 

/anaconda/envs/env2_bioformats/lib/python2.7/site-packages/bioformats/omexml.pyc in __init__(self, xml) 
    318   if isinstance(xml, str): 
    319    xml = xml.encode("utf-8") 
--> 320   self.dom = ElementTree.ElementTree(ElementTree.fromstring(xml)) 
    321 
    322   # determine OME namespaces 

<string> in XML(text) 

UnicodeEncodeError: 'ascii' codec can't encode character u'\xb5' in position 1623: ordinal not in range(128) 

這裏有一個代表test image專有顯微鏡格式

+1

你可以添加上傳一個示例圖像嗎? –

+0

@MaximilianPeters,我剛剛添加了一個.lsm文件進行測試。任何建議,將不勝感激。謝謝! – puifais

回答

1

感謝您加入樣本圖像。這非常有幫助!

讓我們先刪除所有不必要的Tkinter代碼,直到我們得到一個允許我們重現錯誤消息的Minimal, Complete and Verifiable Example

import javabridge as jv 
import bioformats as bf 

jv.start_vm(class_path=bf.JARS, max_heap_size='12G') 

file_full_path = '/path/to/Cell1.lsm' 

md = bf.get_omexml_metadata(file_full_path) 

ome = bf.OMEXML(md) 

jv.kill_vm() 

我們先了解一下3i SlideBook SlideBook6Reader library not found一些警告信息,但我們認爲can apparently ignore

你的錯誤信息讀取UnicodeEncodeError: 'ascii' codec can't encode character u'\xb5' in position 1623: ordinal not in range(128),所以讓我們看看有什麼我們可以發現周圍的位置1623

如果添加print mdmd = bf.get_omexml_metadata(file_full_path)後,用元數據的整個XML被打印出來。讓我們放大:

>>> print md[1604:1627] 
PhysicalSizeXUnit="µm" 

所以,µ字符是罪魁禍首,它不能與'ascii' codec編碼。

回首回溯:

/anaconda/envs/env2_bioformats/lib/python2.7/site-packages/bioformats/omexml.pyc in __init__(self, xml) 
    318   if isinstance(xml, str): 
    319    xml = xml.encode("utf-8") 
--> 320   self.dom = ElementTree.ElementTree(ElementTree.fromstring(xml)) 
    321 
    322   # determine OME namespaces 

我們看到,在線路發生錯誤之前,我們編碼我們xmlutf-8,應該解決我們的問題。那爲什麼不發生?

如果我們添加print type(md),我們會返回<type 'unicode'>而不是<type 'str'>作爲代碼預期。所以這是omexml.py中的一個錯誤!

要解決此問題,請執行以下操作(可能需要root用戶);

  • 轉到/anaconda/envs/env2_bioformats/lib/python2.7/site-packages/bioformats/
  • 除去omexml.pyc
  • omexml.py變更線318從isinstance(xml, str):if isinstance(xml, basestring):

basestringstrunicode超類。它用於測試對象是否爲strunicode的實例。

我想爲此提交一個錯誤,但它似乎已經有一個open issue

+1

非常感謝你! (哦,你的生物給了我一個笑聲:)) – puifais

相關問題