Mercurial > hg > orthanc
annotate Resources/Samples/Python/ContinuousPatientAnonymization.py @ 2244:a3a65de1840f
shared copyright with osimis
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 04 Jan 2017 12:09:40 +0100 |
parents | 65b1ce7cb84f |
children | 878b59270859 |
rev | line source |
---|---|
1340 | 1 #!/usr/bin/python |
2 | |
3 # Orthanc - A Lightweight, RESTful DICOM Store | |
1900 | 4 # Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
1340 | 5 # Department, University Hospital of Liege, Belgium |
2244
a3a65de1840f
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2032
diff
changeset
|
6 # Copyright (C) 2017 Osimis, Belgium |
1340 | 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 | |
23 import time | |
24 import sys | |
25 import RestToolbox | |
26 import md5 | |
27 | |
28 | |
29 ## | |
30 ## Print help message | |
31 ## | |
32 | |
33 if len(sys.argv) != 3: | |
34 print(""" | |
35 Sample script that anonymizes patients in real-time. A patient gets | |
36 anonymized as soon as she gets stable (i.e. when no DICOM instance has | |
37 been received for this patient for a sufficient amount of time - cf. | |
38 the configuration option "StableAge"). | |
39 | |
40 Usage: %s [hostname] [HTTP port] | |
2032
65b1ce7cb84f
Replaced "localhost" by "127.0.0.1", as it might impact performance on Windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
41 For instance: %s 127.0.0.1 8042 |
1340 | 42 """ % (sys.argv[0], sys.argv[0])) |
43 exit(-1) | |
44 | |
45 URL = 'http://%s:%d' % (sys.argv[1], int(sys.argv[2])) | |
46 | |
47 | |
48 | |
49 ## | |
50 ## The following function is called whenever a patient gets stable | |
51 ## | |
52 | |
53 COUNT = 1 | |
54 | |
55 def AnonymizePatient(path): | |
56 global URL | |
57 global COUNT | |
58 | |
59 patient = RestToolbox.DoGet(URL + path) | |
60 patientID = patient['MainDicomTags']['PatientID'] | |
61 | |
62 # Ignore anonymized patients | |
63 if not 'AnonymizedFrom' in patient: | |
64 print('Patient with ID "%s" is stabilized: anonymizing it...' % (patientID)) | |
65 | |
66 # The PatientID after anonymization is taken as the 8 first | |
67 # characters from the MD5 hash of the original PatientID | |
68 anonymizedID = md5.new(patientID).hexdigest()[:8] | |
69 anonymizedName = 'Anonymized patient %d' % COUNT | |
70 COUNT += 1 | |
71 | |
72 RestToolbox.DoPost(URL + path + '/anonymize', | |
73 { 'Replace' : { 'PatientID' : anonymizedID, | |
74 'PatientName' : anonymizedName } }) | |
75 | |
76 # Delete the source patient after the anonymization | |
77 RestToolbox.DoDelete(URL + change['Path']) | |
78 | |
79 | |
80 | |
81 ## | |
82 ## Main loop that listens to the changes API. | |
83 ## | |
84 | |
85 current = 0 | |
86 while True: | |
87 r = RestToolbox.DoGet(URL + '/changes', { | |
88 'since' : current, | |
89 'limit' : 4 # Retrieve at most 4 changes at once | |
90 }) | |
91 | |
92 for change in r['Changes']: | |
93 if change['ChangeType'] == 'StablePatient': | |
94 AnonymizePatient(change['Path']) | |
95 | |
96 current = r['Last'] | |
97 | |
98 if r['Done']: | |
99 print('Everything has been processed: Waiting...') | |
100 time.sleep(1) |