2017-04-24 1 views
3

현미경 이미지 (.lsm, .czi, .lif)로 읽기 위해 Python에서 bioformats를 사용하려고합니다. , 당신은 그것의 이름을 짓는다), 메타 데이터를 출력하고, 이미지를 보여준다. ome = bf.OMEXML(md)에서 오류가 발생합니다 (아래). 나는 그것이 md 안에 저장된 정보에 대해 이야기하고 있다고 생각합니다. md에있는 정보가 모두 ASCII가 아닌 것을 좋아합니다. 그러나이 문제를 어떻게 극복 할 수 있습니까? 이 내가 쓴 것입니다 :Bioformats-Python 오류 : OMEXML()을 사용할 때 'ascii'코덱이 u ' xb5'문자를 인코딩 할 수 없습니다.

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') 

사용자가

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

는 여기

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) 

보기 메타 데이터 원 NumPy와 배열에

#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) 

넣고 이미지와 함께 작업 할 파일을 선택 전자 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) 

여기

+1

하나의 예제 이미지를 업로드 할 수 있습니까? –

+0

@ MaximilianPeters, 테스트 용으로 .lsm 파일을 추가했습니다. 모든 제안을 부탁드립니다. 감사! – puifais

답변

1

샘플 이미지를 추가 주셔서 감사합니다 독점 현미경 형식의 대표 test image입니다. 그것은 대단히 도움이되었다!

오류 메시지를 재현 할 수있는 Minimal, Complete and Verifiable Example이 생길 때까지 먼저 불필요한 Tkinter 코드를 모두 제거해 보겠습니다.

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)를 읽고, 그래서 당신은 print mdmd = bf.get_omexml_metadata(file_full_path) 후, 메타 데이터가 전체 XML이 인쇄됩니다 추가하는 경우의 우리 위치 1623

주위를 찾을 수 있는지 살펴 보자. 의는 확대하자

>>> 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의 버그!

이 문제를 해결하려면 다음을 수행하십시오 (루트 여야 함). /anaconda/envs/env2_bioformats/lib/python2.7/site-packages/bioformats/

    • 이동 isinstance(xml, str):에서 if isinstance(xml, basestring):

    basestringstrunicode의 슈퍼 클래스입니다에 omexml.py 변화 라인 (318)에 omexml.pyc

  • 를 제거합니다. 객체가 str 또는 unicode의 인스턴스인지 여부를 테스트하는 데 사용됩니다.

    버그를 신고하고 싶지만 이미 open issue 인 것 같습니다.

  • +1

    정말 고마워요! (오, 당신의 생체 내 웃음 준 :)) – puifais

    관련 문제