CellProfiler / python-bioformats

Read and write life sciences file formats
Other
129 stars 45 forks source link

Setting AcquiredTime only works for first image #38

Open hackermd opened 8 years ago

hackermd commented 8 years ago

Setting the "AquiredDate" attribute for Image elements works only for the first image

import bioformats

metadata = bioformats.OMEXML()
metadata.image_count = 10
for i in xrange(metadata.image_count):
    metadata.image(i).AcquiredDate = bioformats.omexml.xsd_now()
metadata.image(0).AcquiredDate
type(metadata.image(0).AcquiredDate)
'2015-10-11T23:01:17.237239'
str

For all other images the value is None.

metadata.image(1).AcquiredDate
type(metadata.image(1).AcquiredDate)

NoneType
hackermd commented 8 years ago

I delved a bit deeper into the above provided example:

Adding an "AcquiredDate" attribute to an Image element metadata.image(n).AcquiredDate, where n > 0, also adds a weird new namespace "ns4" to the XML: xmlns:ns4="{\'sa\': None, \'ome\': \'http://www.openmicroscopy.org/Schemas/ome/2013-06s\', \'spw\': None}".

Here is an example:

import bioformats

metadata = bioformats.OMEXML()
metadata.image(0).AcquiredDate = "2013-10-24T11:52:33+01:00"
metadata.to_xml()

gives

'<ome:OME xmlns:ns2="%(NS_BINARY_FILE)s" xmlns:ome="http://www.openmicroscopy.org/Schemas/ome/2013-06s" xmlns:sa="http://www.openmicroscopy.org/Schemas/sa/2013-06s" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.openmicroscopy.org/Schemas/OME/2013-06 http://www.openmicroscopy.org/Schemas/OME/2012-03/ome.xsd">\n  <ome:Image ID="Image:0" Name="default.png">\n    <ome:AcquiredDate>2013-10-24T11:52:33+01:00</ome:AcquiredDate>\n    <ome:Pixels DimensionOrder="XYCTZ" ID="Pixels:0" SizeC="1" SizeT="1" SizeX="512" SizeY="512" SizeZ="1" Type="uint8">\n<ome:Channel ID="Channel:0:0" SamplesPerPixel="1">\n        <ome:LightPath />\n      </ome:Channel>\n      <ns2:BinData BigEndian="false" Length="0" />\n    </ome:Pixels>\n  </ome:Image>\n  <sa:StructuredAnnotations />\n</ome:OME>'

adding an additional image element and setting the "AcquiredDate" attribute

metadata.image_count = 2
metadata.image(1).AcquiredDate = "2013-10-24T11:52:33+01:00"
metadata.to_xml()

gives

'<ome:OME xmlns:ns2="%(NS_BINARY_FILE)s" xmlns:ns4="{\'sa\': None, \'ome\': \'http://www.openmicroscopy.org/Schemas/ome/2013-06s\', \'spw\': None}" xmlns:ome="http://www.openmicroscopy.org/Schemas/ome/2013-06s" xmlns:sa="http://www.openmicroscopy.org/Schemas/sa/2013-06s" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.openmicroscopy.org/Schemas/OME/2013-06 http://www.openmicroscopy.org/Schemas/OME/2012-03/ome.xsd">\n  <ome:Image ID="Image:0" Name="default.png">\n    <ome:AcquiredDate>2013-10-24T11:52:33+01:00</ome:AcquiredDate>\n    <ome:Pixels DimensionOrder="XYCTZ" ID="Pixels:0" SizeC="1" SizeT="1" SizeX="512" SizeY="512" SizeZ="1" Type="uint8">\n<ome:Channel ID="Channel:0:0" SamplesPerPixel="1">\n        <ome:LightPath />\n      </ome:Channel>\n      <ns2:BinData BigEndian="false" Length="0" />\n    </ome:Pixels>\n  </ome:Image>\n  <sa:StructuredAnnotations />\n<ome:Image ID="d85cd094-6747-432e-971d-bb288d8da4aa" Name="default.png"><ns4:AcquiredDate>2015-10-12T23:01:05.565919</ns4:AcquiredDate><ome:Pixels DimensionOrder="XYCTZ" ID="075d9d9c-757e-4f5d-a798-5aab4c2300e5" SizeC="1" SizeT="1" SizeX="512" SizeY="512" SizeZ="1" Type="uint8"><ome:Channel ID="Channel2:0" Name="Channel2:0" SamplesPerPixel="1" /></ome:Pixels><ns4:AcquiredDate>2013-10-24T11:52:33+01:00</ns4:AcquiredDate></ome:Image></ome:OME>'

As a side note, I just realized that the OME model doesn't have an "AcquiredDate" attribute. It's called "AcquisitionDate".

hackermd commented 8 years ago

I traced down the bug and send a pull request.

hackermd commented 8 years ago

Thanks for merging the pull request into the master branch! Could you please also make the changes available on pypi!? Would make things easier for me..