使用Python上的Bio-Formats保存3D +时间(XYCZT)堆栈

时间:2015-09-03 09:14:50

标签: python python-2.7 numpy metadata tiff

使用Bioformats标准从Python中的Numpy数组中保存3D + Time Tiff文件时遇到了一些麻烦。

首先,我有一个5维的numpy数组,在我的情况下订购了XYCZT(267,518,1,331,3)。

这是我到目前为止的功能:

# Write file to disk
def write(img_XYCZT, path, type='uint16', verbose=True):

import bioformats.omexml as ome
import javabridge as jutil
import bioformats
import numpy as np
import os
import sys

if verbose:
    print('Dimensions (XYCZT): ' + str(np.shape(img_XYCZT)))
    sys.stdout.flush()

# Get the new dimensions
SizeX = np.shape(img_XYCZT)[0]
SizeY = np.shape(img_XYCZT)[1]
SizeC = np.shape(img_XYCZT)[2]
SizeZ = np.shape(img_XYCZT)[3]
SizeT = np.shape(img_XYCZT)[4]

# Start JVM for bioformats
jutil.start_vm(class_path=bioformats.JARS)

# Getting metadata info
omexml = ome.OMEXML()
omexml.image(0).Name = os.path.split(path)[1]
p = omexml.image(0).Pixels
assert isinstance(p, ome.OMEXML.Pixels)
p.SizeX = SizeX
p.SizeY = SizeY
p.SizeC = SizeC
p.SizeT = SizeT
p.SizeZ = SizeZ
p.DimensionOrder = ome.DO_XYCZT
p.PixelType = type
p.channel_count = SizeC
p.plane_count = SizeZ
p.Channel(0).SamplesPerPixel = SizeC
omexml.structured_annotations.add_original_metadata(ome.OM_SAMPLES_PER_PIXEL, str(SizeC))

# Converting to omexml
xml = omexml.to_xml()

# Write file using Bioformats
if verbose:
    print ('Writing frames:'),
    sys.stdout.flush()

for frame in range(SizeT):
    if verbose:
        print('[' + str(frame + 1) + ']'),
        sys.stdout.flush()

    index = frame

    pixel_buffer = bioformats.formatwriter.convert_pixels_to_buffer(img_XYCZT[:, :, :, :, frame], type)

    script = """
    importClass(Packages.loci.formats.services.OMEXMLService,
                Packages.loci.common.services.ServiceFactory,
                Packages.loci.formats.out.TiffWriter);

    var service = new ServiceFactory().getInstance(OMEXMLService);
    var metadata = service.createOMEXMLMetadata(xml);
    var writer = new TiffWriter();
    writer.setBigTiff(true);
    writer.setMetadataRetrieve(metadata);
    writer.setId(path);
    writer.setInterleaved(true);
    writer.saveBytes(index, buffer);
    writer.close();
    """
    jutil.run_script(script, dict(path=path, xml=xml, index=index, buffer=pixel_buffer))

if verbose:
    print ('[Done]')
    sys.stdout.flush()

if verbose:
    print('File saved on ' + str(path))
    sys.stdout.flush()

img_XYCZT numpy数组,路径保存文件的位置。该功能可能会为元数据使用大量冗余,但那就是我为它的工作而奋斗......

在斐济检查保存的文件,Z信息为C通道: special entry in the index of the parent repo

该文件根本没有Z维...我已经在这方面苦苦挣扎了一段时间,非常感谢任何帮助!

由于

0 个答案:

没有答案