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