-
Notifications
You must be signed in to change notification settings - Fork 0
/
ivDicom.py
1621 lines (1361 loc) · 51 KB
/
ivDicom.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
#--------------------------------------------------------------------------
# Software: img2dcm
# Copyright: (C) 2001 Centro de Pesquisas Renato Archer
# Homepage: https://bitbucket.org/tfmoraes/img2dcm
# Contact: [email protected]
# License: GNU - GPL 2 (LICENSE.txt/LICENCA.txt)
#--------------------------------------------------------------------------
# Este programa e software livre; voce pode redistribui-lo e/ou
# modifica-lo sob os termos da Licenca Publica Geral GNU, conforme
# publicada pela Free Software Foundation; de acordo com a versao 2
# da Licenca.
#
# Este programa eh distribuido na expectativa de ser util, mas SEM
# QUALQUER GARANTIA; sem mesmo a garantia implicita de
# COMERCIALIZACAO ou de ADEQUACAO A QUALQUER PROPOSITO EM
# PARTICULAR. Consulte a Licenca Publica Geral GNU para obter mais
# detalhes.
#--------------------------------------------------------------------------
# In DICOM file format, if multiple values are present for the
# "Window Center" (Level) and "Window Width", both attributes
# shall have the same number of values and shall be considered as
# pairs. Multiple values indicate that multiple alternative views
# may be presented. In this Parser, by default, we only consider
# one pair per time. This pair is determined by WL_PRESET
# constant or might be set by the user when calling GetWindowWidth
# and GetWindowLevel.
WL_PRESET = 0 # index of selected window and level tuple (if multiple)
WL_MULT = 0 # allow selection of multiple window and level tuples if 1
# dictionary to be used by module functions
info = {}
# keys to be used to generate dictionary info
INFO_KEYS = \
['AcquisitionDate',
'AcquisitionGantryTilt',
'AcquisitionModality',
'AcquisitionNumber',
'AcquisionSequence',
'AcquisitionTime',
'EquipmentKVP',
'EquipmentInstitutionName',
'EquipmentManufacturer',
'EquipmentXRayTubeCurrent',
'ImageColumnOrientation',
'ImageConvolutionKernel',
'ImageDataType',
'ImageLocation',
'ImageNumber',
'ImagePixelSpacingX',
'ImagePixelSpacingY',
'ImagePosition',
'ImageRowOrientation',
'ImageSamplesPerPixel',
'ImageSeriesNumber',
'ImageThickness',
'ImageWindowLevel',
'ImageWindowWidth',
'PatientAge',
'PatientBirthDate',
'PatientGender',
'PatientName',
'PhysicianName',
'StudyID',
'StudyInstanceUID',
'StudyAdmittingDiagnosis',
]
import vtk
import vtkgdcm
import gdcm
class Parser():
"""
Medical image parser. Used to parse medical image tags.
It supports:
- ACR-NEMA version 1 and 2
- DICOM 3.0 (including JPEG-lossless and lossy-, RLE)
- Papyrus V2 and V3 file headers
GDCM was used to develop this class.
How to use:
>>> image = ivMedicalImageParser()
>>> image.SetFileName("/home/usr/0.dcm")
# then you might call method related to tag of interest
>>> print image.GetImagePixelSpacing()
"""
def __init__(self):
self.filename = None
self.vtkgdcm_reader = vtkgdcm.vtkGDCMImageReader()
def GetAcquisitionDate(self):
"""
Return string containing the acquisition date using the
format "dd/mm/yyyy".
Return "" (empty string) if not set.
DICOM standard tag (0x0008,0x0022) was used.
"""
# TODO: internationalize data
date = self.vtkgdcm_reader.GetMedicalImageProperties()\
.GetAcquisitionDate()
if (date):
year = date[0:4]
month = date[4:6]
day = date[6:8]
return day + "/" + month + "/" + year
return ""
def GetAcquisitionNumber(self):
"""
Return integer related to acquisition of this slice.
Return None if field is not defined.
DICOM standard tag (0x0020, 0x0012) was used.
"""
tag = gdcm.Tag(0x0020, 0x0012)
ds = self.gdcm_reader.GetFile().GetDataSet()
if ds.FindDataElement(tag):
data = ds.GetDataElement(tag).GetValue()
if (data):
return int(str(data))
return None
def GetAcquisitionTime(self):
"""
Return string containing the acquisition time using the
format "hh:mm:ss".
Return "" (empty string) if not set.
DICOM standard tag (0x0008,0x0032) was used.
"""
time = self.vtkgdcm_reader.GetMedicalImageProperties()\
.GetAcquisitionTime()
if (time):
hh = time[0:2]
mm = time[2:4]
ss = time[4:6]
return hh + ":" + mm + ":" + ss
return ""
def GetPatientAdmittingDiagnosis(self):
"""
Return admitting diagnosis description (string).
Return "" (empty string) if not defined.
DICOM standard tag (0x0008,0x1080) was used.
"""
tag = gdcm.Tag(0x0008, 0x1080)
sf = gdcm.StringFilter()
sf.SetFile(self.gdcm_reader.GetFile())
res = sf.ToStringPair(tag)
if (res[1]):
return int(res[1])
return ""
def SetFileName(self, filename):
"""
Set file name to be parsed given its filename (this should
include the full path of the file of interest).
Return True/False if file could be read.
"""
import os.path as path
filename = path.abspath(filename)
if path.isfile(filename):
# Several information can be acquired from DICOM using
# vtkgdcm.vtkGDCMImageReader.GetMedicalImageProperties()
# but some tags (such as spacing) can only be achieved
# with gdcm.ImageReader()
# used to parse DICOM files - similar to vtkDICOMParser
gdcm_reader = gdcm.ImageReader()
gdcm_reader.SetFileName(filename)
# if DICOM file is null vtkGDCMImageReader raises vtk
# exception
if not gdcm_reader.Read():
return False
vtkgdcm_reader = self.vtkgdcm_reader
vtkgdcm_reader.SetFileName(filename)
vtkgdcm_reader.Update()
self.filename = filename
self.gdcm_reader = gdcm_reader
self.vtkgdcm_reader = vtkgdcm_reader
return True
return False
def GetImageWindowLevel(self, preset=WL_PRESET, multiple=WL_MULT):
"""
Return image window center / level (related to brightness).
This is an integer or a floating point. If the value can't
be read, return None.
By default, only one level value is returned, according to
"preset" parameter. If no value is passed, WL_PRESET constant
is used. In case one wishes to acquire a list with all
level values, one should set "multiple" parameter to True.
Return None if field is not defined.
DICOM standard tag (0x0028,0x1050) was used.
"""
tag = gdcm.Tag(0x0028, 0x1050)
ds = self.gdcm_reader.GetFile().GetDataSet()
if ds.FindDataElement(tag):
data = str(ds.GetDataElement(tag).GetValue())
if (data):
# Usually 'data' is a number. However, in some DICOM
# files, there are several values separated by '\'.
# If multiple values are present for the "Window Center"
# we choose only one. As this should be paired to "Window
# Width", it is set based on WL_PRESET
value_list = [eval(value) for value in data.split('\\')]
if multiple:
return value_list
else:
return value_list[preset]
return None
def GetImageWindowWidth(self, preset=WL_PRESET, multiple=WL_MULT):
"""
Return image window width (related to contrast). This is an
integer or a floating point. If the value can't be read,
return None.
By default, only one width value is returned, according to
"preset" parameter. If no value is passed, WL_PRESET constant
is used. In case one wishes to acquire a list with all
preset values, one should set "multiple" parameter to True.
Return None if field is not defined.
DICOM standard tag (0x0028,0x1051) was used.
"""
tag = gdcm.Tag(0x0028,0x1051)
ds = self.gdcm_reader.GetFile().GetDataSet()
if ds.FindDataElement(tag):
data = str(ds.GetDataElement(tag).GetValue())
if (data):
# Usually 'data' is a number. However, in some DICOM
# files, there are several values separated by '\'.
# If multiple values are present for the "Window Center"
# we choose only one. As this should be paired to "Window
# Width", it is set based on WL_PRESET
value_list = [float(value) for value in data.split('\\')]
if multiple:
return str(value_list)
else:
return str(value_list[preset])
return None
def GetImagePosition(self):
"""
Return [x, y, z] (number list) related to coordinates
of the upper left corner voxel (first voxel transmitted).
This value is given in mm. Number might be floating point
or integer.
Return None if field is not defined.
DICOM standard tag (0x0020, 0x0032) was used.
"""
tag = gdcm.Tag(0x0020, 0x0032)
ds = self.gdcm_reader.GetFile().GetDataSet()
if ds.FindDataElement(tag):
data = str(ds.GetDataElement(tag).GetValue())
if (data):
return [eval(value) for value in data.split('\\')]
return None
def GetImageLocation(self):
"""
Return image location (floating value), related to the
series acquisition.
Return None if field is not defined.
DICOM standard tag (0x0020, 0x0032) was used.
"""
tag = gdcm.Tag(0x0020, 0x1041)
ds = self.gdcm_reader.GetFile().GetDataSet()
if ds.FindDataElement(tag):
data = str(ds.GetDataElement(tag).GetValue())
if (data):
return eval(data)
return None
def GetImageOffset(self):
"""
Return image pixel offset (memory position).
Return None if field is not defined.
DICOM standard tag (0x7fe0, 0x0010) was used.
"""
tag = gdcm.Tag(0x7fe0, 0x0010)
ds = self.gdcm_reader.GetFile().GetDataSet()
if ds.FindDataElement(tag):
data = str(ds.GetDataElement(tag).GetValue())
if (data):
return int(data.split(':')[1])
return None
def GetImageSeriesNumber(self):
"""
Return integer related to acquisition series where this
slice is included.
Return None if field is not defined.
DICOM standard tag (0x0020, 0x0011) was used.
"""
tag = gdcm.Tag(0x0020, 0x0011)
ds = self.gdcm_reader.GetFile().GetDataSet()
if ds.FindDataElement(tag):
data = str(ds.GetDataElement(tag).GetValue())
if (data):
return int(data)
return None
def GetPixelSpacing(self):
"""
Return [x, y] (number list) related to the distance between
each pair of pixel. That is, adjacent row spacing (delimiter)
and adjacent column spacing. Values are usually floating point
and represent mm.
Return None if field is not defined.
DICOM standard tag (0x0028, 0x0030) was used.
"""
tag = gdcm.Tag(0x0028, 0x0030)
ds = self.gdcm_reader.GetFile().GetDataSet()
if ds.FindDataElement(tag):
data = str(ds.GetDataElement(tag).GetValue())
if (data):
return [eval(value) for value in data.split('\\')]
return None
def GetImagePixelSpacingY(self):
"""
Return spacing between adjacent pixels considerating y axis
(height). Values are usually floating point and represent mm.
Return None if field is not defined.
DICOM standard tag (0x0028, 0x0030) was used.
"""
spacing = self.GetPixelSpacing()
if spacing:
return spacing[1]
return None
def GetImagePixelSpacingX(self):
"""
Return spacing between adjacent pixels considerating x axis
(width). Values are usually floating point and represent mm.
Return None if field is not defined.
DICOM standard tag (0x0028, 0x0030) was used.
"""
spacing = self.GetPixelSpacing()
if spacing:
return spacing[0]
return None
def GetPatientWeight(self):
"""
Return patient's weight as a float value (kilograms).
Return None if field is not defined.
DICOM standard tag (0x0010, 0x1030) was used.
"""
tag = gdcm.Tag(0x0010, 0x1030)
ds = self.gdcm_reader.GetFile().GetDataSet()
if ds.FindDataElement(tag):
data = str(ds.GetDataElement(tag).GetValue())
if (data):
return float(data)
return None
def GetPatientHeight(self):
"""
Return patient's height as a float value (meters).
Return None if field is not defined.
DICOM standard tag (0x0010, 0x1030) was used.
"""
tag = gdcm.Tag(0x0010, 0x1020)
ds = self.gdcm_reader.GetFile().GetDataSet()
if ds.FindDataElement(tag):
data = str(ds.GetDataElement(tag).GetValue())
if (data):
return float(data)
return None
def GetPatientAddress(self):
"""
Return string containing patient's address.
DICOM standard tag (0x0010, 0x1040) was used.
"""
tag = gdcm.Tag(0x0010, 0x1040)
ds = self.gdcm_reader.GetFile().GetDataSet()
if ds.FindDataElement(tag):
data = str(ds.GetDataElement(tag).GetValue())
if (data):
return data
return None
def GetPatientMilitarRank(self):
"""
Return string containing patient's militar rank.
Return None if field is not defined.
DICOM standard tag (0x0010, 0x1080) was used.
"""
tag = gdcm.Tag(0x0010,0x1080)
ds = self.gdcm_reader.GetFile().GetDataSet()
if ds.FindDataElement(tag):
data = str(ds.GetDataElement(tag).GetValue())
if (data):
return data
return None
def GetPatientMilitarBranch(self):
"""
Return string containing the militar branch.
The country allegiance may also be included
(e.g. B.R. Army).
Return None if field is not defined.
DICOM standard tag (0x0010, 0x1081) was used.
"""
tag = gdcm.Tag(0x0010,0x1081)
ds = self.gdcm_reader.GetFile().GetDataSet()
if ds.FindDataElement(tag):
data = str(ds.GetDataElement(tag).GetValue())
if (data):
return data
return None
def GetPatientCountry(self):
"""
Return string containing the country where the patient
currently resides.
Return None if field is not defined.
DICOM standard tag (0x0010, 0x2150) was used.
"""
tag = gdcm.Tag(0x0010,0x2150)
ds = self.gdcm_reader.GetFile().GetDataSet()
if ds.FindDataElement(tag):
data = str(ds.GetDataElement(tag).GetValue())
if (data):
return data
return None
def GetPatientRegion(self):
"""
Return string containing the region where the patient
currently resides.
Return None if field is not defined.
DICOM standard tag (0x0010, 0x2152) was used.
"""
tag = gdcm.Tag(0x0010,0x2152)
ds = self.gdcm_reader.GetFile().GetDataSet()
if ds.FindDataElement(tag):
data = str(ds.GetDataElement(tag).GetValue())
if (data):
return data
return None
def GetPatientTelephone(self):
"""
Return string containing the patient's telephone number.
Return None if field is not defined.
DICOM standard tag (0x0010, 0x2154) was used.
"""
tag = gdcm.Tag(0x0010,0x2154)
ds = self.gdcm_reader.GetFile().GetDataSet()
if ds.FindDataElement(tag):
data = str(ds.GetDataElement(tag).GetValue())
if (data):
return data
return None
def GetPatientResponsible(self):
"""
Return string containing the name of the person with
medical decision authority in regards to this patient.
Return None if field is not defined.
DICOM standard tag (0x0010, 0x2297) was used.
"""
tag = gdcm.Tag(0x0010,0x2297)
ds = self.gdcm_reader.GetFile().GetDataSet()
if ds.FindDataElement(tag):
data = str(ds.GetDataElement(tag).GetValue())
if (data):
return data
return None
def GetPatientResponsibleRole(self):
"""
Return string containing the relationship of the responsible
person in regards to this patient.
Return None if field is not defined.
DICOM standard tag (0x0010, 0x2298) was used.
"""
tag = gdcm.Tag(0x0010,0x2298)
ds = self.gdcm_reader.GetFile().GetDataSet()
if ds.FindDataElement(tag):
data = str(ds.GetDataElement(tag).GetValue())
if (data):
return data
return None
def GetPatientResponsibleOrganization(self):
"""
Return string containing the organization name with
medical decision authority in regards to this patient.
Return None if field is not defined.
DICOM standard tag (0x0010, 0x2299) was used.
"""
tag = gdcm.Tag(0x0010,0x2299)
ds = self.gdcm_reader.GetFile().GetDataSet()
if ds.FindDataElement(tag):
data = str(ds.GetDataElement(tag).GetValue())
if (data):
return data
return None
def GetPatientMedicalCondition(self):
"""
Return string containing patient medical conditions
(e.g. contagious illness, drug allergies, etc.).
Return None if field is not defined.
DICOM standard tag (0x0010, 0x2000) was used.
"""
tag = gdcm.Tag(0x0010,0x2000)
ds = self.gdcm_reader.GetFile().GetDataSet()
if ds.FindDataElement(tag):
data = str(ds.GetDataElement(tag).GetValue())
if (data):
return data
return None
def GetPatientContrastAllergies(self):
"""
Return string containing description of prior alergical
reactions to contrast agents.
Return None if field is not defined.
DICOM standard tag (0x0008, 0x2110) was used.
"""
tag = gdcm.Tag(0x0008, 0x2110)
ds = self.gdcm_reader.GetFile().GetDataSet()
if ds.FindDataElement(tag):
data = str(ds.GetDataElement(tag).GetValue())
if (data):
return data
return None
def GetPhysicianName(self):
"""
Return string containing patient's primary (reffering)
physician.
Return None if field is not defined.
DICOM standard tag (0x0008, 0x0090) was used.
"""
#tag = gdcm.Tag(0x0008, 0x0090)
tag = gdcm.Tag(0x0008,0x1060)
ds = self.gdcm_reader.GetFile().GetDataSet()
if ds.FindDataElement(tag):
data = str(ds.GetDataElement(tag).GetValue())
if (data):
return data
return ""
def GetPhysicianAddress(self):
"""
Return string containing physician's address.
Return None if field is not defined.
DICOM standard tag (0x0008, 0x0092) was used.
"""
tag = gdcm.Tag(0x0008, 0x0092)
ds = self.gdcm_reader.GetFile().GetDataSet()
if ds.FindDataElement(tag):
data = str(ds.GetDataElement(tag).GetValue())
if (data):
return data
return None
def GetPhysicianTelephone(self):
"""
Return string containing physician's telephone.
Return None if field is not defined.
DICOM standard tag (0x0008, 0x0094) was used.
"""
tag = gdcm.Tag(0x0008, 0x0094)
ds = self.gdcm_reader.GetFile().GetDataSet()
if ds.FindDataElement(tag):
data = str(ds.GetDataElement(tag).GetValue())
if (data):
return data
return None
def GetImageType(self):
"""
Return list containing strings related to image origin.
Eg: ["ORIGINAL", "PRIMARY", "AXIAL"] or ["DERIVED",
"SECONDARY", "OTHER"]
Return None if field is not defined.
Critical DICOM tag (0x0008, 0x0008). Cannot be editted.
"""
tag = gdcm.Tag(0x0008, 0x0008)
ds = self.gdcm_reader.GetFile().GetDataSet()
if ds.FindDataElement(tag):
data = str(ds.GetDataElement(tag).GetValue())
if (data):
return data.split('\\')
return None
def GetSOPClassUID(self):
"""
Return string containing the Unique Identifier for the SOP
class.
Return None if field is not defined.
Critical DICOM tag (0x0008, 0x0016). Cannot be edited.
"""
tag = gdcm.Tag(0x0008, 0x0016)
ds = self.gdcm_reader.GetFile().GetDataSet()
if ds.FindDataElement(tag):
data = str(ds.GetDataElement(tag).GetValue())
if (data):
return data
return None
def GetSOPInstanceUID(self):
"""
Return string containing Unique Identifier for the SOP
instance.
Return None if field is not defined.
Critical DICOM tag (0x0008, 0x0018). Cannot be edited.
"""
tag = gdcm.Tag(0x0008, 0x0018)
ds = self.gdcm_reader.GetFile().GetDataSet()
if ds.FindDataElement(tag):
data = str(ds.GetDataElement(tag).GetValue())
if (data):
return data
return None
def GetStudyInstanceUID(self):
"""
Return string containing Unique Identifier of the
Study Instance.
Return None if field is not defined.
Critical DICOM Tag (0x0020,0x000D). Cannot be edited.
"""
tag = gdcm.Tag(0x0020,0x000D)
ds = self.gdcm_reader.GetFile().GetDataSet()
if ds.FindDataElement(tag):
data = str(ds.GetDataElement(tag).GetValue())
if (data):
return data
return None
def GetImagePatientOrientation(self):
"""
Return matrix [x0, x1, x2, y0, y1, y2] related to patient
image acquisition orientation. All values are in floating
point representation. The first three values are associated
to row orientation and the three last values are related
to column orientation.
Return None if field is not defined.
Critical DICOM tag (0x0020,0x0037). Cannot be edited.
"""
tag = gdcm.Tag(0x0020,0x0037)
ds = self.gdcm_reader.GetFile().GetDataSet()
if ds.FindDataElement(tag):
data = str(ds.GetDataElement(tag).GetValue())
if (data):
return [float(value) for value in data.split('\\')]
return [1.0, 0.0, 0.0, 0.0, 1.0, 0.0]
def GetImageColumnOrientation(self):
"""
Return matrix [x0, x1, x2] related to patient images'
column acquisition orientation. All values are in floating
point representation.
Return None if field is not defined.
Critical DICOM tag (0x0020,0x0037). Cannot be edited.
"""
tag = gdcm.Tag(0x0020,0x0037)
ds = self.gdcm_reader.GetFile().GetDataSet()
if ds.FindDataElement(tag):
data = str(ds.GetDataElement(tag).GetValue())
if (data):
return [float(value) for value in data.split('\\')[3:6]]
return [0.0, 1.0, 0.0]
def GetImageRowOrientation(self):
"""
Return matrix [y0, y1, y2] related to patient images'
row acquisition orientation. All values are in floating
point representation.
Return None if field is not defined.
Critical DICOM tag (0x0020,0x0037). Cannot be edited.
"""
tag = gdcm.Tag(0x0020,0x0037)
ds = self.gdcm_reader.GetFile().GetDataSet()
if ds.FindDataElement(tag):
data = str(ds.GetDataElement(tag).GetValue())
if (data):
return [float(value) for value in data.split('\\')[0:3]]
return [1.0, 0.0, 0.0]
def GetFrameReferenceUID(self):
"""
Return string containing Frame of Reference UID.
Return None if field is not defined.
Critical DICOM tag (0x0020,0x0052). Cannot be edited.
"""
tag = gdcm.Tag(0x0020,0x0052)
ds = self.gdcm_reader.GetFile().GetDataSet()
if ds.FindDataElement(tag):
data = str(ds.GetDataElement(tag).GetValue())
if (data):
return data
return None
def GetImageSamplesPerPixel(self):
"""
Return integer related to Samples per Pixel. Eg. 1.
Return None if field is not defined.
Critical DICOM tag (0x0028,0x0002). Cannot be edited.
"""
tag = gdcm.Tag(0x0028, 0x0002)
sf = gdcm.StringFilter()
sf.SetFile(self.gdcm_reader.GetFile())
res = sf.ToStringPair(tag)
if (res[1]):
return int(res[1])
return None
def GetPhotometricInterpretation(self):
"""
Return string containing the photometric interpretation.
Eg. "MONOCHROME2".
Return None if field is not defined.
Critical DICOM tag (0x0028,0x0004). Cannot be edited.
"""
tag = gdcm.Tag(0x0028, 0x0004)
sf = gdcm.StringFilter()
sf.SetFile(self.gdcm_reader.GetFile())
res = sf.ToStringPair(tag)
if (res[1]):
return res[1]
return None
def GetBitsStored(self):
"""
Return number related to number of bits stored.
Eg. 12 or 16.
Return None if field is not defined.
Critical DICOM tag (0x0028,0x0101). Cannot be edited.
"""
tag = gdcm.Tag(0x0028, 0x0101)
sf = gdcm.StringFilter()
sf.SetFile(self.gdcm_reader.GetFile())
res = sf.ToStringPair(tag)
if (res[1]):
return int(res[1])
return None
def GetHighBit(self):
"""
Return string containing hight bit. This is commonly 11 or 15.
Return None if field is not defined.
Critical DICOM tag (0x0028,0x0102). Cannot be edited.
"""
tag = gdcm.Tag(0x0028, 0x0102)
sf = gdcm.StringFilter()
sf.SetFile(self.gdcm_reader.GetFile())
res = sf.ToStringPair(tag)
if (res[1]):
return int(res[1])
return None
def GetProtocolName(self):
"""
Return protocol name (string). This info varies according to
manufactor and software interface. Eg. "FACE", "aFaceSpi",
"1551515/2" or "./protocols/user1.pfossa.pro".
Return None if field is not defined.
DICOM standard tag (0x0018, 0x1030) was used.
"""
tag = gdcm.Tag(0x0018, 0x1030)
ds = self.gdcm_reader.GetFile().GetDataSet()
if ds.FindDataElement(tag):
data = str(ds.GetDataElement(tag).GetValue())
if (data):
return data
return None
def GetAcquisionSequence(self):
"""
Return description (string) of the sequence how data was
acquired. That is:
- SE = Spin Echo
- IR = Inversion Recovery
- GR = Gradient Recalled
- EP = Echo Planar
- RM = Research Mode
In some cases this information is presented in other forms:
- HELICAL_CT
- SCANOSCOPE
Return None if field is not defined.
Critical DICOM tag (0x0018, 0x0020). Cannot be edited.
"""
tag = gdcm.Tag(0x0018, 0x0020)
ds = self.gdcm_reader.GetFile().GetDataSet()
if ds.FindDataElement(tag):
data = str(ds.GetDataElement(tag).GetValue())
if (data):
return data
return None
def GetInstitutionName(self):
"""
Return instution name (string) of the institution where the
acquisitin quipment is located.
DICOM standard tag (0x0008, 0x0080) was used.
"""
tag = gdcm.Tag(0x0008, 0x0080)
ds = self.gdcm_reader.GetFile().GetDataSet()
if ds.FindDataElement(tag):
data = str(ds.GetDataElement(tag).GetValue())
if (data):
return data
0x0008, 0x0081
def GetInstitutionAddress(self):
"""
Return mailing address (string) of the institution where the
acquisitin quipment is located. Some institutions record only
the city, other record the full address.
Return None if field is not defined.
DICOM standard tag (0x0008, 0x0081) was used.
"""
tag = gdcm.Tag(0x0008, 0x0081)
ds = self.gdcm_reader.GetFile().GetDataSet()
if ds.FindDataElement(tag):
data = str(ds.GetDataElement(tag).GetValue())
if (data):
return data
return None
def GetStudyInstanceUID(self):
"""
Return Study Instance UID (string), related to series being
analized.
Return None if field is not defined.
Critical DICOM tag (0x0020, 0x000D). Cannot be edited.
"""
tag = gdcm.Tag(0x0020, 0x000D)
ds = self.gdcm_reader.GetFile().GetDataSet()
if ds.FindDataElement(tag):
data = str(ds.GetDataElement(tag).GetValue())
if (data):
return data
return None
def GetPatientOccupation(self):
"""
Return occupation of the patient (string).
Return None if field is not defined.
DICOM standard tag (0x0010,0x2180) was used.
"""
tag = gdcm.Tag(0x0010, 0x2180)
ds = self.gdcm_reader.GetFile().GetDataSet()
if ds.FindDataElement(tag):
data = str(ds.GetDataElement(tag).GetValue())
if (data):
return data
return None
def _GetPixelRepresentation(self):
"""
Return pixel representation of the data sample. Each sample
should have the same pixel representation. Common values are:
- 0000H = unsigned integer.
- 0001H = 2's complement.
DICOM standard tag (0x0028, 0x0103) was used.
"""
tag = gdcm.Tag(0x0028, 0x0103)
sf = gdcm.StringFilter()
sf.SetFile(self.gdcm_reader.GetFile())
res = sf.ToStringPair(tag)
if (res[1]):
return int(res[1])
return None
def _GetBitsAllocated(self):
"""
Return integer containing the number of bits allocated for
each pixel sample. Each sample should have the same number
of bits allocated. Usually this value is 8, *16*, 32, 64.
DICOM standard tag (0x0028, 0x0100) was used.
"""
tag = gdcm.Tag(0x0028, 0x0100)
sf = gdcm.StringFilter()
sf.SetFile(self.gdcm_reader.GetFile())
res = sf.ToStringPair(tag)
if (res[1]):
return int(res[1])
return None
def GetImageDataType(self):
"""
Return image's pixel representation data type (string). This
might be:
- Float64
- Int8
- Int16
- Int32
- UInt16
Return None otherwise.
"""
repres = self._GetPixelRepresentation()
bits = self._GetBitsAllocated()
if not bits:
answer = None
else:
answer = "UInt16"
if bits == 8:
answer = "Int8"
elif bits == 16:
if repres:
answer = "Int16"
elif bits == 32:
answer = "Int32"
elif bits == 64:
answer = "Float64"
return answer
def GetPatientBirthDate(self):
"""