Mercurial > hg > orthanc
annotate OrthancServer/Resources/Samples/Python/ContinuousPatientAnonymization.py @ 4864:09d06d99745c openssl-3.x
integration mainline->openssl-3.x
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 24 Dec 2021 17:18:06 +0100 |
parents | ffc03a1d60c3 |
children | 43e613a7756b |
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 |
4437
d9473bd5ed43
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
6 # Copyright (C) 2017-2021 Osimis S.A., Belgium |
4831
7053502fbf97
added copyright UCLouvain
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
7 # Copyright (C) 2021-2021 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
1340 | 8 # |
9 # This program is free software: you can redistribute it and/or | |
10 # modify it under the terms of the GNU General Public License as | |
11 # published by the Free Software Foundation, either version 3 of the | |
12 # License, or (at your option) any later version. | |
13 # | |
14 # This program is distributed in the hope that it will be useful, but | |
15 # WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 # General Public License for more details. | |
18 # | |
19 # You should have received a copy of the GNU General Public License | |
20 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
21 | |
22 | |
23 | |
24 import time | |
25 import sys | |
26 import RestToolbox | |
4855
ffc03a1d60c3
fix sample scripts for Python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4831
diff
changeset
|
27 import hashlib |
1340 | 28 |
29 | |
30 ## | |
31 ## Print help message | |
32 ## | |
33 | |
34 if len(sys.argv) != 3: | |
35 print(""" | |
36 Sample script that anonymizes patients in real-time. A patient gets | |
37 anonymized as soon as she gets stable (i.e. when no DICOM instance has | |
38 been received for this patient for a sufficient amount of time - cf. | |
39 the configuration option "StableAge"). | |
40 | |
41 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
|
42 For instance: %s 127.0.0.1 8042 |
1340 | 43 """ % (sys.argv[0], sys.argv[0])) |
44 exit(-1) | |
45 | |
46 URL = 'http://%s:%d' % (sys.argv[1], int(sys.argv[2])) | |
47 | |
48 | |
49 | |
50 ## | |
51 ## The following function is called whenever a patient gets stable | |
52 ## | |
53 | |
54 COUNT = 1 | |
55 | |
56 def AnonymizePatient(path): | |
57 global URL | |
58 global COUNT | |
59 | |
60 patient = RestToolbox.DoGet(URL + path) | |
61 patientID = patient['MainDicomTags']['PatientID'] | |
62 | |
63 # Ignore anonymized patients | |
64 if not 'AnonymizedFrom' in patient: | |
65 print('Patient with ID "%s" is stabilized: anonymizing it...' % (patientID)) | |
66 | |
67 # The PatientID after anonymization is taken as the 8 first | |
68 # characters from the MD5 hash of the original PatientID | |
4855
ffc03a1d60c3
fix sample scripts for Python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4831
diff
changeset
|
69 h = hashlib.md5(patientID.encode('ascii')) |
ffc03a1d60c3
fix sample scripts for Python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4831
diff
changeset
|
70 anonymizedID = h.hexdigest()[:8] |
1340 | 71 anonymizedName = 'Anonymized patient %d' % COUNT |
72 COUNT += 1 | |
73 | |
74 RestToolbox.DoPost(URL + path + '/anonymize', | |
75 { 'Replace' : { 'PatientID' : anonymizedID, | |
4855
ffc03a1d60c3
fix sample scripts for Python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4831
diff
changeset
|
76 'PatientName' : anonymizedName }, |
ffc03a1d60c3
fix sample scripts for Python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4831
diff
changeset
|
77 'Force' : True }) |
1340 | 78 |
79 # Delete the source patient after the anonymization | |
80 RestToolbox.DoDelete(URL + change['Path']) | |
81 | |
82 | |
83 | |
84 ## | |
85 ## Main loop that listens to the changes API. | |
86 ## | |
87 | |
88 current = 0 | |
89 while True: | |
90 r = RestToolbox.DoGet(URL + '/changes', { | |
91 'since' : current, | |
92 'limit' : 4 # Retrieve at most 4 changes at once | |
93 }) | |
94 | |
95 for change in r['Changes']: | |
96 if change['ChangeType'] == 'StablePatient': | |
97 AnonymizePatient(change['Path']) | |
98 | |
99 current = r['Last'] | |
100 | |
101 if r['Done']: | |
102 print('Everything has been processed: Waiting...') | |
103 time.sleep(1) |