python - 生物格式-Python 错误 : 'ascii' codec can't encode character u'\xb5' when using OMEXML()

标签 python xml ascii bioinformatics biopython

我正在尝试使用 Python 中的生物格式来读取显微镜图像(.lsm、.czi、.lif,随便你怎么说),打印出元数据,然后显示图像。 ome = bf.OMEXML(md) 给我一个错误(如下)。我认为它是在谈论存储在 md 中的信息。它不喜欢 md 中的信息不全是 ASCII。但是我该如何克服这个问题呢? 这是我写的:

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

用户选择要使用的文件

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

将图片放入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)

显示需要的元数据

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

这是我得到的错误:

---------------------------------------------------------------------------
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具有专有显微镜格式

最佳答案

感谢您添加示例图片。这帮了大忙!

让我们首先删除所有不必要的 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

我们看到在错误发生之前的行中,我们对 xml 进行了编码至 utf-8 ,那应该可以解决我们的问题。那为什么没有发生呢?

如果我们添加 print type(md)我们回来了<type 'unicode'>而不是 <type 'str'>正如代码预期的那样.. 所以这是 omexml.py 中的错误!

要解决这个问题,请执行以下操作(您可能需要是 root);

  • 转到 /anaconda/envs/env2_bioformats/lib/python2.7/site-packages/bioformats/
  • 删除omexml.pyc
  • omexml.pyisinstance(xml, str): 更改第 318 行至 if isinstance(xml, basestring):

basestring str 的父类(super class)和 unicode .用于测试一个对象是否是str的实例或 unicode .

我想为此提交一个错误,但似乎已经有一个 open issue .

关于python - 生物格式-Python 错误 : 'ascii' codec can't encode character u'\xb5' when using OMEXML(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43598709/

相关文章:

xml - XSL递归轴故障?

r - 在 R 中处理 ASCII

javascript - 如何将ASCII码字符串转换为字符串?

python - 使用 pip 安装 mysqlclient 时出错,找不到 -lssl 的库

python - 如何使用 Beautiful Soup 以正确的顺序提取数据

python - NoReverseMatch at/home/u'home' 不是已注册的命名空间

python - 有没有办法为 python 枚举指定默认值?

c# - 如何防止 XXE 攻击(.NET 中的 XmlDocument)

xml - xslt中for循环的使用方法

colors - 如何在控制台中使用 ANSI 转义序列将斜体或粗体等格式应用于文本?