跳转到主要内容

OME数据模型的Python数据类

项目描述

ome-types

License Version CondaVersion Python Version Tests Docs codecov Benchmarks

OME数据模型的纯Python实现

ome_types 提供了一组Python数据类和实用函数,用于将 OME-XML格式 解析成完全类型化的Python对象,以便在Python中进行交互式或程序性访问。它还可以将这些Python对象输出为有效的OME-XML。 ome_types 是一个 纯Python 库,不需要Java虚拟机。

注意:生成的Python代码可以在 built 分支 中看到。(有关详细信息,请参阅 代码生成 部分)。

📖   文档

安装

通过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_xmlfrom_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 (122.1 kB 查看哈希值)

上传时间 源代码

构建分发

ome_types-0.5.2-py3-none-any.whl (248.2 kB 查看哈希值)

上传时间 Python 3

支持者