comparison Resources/GenerateAnonymizationProfile.py @ 2312:8700dcaa02e5 issue-46-anonymization

GenerateAnonymizationProfile.py
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 12 Jul 2017 16:44:33 +0200
parents
children d19e716b79fa
comparison
equal deleted inserted replaced
2311:78dcb3ddea9f 2312:8700dcaa02e5
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 Osimis, 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()' % tag, name)
78 if name in [
79 'Patient\'s Name',
80 'Patient ID',
81 ]:
82 FormatLine('// Tag (%s) is set below (*)' % tag, name)
83 elif profile == 'X':
84 FormatLine('removals_.insert(DicomTag(%s));' % tag, name)
85 elif profile.startswith('X/'):
86 FormatLine('removals_.insert(DicomTag(%s)); /* %s */' % (tag, profile), name)
87 elif profile == 'Z':
88 FormatLine('clearings_.insert(DicomTag(%s));' % tag, name)
89 elif profile == 'D' or profile.startswith('Z/'):
90 FormatLine('clearings_.insert(DicomTag(%s)); /* %s */' % (tag, profile), name)
91 elif profile == 'U':
92 FormatLine('clearings_.insert(DicomTag(%s)); /* TODO UID */' % (tag), name)
93 else:
94 FormatUnknown(rawTag, name, profile)
95
96 for line in sorted(LINES):
97 print line
98