GDCM: DumbAnonymizer.py

GDCM 3.0.25

DumbAnonymizer.py
1
14
15"""
16This example shows how one can use the gdcm.Anonymizer in 'dumb' mode.
17This class becomes really handy when one knows which particular tag to fill in.
18
19Usage:
20
21 python DumbAnonymizer.py gdcmData/012345.002.050.dcm out.dcm
22
23"""
24
25import gdcm
26
27# http://www.oid-info.com/get/1.3.6.1.4.17434
28THERALYS_ORG_ROOT = "1.3.6.1.4.17434"
29
30tag_rules={
31 # Value
32 (0x0012,0x0010):("Value","MySponsorName"),
33 (0x0012,0x0020):("Value","MyProtocolID"),
34 (0x0012,0x0021):("Value","MyProtocolName"),
35 (0x0012,0x0062):("Value","YES"),
36 (0x0012,0x0063):("Value","MyDeidentificationMethod"),
37
38 # Method
39 #(0x0002,0x0003):("Method","GenerateMSOPId"),
40 #(0x0008,0x1155):("Method","GenerateMSOPId"),
41 (0x0008,0x0018):("Method","GenerateMSOPId"),
42 (0x0010,0x0010):("Method","GetSponsorInitials"),
43 (0x0010,0x0020):("Method","GetSponsorId"),
44 (0x0012,0x0030):("Method","GetSiteId"),
45 (0x0012,0x0031):("Method","GetSiteName"),
46 (0x0012,0x0040):("Method","GetSponsorId"),
47 (0x0012,0x0050):("Method","GetTPId"),
48 (0x0018,0x0022):("Method","KeepIfExist"),
49 (0x0018,0x1315):("Method","KeepIfExist"),
50 (0x0020,0x000d):("Method","GenerateStudyId"),
51 (0x0020,0x000e):("Method","GenerateSeriesId"),
52 (0x0020,0x1002):("Method","GetNumberOfFrames"),
53 (0x0020,0x0020):("Method","GetPatientOrientation"),
54 # Other:
55 (0x0012,0x0051):("Patient Field","Type Examen"),
56 (0x0018,0x1250):("Sequence Field","Receive Coil"),
57 (0x0018,0x0088):("Sequence Field","Spacing Between Slice"),
58 (0x0018,0x0095):("Sequence Field","Pixel Bandwidth"),
59 (0x0018,0x0082):("Sequence Field","Inversion Time"),
60}
61
62class MyAnon:
63 def __init__(self):
64 self.studyuid = None
65 self.seriesuid = None
66 generator = gdcm.UIDGenerator()
67 if not self.studyuid:
68 self.studyuid = generator.Generate()
69 if not self.seriesuid:
70 self.seriesuid = generator.Generate()
71 def GetSponsorInitials(self):
72 return "dummy^foobar"
73 def GenerateStudyId(self):
74 return self.studyuid
75 def GenerateSeriesId(self):
76 return self.seriesuid
77 #def GenerateMSOPId(self):
78 def GenerateMSOPId(self):
79 generator = gdcm.UIDGenerator()
80 return generator.Generate()
81 def GetSiteId(self):
82 return "MySiteId"
83 def GetSiteName(self):
84 return "MySiteName"
85 def GetSponsorId(self):
86 return "MySponsorId"
87 def GetTPId(self):
88 return "MyTP"
89
90if __name__ == "__main__":
91 import sys
92 gdcm.FileMetaInformation.SetSourceApplicationEntityTitle( "DumbAnonymizer" )
93 gdcm.UIDGenerator.SetRoot( THERALYS_ORG_ROOT )
94
95 r = gdcm.Reader()
96 filename = sys.argv[1]
97 r.SetFileName( filename )
98 if not r.Read(): sys.exit(1)
99
100 obj = MyAnon()
101
102 w = gdcm.Writer()
103 ano = gdcm.Anonymizer()
104 ano.SetFile( r.GetFile() )
105 ano.RemoveGroupLength()
106 for tag,rule in tag_rules.items():
107 if rule[0] == 'Value':
108 print tag,rule
109 ano.Replace( gdcm.Tag( tag[0], tag[1] ), rule[1] )
110 elif rule[0] == 'Method':
111 print tag,rule
112 # result = locals()[rule[1]]()
113 methodname = rule[1]
114 if hasattr(obj, methodname):
115 _member = getattr(obj, methodname)
116 result = _member()
117 ano.Replace( gdcm.Tag( tag[0], tag[1] ), result )
118 else:
119 print "Problem with: ", methodname
120
121 outfilename = sys.argv[2]
122 w.SetFileName( outfilename )
123 w.SetFile( ano.GetFile() )
124 if not w.Write(): sys.exit(1)
gdcm::Anonymizer
Anonymizer.
Definition gdcmAnonymizer.h:78
gdcm::FileMetaInformation::SetSourceApplicationEntityTitle
static void SetSourceApplicationEntityTitle(const char *title)
gdcm::Reader
Reader ala DOM (Document Object Model)
Definition gdcmReader.h:54
gdcm::Tag
Class to represent a DICOM Data Element (Attribute) Tag (Group, Element).
Definition gdcmTag.h:39
gdcm::UIDGenerator
Class for generating unique UID.
Definition gdcmUIDGenerator.h:28
gdcm::UIDGenerator::SetRoot
static void SetRoot(const char *root)
gdcm::Writer
Writer ala DOM (Document Object Model)
Definition gdcmWriter.h:49