comparison Resources/Samples/Python/ContinuousPatientAnonymization.py @ 1340:0ad4773f28b3

new Python sample
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 03 Apr 2015 13:31:00 +0200
parents
children b1291df2f780
comparison
equal deleted inserted replaced
1339:a87d3288bd39 1340:0ad4773f28b3
1 #!/usr/bin/python
2
3 # Orthanc - A Lightweight, RESTful DICOM Store
4 # Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics
5 # Department, University Hospital of Liege, Belgium
6 #
7 # This program is free software: you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License as
9 # published by the Free Software Foundation, either version 3 of the
10 # License, or (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20
21
22 import time
23 import sys
24 import RestToolbox
25 import md5
26
27
28 ##
29 ## Print help message
30 ##
31
32 if len(sys.argv) != 3:
33 print("""
34 Sample script that anonymizes patients in real-time. A patient gets
35 anonymized as soon as she gets stable (i.e. when no DICOM instance has
36 been received for this patient for a sufficient amount of time - cf.
37 the configuration option "StableAge").
38
39 Usage: %s [hostname] [HTTP port]
40 For instance: %s localhost 8042
41 """ % (sys.argv[0], sys.argv[0]))
42 exit(-1)
43
44 URL = 'http://%s:%d' % (sys.argv[1], int(sys.argv[2]))
45
46
47
48 ##
49 ## The following function is called whenever a patient gets stable
50 ##
51
52 COUNT = 1
53
54 def AnonymizePatient(path):
55 global URL
56 global COUNT
57
58 patient = RestToolbox.DoGet(URL + path)
59 patientID = patient['MainDicomTags']['PatientID']
60
61 # Ignore anonymized patients
62 if not 'AnonymizedFrom' in patient:
63 print('Patient with ID "%s" is stabilized: anonymizing it...' % (patientID))
64
65 # The PatientID after anonymization is taken as the 8 first
66 # characters from the MD5 hash of the original PatientID
67 anonymizedID = md5.new(patientID).hexdigest()[:8]
68 anonymizedName = 'Anonymized patient %d' % COUNT
69 COUNT += 1
70
71 RestToolbox.DoPost(URL + path + '/anonymize',
72 { 'Replace' : { 'PatientID' : anonymizedID,
73 'PatientName' : anonymizedName } })
74
75 # Delete the source patient after the anonymization
76 RestToolbox.DoDelete(URL + change['Path'])
77
78
79
80 ##
81 ## Main loop that listens to the changes API.
82 ##
83
84 current = 0
85 while True:
86 r = RestToolbox.DoGet(URL + '/changes', {
87 'since' : current,
88 'limit' : 4 # Retrieve at most 4 changes at once
89 })
90
91 for change in r['Changes']:
92 if change['ChangeType'] == 'StablePatient':
93 AnonymizePatient(change['Path'])
94
95 current = r['Last']
96
97 if r['Done']:
98 print('Everything has been processed: Waiting...')
99 time.sleep(1)