annotate OrthancServer/Resources/Samples/Python/ContinuousPatientAnonymization.py @ 4044:d25f4c0fa160 framework

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