annotate Resources/Samples/Python/ContinuousPatientAnonymization.py @ 1610:2dff2bdffdb8

font support within Orthanc
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 04 Sep 2015 13:44:21 +0200
parents 0ad4773f28b3
children b1291df2f780
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
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 # Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 # Department, University Hospital of Liege, Belgium
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 #
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 # 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
9 # published by the Free Software Foundation, either version 3 of the
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 # License, or (at your option) any later version.
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 #
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 # 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
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 # General Public License for more details.
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 #
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 # 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
18 # 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
19
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 import time
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 import sys
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 import RestToolbox
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 import md5
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26
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 ## Print help message
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 ##
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 if len(sys.argv) != 3:
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 print("""
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 Sample script that anonymizes patients in real-time. A patient gets
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 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
36 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
37 the configuration option "StableAge").
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 Usage: %s [hostname] [HTTP port]
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 For instance: %s localhost 8042
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 """ % (sys.argv[0], sys.argv[0]))
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 exit(-1)
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 URL = 'http://%s:%d' % (sys.argv[1], int(sys.argv[2]))
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45
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 ## The following function is called whenever a patient gets stable
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 ##
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 COUNT = 1
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 def AnonymizePatient(path):
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 global URL
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 global COUNT
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 patient = RestToolbox.DoGet(URL + path)
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 patientID = patient['MainDicomTags']['PatientID']
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 # Ignore anonymized patients
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 if not 'AnonymizedFrom' in patient:
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 print('Patient with ID "%s" is stabilized: anonymizing it...' % (patientID))
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 # The PatientID after anonymization is taken as the 8 first
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 # characters from the MD5 hash of the original PatientID
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 anonymizedID = md5.new(patientID).hexdigest()[:8]
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 anonymizedName = 'Anonymized patient %d' % COUNT
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 COUNT += 1
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 RestToolbox.DoPost(URL + path + '/anonymize',
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 { 'Replace' : { 'PatientID' : anonymizedID,
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 'PatientName' : anonymizedName } })
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 # Delete the source patient after the anonymization
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 RestToolbox.DoDelete(URL + change['Path'])
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77
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 ## Main loop that listens to the changes API.
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 ##
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 current = 0
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 while True:
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 r = RestToolbox.DoGet(URL + '/changes', {
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 'since' : current,
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 'limit' : 4 # Retrieve at most 4 changes at once
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 })
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 for change in r['Changes']:
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 if change['ChangeType'] == 'StablePatient':
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 AnonymizePatient(change['Path'])
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 current = r['Last']
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 if r['Done']:
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 print('Everything has been processed: Waiting...')
0ad4773f28b3 new Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 time.sleep(1)