OME数据模型的Python数据类
项目描述
ome-types
OME数据模型的纯Python实现
ome_types
提供了一组Python数据类和实用函数,用于将 OME-XML格式 解析成完全类型化的Python对象,以便在Python中进行交互式或程序性访问。它还可以将这些Python对象输出为有效的OME-XML。 ome_types
是一个 纯Python 库,不需要Java虚拟机。
📖 文档
安装
通过pip
pip install ome-types
包含所有可选依赖项
# lxml => if you ...
# - want to use lxml as the XML parser
# - want to validate XML against the ome.xsd schema
# - want to use XML documents older than the 2016-06 schema
# pint => if you want to use object.<field>_quantity properties
# xmlschema => if you want to validate XML but DON'T want lxml
pip install ome-types[lxml,pint]
通过conda
conda install -c conda-forge ome-types
从github(最新开发版本)
pip install git+https://github.com/tlambert03/ome-types.git
用法
将XML字符串或文件路径转换为 ome_types.model.OME
实例
(XML字符串/文件将根据ome.xsd模式进行验证)
from ome_types import from_xml
ome = from_xml('tests/data/hcs.ome.xml')
从OME-TIFF中提取OME元数据
from ome_types import from_tiff
ome2 = from_tiff('tests/data/ome.tiff')
通过Python对象操作元数据
from_xml
和from_tiff
都返回ome_types.model.OME
的一个实例。在ome_types.model
中的所有类都遵循OME数据模型的命名约定,但使用snake_case
属性名代替CamelCase
,以与Python生态系统保持一致。
In [2]: ome = from_xml('tests/data/hcs.ome.xml')
In [3]: ome
Out[3]:
OME(
images=[<1 Images>],
plates=[<1 Plates>],
)
In [4]: ome.plates[0]
Out[4]:
Plate(
id='Plate:1',
name='Control Plate',
column_naming_convention='letter',
columns=12,
row_naming_convention='number',
rows=8,
wells=[<1 Wells>],
)
In [5]: ome.images[0]
Out[5]:
Image(
id='Image:0',
name='Series 1',
pixels=Pixels(
id='Pixels:0',
dimension_order='XYCZT',
size_c=3,
size_t=16,
size_x=1024,
size_y=1024,
size_z=1,
type='uint16',
bin_data=[<1 Bin_Data>],
channels=[<3 Channels>],
physical_size_x=0.207,
physical_size_y=0.207,
time_increment=120.1302,
),
acquisition_date=datetime.fromisoformat('2008-02-06T13:43:19'),
description='An example OME compliant file, based on Olympus.oib',
)
可以删除或更改对象
In [6]: from ome_types.model.simple_types import UnitsLength
In [7]: from ome_types.model.channel import AcquisitionMode
In [8]: ome.images[0].description = "This is the new description."
In [9]: ome.images[0].pixels.physical_size_x = 350.0
In [10]: ome.images[0].pixels.physical_size_x_unit = UnitsLength.NANOMETER
In [11]: for c in ome.images[0].pixels.channels:
c.acquisition_mode = AcquisitionMode.SPINNING_DISK_CONFOCAL
可以通过构建新的OME模型对象来添加元素
In [12]: from ome_types.model import Instrument, Microscope, Objective, InstrumentRef
In [13]: microscope_mk4 = Microscope(
manufacturer='OME Instruments',
model='Lab Mk4',
serial_number='L4-5678',
)
In [14]: objective_40x = Objective(
manufacturer='OME Objectives',
model='40xAir',
nominal_magnification=40.0,
)
In [15]: instrument = Instrument(
microscope=microscope_mk4,
objectives=[objective_40x],
)
In [16]: ome.instruments.append(instrument)
In [17]: ome.images[0].instrument_ref = InstrumentRef(id=instrument.id)
In [18]: ome.instruments
Out[18]:
[Instrument(
id='Instrument:1',
microscope=Microscope(
manufacturer='OME Instruments',
model='Lab Mk4',
serial_number='L4-5678',
),
objectives=[<1 Objectives>],
)]
导出为OME-XML字符串
最后,您可以生成OME模型对象的OME-XML表示,用于写入独立的.ome.xml
文件或将它插入到OME-TIFF文件的头部
In [19]: from ome_types import to_xml
In [20]: print(to_xml(ome))
<OME ...>
<Plate ColumnNamingConvention="letter" Columns="12" ID="Plate:1" ...>
...
</Plate>
<Instrument ID="Instrument:1">
<Microscope Manufacturer="OME Instruments" Model="Lab Mk4" SerialNumber="L4-5678" />
<Objective Manufacturer="OME Objectives" Model="40xAir" ID="Objective:1"
NominalMagnification="40.0" />
</Instrument>
<Image ID="Image:0" Name="Series 1">
<AcquisitionDate>2008-02-06T13:43:19</AcquisitionDate>
<Description>This is the new description.</Description>
<InstrumentRef ID="Instrument:1" />
<Pixels ... PhysicalSizeX="350.0" PhysicalSizeXUnit="nm" ...>
<Channel AcquisitionMode="SpinningDiskConfocal" ...>
...
</Pixels>
</Image>
</OME>
代码生成
这个库的大部分内容(即在ome_types._autogenerated
内部的模块)在安装时生成,因此不会提交到源代码(或在主分支中可见)。
您可以在构建分支中看到主分支生成的代码
src/ome_autogen
目录中的包将ome.xsd模式转换为有效的Python代码。要在开发环境中运行代码生成脚本,请克隆此仓库并执行
python -m src.ome_autogen
完整模型的文档和类型可以在API参考中找到
贡献
要本地克隆和安装此仓库
git clone https://github.com/tlambert03/ome-types.git
cd ome-types
pip install -e .[test,dev]
我们在持续集成期间使用pre-commit
来运行各种代码质量检查。如果您想在提交代码之前确保您的代码通过这些检查,您应该在克隆此仓库后安装pre-commit
pre-commit install
重新生成模型
如果您修改了src/ome_autogen
中的任何内容,您可能需要使用以下命令重新生成模型
python -m src.ome_autogen
运行测试
要运行测试
pytest
项目详情
下载文件
下载您平台的文件。如果您不确定该选择哪一个,请了解有关 安装包 的更多信息。
源代码分发
构建分发
ome_types-0.5.2.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | c275a1efcd0ab4e16a067f23443bb5a6edecf53943205326e64ada3ec66d4ed9 |
|
MD5 | a9d66f823b010c7327a16be4b2344725 |
|
BLAKE2b-256 | 088752085ca3830bcd28a7e2681678a251bf30179c5329e2290b2de9f52bbb37 |
ome_types-0.5.2-py3-none-any.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 5aeb8ff458b6180a64422398df9a0ade97f251b990179b53ba9ece61a471c03c |
|
MD5 | 32960ad98571f4489c1c5ec582af8c6d |
|
BLAKE2b-256 | 1f0e057f0c3e77d2875ac8562be4efd12d7ef35a1083a6840167cb0fc7c17be7 |