-
Notifications
You must be signed in to change notification settings - Fork 2
/
sitk_ims_file_io.py
executable file
·1514 lines (1415 loc) · 65.4 KB
/
sitk_ims_file_io.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
# =========================================================================
#
# Copyright Ziv Yaniv
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0.txt
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# =========================================================================
"""
SimpleITK Imaris IO module.
This module enables reading and writing of SimpleITK images from and to the
Imaris file format. It does not support IO for additional data elements stored
in Imaris files (e.g. meshes, spots).
The Imaris file format utilizes hdf5 and is described online:
https://imaris.oxinst.com/support/imaris-file-format
A SimpleITK image read from an Imaris file will contain both the raw pixel
information and a metadata dictionary.
The metadata dictionary contains the following keys-values:
unit_metadata_key - string denoting the physical units of the image origin,
and spacing.
time_metadata_key - string denoting the time associated with the image in
('%Y-%m-%d %H:%M:%S.%f' -
Year-month-day hour:minute:second.microsecond) format.
channels_metadata_key - XML string denoting channel information.
XML structure:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="imaris_channels_information">
<xs:complexType>
<xs:sequence>
<xs:element name="channel">
<xs:complexType>
<xs:element type="xs:string" name="name"/>
<xs:element type="xs:string" name="description"/>
<xs:element type="xs:string" name="color"/>
<xs:element type="xs:string" name="range"/>
<xs:element type="xs:string" name="gamma" minOccurs="0" maxOccurs="1"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
"""
import h5py
import SimpleITK as sitk
import numpy as np
import copy
import datetime
import xml.etree.ElementTree as et
unit2mm_conversion = {"m": 1000.0, "mm": 1.0, "um": 1.0 / 1000.0, "nm": 1.0 / 1000000.0}
"""Conversion factors between the various units supported by Imaris and mm, which
is the common unit in SimpleITK."""
unit_metadata_key = "unit"
time_metadata_key = "times"
# This is the time format used by most files, includes minutes, seconds and
# microseconds (zero-padded on the left) in some rare cases the time does not
# include the microseconds, so we have a "fallback"
time_str_format = "%Y-%m-%d %H:%M:%S.%f"
fallback_time_str_format = "%Y-%m-%d %H:%M:%S"
channels_metadata_key = "imaris_channels_information"
file_format_versions = ["5.5.0"]
default_dataset_info_dirname = "DataSetInfo"
default_dataset_dirname = "DataSet"
supported_pixel_types = {
sitk.sitkUInt8: "8-bit unsigned integer",
sitk.sitkVectorUInt8: "vector of 8-bit unsigned integer",
sitk.sitkUInt16: "16-bit unsigned integer",
sitk.sitkVectorUInt16: "vector of 16-bit unsigned integer",
sitk.sitkUInt32: "32-bit unsigned integer",
sitk.sitkVectorUInt32: "vector of 32-bit unsigned integer",
sitk.sitkFloat32: "32-bit float",
sitk.sitkVectorFloat32: "vector of 32-bit float",
}
# Map SimpleITK pixel types to the corresponding Imaris pixel types.
pixel_type_to_scalar_type = {
sitk.sitkUInt8: sitk.sitkUInt8,
sitk.sitkVectorUInt8: sitk.sitkUInt8,
sitk.sitkUInt16: sitk.sitkUInt16,
sitk.sitkVectorUInt16: sitk.sitkUInt16,
sitk.sitkUInt32: sitk.sitkUInt32,
sitk.sitkVectorUInt32: sitk.sitkUInt32,
sitk.sitkFloat32: sitk.sitkFloat32,
sitk.sitkVectorFloat32: sitk.sitkFloat32,
}
def read_metadata(file_name):
"""
Read the meta-data contained in the Imaris file.
Parameters
----------
file_name (str): Path to Imaris file from which we read.
Returns
-------
meta_data_dict (dictionary): Dictionary containing the following information:
times (list(datetime)): datetime objects corresponding to the temporal image times.
unit (str): unit of physical size (m, mm, um, nm).
sizes (list(list[int])): number of pixels in volume [x,y,z] for each of the existing resolution levels.
spacings (list(list[float])): spacing in 'units' per resolution level.
origin(list[float]): SimpleITK origin in units. SimpleITK origin
is at the center of the first voxel.
channels_information (list[(i,dict)]): Channel information tuples with first entry the channel index
and second entry dictionary that includes 'name',
'description','color' or 'color_table', 'alpha', 'range', 'gamma', with
'gamma' being optional.
storage_settings(list(list[tuple,string,int])): Storage settings per resolution level, tuple with hdf5 chunk
size, string denoting compression type (imaris only supports gzip),
int representing compression options for gzip this is an int in [0,9].
sitk_pixel_type: Image's SimpleITK pixel type.
""" # noqa
meta_data_dict = {}
with h5py.File(file_name, "r") as f:
if f.attrs["ImarisVersion"].tobytes().decode("UTF-8") in file_format_versions:
dataset_info_dirname = (
f.attrs["DataSetInfoDirectoryName"].tobytes().decode("UTF-8")
)
dataset_dirname = f.attrs["DataSetDirectoryName"].tobytes().decode("UTF-8")
time_point_number = int(
(
f[dataset_info_dirname]["TimeInfo"]
.attrs["DatasetTimePoints"]
.tobytes()
)
)
meta_data_dict["times"] = []
for i in range(1, time_point_number + 1):
try:
meta_data_dict["times"].append(
datetime.datetime.strptime(
f[dataset_info_dirname]["TimeInfo"]
.attrs[f"TimePoint{i}"]
.tobytes()
.decode("UTF-8"),
time_str_format,
)
)
except ValueError:
meta_data_dict["times"].append(
datetime.datetime.strptime(
f[dataset_info_dirname]["TimeInfo"]
.attrs[f"TimePoint{i}"]
.tobytes()
.decode("UTF-8"),
fallback_time_str_format,
)
)
meta_data_dict["unit"] = (
f[dataset_info_dirname]["Image"].attrs["Unit"].tobytes().decode("UTF-8")
)
resolution_sizes = []
storage_info = []
for i in range(len(f[dataset_dirname])):
resolution_name = f"ResolutionLevel {i}"
resolution_sizes.append(
[
int(
f[dataset_dirname][resolution_name]["TimePoint 0"][
"Channel 0"
]
.attrs["ImageSizeX"]
.tobytes()
),
int(
f[dataset_dirname][resolution_name]["TimePoint 0"][
"Channel 0"
]
.attrs["ImageSizeY"]
.tobytes()
),
int(
f[dataset_dirname][resolution_name]["TimePoint 0"][
"Channel 0"
]
.attrs["ImageSizeZ"]
.tobytes()
),
]
)
storage_info.append(
[
f[dataset_dirname][resolution_name]["TimePoint 0"]["Channel 0"][
"Data"
].chunks,
f[dataset_dirname][resolution_name]["TimePoint 0"]["Channel 0"][
"Data"
].compression,
f[dataset_dirname][resolution_name]["TimePoint 0"]["Channel 0"][
"Data"
].compression_opts,
]
)
meta_data_dict["sizes"] = resolution_sizes
meta_data_dict["storage_settings"] = storage_info
# Coordinates of the corners of the imaris volume's bounding box
min_x = float(f[dataset_info_dirname]["Image"].attrs["ExtMin0"].tobytes())
max_x = float(f[dataset_info_dirname]["Image"].attrs["ExtMax0"].tobytes())
min_y = float(f[dataset_info_dirname]["Image"].attrs["ExtMin1"].tobytes())
max_y = float(f[dataset_info_dirname]["Image"].attrs["ExtMax1"].tobytes())
min_z = float(f[dataset_info_dirname]["Image"].attrs["ExtMin2"].tobytes())
max_z = float(f[dataset_info_dirname]["Image"].attrs["ExtMax2"].tobytes())
x_size = max_x - min_x
y_size = max_y - min_y
z_size = max_z - min_z
meta_data_dict["spacings"] = [
[x_size / sz[0], y_size / sz[1], z_size / sz[2]]
for sz in meta_data_dict["sizes"]
]
# SimpleITK image origin is 0.5*(pixel spacing) from the corner of the volume.
meta_data_dict["origin"] = [
m_val + 0.5 * spc
for m_val, spc in zip(
[min_x, min_y, min_z], meta_data_dict["spacings"][0]
)
]
# Get the number of channels from a group that is guarenteed to exist
num_channels = len(f[dataset_dirname]["ResolutionLevel 0"]["TimePoint 0"])
# Get the pixel type
meta_data_dict["sitk_pixel_type"] = sitk.GetImageFromArray(
f[dataset_dirname]["ResolutionLevel 0"]["TimePoint 0"]["Channel 0"][
"Data"
][0:1, 0:1, 0:1]
).GetPixelID()
# Get the per-channel metadata.
channels_information = []
for i in range(num_channels):
channel_information = {}
channel_str = f"Channel {i}"
channel_information["name"] = (
f[dataset_info_dirname][channel_str]
.attrs["Name"]
.tobytes()
.decode("UTF-8")
)
if channel_information["name"] == "\x00": # null byte
channel_information["name"] = ""
channel_information["description"] = (
f[dataset_info_dirname][channel_str]
.attrs["Description"]
.tobytes()
.decode("UTF-8")
)
if channel_information["description"] == "\x00": # null byte
channel_information["description"] = ""
color_mode = (
f[dataset_info_dirname][channel_str]
.attrs["ColorMode"]
.tobytes()
.decode("UTF-8")
)
# color is a list of float values in [0.0, 1.0] in r,g,b order.
# color table is just a longer list of colors in r,g,b order.
if color_mode == "BaseColor":
color_info = f[dataset_info_dirname][channel_str].attrs["Color"]
color_key = "color"
elif color_mode == "TableColor":
# The actual color table is stored either as a dataset or as an attribute
if "ColorTable" in f[dataset_info_dirname][channel_str].attrs:
color_info = f[dataset_info_dirname][channel_str].attrs[
"ColorTable"
]
else:
color_info = f[dataset_info_dirname][channel_str]["ColorTable"][
0:-1
]
color_key = "color_table"
channel_information[color_key] = [
float(val) for val in color_info.tobytes().split()
]
channel_information["range"] = [
float(val)
for val in f[dataset_info_dirname][channel_str]
.attrs["ColorRange"]
.tobytes()
.split()
]
channel_information["alpha"] = float(
f[dataset_info_dirname][channel_str].attrs["ColorOpacity"].tobytes()
)
try: # Some images have a gamma value, some don't
channel_information["gamma"] = float(
f[dataset_info_dirname][channel_str]
.attrs["GammaCorrection"]
.tobytes()
)
except Exception:
pass
channels_information.append((i, channel_information))
meta_data_dict["channels_information"] = channels_information
return meta_data_dict
def _ims_set_nullterm_str_attribute(hdf_object, attribute_name, attribute_value):
"""
Set the value of an attribute attached to the given object. If the attribute
does not exist it is created. Attribute is encoded as
an array of fixed length (length of 1) null terminated strings. This encoding
is specific to imaris and is problematic. The individual string length should
be two, 'a\x00', but this is not how imaris encodes it.
This function uses the low level h5py API because the high level API will
always write the fixed length strings as H5T_STR_NULLPAD and not H5T_STR_NULLTERM
which is what Imaris is expecting.
For additional details see the HDF discourse:
https://forum.hdfgroup.org/t/nullpad-nullterm-strings/9107
Parameters
----------
hdf_object (File/Group/Dataset): Attribute will be attached to this object.
attribute_name (str): Attribute name.
attribute_value (str): Byte string representation of the attribute value (i.e.
b'255' or b'255.000').
"""
# Because we are dealing with fixed length strings we delete the attribute
# and create it again with the current size. If the attribute doesn't exist
# we just catch the exception and ignore.
try:
del hdf_object.attrs[attribute_name]
except KeyError:
pass
type_id = h5py.h5t.TypeID.copy(h5py.h5t.C_S1)
type_id.set_size(1)
type_id.set_strpad(h5py.h5t.STR_NULLTERM)
attribute_arr = np.frombuffer(attribute_value, dtype="|S1")
space = h5py.h5s.create_simple((len(attribute_arr),))
attribute_id = h5py.h5a.create(
hdf_object.id, attribute_name.encode("UTF-8"), type_id, space
)
attribute_id.write(attribute_arr, mtype=attribute_id.get_type())
def write_channels_metadata(meta_data_dict, file_name, access_mode="a"):
"""
Write the channel metadata into the given file. If file doesn't exist create it.
If the file exists, the channel indexes given in the meta_data_dict must be in
the existing range.
Parameters
----------
meta_data_dict(dictionary): see dictionary description in the read_metadata function.
file_name: write to this file.
access_mode: file access mode, default is append.
"""
# Open the file for reading and writing. If it doesn't exist, create.
with h5py.File(file_name, access_mode) as f:
try: # If file already exists check the imaris file format version and get number of channels.
imaris_format_version = f.attrs["ImarisVersion"].tobytes().decode("UTF-8")
if imaris_format_version not in file_format_versions:
raise ValueError(
f"Unsupported imaris file format version {imaris_format_version}."
)
dataset_dirname = f.attrs["DataSetDirectoryName"].tobytes().decode("UTF-8")
dataset_info_dirname = (
f.attrs["DataSetInfoDirectoryName"].tobytes().decode("UTF-8")
)
num_channels = len(f[dataset_dirname]["ResolutionLevel 0"]["TimePoint 0"])
except KeyError: # We are dealing with a new file.
num_channels = len(meta_data_dict["channels_information"])
dataset_info_dirname = default_dataset_info_dirname
dataset_dirname = default_dataset_dirname
_ims_set_nullterm_str_attribute(f, "ImarisDataSet", b"ImarisDataSet")
_ims_set_nullterm_str_attribute(f, "ImarisVersion", b"5.5.0")
_ims_set_nullterm_str_attribute(
f, "DataSetInfoDirectoryName", dataset_info_dirname.encode("UTF-8")
)
_ims_set_nullterm_str_attribute(
f, "DataSetDirectoryName", dataset_dirname.encode("UTF-8")
)
f.attrs["NumberOfDataSets"] = np.array([1], dtype=np.uint32)
f.create_group(dataset_info_dirname + "/ImarisDataSet")
_ims_set_nullterm_str_attribute(
f[dataset_info_dirname]["ImarisDataSet"], "Creator", b"SimpleITK"
)
_ims_set_nullterm_str_attribute(
f[dataset_info_dirname]["ImarisDataSet"], "NumberOfImages", b"1"
)
_ims_set_nullterm_str_attribute(
f[dataset_info_dirname]["ImarisDataSet"],
"Version",
str(sitk.Version()).encode("UTF-8"),
)
f.create_group(dataset_info_dirname + "/Imaris")
_ims_set_nullterm_str_attribute(
f[dataset_info_dirname]["Imaris"], "ThumbnailMode", b"thumbnailNone"
)
_ims_set_nullterm_str_attribute(
f[dataset_info_dirname]["Imaris"],
"Version",
str(sitk.Version()).encode("UTF-8"),
)
for i in range(num_channels):
f.create_group(dataset_info_dirname + f"/Channel {i}")
indexes, _ = zip(*meta_data_dict["channels_information"])
if not all([i in range(num_channels) for i in indexes]):
raise ValueError(
f"The index of one or more channels in meta data dictionary is outside the expected range [0, {num_channels-1}]." # noqa: E501
)
# Write the channel information, if it exists in the dictionary.
# When modifying an existing file some of the information
# may not exist, i.e. we are only changing the channel colors.
# Imaris supports two color modes ['BaseColor', 'TableColor'].
for i, channel_information in meta_data_dict["channels_information"]:
channel_str = f"Channel {i}"
if "name" in channel_information:
_ims_set_nullterm_str_attribute(
f[dataset_info_dirname][channel_str],
"Name",
channel_information["name"].encode("UTF-8"),
)
if "description" in channel_information:
_ims_set_nullterm_str_attribute(
f[dataset_info_dirname][channel_str],
"Description",
channel_information["description"].encode("UTF-8"),
)
prev_color_mode = (
f[dataset_info_dirname][channel_str]
.attrs["ColorMode"]
.tobytes()
.decode("UTF-8")
if "ColorMode" in f[dataset_info_dirname][channel_str].attrs
else ""
)
if (
"color" in channel_information or "color_table" in channel_information
) and prev_color_mode == "TableColor":
del f[dataset_info_dirname][channel_str].attrs["ColorTableLength"]
if "ColorTable" not in f[dataset_info_dirname][channel_str].attrs:
del f[dataset_info_dirname][channel_str]["ColorTable"]
if "color" in channel_information:
_ims_set_nullterm_str_attribute(
f[dataset_info_dirname][channel_str], "ColorMode", b"BaseColor"
)
_ims_set_nullterm_str_attribute(
f[dataset_info_dirname][channel_str],
"Color",
" ".join([f"{v:.3f}" for v in channel_information["color"]]).encode(
"UTF-8"
),
)
elif "color_table" in channel_information:
if prev_color_mode == "BaseColor":
del f[dataset_info_dirname][channel_str].attrs["Color"]
# Imaris expects the color table infromation to be either in an attribute
# or in a dataset.
# For some reason, I can't get h5py to write the dataset in the format expected by Imaris.
# String, Fixed length=1, padding=H5T_STR_NULLTERM, cset = H5T_CSET_ASCII
# The padding is always H5T_STR_NULLPAD.
# Tried a workaround similar to that described on SO, creating a custom type but that didn't work:
# https://stackoverflow.com/questions/38267076/how-to-write-a-dataset-of-null-terminated-fixed-length-strings-with-h5py
# tid = h5py.h5t.C_S1.copy()
# tid.set_strpad(h5py.h5t.STR_NULLTERM)
# H5T_C_S1_1 = h5py.Datatype(tid)
#
# The current "solution" is to write the color table information as an
# attribute and if that fails write as dataset so the information isn't lost.
# If the color table is large (>64K bytes) then writting
# to attribute will fail as it is larger than the HDF5 limit. We then save it as
# dataset even if imaris will not read it. We can export the file settings which will
# export the color table as a text file. We can then import the color table back directly
# from imaris and save the file.
# Possibly revisit, using low level h5py API as done for the
# attribute writing.
try:
f[dataset_info_dirname][channel_str].attrs[
"ColorTable"
] = np.frombuffer(
(
" ".join(
[f"{v:.3f}" for v in channel_information["color_table"]]
)
+ " "
).encode("UTF-8"),
dtype="S1",
)
except RuntimeError:
f[dataset_info_dirname][channel_str].create_dataset(
"ColorTable",
data=np.frombuffer(
(
" ".join(
[
f"{v:.3f}"
for v in channel_information["color_table"]
]
)
+ " "
).encode("UTF-8"),
dtype="S1",
),
)
_ims_set_nullterm_str_attribute(
f[dataset_info_dirname][channel_str],
"ColorTableLength",
str(int(len(channel_information["color_table"]) / 3)).encode(
"UTF-8"
),
)
_ims_set_nullterm_str_attribute(
f[dataset_info_dirname][channel_str], "ColorMode", b"TableColor"
)
if "range" in channel_information:
_ims_set_nullterm_str_attribute(
f[dataset_info_dirname][channel_str],
"ColorRange",
" ".join([f"{v:.3f}" for v in channel_information["range"]]).encode(
"UTF-8"
),
)
if "gamma" in channel_information:
_ims_set_nullterm_str_attribute(
f[dataset_info_dirname][channel_str],
"GammaCorrection",
f'{channel_information["gamma"]:.3f}'.encode("UTF-8"),
)
if "alpha" in channel_information:
_ims_set_nullterm_str_attribute(
f[dataset_info_dirname][channel_str],
"ColorOpacity",
f'{channel_information["alpha"]:.3f}'.encode("UTF-8"),
)
def write_named_channels_metadata(
meta_data_dict, file_name, channel_prefix_separator=""
):
"""
Overwrite meta-data in the Imaris file, where channels are specified using their names. A
channel name consists of three parts prefix+separtor_character+postfix. The given channel
name is compared to existing channel names as follows:
if separtor_character==''
prefix1+separtor_character+postfix1 == prefix2+separtor_character+postfix2
else
postfix1==postfix2
If the separtor_character appears more than once in the name the postfix is the substring
that appears after the last instance: abc:def:ghi with separtor_character==':' means
the prefix is "abc:def" and the postfix is "ghi".
Parameters
----------
meta_data_dict(dictionary): see dictionary description in the read_metadata function.
file_name: write to this file.
channel_prefix_separator: character separator described above.
Returns
-------
bool: False, if none of the given channels from the meta_data_dict are
found in the file, otherwise True.
"""
channelname2index = {}
# Open the file for reading.
with h5py.File(file_name, "r") as f:
dataset_info_dirname = (
f.attrs["DataSetInfoDirectoryName"].tobytes().decode("UTF-8")
)
dataset_dirname = f.attrs["DataSetDirectoryName"].tobytes().decode("UTF-8")
num_channels = len(f[dataset_dirname]["ResolutionLevel 0"]["TimePoint 0"])
for i in range(num_channels):
cname = (
f[dataset_info_dirname][f"Channel {i}"]
.attrs["Name"]
.tobytes()
.decode("UTF-8")
)
if channel_prefix_separator:
cname = (cname.split(channel_prefix_separator)[-1]).strip()
channelname2index[cname] = i
indexed_channel_information = []
for cname, channel_information in meta_data_dict["channels_information"]:
if channel_prefix_separator:
cname = (cname.split(channel_prefix_separator)[-1]).strip()
if cname in channelname2index:
indexed_channel_information.append(
(channelname2index[cname], channel_information)
)
if not indexed_channel_information:
return False
# Make a copy of the meta-data dictionary and modify the channel information to be index based and not name
# based.
new_meta_data_dict = copy.deepcopy(meta_data_dict)
new_meta_data_dict["channels_information"] = indexed_channel_information
write_channels_metadata(meta_data_dict, file_name)
return True
def read(
file_name,
time_index=0,
resolution_index=0,
channel_index=None,
sub_ranges=None,
vector_pixels=False,
convert_to_mm=False,
):
"""
Read all or part of an image into a SimpleITK image. All indexing is zero
based.
Parameters
----------
file_name: Read from this imaris image.
time_index: Read data for the specified time index.
resolution_index: Read data from the specified resolution index.
channel_index (list of ints or a single int): Read data from specified channel(s),
if set to None read all channels.
sub_ranges (list[range, range, range]): Read a sub-range of the image.
vector_pixels (bool): If True, then the returned image will have vector pixels representing
the channels, otherwise it will be a 4D image where the fourth index
represents the channel.
convert_to_mm (bool): The returned image origin and spacing are in the native units (e.g. um)
or in mm, native SimpleITK units. This is relevant for registration
purposes. If original units are um and they are converted to mm it
can lead to computational instabilities because we are dealing with
very small numeric values.
Returns
-------
image (SimpleITK.Image): Either a 3D or 4D SimpleITK image, depending on the vector_pixels
parameter.
"""
meta_data_dict = read_metadata(file_name)
num_channels = len(meta_data_dict["channels_information"])
# Validate the input.
if convert_to_mm and meta_data_dict["unit"] not in unit2mm_conversion.keys():
raise ValueError(
f'Cannot convert to mm, image units ({meta_data_dict["unit"]}) do not appear in the conversion dictionary.'
)
if time_index not in range(len(meta_data_dict["times"])):
raise ValueError(
f'Given time index ({time_index}) is outside valid range [0,{len(meta_data_dict["times"])}).'
)
if resolution_index not in range(len(meta_data_dict["spacings"])):
raise ValueError(
f'Given resolution index ({resolution_index}) is outside valid range [0,{len(meta_data_dict["spacings"])}).'
)
if channel_index is not None:
try:
_ = iter(channel_index)
except TypeError:
channel_index = [channel_index]
for ci in channel_index:
if ci not in range(num_channels):
raise ValueError(
f"Given channel index ({ci}) is outside valid range [0,{num_channels})."
)
else:
channel_index = range(num_channels)
image_origin = meta_data_dict["origin"]
image_spacing = meta_data_dict["spacings"][resolution_index]
image_size = meta_data_dict["sizes"][resolution_index]
read_ranges = [range(0, sz) for sz in image_size]
if sub_ranges: # Check that given sub ranges are inside the full image range
for fr, sr in zip(read_ranges, sub_ranges):
if sr.start not in fr or (sr.stop - 1) not in fr:
raise ValueError("Sub ranges are outside the full image extent.")
read_ranges = sub_ranges
image_origin = [
org + sr.start * spc
for org, spc, sr in zip(image_origin, image_spacing, sub_ranges)
]
if convert_to_mm:
image_origin = [
v * unit2mm_conversion[meta_data_dict["unit"]] for v in image_origin
]
image_spacing = [
v * unit2mm_conversion[meta_data_dict["unit"]] for v in image_spacing
]
with h5py.File(
file_name, "r", rdcc_nbytes=30 * 1048576
) as f: # open file with 30Mb chunk cache
dataset_dirname = f.attrs["DataSetDirectoryName"].tobytes().decode("UTF-8")
sitk_imaris_channels_list = []
for ci in channel_index:
sitk_imaris_channels_list.append(
sitk.GetImageFromArray(
f[dataset_dirname][f"ResolutionLevel {resolution_index}"][
f"TimePoint {time_index}"
][f"Channel {ci}"]["Data"][
read_ranges[2].start : read_ranges[2].stop, # noqa: E203
read_ranges[1].start : read_ranges[1].stop, # noqa: E203
read_ranges[0].start : read_ranges[0].stop, # noqa: E203
]
)
)
sitk_imaris_channels_list[-1].SetOrigin(image_origin)
sitk_imaris_channels_list[-1].SetSpacing(image_spacing)
if len(sitk_imaris_channels_list) > 1:
if vector_pixels:
image = sitk.Compose(sitk_imaris_channels_list)
else:
image = sitk.JoinSeries(sitk_imaris_channels_list)
else:
image = sitk_imaris_channels_list[0]
image.SetMetaData(
unit_metadata_key, meta_data_dict["unit"] if not convert_to_mm else "mm"
)
image.SetMetaData(
time_metadata_key,
datetime.datetime.strftime(
meta_data_dict["times"][time_index], time_str_format
),
)
# Encode the Imaris channels information in xml.
image.SetMetaData(
channels_metadata_key,
channels_information_list2xmlstr(
[meta_data_dict["channels_information"][ci] for ci in channel_index]
),
)
return image
def channels_information_xmlstr2list(channels_information_xml_str):
"""
Convert the xml string representing the Imaris channel information to a
list containing that information, same as in the dictionary returned by
the read_metadata function.
Parameters
----------
channels_information_xml_str (string with xml structure):
Returns
-------
List with channel information.
"""
channels_information = []
channels_xml_information = list(et.fromstring(channels_information_xml_str))
for i, channel_xml_info in enumerate(channels_xml_information):
channel_info = {}
channel_info["name"] = channel_xml_info.find("name").text
if channel_info["name"] is None:
channel_info["name"] = ""
channel_info["description"] = channel_xml_info.find("description").text
if channel_info["description"] is None:
channel_info["description"] = ""
if channel_xml_info.find("color") is not None:
channel_info["color"] = [
float(c) / 255
for c in channel_xml_info.find("color").text.replace(",", " ").split()
]
elif channel_xml_info.find("color_table") is not None:
channel_info["color_table"] = [
float(c) / 255
for c in channel_xml_info.find("color_table")
.text.replace(",", " ")
.split()
]
channel_info["range"] = [
float(c)
for c in channel_xml_info.find("range").text.replace(",", " ").split()
]
if channel_xml_info.find("gamma") is not None: # Gamma is optional
channel_info["gamma"] = float(channel_xml_info.find("gamma").text)
channel_info["alpha"] = float(channel_xml_info.find("alpha").text)
channels_information.append([i, channel_info])
return channels_information
def channels_information_list2xmlstr(channels_information_list):
"""
Convert the list containing the Imaris channel information to a
xml string. Used for encoding the information in a SimpleITK.Image metadata
dictionary.
Parameters
----------
channels_information_list (list): list with channel information.
Returns
-------
XML string representation of the channel information.
"""
# Encode the Imaris channels information in xml.
xml_root = et.Element(channels_metadata_key)
xml_root.append(et.Comment("generated by SimpleITK"))
for _, channel_information in channels_information_list:
child = et.SubElement(xml_root, "channel")
current_field = et.SubElement(child, "name")
current_field.text = channel_information["name"]
current_field = et.SubElement(child, "description")
current_field.text = channel_information["description"]
# set the color information
if "color" in channel_information:
current_field = et.SubElement(child, "color")
color_info = channel_information["color"]
elif "color_table" in channel_information:
current_field = et.SubElement(child, "color_table")
color_info = channel_information["color_table"]
current_field.text = ", ".join([str(int(c * 255 + 0.5)) for c in color_info])
current_field = et.SubElement(child, "range")
current_field.text = (
f'{channel_information["range"][0]}, {channel_information["range"][1]}'
)
current_field = et.SubElement(child, "alpha")
current_field.text = str(channel_information["alpha"])
if "gamma" in channel_information: # Some images have gamma value some not
current_field = et.SubElement(child, "gamma")
current_field.text = str(channel_information["gamma"])
# Specify encoding as unicode to get a regular string, default is bytestring
return et.tostring(xml_root, encoding="unicode")
def write(sitk_image, file_name):
"""
Write the given image to the file in Imaris format. If the SimpleITK image
metadata dictionary contains information describing the channels and their
display settings in Imaris these are used otherwise a default repetitive
RGB color scheme is used.
Parameters
----------
sitk_image (SimpleITK.Image): Input image in SimpleITK format.
file_name (string): Output file name.
"""
vector_pixels = sitk_image.GetNumberOfComponentsPerPixel() > 1
if vector_pixels:
number_of_channels = sitk_image.GetNumberOfComponentsPerPixel()
elif sitk_image.GetDimension() == 4:
number_of_channels = sitk_image.GetSize()[3]
else:
number_of_channels = 1
# Validate the input.
if sitk_image.GetPixelID() not in supported_pixel_types:
raise TypeError(
f"Imaris format does not support pixel type {sitk_image.GetPixelIDTypeAsString()}.\nSupported types include: " # noqa: E501
+ ", ".join(list(supported_pixel_types.values()))
+ "."
)
if not (
np.isclose(
np.array(sitk_image.GetDirection()),
np.eye(sitk_image.GetDimension()).ravel(),
)
).all():
raise TypeError(
"Imaris format does not support non-identity direction cosine matrix."
)
meta_data_dict = {}
channels_information = []
try:
channels_information = channels_information_xmlstr2list(
sitk_image.GetMetaData(channels_metadata_key)
)
if len(channels_information) != number_of_channels:
raise ValueError(
f"Corrupt SimpleITK image, number of channels does not match meta data dictionary entry (key: {channels_metadata_key})" # noqa: E501
)
except RuntimeError: # channels information is missing, we'll create it
default_colors = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]
for i in range(number_of_channels):
channel_info = {}
channel_info["name"] = f"ch {i+1}"
channel_info["description"] = ""
channel_info["color"] = default_colors[i % len(default_colors)]
channel_info["range"] = [0.0, 255.0]
channel_info["gamma"] = 1.0
channel_info["alpha"] = 1.0
channels_information.append((i, channel_info))
meta_data_dict["channels_information"] = channels_information
write_channels_metadata(
meta_data_dict=meta_data_dict, file_name=file_name, access_mode="w"
)
with h5py.File(file_name, "a") as f:
dataset_info_dirname = default_dataset_info_dirname
dataset_dirname = default_dataset_dirname
f.create_group(dataset_info_dirname + "/TimeInfo")
_ims_set_nullterm_str_attribute(
f[dataset_info_dirname]["TimeInfo"], "DatasetTimePoints", b"1"
)
_ims_set_nullterm_str_attribute(
f[dataset_info_dirname]["TimeInfo"], "FileTimePoints", b"1"
)
# For some reason the TimePoint attributes start with 1 and not 0.
_ims_set_nullterm_str_attribute(
f[dataset_info_dirname]["TimeInfo"],
"TimePoint1",
sitk_image.GetMetaData(time_metadata_key).encode("UTF-8")
if sitk_image.HasMetaDataKey(time_metadata_key)
else str(datetime.datetime.now()).encode("UTF-8"),
)
f.create_group(dataset_info_dirname + "/Image")
unit_str = (
sitk_image.GetMetaData(unit_metadata_key)
if sitk_image.HasMetaDataKey(unit_metadata_key)
else "mm"
)
_ims_set_nullterm_str_attribute(
f[dataset_info_dirname]["Image"], "Unit", unit_str.encode("UTF-8")
)
image_size = sitk_image.GetSize()[
0:3
] # Get the size for vector or scalar pixel types
_ims_set_nullterm_str_attribute(
f[dataset_info_dirname]["Image"], "X", str(image_size[0]).encode("UTF-8")
)
_ims_set_nullterm_str_attribute(
f[dataset_info_dirname]["Image"], "Y", str(image_size[1]).encode("UTF-8")
)
_ims_set_nullterm_str_attribute(
f[dataset_info_dirname]["Image"], "Z", str(image_size[2]).encode("UTF-8")
)
image_origin = sitk_image.GetOrigin()[0:3]
image_spacing = sitk_image.GetSpacing()[0:3]
min_ext = [org - 0.5 * spc for org, spc in zip(image_origin, image_spacing)]
image_edge = (
sitk_image.TransformIndexToPhysicalPoint(image_size)
if vector_pixels or number_of_channels == 1
else sitk_image.TransformIndexToPhysicalPoint(image_size + (0,))[0:3]
)
max_ext = [edg - 0.5 * spc for edg, spc in zip(image_edge, image_spacing)]
_ims_set_nullterm_str_attribute(
f[dataset_info_dirname]["Image"],
"ExtMin0",
str(min_ext[0]).encode("UTF-8"),
)
_ims_set_nullterm_str_attribute(
f[dataset_info_dirname]["Image"],
"ExtMin1",
str(min_ext[1]).encode("UTF-8"),
)
_ims_set_nullterm_str_attribute(
f[dataset_info_dirname]["Image"],
"ExtMin2",
str(min_ext[2]).encode("UTF-8"),
)
_ims_set_nullterm_str_attribute(
f[dataset_info_dirname]["Image"],
"ExtMax0",
str(max_ext[0]).encode("UTF-8"),
)
_ims_set_nullterm_str_attribute(
f[dataset_info_dirname]["Image"],
"ExtMax1",
str(max_ext[1]).encode("UTF-8"),
)
_ims_set_nullterm_str_attribute(
f[dataset_info_dirname]["Image"],
"ExtMax2",
str(max_ext[2]).encode("UTF-8"),
)
for i in range(number_of_channels):
grp = f.create_group(
dataset_dirname + f"/ResolutionLevel 0/TimePoint 0/Channel {i}"
)
_ims_set_nullterm_str_attribute(
grp, "ImageSizeX", str(image_size[0]).encode("UTF-8")
)