forked from CellProfiler/python-bioformats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathomexml.py
1732 lines (1377 loc) · 59.3 KB
/
omexml.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Python-bioformats is distributed under the GNU General Public
# License, but this file is licensed under the more permissive BSD
# license. See the accompanying file LICENSE for details.
#
# Copyright (c) 2009-2014 Broad Institute
# All rights reserved.
"""omexml.py read and write OME xml
"""
from __future__ import absolute_import, unicode_literals
import xml.etree.ElementTree
from xml.etree import cElementTree as ElementTree
import sys
if sys.version_info.major == 3:
from io import StringIO
uenc = 'unicode'
else:
from cStringIO import StringIO
uenc = 'utf-8'
import datetime
import logging
from functools import reduce
logger = logging.getLogger(__file__)
import re
import uuid
def xsd_now():
'''Return the current time in xsd:dateTime format'''
return datetime.datetime.now().isoformat()
DEFAULT_NOW = xsd_now()
#
# The namespaces
#
NS_BINARY_FILE = "http://www.openmicroscopy.org/Schemas/BinaryFile/2013-06"
NS_ORIGINAL_METADATA = "openmicroscopy.org/OriginalMetadata"
NS_DEFAULT = "http://www.openmicroscopy.org/Schemas/{ns_key}/2013-06"
NS_RE = r"http://www.openmicroscopy.org/Schemas/(?P<ns_key>.*)/[0-9/-]"
default_xml = """<?xml version="1.0" encoding="UTF-8"?>
<!-- Warning: this comment is an OME-XML metadata block, which contains
crucial dimensional parameters and other important metadata. Please edit
cautiously (if at all), and back up the original data before doing so.
For more information, see the OME-TIFF documentation:
https://docs.openmicroscopy.org/latest/ome-model/ome-tiff/ -->
<OME xmlns="http://www.openmicroscopy.org/Schemas/OME/2016-06"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.openmicroscopy.org/Schemas/OME/2016-06 http://www.openmicroscopy.org/Schemas/OME/2016-06/ome.xsd">
<Image ID="Image:0" Name="default.png">
<AcquisitionDate>%(DEFAULT_NOW)s</AcquisitionDate>
<Pixels BigEndian="false"
DimensionOrder="XYCZT"
ID="Pixels:0"
Interleaved="false"
SizeC="1"
SizeT="1"
SizeX="512"
SizeY="512"
SizeZ="1"
Type="uint8">
<Channel ID="Channel:0:0" SamplesPerPixel="1">
<LightPath/>
</Channel>
</Pixels>
</Image>
<StructuredAnnotations xmlns="{ns_sa_default}s"/>
</OME>""".format(ns_ome_default=NS_DEFAULT.format(ns_key='ome'), ns_sa_default=NS_DEFAULT.format(ns_key='sa'))
#
# These are the OME-XML pixel types - not all supported by subimager
#
PT_INT8 = "int8"
PT_INT16 = "int16"
PT_INT32 = "int32"
PT_UINT8 = "uint8"
PT_UINT16 = "uint16"
PT_UINT32 = "uint32"
PT_FLOAT = "float"
PT_BIT = "bit"
PT_DOUBLE = "double"
PT_COMPLEX = "complex"
PT_DOUBLECOMPLEX = "double-complex"
#
# The allowed dimension types
#
DO_XYZCT = "XYZCT"
DO_XYZTC = "XYZTC"
DO_XYCTZ = "XYCTZ"
DO_XYCZT = "XYCZT"
DO_XYTCZ = "XYTCZ"
DO_XYTZC = "XYTZC"
#
# Original metadata corresponding to TIFF tags
# The text for these can be found in
# loci.formats.in.BaseTiffReader.initStandardMetadata
#
'''IFD # 254'''
OM_NEW_SUBFILE_TYPE = "NewSubfileType"
'''IFD # 256'''
OM_IMAGE_WIDTH = "ImageWidth"
'''IFD # 257'''
OM_IMAGE_LENGTH = "ImageLength"
'''IFD # 258'''
OM_BITS_PER_SAMPLE = "BitsPerSample"
'''IFD # 262'''
OM_PHOTOMETRIC_INTERPRETATION = "PhotometricInterpretation"
PI_WHITE_IS_ZERO = "WhiteIsZero"
PI_BLACK_IS_ZERO = "BlackIsZero"
PI_RGB = "RGB"
PI_RGB_PALETTE = "Palette"
PI_TRANSPARENCY_MASK = "Transparency Mask"
PI_CMYK = "CMYK"
PI_Y_CB_CR = "YCbCr"
PI_CIE_LAB = "CIELAB"
PI_CFA_ARRAY = "Color Filter Array"
'''BioFormats infers the image type from the photometric interpretation'''
OM_METADATA_PHOTOMETRIC_INTERPRETATION = "MetaDataPhotometricInterpretation"
MPI_RGB = "RGB"
MPI_MONOCHROME = "Monochrome"
MPI_CMYK = "CMYK"
'''IFD # 263'''
OM_THRESHHOLDING = "Threshholding" # (sic)
'''IFD # 264 (but can be 265 if the orientation = 8)'''
OM_CELL_WIDTH = "CellWidth"
'''IFD # 265'''
OM_CELL_LENGTH = "CellLength"
'''IFD # 266'''
OM_FILL_ORDER = "FillOrder"
'''IFD # 279'''
OM_DOCUMENT_NAME = "Document Name"
'''IFD # 271'''
OM_MAKE = "Make"
'''IFD # 272'''
OM_MODEL = "Model"
'''IFD # 274'''
OM_ORIENTATION = "Orientation"
'''IFD # 277'''
OM_SAMPLES_PER_PIXEL = "SamplesPerPixel"
'''IFD # 280'''
OM_MIN_SAMPLE_VALUE = "MinSampleValue"
'''IFD # 281'''
OM_MAX_SAMPLE_VALUE = "MaxSampleValue"
'''IFD # 282'''
OM_X_RESOLUTION = "XResolution"
'''IFD # 283'''
OM_Y_RESOLUTION = "YResolution"
'''IFD # 284'''
OM_PLANAR_CONFIGURATION = "PlanarConfiguration"
PC_CHUNKY = "Chunky"
PC_PLANAR = "Planar"
'''IFD # 286'''
OM_X_POSITION = "XPosition"
'''IFD # 287'''
OM_Y_POSITION = "YPosition"
'''IFD # 288'''
OM_FREE_OFFSETS = "FreeOffsets"
'''IFD # 289'''
OM_FREE_BYTECOUNTS = "FreeByteCounts"
'''IFD # 290'''
OM_GRAY_RESPONSE_UNIT = "GrayResponseUnit"
'''IFD # 291'''
OM_GRAY_RESPONSE_CURVE = "GrayResponseCurve"
'''IFD # 292'''
OM_T4_OPTIONS = "T4Options"
'''IFD # 293'''
OM_T6_OPTIONS = "T6Options"
'''IFD # 296'''
OM_RESOLUTION_UNIT = "ResolutionUnit"
'''IFD # 297'''
OM_PAGE_NUMBER = "PageNumber"
'''IFD # 301'''
OM_TRANSFER_FUNCTION = "TransferFunction"
'''IFD # 305'''
OM_SOFTWARE = "Software"
'''IFD # 306'''
OM_DATE_TIME = "DateTime"
'''IFD # 315'''
OM_ARTIST = "Artist"
'''IFD # 316'''
OM_HOST_COMPUTER = "HostComputer"
'''IFD # 317'''
OM_PREDICTOR = "Predictor"
'''IFD # 318'''
OM_WHITE_POINT = "WhitePoint"
'''IFD # 322'''
OM_TILE_WIDTH = "TileWidth"
'''IFD # 323'''
OM_TILE_LENGTH = "TileLength"
'''IFD # 324'''
OM_TILE_OFFSETS = "TileOffsets"
'''IFD # 325'''
OM_TILE_BYTE_COUNT = "TileByteCount"
'''IFD # 332'''
OM_INK_SET = "InkSet"
'''IFD # 33432'''
OM_COPYRIGHT = "Copyright"
#
# Well row/column naming conventions
#
NC_LETTER = "letter"
NC_NUMBER = "number"
def page_name_original_metadata(index):
'''Get the key name for the page name metadata data for the indexed tiff page
These are TIFF IFD #'s 285+
index - zero-based index of the page
'''
return "PageName #%d" % index
def get_text(node):
'''Get the contents of text nodes in a parent node'''
return node.text
def set_text(node, text):
'''Set the text of a parent'''
node.text = text
def qn(namespace, tag_name):
'''Return the qualified name for a given namespace and tag name
This is the ElementTree representation of a qualified name
'''
return "{%s}%s" % (namespace, tag_name)
def split_qn(qn):
'''Split a qualified tag name or return None if namespace not present'''
m = re.match('\{(.*)\}(.*)', qn)
return m.group(1), m.group(2) if m else None
def get_namespaces(node):
'''Get top-level XML namespaces from a node.'''
ns_lib = {'ome': None, 'sa': None, 'spw': None}
for child in node.iter():
ns = split_qn(child.tag)[0]
match = re.match(NS_RE, ns)
if match:
ns_key = match.group('ns_key').lower()
ns_lib[ns_key] = ns
return ns_lib
def get_float_attr(node, attribute):
'''Cast an element attribute to a float or return None if not present'''
attr = node.get(attribute)
return None if attr is None else float(attr)
def get_int_attr(node, attribute):
'''Cast an element attribute to an int or return None if not present'''
attr = node.get(attribute)
return None if attr is None else int(attr)
def make_text_node(parent, namespace, tag_name, text):
'''Either make a new node and add the given text or replace the text
parent - the parent node to the node to be created or found
namespace - the namespace of the node's qualified name
tag_name - the tag name of the node's qualified name
text - the text to be inserted
'''
qname = qn(namespace, tag_name)
node = parent.find(qname)
if node is None:
node = ElementTree.SubElement(parent, qname)
set_text(node, text)
class OMEXML(object):
'''Reads and writes OME-XML with methods to get and set it.
The OMEXML class has four main purposes: to parse OME-XML, to output
OME-XML, to provide a structured mechanism for inspecting OME-XML and to
let the caller create and modify OME-XML.
There are two ways to invoke the constructor. If you supply XML as a string
or unicode string, the constructor will parse it and will use it as the
base for any inspection and modification. If you don't supply XML, you'll
get a bland OME-XML object which has a one-channel image. You can modify
it programatically and get the modified OME-XML back out by calling to_xml.
There are two ways to get at the XML. The arduous way is to get the
root_node of the DOM and explore it yourself using the DOM API
(http://docs.python.org/library/xml.dom.html#module-xml.dom). The easy way,
where it's supported is to use properties on OMEXML and on some of its
derived objects. For instance:
>>> o = OMEXML()
>>> print o.image().AcquisitionDate
will get you the date that image # 0 was acquired.
>>> o = OMEXML()
>>> o.image().Name = "MyImage"
will set the image name to "MyImage".
You can add and remove objects using the "count" properties. Each of these
handles hooking up and removing orphaned elements for you and should be
less error prone than creating orphaned elements and attaching them. For
instance, to create a three-color image:
>>> o = OMEXML()
>>> o.image().Pixels.channel_count = 3
>>> o.image().Pixels.Channel(0).Name = "Red"
>>> o.image().Pixels.Channel(1).Name = "Green"
>>> o.image().Pixels.Channel(2).Name = "Blue"
See the `OME-XML schema documentation <http://git.openmicroscopy.org/src/develop/components/specification/Documentation/Generated/OME-2011-06/ome.html>`_.
'''
def __init__(self, xml=None):
if xml is None:
xml = default_xml
if isinstance(xml, str):
xml = xml.encode("utf-8")
try:
self.dom = ElementTree.ElementTree(ElementTree.fromstring(xml))
except UnicodeEncodeError:
xml = xml.encode("utf-8")
self.dom = ElementTree.ElementTree(ElementTree.fromstring(xml))
# determine OME namespaces
self.ns = get_namespaces(self.dom.getroot())
if self.ns['ome'] is None:
raise Exception("Error: String not in OME-XML format")
def __str__(self):
#
# need to register the ome namespace because BioFormats expects
# that namespace to be the default or to be explicitly named "ome"
#
for ns_key in ["ome", "sa", "spw"]:
ns = self.ns.get(ns_key) or NS_DEFAULT.format(ns_key=ns_key)
ElementTree.register_namespace(ns_key, ns)
ElementTree.register_namespace("om", NS_ORIGINAL_METADATA)
result = StringIO()
ElementTree.ElementTree(self.root_node).write(result,
encoding=uenc,
method="xml")
return result.getvalue()
def to_xml(self, indent="\t", newline="\n", encoding=uenc):
return str(self)
def get_ns(self, key):
return self.ns[key]
@property
def root_node(self):
return self.dom.getroot()
def get_image_count(self):
'''The number of images (= series) specified by the XML'''
return len(self.root_node.findall(qn(self.ns['ome'], "Image")))
def set_image_count(self, value):
'''Add or remove image nodes as needed'''
assert value > 0
root = self.root_node
if self.image_count > value:
image_nodes = root.find(qn(self.ns['ome'], "Image"))
for image_node in image_nodes[value:]:
root.remove(image_node)
while(self.image_count < value):
new_image = self.Image(ElementTree.SubElement(root, qn(self.ns['ome'], "Image")))
new_image.ID = str(uuid.uuid4())
new_image.Name = "default.png"
new_image.AcquisitionDate = xsd_now()
new_pixels = self.Pixels(
ElementTree.SubElement(new_image.node, qn(self.ns['ome'], "Pixels")))
new_pixels.ID = str(uuid.uuid4())
new_pixels.DimensionOrder = DO_XYCTZ
new_pixels.PixelType = PT_UINT8
new_pixels.SizeC = 1
new_pixels.SizeT = 1
new_pixels.SizeX = 512
new_pixels.SizeY = 512
new_pixels.SizeZ = 1
new_channel = self.Channel(
ElementTree.SubElement(new_pixels.node, qn(self.ns['ome'], "Channel")))
new_channel.ID = "Channel%d:0" % self.image_count
new_channel.Name = new_channel.ID
new_channel.SamplesPerPixel = 1
image_count = property(get_image_count, set_image_count)
@property
def plates(self):
return self.PlatesDucktype(self.root_node)
@property
def structured_annotations(self):
'''Return the structured annotations container
returns a wrapping of OME/StructuredAnnotations. It creates
the element if it doesn't exist.
'''
node = self.root_node.find(qn(self.ns['sa'], "StructuredAnnotations"))
if node is None:
node = ElementTree.SubElement(
self.root_node, qn(self.ns['sa'], "StructuredAnnotations"))
return self.StructuredAnnotations(node)
class Image(object):
'''Representation of the OME/Image element'''
def __init__(self, node):
'''Initialize with the DOM Image node'''
self.node = node
self.ns = get_namespaces(self.node)
def get_ID(self):
return self.node.get("ID")
def set_ID(self, value):
self.node.set("ID", value)
ID = property(get_ID, set_ID)
def get_Name(self):
return self.node.get("Name")
def set_Name(self, value):
self.node.set("Name", value)
Name = property(get_Name, set_Name)
def get_AcquisitionDate(self):
'''The date in ISO-8601 format'''
acquired_date = self.node.find(qn(self.ns["ome"], "AcquisitionDate"))
if acquired_date is None:
return None
return get_text(acquired_date)
def set_AcquisitionDate(self, date):
acquired_date = self.node.find(qn(self.ns["ome"], "AcquisitionDate"))
if acquired_date is None:
acquired_date = ElementTree.SubElement(
self.node, qn(self.ns["ome"], "AcquisitionDate"))
set_text(acquired_date, date)
AcquisitionDate = property(get_AcquisitionDate, set_AcquisitionDate)
@property
def Pixels(self):
'''The OME/Image/Pixels element.
Example:
>>> md = bioformats.OMEXML(bioformats.get_omexml_metadata(filename))
>>> pixels = md.image().Pixels
>>> channel_count = pixels.SizeC
>>> stack_count = pixels.SizeZ
>>> timepoint_count = pixels.SizeT
'''
return OMEXML.Pixels(self.node.find(qn(self.ns['ome'], "Pixels")))
def roiref(self, index=0):
'''The OME/Image/ROIRef element'''
return OMEXML.ROIRef(self.node.findall(qn(self.ns['ome'], "ROIRef"))[index])
def get_roiref_count(self):
return len(self.node.findall(qn(self.ns['ome'], "ROIRef")))
def set_roiref_count(self, value):
'''Add or remove roirefs as needed'''
assert value > 0
if self.roiref_count > value:
roiref_nodes = self.node.find(qn(self.ns['ome'], "ROIRef"))
for roiref_node in roiref_nodes[value:]:
self.node.remove(roiref_node)
while(self.roiref_count < value):
iteration = self.roiref_count - 1
new_roiref = OMEXML.ROIRef(ElementTree.SubElement(self.node, qn(self.ns['ome'], "ROIRef")))
new_roiref.set_ID("ROI:" + str(iteration))
roiref_count = property(get_roiref_count, set_roiref_count)
def image(self, index=0):
'''Return an image node by index'''
return self.Image(self.root_node.findall(qn(self.ns['ome'], "Image"))[index])
class Channel(object):
'''The OME/Image/Pixels/Channel element'''
def __init__(self, node):
self.node = node
self.ns = get_namespaces(node)
def get_ID(self):
return self.node.get("ID")
def set_ID(self, value):
self.node.set("ID", value)
ID = property(get_ID, set_ID)
def get_Name(self):
return self.node.get("Name")
def set_Name(self, value):
self.node.set("Name", value)
Name = property(get_Name, set_Name)
def get_SamplesPerPixel(self):
return get_int_attr(self.node, "SamplesPerPixel")
def set_SamplesPerPixel(self, value):
self.node.set("SamplesPerPixel", str(value))
SamplesPerPixel = property(get_SamplesPerPixel, set_SamplesPerPixel)
#---------------------
# The following section is from the Allen Institute for Cell Science version of this file
# which can be found at https://github.com/AllenCellModeling/aicsimageio/blob/master/aicsimageio/vendor/omexml.py
class TiffData(object):
"""The OME/Image/Pixels/TiffData element
<TiffData FirstC="0" FirstT="0" FirstZ="0" IFD="0" PlaneCount="1">
<UUID FileName="img40_1.ome.tif">urn:uuid:ef8af211-b6c1-44d4-97de-daca46f16346</UUID>
</TiffData>
For our purposes, there will be one TiffData per 2-dimensional image plane.
"""
def __init__(self, node):
self.node = node
self.ns = get_namespaces(self.node)
def get_FirstZ(self):
'''The Z index of the plane'''
return get_int_attr(self.node, "FirstZ")
def set_FirstZ(self, value):
self.node.set("FirstZ", str(value))
FirstZ = property(get_FirstZ, set_FirstZ)
def get_FirstC(self):
'''The channel index of the plane'''
return get_int_attr(self.node, "FirstC")
def set_FirstC(self, value):
self.node.set("FirstC", str(value))
FirstC = property(get_FirstC, set_FirstC)
def get_FirstT(self):
'''The T index of the plane'''
return get_int_attr(self.node, "FirstT")
def set_FirstT(self, value):
self.node.set("FirstT", str(value))
FirstT = property(get_FirstT, set_FirstT)
def get_IFD(self):
'''plane index within tiff file'''
return get_int_attr(self.node, "IFD")
def set_IFD(self, value):
self.node.set("IFD", str(value))
IFD = property(get_IFD, set_IFD)
def get_plane_count(self):
'''How many planes in this TiffData. Should always be 1'''
return get_int_attr(self.node, "PlaneCount")
def set_plane_count(self, value):
self.node.set("PlaneCount", str(value))
plane_count = property(get_plane_count, set_plane_count)
class Plane(object):
'''The OME/Image/Pixels/Plane element
The Plane element represents one 2-dimensional image plane. It
has the Z, C and T indices of the plane and optionally has the
X, Y, Z, exposure time and a relative time delta.
'''
def __init__(self, node):
self.node = node
self.ns = get_namespaces(self.node)
def get_TheZ(self):
'''The Z index of the plane'''
return get_int_attr(self.node, "TheZ")
def set_TheZ(self, value):
self.node.set("TheZ", str(value))
TheZ = property(get_TheZ, set_TheZ)
def get_TheC(self):
'''The channel index of the plane'''
return get_int_attr(self.node, "TheC")
def set_TheC(self, value):
self.node.set("TheC", str(value))
TheC = property(get_TheC, set_TheC)
def get_TheT(self):
'''The T index of the plane'''
return get_int_attr(self.node, "TheT")
def set_TheT(self, value):
self.node.set("TheT", str(value))
TheT = property(get_TheT, set_TheT)
def get_DeltaT(self):
'''# of seconds since the beginning of the experiment'''
return get_float_attr(self.node, "DeltaT")
def set_DeltaT(self, value):
self.node.set("DeltaT", str(value))
DeltaT = property(get_DeltaT, set_DeltaT)
def get_ExposureTime(self):
exposure_time = self.node.get("ExposureTime")
if exposure_time is not None:
return float(exposure_time)
return None
def set_ExposureTime(self, value):
'''Units are seconds. Duration of acquisition????'''
self.node.set("ExposureTime", str(value))
ExposureTime = property(get_ExposureTime, set_ExposureTime)
def get_PositionX(self):
'''X position of stage'''
position_x = self.node.get("PositionX")
if position_x is not None:
return float(position_x)
return None
def set_PositionX(self, value):
self.node.set("PositionX", str(value))
PositionX = property(get_PositionX, set_PositionX)
def get_PositionY(self):
'''Y position of stage'''
return get_float_attr(self.node, "PositionY")
def set_PositionY(self, value):
self.node.set("PositionY", str(value))
PositionY = property(get_PositionY, set_PositionY)
def get_PositionZ(self):
'''Z position of stage'''
return get_float_attr(self.node, "PositionZ")
def set_PositionZ(self, value):
self.node.set("PositionZ", str(value))
PositionZ = property(get_PositionZ, set_PositionZ)
def get_PositionXUnit(self):
return self.node.get("PositionXUnit")
def set_PositionXUnit(self, value):
self.node.set("PositionXUnit", str(value))
PositionXUnit = property(get_PositionXUnit, set_PositionXUnit)
def get_PositionYUnit(self):
return self.node.get("PositionYUnit")
def set_PositionYUnit(self, value):
self.node.set("PositionYUnit", str(value))
PositionYUnit = property(get_PositionYUnit, set_PositionYUnit)
def get_PositionZUnit(self):
return self.node.get("PositionZUnit")
def set_PositionZUnit(self, value):
self.node.set("PositionZUnit", str(value))
PositionZUnit = property(get_PositionZUnit, set_PositionZUnit)
class Pixels(object):
'''The OME/Image/Pixels element
The Pixels element represents the pixels in an OME image and, for
an OME-XML encoded image, will actually contain the base-64 encoded
pixel data. It has the X, Y, Z, C, and T extents of the image
and it specifies the channel interleaving and channel depth.
'''
def __init__(self, node):
self.node = node
self.ns = get_namespaces(self.node)
def get_ID(self):
return self.node.get("ID")
def set_ID(self, value):
self.node.set("ID", value)
ID = property(get_ID, set_ID)
def get_DimensionOrder(self):
'''The ordering of image planes in the file
A 5-letter code indicating the ordering of pixels, from the most
rapidly varying to least. Use the DO_* constants (for instance
DO_XYZCT) to compare and set this.
'''
return self.node.get("DimensionOrder")
def set_DimensionOrder(self, value):
self.node.set("DimensionOrder", value)
DimensionOrder = property(get_DimensionOrder, set_DimensionOrder)
def get_PixelType(self):
'''The pixel bit type, for instance PT_UINT8
The pixel type specifies the datatype used to encode pixels
in the image data. You can use the PT_* constants to compare
and set the pixel type.
'''
return self.node.get("Type")
def get_PhysicalSizeXUnit(self):
'''The unit of length of a pixel in X direction.'''
return self.node.get("PhysicalSizeXUnit")
def set_PhysicalSizeXUnit(self, value):
self.node.set("PhysicalSizeXUnit", str(value))
PhysicalSizeXUnit = property(get_PhysicalSizeXUnit, set_PhysicalSizeXUnit)
def get_PhysicalSizeYUnit(self):
'''The unit of length of a pixel in Y direction.'''
return self.node.get("PhysicalSizeYUnit")
def set_PhysicalSizeYUnit(self, value):
self.node.set("PhysicalSizeYUnit", str(value))
PhysicalSizeYUnit = property(get_PhysicalSizeYUnit, set_PhysicalSizeYUnit)
def get_PhysicalSizeZUnit(self):
'''The unit of length of a voxel in Z direction.'''
return self.node.get("PhysicalSizeZUnit")
def set_PhysicalSizeZUnit(self, value):
self.node.set("PhysicalSizeZUnit", str(value))
PhysicalSizeZUnit = property(get_PhysicalSizeZUnit, set_PhysicalSizeZUnit)
def get_PhysicalSizeX(self):
'''The length of a single pixel in X direction.'''
return get_float_attr(self.node, "PhysicalSizeX")
def set_PhysicalSizeX(self, value):
self.node.set("PhysicalSizeX", str(value))
PhysicalSizeX = property(get_PhysicalSizeX, set_PhysicalSizeX)
def get_PhysicalSizeY(self):
'''The length of a single pixel in Y direction.'''
return get_float_attr(self.node, "PhysicalSizeY")
def set_PhysicalSizeY(self, value):
self.node.set("PhysicalSizeY", str(value))
PhysicalSizeY = property(get_PhysicalSizeY, set_PhysicalSizeY)
def get_PhysicalSizeZ(self):
'''The size of a voxel in Z direction or None for 2D images.'''
return get_float_attr(self.node, "PhysicalSizeZ")
def set_PhysicalSizeZ(self, value):
self.node.set("PhysicalSizeZ", str(value))
PhysicalSizeZ = property(get_PhysicalSizeZ, set_PhysicalSizeZ)
def set_PixelType(self, value):
self.node.set("Type", value)
PixelType = property(get_PixelType, set_PixelType)
def get_SizeX(self):
'''The dimensions of the image in the X direction in pixels'''
return get_int_attr(self.node, "SizeX")
def set_SizeX(self, value):
self.node.set("SizeX", str(value))
SizeX = property(get_SizeX, set_SizeX)
def get_SizeY(self):
'''The dimensions of the image in the Y direction in pixels'''
return get_int_attr(self.node, "SizeY")
def set_SizeY(self, value):
self.node.set("SizeY", str(value))
SizeY = property(get_SizeY, set_SizeY)
def get_SizeZ(self):
'''The dimensions of the image in the Z direction in pixels'''
return get_int_attr(self.node, "SizeZ")
def set_SizeZ(self, value):
self.node.set("SizeZ", str(value))
SizeZ = property(get_SizeZ, set_SizeZ)
def get_SizeT(self):
'''The dimensions of the image in the T direction in pixels'''
return get_int_attr(self.node, "SizeT")
def set_SizeT(self, value):
self.node.set("SizeT", str(value))
SizeT = property(get_SizeT, set_SizeT)
def get_SizeC(self):
'''The dimensions of the image in the C direction in pixels'''
return get_int_attr(self.node, "SizeC")
def set_SizeC(self, value):
self.node.set("SizeC", str(value))
SizeC = property(get_SizeC, set_SizeC)
def get_channel_count(self):
'''The number of channels in the image
You can change the number of channels in the image by
setting the channel_count:
pixels.channel_count = 3
pixels.Channel(0).Name = "Red"
...
'''
return len(self.node.findall(qn(self.ns['ome'], "Channel")))
def set_channel_count(self, value):
assert value > 0
channel_count = self.channel_count
if channel_count > value:
channels = self.node.findall(qn(self.ns['ome'], "Channel"))
for channel in channels[value:]:
self.node.remove(channel)
else:
for _ in range(channel_count, value):
new_channel = OMEXML.Channel(
ElementTree.SubElement(self.node, qn(self.ns['ome'], "Channel")))
new_channel.ID = str(uuid.uuid4())
new_channel.Name = new_channel.ID
new_channel.SamplesPerPixel = 1
channel_count = property(get_channel_count, set_channel_count)
def Channel(self, index=0):
'''Get the indexed channel from the Pixels element'''
channel = self.node.findall(qn(self.ns['ome'], "Channel"))[index]
return OMEXML.Channel(channel)
channel = Channel
def get_plane_count(self):
'''The number of planes in the image
An image with only one plane or an interleaved color plane will
often not have any planes.
You can change the number of planes in the image by
setting the plane_count:
pixels.plane_count = 3
pixels.Plane(0).TheZ=pixels.Plane(0).TheC=pixels.Plane(0).TheT=0
...
'''
return len(self.node.findall(qn(self.ns['ome'], "Plane")))
def set_plane_count(self, value):
assert value >= 0
plane_count = self.plane_count
if plane_count > value:
planes = self.node.findall(qn(self.ns['ome'], "Plane"))
for plane in planes[value:]:
self.node.remove(plane)
else:
for _ in range(plane_count, value):
new_plane = OMEXML.Plane(
ElementTree.SubElement(self.node, qn(self.ns['ome'], "Plane")))
plane_count = property(get_plane_count, set_plane_count)
def Plane(self, index=0):
'''Get the indexed plane from the Pixels element'''
plane = self.node.findall(qn(self.ns['ome'], "Plane"))[index]
return OMEXML.Plane(plane)
plane = Plane
def get_tiffdata_count(self):
return len(self.node.findall(qn(self.ns['ome'], "TiffData")))
def set_tiffdata_count(self, value):
assert value >= 0
tiffdatas = self.node.findall(qn(self.ns['ome'], "TiffData"))
for td in tiffdatas:
self.node.remove(td)
for _ in range(0, value):
new_tiffdata = OMEXML.TiffData(
ElementTree.SubElement(self.node, qn(self.ns['ome'], "TiffData")))
tiffdata_count = property(get_tiffdata_count, set_tiffdata_count)
def tiffdata(self, index=0):
data = self.node.findall(qn(self.ns['ome'], "TiffData"))[index]
return OMEXML.TiffData(data)
class Instrument(object):
'''Representation of the OME/Instrument element'''
def __init__(self, node):
self.node = node
self.ns = get_namespaces(self.node)
def get_ID(self):
return self.node.get("ID")
def set_ID(self, value):
self.node.set("ID", value)
ID = property(get_ID, set_ID)
@property
def Detector(self):
return OMEXML.Detector(self.node.find(qn(self.ns['ome'], "Detector")))
@property
def Objective(self):
return OMEXML.Objective(self.node.find(qn(self.ns['ome'], "Objective")))
def instrument(self, index=0):
return self.Instrument(self.root_node.findall(qn(self.ns['ome'], "Instrument"))[index])
class Objective(object):
def __init__(self, node):
self.node = node
self.ns = get_namespaces(self.node)
def get_ID(self):
return self.node.get("ID")
def set_ID(self, value):
self.node.set("ID", value)
ID = property(get_ID, set_ID)
def get_LensNA(self):
return self.node.get("LensNA")
def set_LensNA(self, value):
self.node.set("LensNA", value)
LensNA = property(get_LensNA, set_LensNA)
def get_NominalMagnification(self):
return self.node.get("NominalMagnification")
def set_NominalMagnification(self, value):
self.node.set("NominalMagnification", value)
NominalMagnification = property(get_NominalMagnification, set_NominalMagnification)
def get_WorkingDistanceUnit(self):
return get_int_attr(self.node, "WorkingDistanceUnit")
def set_WorkingDistanceUnit(self, value):
self.node.set("WorkingDistanceUnit", str(value))
WorkingDistanceUnit = property(get_WorkingDistanceUnit, set_WorkingDistanceUnit)
class Detector(object):
def __init__(self, node):
self.node = node
self.ns = get_namespaces(self.node)
def get_ID(self):
return self.node.get("ID")
def set_ID(self, value):
self.node.set("ID", value)
ID = property(get_ID, set_ID)
def get_Gain(self):
return self.node.get("Gain")
def set_Gain(self, value):
self.node.set("Gain", value)
Gain = property(get_Gain, set_Gain)
def get_Model(self):
return self.node.get("Model")
def set_Model(self, value):
self.node.set("Model", value)
Model = property(get_Model, set_Model)
def get_Type(self):
return get_int_attr(self.node, "Type")
def set_Type(self, value):
self.node.set("Type", str(value))
Type = property(get_Type, set_Type)
class StructuredAnnotations(dict):
'''The OME/StructuredAnnotations element
Structured annotations let OME-XML represent metadata from other file
formats, for example the tag metadata in TIFF files. The
StructuredAnnotations element is a container for the structured
annotations.
Images can have structured annotation references. These match to
the IDs of structured annotations in the StructuredAnnotations
element. You can get the structured annotations in an OME-XML document
using a dictionary interface to StructuredAnnotations.
Pragmatically, TIFF tag metadata is stored as key/value pairs in
OriginalMetadata annotations - in the context of CellProfiler,
callers will be using these to read tag data that's not represented