-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathdatasets.py
More file actions
979 lines (781 loc) · 50.2 KB
/
Copy pathdatasets.py
File metadata and controls
979 lines (781 loc) · 50.2 KB
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
import glob, csv, re, stat
from abc import ABCMeta
from itertools import combinations
from ContinuousRegistration.Source.metrics import tre, hausdorff, inverse_consistency, label_overlap
from ContinuousRegistration.Source.util import *
class Dataset:
"""
Base class for datasets. The derived classes need only to implement disk access and how to evaluate the dataset.
Everything else is handled by this class. See metrics.py for available evaluation metrics.
"""
__metaclass__ = ABCMeta
def generator(self):
for file_name in self.file_names:
yield file_name
@staticmethod
def make_shell_scripts(superelastix, blueprint_file_name, file_names, output_directory, script_file_extension):
if not os.path.exists(os.path.join(output_directory, 'sh')):
os.mkdir(os.path.join(output_directory, 'sh'))
COMMAND_TEMPLATE = '#!/bin/sh\n%s --conf "%s" --in %s %s --out DisplacementField="%s" --loglevel trace --logfile "%s"'
# Fixed=image0, Moving=image1, Output: image1_to_image0
root = splitext(file_names['disp_field_file_names'][0])[0]
shell_script_file_name_0 = os.path.join(output_directory, 'sh',
root.replace('/', '_').replace('\\', '_').replace('.', '_') + script_file_extension)
if 'mask_file_names' not in file_names:
file_names['mask_file_names'] = ('', '')
with open(shell_script_file_name_0, 'w') as shell_script:
shell_script.write(COMMAND_TEMPLATE % (superelastix, blueprint_file_name,
'FixedImage="%s" MovingImage="%s" ' % tuple(file_names['image_file_names']),
'FixedMask="%s" MovingMask="%s"' % tuple(file_names['mask_file_names']),
os.path.join(output_directory, file_names['disp_field_file_names'][0]),
os.path.splitext(shell_script_file_name_0)[0] + '.log'))
os.chmod(shell_script_file_name_0,
os.stat(shell_script_file_name_0).st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
logging.info('Wrote %s' % shell_script_file_name_0)
# Fixed=image1, Moving=image0, Output: image0_to_image1
root = splitext(file_names['disp_field_file_names'][1])[0]
shell_script_file_name_1 = os.path.join(output_directory, 'sh',
root.replace('/', '_').replace('\\', '_').replace('.', '_') + script_file_extension)
if 'mask_file_names' not in file_names:
file_names['mask_file_names'] = ('', '')
with open(shell_script_file_name_1, 'w') as shell_script:
shell_script.write(COMMAND_TEMPLATE % (superelastix, blueprint_file_name,
'FixedImage="%s" MovingImage="%s" ' % tuple(file_names['image_file_names'][::-1]),
'FixedMask="%s" MovingMask="%s"' % tuple(file_names['mask_file_names'][::-1]),
os.path.join(output_directory, file_names['disp_field_file_names'][1]),
os.path.splitext(shell_script_file_name_1)[0] + '.log'))
os.chmod(shell_script_file_name_1,
os.stat(shell_script_file_name_0).st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
logging.info('Wrote %s' % shell_script_file_name_1)
@staticmethod
def evaluate():
"""
The derived class should either call evaluate_point_set,
`evaluate_label_image` implement its own metrics, or combinations here
"""
pass
def evaluate_point_set(self, superelastix, file_names, output_directory):
""" Default evaluation method for point set ground truths
:param superelastix:
:param file_names:
:param output_directory:
:return:
"""
disp_field_paths = (
os.path.join(output_directory, file_names['disp_field_file_names'][0]),
os.path.join(output_directory, file_names['disp_field_file_names'][1]))
point_sets = (self.read_point_set(file_names['ground_truth_file_names'][0]),
self.read_point_set(file_names['ground_truth_file_names'][1]))
tre_0, tre_1 = tre(superelastix, point_sets, disp_field_paths)
hausdorff_0, hausdorff_1 = hausdorff(superelastix, point_sets, disp_field_paths)
inverse_consistency_0, inverse_consistency_1 = inverse_consistency(
superelastix, disp_field_paths, file_names['mask_file_names'])
return {
file_names['disp_field_file_names'][0]:
merge_dicts(tre_0, hausdorff_0, inverse_consistency_0),
file_names['disp_field_file_names'][1]:
merge_dicts(tre_1, hausdorff_1, inverse_consistency_1)
}
@staticmethod
def evaluate_label(superelastix, file_names, output_directory):
""" Default evaluation method for label ground truths
:param superelastix:
:param file_names:
:param output_directory:
:return:
"""
disp_field_paths = (
os.path.join(output_directory, file_names['disp_field_file_names'][0]),
os.path.join(output_directory, file_names['disp_field_file_names'][1]),
)
inverse_consistency_0, inverse_consistency_1 = inverse_consistency(
superelastix, disp_field_paths, file_names['mask_file_names'])
dice_0, dice_1 = label_overlap(superelastix, file_names['ground_truth_file_names'], disp_field_paths)
return {
file_names['disp_field_file_names'][0]:
merge_dicts(inverse_consistency_0, dice_0),
file_names['disp_field_file_names'][1]:
merge_dicts(inverse_consistency_1, dice_1)
}
@staticmethod
def make_images(superelastix, file_names, output_directory):
disp_field_0_file_name = os.path.join(output_directory, file_names['disp_field_file_names'][0])
disp_field_1_file_name = os.path.join(output_directory, file_names['disp_field_file_names'][1])
warp_image(superelastix, file_names['image_file_names'][1], disp_field_0_file_name, 'image')
warp_image(superelastix, file_names['image_file_names'][0], disp_field_1_file_name, 'image')
@staticmethod
def make_labels(superelastix, file_names, output_directory):
disp_field_0_file_name = os.path.join(output_directory, file_names['disp_field_file_names'][0])
disp_field_1_file_name = os.path.join(output_directory, file_names['disp_field_file_names'][1])
warp_image(superelastix, file_names['ground_truth_file_names'][1], disp_field_0_file_name, 'label')
warp_image(superelastix, file_names['ground_truth_file_names'][0], disp_field_1_file_name, 'label')
@staticmethod
def make_difference_images(superelastix, file_names, output_directory):
disp_field_0_file_name = os.path.join(output_directory, file_names['disp_field_file_names'][0])
disp_field_1_file_name = os.path.join(output_directory, file_names['disp_field_file_names'][1])
image_1_to_0 = sitk.ReadImage(warp_image(superelastix, file_names['image_file_names'][1], disp_field_0_file_name, 'result_image'))
image_0_to_1 = sitk.ReadImage(warp_image(superelastix, file_names['image_file_names'][0], disp_field_1_file_name, 'result_image'))
difference_image_1_to_0_base_name, difference_image_1_to_0_file_name_ext = splitext(disp_field_0_file_name)
difference_image_1_to_0_file_name = difference_image_1_to_0_base_name + '_difference_image' + difference_image_1_to_0_file_name_ext
difference_image_0_to_1_base_name, difference_image_0_to_1_file_name_ext = splitext(disp_field_0_file_name)
difference_image_0_to_1_file_name = difference_image_0_to_1_base_name + '_difference_image' + difference_image_0_to_1_file_name_ext
image_0 = sitk.ReadImage(file_names['image_file_names'][0])
image_1 = sitk.ReadImage(file_names['image_file_names'][1])
difference_image_1_to_0 = sitk.Cast(image_0, sitk.sitkFloat32) - sitk.Cast(image_1_to_0, sitk.sitkFloat32)
difference_image_0_to_1 = sitk.Cast(image_1, sitk.sitkFloat32) - sitk.Cast(image_0_to_1, sitk.sitkFloat32)
sitk.WriteImage(difference_image_1_to_0, difference_image_1_to_0_file_name)
sitk.WriteImage(difference_image_0_to_1, difference_image_0_to_1_file_name)
@staticmethod
def make_checkerboards(superelastix, file_names, output_directory):
disp_field_0_file_name = os.path.join(output_directory, file_names['disp_field_file_names'][0])
disp_field_1_file_name = os.path.join(output_directory, file_names['disp_field_file_names'][1])
disp_field_0_path, disp_field_0_ext = splitext(disp_field_0_file_name)
disp_field_1_path, disp_field_1_ext = splitext(disp_field_1_file_name)
checkerboard_0_file_name = disp_field_0_path + '_checkerboard' + disp_field_0_ext
checkerboard_1_file_name = disp_field_1_path + '_checkerboard' + disp_field_1_ext
image_0 = sitk.RescaleIntensity(sitk.ReadImage(file_names['image_file_names'][0]))
image_1 = sitk.RescaleIntensity(sitk.ReadImage(file_names['image_file_names'][1]))
# TODO: Better way of creating checkerboard pattern
big_number = 1e9
sitk.WriteImage(sitk.CheckerBoard(image_0 < -big_number, image_0 > -big_number, (5,)*image_0.GetDimension()), checkerboard_0_file_name)
sitk.WriteImage(sitk.CheckerBoard(image_1 < -big_number, image_1 > -big_number, (5,)*image_0.GetDimension()), checkerboard_1_file_name)
warp_image(superelastix, checkerboard_1_file_name, disp_field_0_file_name, 'checkerboard')
warp_image(superelastix, checkerboard_0_file_name, disp_field_1_file_name, 'checkerboard')
@staticmethod
def make_image_checkerboards(superelastix, file_names, output_directory):
disp_field_0_file_name = os.path.join(output_directory, file_names['disp_field_file_names'][0])
disp_field_1_file_name = os.path.join(output_directory, file_names['disp_field_file_names'][1])
disp_field_0_path, disp_field_0_ext = splitext(disp_field_0_file_name)
disp_field_1_path, disp_field_1_ext = splitext(disp_field_1_file_name)
image_checkerboard_0_file_name = disp_field_0_path + '_image_checkerboard' + disp_field_0_ext
image_checkerboard_1_file_name = disp_field_1_path + '_image_checkerboard' + disp_field_1_ext
warped_image_0_to_1_file_name = warp_image(superelastix, file_names['image_file_names'][0],
disp_field_1_file_name, 'result_image')
warped_image_1_to_0_file_name = warp_image(superelastix, file_names['image_file_names'][1],
disp_field_0_file_name, 'result_image')
image_0 = sitk.RescaleIntensity(sitk.ReadImage(file_names['image_file_names'][0]))
image_1 = sitk.RescaleIntensity(sitk.ReadImage(file_names['image_file_names'][1]))
sitk.WriteImage(sitk.CheckerBoard(image_0, sitk.RescaleIntensity(sitk.ReadImage(warped_image_1_to_0_file_name, image_1.GetPixelID())),
(5,)*image_0.GetDimension()), image_checkerboard_0_file_name)
sitk.WriteImage(sitk.CheckerBoard(image_1, sitk.RescaleIntensity(sitk.ReadImage(warped_image_0_to_1_file_name, image_1.GetPixelID())),
(5,)*image_1.GetDimension()), image_checkerboard_1_file_name)
@staticmethod
def make_label_checkerboards(superelastix, file_names, output_directory):
disp_field_0_file_name = os.path.join(output_directory, file_names['disp_field_file_names'][0])
disp_field_1_file_name = os.path.join(output_directory, file_names['disp_field_file_names'][1])
disp_field_0_path, disp_field_0_ext = splitext(disp_field_0_file_name)
disp_field_1_path, disp_field_1_ext = splitext(disp_field_1_file_name)
image_checkerboard_0_file_name = disp_field_0_path + '_label_checkerboard' + disp_field_0_ext
image_checkerboard_1_file_name = disp_field_1_path + '_label_checkerboard' + disp_field_1_ext
warped_image_0_file_name = warp_image(superelastix, file_names['ground_truth_file_names'][0],
disp_field_1_file_name, 'label_checkerboard')
warped_image_1_file_name = warp_image(superelastix, file_names['ground_truth_file_names'][1],
disp_field_0_file_name, 'label_checkerboard')
label_0 = sitk.ReadImage(file_names['ground_truth_file_names'][0])
label_1 = sitk.ReadImage(file_names['ground_truth_file_names'][1])
sitk.WriteImage(sitk.CheckerBoard(label_0, sitk.ReadImage(warped_image_1_file_name, label_0.GetPixelID()),
(5,) * label_0.GetDimension()),
image_checkerboard_0_file_name)
sitk.WriteImage(sitk.CheckerBoard(label_1, sitk.ReadImage(warped_image_0_file_name, label_1.GetPixelID()),
(5,) * label_1.GetDimension()),
image_checkerboard_1_file_name)
@staticmethod
def read_point_set(file_name):
return read_pts(file_name)
class BRAIN2D(Dataset):
"""
Convenience data set for quickly testing registrations of brains in 2d.
Useful when tuning very slow methods like ANTs. Simply wraps some
of our testing data and exposes it as a dataset.
"""
def __init__(self, input_directory, output_directory):
self.name = 'BRAIN2D'
self.category = 'Brain'
self.file_names = [{
'image_file_names': (
os.path.join(input_directory, 'r16slice.nii.gz'),
os.path.join(input_directory, 'r64slice.nii.gz')
),
'ground_truth_file_names': (
os.path.join(input_directory, 'r16slice_mask.nii.gz'),
os.path.join(input_directory, 'r64slice_mask.nii.gz'),
),
'mask_file_names': (
os.path.join(input_directory, 'r16slice_mask.nii.gz'),
os.path.join(input_directory, 'r64slice_mask.nii.gz'),
)
}]
self.file_names[0]['disp_field_file_names'] = create_disp_field_names(self.file_names[0]['image_file_names'], self.name)
def evaluate(self, superelastix, file_names, output_directory):
return self.evaluate_label(superelastix, file_names, output_directory)
class LUNG2D(Dataset):
"""
Convenience data set for quickly testing registrations of lungs in 2d.
Useful when tuning very slow methods like ANTs. Simply wraps some
of our testing data and exposes it as a test dataset.
"""
def __init__(self, input_directory, output_directory):
self.name = 'LUNG2D'
self.category = 'Lung'
self.file_names = [{
'image_file_names': (
os.path.join(input_directory, 'Lung2dFixedImage.nii.gz'),
os.path.join(input_directory, 'Lung2dMovingImage.nii.gz')
),
'ground_truth_file_names': (
os.path.join(input_directory, 'Lung2dFixedMask.nii.gz'),
os.path.join(input_directory, 'Lung2dMovingMask.nii.gz'),
),
'mask_file_names': (
os.path.join(input_directory, 'Lung2dFixedMask.nii.gz'),
os.path.join(input_directory, 'Lung2dMovingMask.nii.gz'),
)
}]
self.file_names[0]['disp_field_file_names'] = create_disp_field_names(self.file_names[0]['image_file_names'], self.name)
def evaluate(self, superelastix, file_names, output_directory):
return self.evaluate_label(superelastix, file_names, output_directory)
class CUMC12(Dataset):
def __init__(self, input_directory, output_directory, max_number_of_registrations):
self.name = 'CUMC12'
self.category = 'Brain'
self.input_directory = input_directory
self.file_names = []
image_file_names = sorted([os.path.join(input_directory, 'Heads', image)
for image in os.listdir(os.path.join(input_directory, 'Heads'))
if image.endswith('.hdr')])
image_file_names = [pair for pair in combinations(image_file_names, 2)]
image_file_names = take(image_file_names, max_number_of_registrations // 2)
label_file_names = sorted([os.path.join(input_directory, 'Atlases', atlas)
for atlas in os.listdir(os.path.join(input_directory, 'Atlases'))
if atlas.endswith('.hdr')])
label_file_names = [pair for pair in combinations(label_file_names, 2)]
label_file_names = take(label_file_names, max_number_of_registrations // 2)
disp_field_file_names = [create_disp_field_names(image_file_name_pair, self.name)
for image_file_name_pair in image_file_names]
mask_file_names = [create_mask_by_thresholding(label_file_name, disp_field_file_name, output_directory, 0., 32, 16)
for label_file_name, disp_field_file_name in zip(label_file_names, disp_field_file_names)]
for image_file_name, mask_file_name, label_file_name, disp_field_file_name \
in zip(image_file_names, mask_file_names, label_file_names, disp_field_file_names):
self.file_names.append({
'image_file_names': image_file_name,
'mask_file_names': mask_file_name,
'ground_truth_file_names': label_file_name,
'disp_field_file_names': disp_field_file_name
})
def evaluate(self, superelastix, file_names, output_directory):
return self.evaluate_label(superelastix, file_names, output_directory)
class DIRLAB(Dataset):
def __init__(self, input_directory, mask_directory, output_directory, max_number_of_registrations):
self.name = 'DIRLAB'
self.category = 'Lung'
self.input_directory = input_directory
file_names = []
# DIR-LAB provides raw binary image only so we write mhd header files for loading the data.
# The image information was retrieved from https://www.dir-lab.com/ReferenceData.html
dirlab_image_information_file_name = os.path.join(os.path.dirname(__file__), "dirlab_image_information.csv")
# Find corresponding subdirectory which contains the id and associate it with this line of the csv file.
# TODO: Maybe not the prettiest approach
sub_directories = [directory for directory in os.listdir(self.input_directory)
if os.path.isdir(os.path.join(input_directory, directory))]
sub_directory_ids = list(map(lambda str: int(re.search(r'\d+', str).group()), sub_directories))
dirlab_image_information = dict()
for image_information in csv.DictReader(open(dirlab_image_information_file_name)):
dirlab_image_information[image_information['id']] = image_information
dirlab_image_information[image_information['id']]['sub_directory'] = \
sub_directories[sub_directory_ids.index(int(image_information['id']))]
def _write_mhd_file(img_file_name):
mhd_file_name = os.path.join(output_directory, 'tmp', 'images',
self.name,
os.path.splitext(os.path.basename(
img_file_name))[0] + '.mhd')
with open(mhd_file_name, 'w') as mhd:
mhd.write('ObjectType = Image\n')
mhd.write('NDims = 3\n')
mhd.write('BinaryData = True\n')
mhd.write('DimSize = %s %s %s\n' % (
dirlab_image_information[id]['x_size'],
dirlab_image_information[id]['y_size'],
dirlab_image_information[id]['z_size']))
mhd.write('ElementSpacing = %s %s %s\n' % (
dirlab_image_information[id]['x_spacing'],
dirlab_image_information[id]['y_spacing'],
dirlab_image_information[id]['z_spacing']))
mhd.write('ElementType = MET_USHORT\n')
mhd.write('ElementDataFile = %s\n' % img_file_name)
return mhd_file_name
os.makedirs(os.path.join(output_directory, 'tmp', 'point_sets', self.name), exist_ok=True)
# Now we have all the information necessary and can generate the header files
for id in dirlab_image_information:
img_0_file_name = glob.glob(os.path.join(input_directory,
dirlab_image_information[id]['sub_directory'],
'Images', '*T00*.img'))[0]
img_1_file_name = glob.glob(os.path.join(input_directory,
dirlab_image_information[id]['sub_directory'],
'Images', '*T50*.img'))[0]
# Write mhd header files
os.makedirs(os.path.join(output_directory, 'tmp', 'images', self.name), exist_ok=True)
mhd_0_file_name = _write_mhd_file(img_0_file_name)
mhd_1_file_name = _write_mhd_file(img_1_file_name)
index_set_0_file_name = glob.glob(os.path.join(input_directory,
dirlab_image_information[id]['sub_directory'],
'ExtremePhases', '*T00_xyz.txt'))[0]
index_set_1_file_name = glob.glob(os.path.join(input_directory,
dirlab_image_information[id]['sub_directory'],
'ExtremePhases', '*T50_xyz.txt'))[0]
point_set_0 = index2point(sitk.ReadImage(mhd_0_file_name), self.read_point_set(index_set_0_file_name))
point_set_1 = index2point(sitk.ReadImage(mhd_1_file_name), self.read_point_set(index_set_1_file_name))
point_set_0_file_name = os.path.join(output_directory, 'tmp', 'point_sets', self.name, os.path.basename(index_set_0_file_name))
point_set_1_file_name = os.path.join(output_directory, 'tmp', 'point_sets', self.name, os.path.basename(index_set_1_file_name))
write_pts(point_set_0, point_set_0_file_name)
write_pts(point_set_1, point_set_1_file_name)
image_file_names = (mhd_0_file_name, mhd_1_file_name)
point_set_file_names = (point_set_0_file_name, point_set_1_file_name)
disp_field_file_names = (
os.path.join(self.name, dirlab_image_information[id]['sub_directory'], 'moving50_to_fixed00.mha'),
os.path.join(self.name, dirlab_image_information[id]['sub_directory'], 'moving00_to_fixed50.mha')
)
if mask_directory is not None and os.path.exists(mask_directory):
mask_file_names = (
os.path.join(mask_directory, dirlab_image_information[id]['sub_directory'],
'Images', os.path.basename(mhd_0_file_name)),
os.path.join(mask_directory, dirlab_image_information[id]['sub_directory'],
'Images', os.path.basename(mhd_1_file_name))
)
else:
# If no mask was provided, just generate mask filled with ones
logging.warning('Masks not found for DIRLAB. Creating mask with elements set to one.')
mask_file_names = [
create_mask_by_size(image_file_names[i],
os.path.join(output_directory, 'tmp', 'masks',
disp_field_file_names[i]))
for i in range(2)
]
file_names.append({
'image_file_names': image_file_names,
'mask_file_names': mask_file_names,
'ground_truth_file_names': point_set_file_names,
'disp_field_file_names': disp_field_file_names
})
self.file_names = take(sort_file_names(file_names),
max_number_of_registrations // 2)
def evaluate(self, superelastix, file_names, output_directory):
return self.evaluate_point_set(superelastix, file_names, output_directory)
@staticmethod
def read_point_set(file_name):
return read_pts(file_name)
class EMPIRE(Dataset):
def __init__(self, input_directory, max_number_of_registrations):
self.name = 'EMPIRE'
self.category = 'Lung'
file_names = []
for i in range(1, 31):
image_file_names = (
os.path.join(input_directory, 'scans', "%02d" % i + '_Fixed.mhd'),
os.path.join(input_directory, 'scans', "%02d" % i + '_Moving.mhd')
)
mask_file_names = (
os.path.join(input_directory, 'lungMasks', "%02d" % i + '_Fixed.mhd'),
os.path.join(input_directory, 'lungMasks', "%02d" % i + '_Moving.mhd')
)
# TODO: Find out output format
disp_field_file_names = (
os.path.join(self.name, "%02d" % i + '_moving1_to_fixed0.nii.gz'),
os.path.join(self.name, "%02d" % i + '_moving0_to_fixed1.nii.gz')
)
file_names.append({
'image_file_names': image_file_names,
'mask_file_names': mask_file_names,
'disp_field_file_names': disp_field_file_names
})
self.file_names = take(sort_file_names(file_names),
max_number_of_registrations // 2)
def evaluate(self, superelastix, file_names, output_directory):
# TODO: Submit to EMPIRE
pass
@staticmethod
def read_point_set(file_name):
return read_pts(file_name)
# Download from http://brain-development.org/brain-atlases/individual-adult-brain-atlases-new/.
# Fill in info and click on "Adult individual atlases (30 MR images and manual segmentations;
# A Hammers, R Allom et al. 2003, IS Gousias et al. 2008, I. Faillenot et al. 2017)". It should
# point to this link:
# http://biomedic.doc.ic.ac.uk/brain-development/downloads/hammers/Hammers_mith-n30r95.tar.gz
# Do not download directly. Fill in information.
class HAMMERS(Dataset):
def __init__(self, input_directory, output_directory, max_number_of_registrations):
self.name = 'HAMMERS'
self.category = 'Brain'
self.input_directory = input_directory
self.file_names = []
image_file_names = sorted([os.path.join(input_directory, image)
for image in os.listdir(input_directory)
if image.endswith('.nii.gz') and not 'seg' in image])
image_file_names = [pair for pair in combinations(image_file_names, 2)]
image_file_names = take(image_file_names, max_number_of_registrations // 2)
label_file_names = sorted([os.path.join(input_directory, image)
for image in os.listdir(input_directory)
if image.endswith('seg.nii.gz')])
label_file_names = [pair for pair in combinations(label_file_names, 2)]
label_file_names = take(label_file_names, max_number_of_registrations // 2)
disp_field_file_names = [create_disp_field_names(image_file_name_pair, self.name)
for image_file_name_pair in image_file_names]
mask_file_names = [create_mask_by_thresholding(label_file_name, disp_field_file_name, output_directory, 0., 32, 16)
for label_file_name, disp_field_file_name in zip(label_file_names, disp_field_file_names)]
for image_file_name, mask_file_name, label_file_name, disp_field_file_name \
in zip(image_file_names, mask_file_names, label_file_names, disp_field_file_names):
self.file_names.append({
'image_file_names': image_file_name,
'mask_file_names': mask_file_name,
'ground_truth_file_names': label_file_name,
'disp_field_file_names': disp_field_file_name
})
def evaluate(self, superelastix, file_names, output_directory):
return self.evaluate_label(superelastix, file_names, output_directory)
class IBSR18(Dataset):
def __init__(self, input_directory, output_directory, max_number_of_registrations):
self.name = 'IBSR18'
self.category = 'Brain'
self.input_directory = input_directory
self.file_names = []
image_file_names = sorted([os.path.join(input_directory, subdirectory, image)
for subdirectory in os.listdir(input_directory) if os.path.isdir(os.path.join(input_directory, subdirectory))
for image in os.listdir(os.path.join(input_directory, subdirectory))
if image.endswith('_ana_strip.nii.gz')])
image_file_names = [pair for pair in combinations(image_file_names, 2)]
image_file_names = take(image_file_names, max_number_of_registrations // 2)
label_file_names = sorted([os.path.join(input_directory, subdirectory, image)
for subdirectory in os.listdir(input_directory) if os.path.isdir(os.path.join(input_directory, subdirectory))
for image in os.listdir(os.path.join(input_directory, subdirectory))
if image.endswith('_seg_ana.nii.gz')])
label_file_names = [pair for pair in combinations(label_file_names, 2)]
label_file_names = take(label_file_names, max_number_of_registrations // 2)
mask_file_names = sorted([os.path.join(input_directory, subdirectory, image)
for subdirectory in os.listdir(input_directory) if os.path.isdir(os.path.join(input_directory, subdirectory))
for image in os.listdir(os.path.join(input_directory, subdirectory))
if image.endswith('_ana_brainmask.nii.gz')])
mask_file_names = [pair for pair in combinations(mask_file_names, 2)]
mask_file_names = take(mask_file_names, max_number_of_registrations // 2)
disp_field_file_names = [create_disp_field_names(image_file_name_pair, self.name)
for image_file_name_pair in image_file_names]
for image_file_name, mask_file_name, label_file_name, disp_field_file_name \
in zip(image_file_names, mask_file_names, label_file_names, disp_field_file_names):
self.file_names.append({
'image_file_names': image_file_name,
'mask_file_names': mask_file_name,
'ground_truth_file_names': label_file_name,
'disp_field_file_names': disp_field_file_name
})
def evaluate(self, superelastix, file_names, output_directory):
return self.evaluate_label(superelastix, file_names, output_directory)
class LPBA40(Dataset):
def __init__(self, input_directory, output_directory, max_number_of_registrations):
self.name = 'LPBA40'
self.category = 'Brain'
self.input_directory = input_directory
self.file_names = []
image_file_names = []
for sub_directory in os.listdir(os.path.join(input_directory, 'delineation_space')):
if os.path.isdir(os.path.join(input_directory, 'delineation_space', sub_directory)):
for image_file_name in os.listdir(os.path.join(input_directory, 'delineation_space', sub_directory)):
if image_file_name.endswith('delineation.skullstripped.hdr'):
image_file_names.append(os.path.join('delineation_space', sub_directory, image_file_name))
label_file_names = []
for sub_directory in os.listdir(os.path.join(input_directory, 'delineation_space')):
if os.path.isdir(os.path.join(input_directory, 'delineation_space', sub_directory)):
for label_file_name in glob.glob(os.path.join(input_directory, 'delineation_space', sub_directory, 'S*delineation.structure.label.hdr')):
input_label_file_name = os.path.join(input_directory, 'delineation_space', sub_directory, label_file_name)
output_label_file_name = os.path.join(output_directory, 'tmp', 'removed_labels', os.path.basename(label_file_name))
# Remove labels outside skullstripped image
if not os.path.exists(output_label_file_name):
os.makedirs(os.path.dirname(output_label_file_name), exist_ok=True)
label = sitk.ChangeLabel(sitk.ReadImage(input_label_file_name), {181: 0, 182: 0})
sitk.WriteImage(label, output_label_file_name)
label_file_names.append(output_label_file_name)
image_file_names = [pair for pair in combinations(sorted(image_file_names), 2)]
label_file_names = [pair for pair in combinations(sorted(label_file_names), 2)]
image_file_names = take(image_file_names, max_number_of_registrations // 2)
label_file_names = take(label_file_names, max_number_of_registrations // 2)
image_file_names = [create_identity_world_information(pair, self.name, input_directory, output_directory)
for pair in image_file_names]
disp_field_file_names = [create_disp_field_names(image_file_name_pair, self.name)
for image_file_name_pair in image_file_names]
mask_file_names = [create_mask_by_thresholding(label, disp_field, output_directory, 0., 32, 30)
for label, disp_field in zip(label_file_names, disp_field_file_names)]
for image_file_name, mask_file_name, label_file_name, disp_field_file_name \
in zip(image_file_names, mask_file_names, label_file_names, disp_field_file_names):
self.file_names.append({
'image_file_names': image_file_name,
'mask_file_names': mask_file_name,
'ground_truth_file_names': label_file_name,
'disp_field_file_names': disp_field_file_name
})
def evaluate(self, superelastix, file_names, output_directory):
return self.evaluate_label(superelastix, file_names, output_directory)
class MGH10(Dataset):
def __init__(self, input_directory, output_directory, max_number_of_registrations):
self.name = 'MGH10'
self.category = 'Brain'
self.input_directory = input_directory
self.file_names = []
image_file_names = sorted([os.path.join(input_directory, 'Heads', image)
for image in os.listdir(os.path.join(input_directory, 'Heads'))
if image.endswith('.hdr')])
image_file_names = [pair for pair in combinations(image_file_names, 2)]
image_file_names = take(image_file_names, max_number_of_registrations // 2)
label_file_names = sorted([os.path.join(input_directory, 'Atlases', atlas)
for atlas in os.listdir(os.path.join(input_directory, 'Atlases'))
if atlas.endswith('.hdr')])
label_file_names = [pair for pair in combinations(label_file_names, 2)]
label_file_names = take(label_file_names, max_number_of_registrations // 2)
disp_field_file_names = [create_disp_field_names(image_file_name_pair, self.name)
for image_file_name_pair in image_file_names]
# Label images do not have any world coordinate information
label_file_names = [copy_information_from_images_to_labels(image_file_name_pair,
label_file_name_pair,
disp_field_file_name_pair,
output_directory,
'MET_USHORT')
for image_file_name_pair, label_file_name_pair, disp_field_file_name_pair
in zip(image_file_names, label_file_names, disp_field_file_names)]
mask_file_names = [create_mask_by_thresholding(label_file_name, disp_field, output_directory, 0., 32, 16)
for label_file_name, disp_field in zip(label_file_names, disp_field_file_names)]
for image_file_name, mask_file_name, label_file_name, disp_field_file_name \
in zip(image_file_names, mask_file_names, label_file_names, disp_field_file_names):
self.file_names.append({
'image_file_names': image_file_name,
'mask_file_names': mask_file_name,
'ground_truth_file_names': label_file_name,
'disp_field_file_names': disp_field_file_name
})
def evaluate(self, superelastix, file_names, output_directory):
return self.evaluate_label(superelastix, file_names, output_directory)
class POPI(Dataset):
def __init__(self, input_directory, mask_directory, output_directory, max_number_of_registrations):
self.name = 'POPI'
self.category = 'Lung'
self.input_directory = input_directory
file_names = []
sub_directories = [directory for directory in os.listdir(self.input_directory)
if os.path.isdir(os.path.join(input_directory, directory))]
for sub_directory in sub_directories:
image_file_names = (os.path.join(input_directory, sub_directory, 'mhd', '00.mhd'),
os.path.join(input_directory, sub_directory, 'mhd', '50.mhd'))
point_set_file_names = (os.path.join(input_directory, sub_directory, 'pts', '00.pts'),
os.path.join(input_directory, sub_directory, 'pts', '50.pts'))
disp_field_file_names = (os.path.join(self.name, sub_directory, 'moving50_to_fixed00.nii.gz'),
os.path.join(self.name, sub_directory, 'moving00_to_fixed50.nii.gz'))
if mask_directory is not None and os.path.exists(mask_directory):
mask_file_names = (os.path.join(mask_directory, sub_directory, 'mhd', '00.mhd'),
os.path.join(mask_directory, sub_directory, 'mhd', '50.mhd'))
else:
# If no mask was provided, just generate mask filled with ones
logging.warning('Masks not found for POPI. Creating mask with elements set to one.')
mask_file_names = [
create_mask_by_size(image_file_names[i],
os.path.join(output_directory, 'tmp', 'masks',
disp_field_file_names[i]))
for i in range(2)
]
file_names.append({
"image_file_names": image_file_names,
"mask_file_names": mask_file_names,
"ground_truth_file_names": point_set_file_names,
"disp_field_file_names": disp_field_file_names
})
self.file_names = take(sort_file_names(file_names),
max_number_of_registrations // 2)
def evaluate(self, superelastix, file_names, output_directory):
return self.evaluate_point_set(superelastix, file_names, output_directory)
@staticmethod
def read_point_set(file_name):
return read_pts(file_name)
class SPREAD(Dataset):
def __init__(self, input_directory, output_directory, max_number_of_registrations):
self.name = 'SPREAD'
self.category = 'Lung'
self.input_directory = input_directory
file_names = []
sub_directories = [directory
for directory in os.listdir(os.path.join(self.input_directory, 'mhd'))
if os.path.isdir(os.path.join(self.input_directory, 'mhd', directory))]
for sub_directory in sub_directories:
image_file_names = (os.path.join(input_directory, 'mhd', sub_directory, 'baseline_1.mha'),
os.path.join(input_directory, 'mhd', sub_directory, 'followup_1.mha'))
mask_file_names = (os.path.join(input_directory, 'mhd', sub_directory, 'baseline_1_mask_vi_semiauto.mha'),
os.path.join(input_directory, 'mhd', sub_directory, 'followup_1_mask_vi_semiauto.mha'))
point_set_0_file_name = os.path.join(input_directory, 'groundtruth', 'distinctivePoints',
sub_directory + '_baseline_1_Cropped_point.txt')
point_set_1_file_name = os.path.join(input_directory, 'groundtruth', 'annotate', 'Consensus',
sub_directory + '_b1f1_point.txt')
point_set_file_names = (point_set_0_file_name, point_set_1_file_name)
disp_field_file_names = (os.path.join(self.name, sub_directory, 'movingfollowup_to_fixedbaseline.mha'),
os.path.join(self.name, sub_directory, 'movingbaseline_to_fixedfollowup.mha'))
file_names.append({
"image_file_names": image_file_names,
"mask_file_names": mask_file_names,
"ground_truth_file_names": point_set_file_names,
"disp_field_file_names": disp_field_file_names
})
self.file_names = take(sort_file_names(file_names),
max_number_of_registrations // 2)
def evaluate(self, superelastix, file_names, output_directory):
return self.evaluate_point_set(superelastix, file_names, output_directory)
@staticmethod
def read_point_set(file_name):
return read_pts(file_name, skiprows=2)
class HBIA(Dataset):
"""
a wrapper for dataset with stain Histology provided by CMP-BIA sections
http://cmp.felk.cvut.cz/~borovji3/?page=dataset
The dataset contains several sets (1st level) of stained tissues in given
scales (2nd level) where the name is `scale_<NUMBER>sc`.
The next level (3rd level) belong to the images and landmarks.
The images share the same name with landmark files and the point are set
across all slices of the particular tissue so the registration pairs are
generated one against all (we can skip symmetric pairs like A->B and B->A)
"""
PATTERN_FOLDER_SCALE = 'scale-%ipc'
def __init__(self, input_directory, output_directory,
max_number_of_registrations, scale=None):
self.name = 'HistoBIA'
self.category = 'cancer'
self.input_directory = input_directory
# set default scale 10%
scale = 10 if scale is None else scale
sub_folders = [d for d in os.listdir(self.input_directory)
if os.path.isdir(os.path.join(input_directory, d))]
assert len(sub_folders) > 0, 'missing downloaded dataset files'
# read all available scales from the first set
scales = [int(re.findall('\\d+', d)[0])
for d in os.listdir(os.path.join(self.input_directory, sub_folders[0]))
if os.path.isdir(os.path.join(input_directory, sub_folders[0], d))]
assert scale in scales, 'not supported dataset scale (%i) from %s' \
% (scale, repr(scales))
file_names = []
for dir_set in sorted(sub_folders):
# create the path to the image/points destination
path_images = os.path.join(self.input_directory, dir_set,
self.PATTERN_FOLDER_SCALE % scale)
# listing all images in the set with given scale
list_image_names = sorted([n for n in os.listdir(path_images)
if os.path.splitext(n)[-1] in ['.png', '.jpg']])
# creating pairs one-to-all with and skipp duplicit symetric pairs
for i, image_name_0 in enumerate(list_image_names):
pints_name_0 = os.path.splitext(image_name_0)[0] + '.csv'
for image_name_1 in list_image_names[i+1:]:
pints_name_1 = os.path.splitext(image_name_1)[0] + '.csv'
# compose the registration image pair
image_names = (os.path.join(path_images, image_name_0),
os.path.join(path_images, image_name_1))
# and pair of landmarks related to the image pair
point_set_names = (os.path.join(path_images, pints_name_0),
os.path.join(path_images, pints_name_1))
# If no mask was provided, generate mask filled with ones
mask_names = [
create_mask_by_size(image_names[i],
os.path.join(output_directory,
'tmp', 'masks',
image_names[i]))
for i in range(2)
]
displace_field_names = \
create_disp_field_names(image_names, self.name)
file_names.append({
"image_file_names": image_names,
"mask_file_names": mask_names,
"ground_truth_file_names": point_set_names,
"disp_field_file_names": displace_field_names,
})
self.file_names = take(sort_file_names(file_names),
max_number_of_registrations // 2)
def evaluate(self, superelastix, file_names, output_directory):
return self.evaluate_point_set(superelastix, file_names, output_directory)
def read_point_set(self, file_name):
return self.read_csv(file_name)
def load_datasets(parameters):
datasets = dict()
if parameters.brain2d_input_directory is not None:
logging.info('Loading dataset Brain2d.')
brain2d = BRAIN2D(parameters.brain2d_input_directory,
parameters.output_directory)
datasets[brain2d.name] = brain2d
logging.debug('Using files:\n{0}'.format(json.dumps(brain2d.file_names, indent=2)))
if parameters.lung2d_input_directory is not None:
logging.info('Loading dataset Lung2d.')
lung2d = LUNG2D(parameters.lung2d_input_directory,
parameters.output_directory)
datasets[lung2d.name] = lung2d
logging.debug('Using files:\n{0}'.format(json.dumps(lung2d.file_names, indent=2)))
if parameters.cumc12_input_directory is not None:
logging.info('Loading dataset CUMC12.')
cumc12 = CUMC12(parameters.cumc12_input_directory,
parameters.output_directory,
parameters.max_number_of_registrations_per_dataset)
datasets[cumc12.name] = cumc12
logging.debug('Using files:\n{0}'.format(json.dumps(cumc12.file_names, indent=2)))
if parameters.dirlab_input_directory is not None:
logging.info('Loading dataset DIRLAB.')
dirlab = DIRLAB(parameters.dirlab_input_directory,
parameters.dirlab_mask_directory,
parameters.output_directory,
parameters.max_number_of_registrations_per_dataset)
datasets[dirlab.name] = dirlab
logging.debug('Using files:\n{0}'.format(json.dumps(dirlab.file_names, indent=2)))
if parameters.empire_input_directory is not None:
logging.info('Loading dataset EMPIRE.')
empire = EMPIRE(parameters.empire_input_directory,
parameters.max_number_of_registrations_per_dataset)
datasets[empire.name] = empire
logging.debug('Using files:\n{0}'.format(json.dumps(empire.file_names, indent=2)))
if parameters.hammers_input_directory is not None:
logging.info('Loading dataset HAMMERS.')
hammers = HAMMERS(parameters.hammers_input_directory,
parameters.output_directory,
parameters.max_number_of_registrations_per_dataset)
datasets[hammers.name] = hammers
logging.debug('Using files:\n{0}'.format(json.dumps(hammers.file_names, indent=2)))
if parameters.ibsr18_input_directory is not None:
logging.info('Loading dataset IBSR18.')
ibsr18 = IBSR18(parameters.ibsr18_input_directory,
parameters.output_directory,
parameters.max_number_of_registrations_per_dataset)
datasets[ibsr18.name] = ibsr18
logging.debug('Using files:\n{0}'.format(json.dumps(ibsr18.file_names, indent=2)))
if parameters.lpba40_input_directory is not None:
logging.info('Loading dataset LPBA40.')
lpba40 = LPBA40(parameters.lpba40_input_directory,
parameters.output_directory,
parameters.max_number_of_registrations_per_dataset)
datasets[lpba40.name] = lpba40
logging.debug('Using files:\n{0}'.format(json.dumps(lpba40.file_names, indent=2)))
if parameters.mgh10_input_directory is not None:
logging.info('Loading dataset MGH10.')
mgh10 = MGH10(parameters.mgh10_input_directory,
parameters.output_directory,
parameters.max_number_of_registrations_per_dataset)
datasets[mgh10.name] = mgh10
logging.debug('Using files:\n{0}'.format(json.dumps(mgh10.file_names, indent=2)))
if parameters.popi_input_directory is not None:
logging.info('Loading dataset POPI.')
popi = POPI(parameters.popi_input_directory,
parameters.popi_mask_directory,
parameters.output_directory,
parameters.max_number_of_registrations_per_dataset)
datasets[popi.name] = popi
logging.debug('Using files:\n{0}'.format(json.dumps(popi.file_names, indent=2)))
if parameters.spread_input_directory is not None:
logging.info('Loading dataset SPREAD.')
spread = SPREAD(parameters.spread_input_directory,
parameters.output_directory,
parameters.max_number_of_registrations_per_dataset)
datasets[spread.name] = spread
logging.debug('Using files:\n{0}'.format(json.dumps(spread.file_names, indent=2)))
if parameters.hbia_input_directory is not None:
logging.info('Loading dataset HistoBIA.')
hbia = HBIA(parameters.hbia_input_directory,
parameters.output_directory,
parameters.max_number_of_registrations_per_dataset,
scale=10)
datasets[hbia.name] = hbia
logging.debug('Using files:\n{0}'.format(json.dumps(hbia.file_names, indent=2)))
return datasets