comparison OrthancServer/Resources/GenerateAnonymizationProfile.py @ 4044:d25f4c0fa160 framework

splitting code into OrthancFramework and OrthancServer
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 10 Jun 2020 20:30:34 +0200
parents Resources/GenerateAnonymizationProfile.py@94f4a18a79cc
children 28944db5318b
comparison
equal deleted inserted replaced
4043:6c6239aec462 4044:d25f4c0fa160
1 #!/usr/bin/env python
2
3 # Orthanc - A Lightweight, RESTful DICOM Store
4 # Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
5 # Department, University Hospital of Liege, Belgium
6 # Copyright (C) 2017-2020 Osimis S.A., Belgium
7 #
8 # This program is free software: you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License as
10 # published by the Free Software Foundation, either version 3 of the
11 # License, or (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21
22 import re
23 import sys
24 import xml.etree.ElementTree as ET
25
26 # Usage:
27 # ./GenerateAnonymizationProfile.py ~/Subversion/dicom-specification/2017c/part15.xml
28
29 if len(sys.argv) != 2:
30 raise Exception('Please provide the path to the part15.xml file from the DICOM standard')
31
32 with open(sys.argv[1], 'r') as f:
33 root = ET.fromstring(f.read())
34
35 br = '{http://docbook.org/ns/docbook}' # Shorthand variable
36
37
38 LINES = []
39
40 def FormatLine(command, name):
41 indentation = 65
42
43 if len(command) > indentation:
44 raise Exception('Too long command')
45
46 line = ' ' + command + (' ' * (indentation - len(command))) + '// ' + name
47 LINES.append(line)
48
49 def FormatUnknown(rawTag, name, profile):
50 FormatLine('// TODO: %s with rule %s' % (rawTag, profile), name)
51
52
53 RAW_TAG_RE = re.compile(r'^\(\s*([0-9A-F]{4})\s*,\s*([0-9A-F]{4})\s*\)$')
54
55
56 for table in root.iter('%stable' % br):
57 if table.attrib['label'] == 'E.1-1':
58 for row in table.find('%stbody' % br).iter('%str' % br):
59 rawTag = row.find('%std[2]/%spara' % (br, br)).text
60 name = row.find('%std[1]/%spara' % (br, br)).text
61 profile = row.find('%std[5]/%spara' % (br, br)).text
62
63 if len(name.strip()) == 0:
64 continue
65
66 match = RAW_TAG_RE.match(rawTag)
67 if match == None:
68 FormatUnknown(rawTag, name, profile)
69 else:
70 tag = '0x%s, 0x%s' % (match.group(1).lower(), match.group(2).lower())
71
72 if name in [
73 'SOP Instance UID',
74 'Series Instance UID',
75 'Study Instance UID',
76 ]:
77 FormatLine('// Tag (%s) is set in Apply() /* %s */' % (tag, profile), name)
78 elif name in [
79 'Referenced Image Sequence',
80 'Source Image Sequence',
81 'Referenced SOP Instance UID',
82 'Frame of Reference UID',
83 'Referenced Frame of Reference UID',
84 'Related Frame of Reference UID',
85 ]:
86 FormatLine('// Tag (%s) => RelationshipsVisitor /* %s */' % (tag, profile), name)
87 elif name in [
88 'Patient\'s Name',
89 'Patient ID',
90 ]:
91 FormatLine('// Tag (%s) is set below (*) /* %s */' % (tag, profile), name)
92 elif profile == 'X':
93 FormatLine('removals_.insert(DicomTag(%s));' % tag, name)
94 elif profile.startswith('X/'):
95 FormatLine('removals_.insert(DicomTag(%s)); /* %s */' % (tag, profile), name)
96 elif profile == 'Z':
97 FormatLine('clearings_.insert(DicomTag(%s));' % tag, name)
98 elif profile == 'D' or profile.startswith('Z/'):
99 FormatLine('clearings_.insert(DicomTag(%s)); /* %s */' % (tag, profile), name)
100 elif profile == 'U':
101 FormatLine('removals_.insert(DicomTag(%s)); /* TODO UID */' % (tag), name)
102 else:
103 FormatUnknown(rawTag, name, profile)
104
105 for line in sorted(LINES):
106 print line
107
108
109 # D - replace with a non-zero length value that may be a dummy value and consistent with the VR
110 # Z - replace with a zero length value, or a non-zero length value that may be a dummy value and consistent with the VR
111 # X - remove
112 # K - keep (unchanged for non-sequence attributes, cleaned for sequences)
113 # C - clean, that is replace with values of similar meaning known not to contain identifying information and consistent with the VR
114 # U - replace with a non-zero length UID that is internally consistent within a set of Instances
115 # Z/D - Z unless D is required to maintain IOD conformance (Type 2 versus Type 1)
116 # X/Z - X unless Z is required to maintain IOD conformance (Type 3 versus Type 2)
117 # X/D - X unless D is required to maintain IOD conformance (Type 3 versus Type 1)
118 # X/Z/D - X unless Z or D is required to maintain IOD conformance (Type 3 versus Type 2 versus Type 1)
119 # X/Z/U* - X unless Z or replacement of contained instance UIDs (U) is required to maintain IOD conformance (Type 3 versus Type 2 versus Type 1 sequences containing UID references)