annotate Resources/Samples/Python/AutoClassify.py @ 1182:d74ac5d0bcaf

New sample: Automated classification of DICOM files
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 09 Oct 2014 17:11:00 +0200
parents
children 6ef2c81581cd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1182
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 #!/usr/bin/python
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 # Orthanc - A Lightweight, RESTful DICOM Store
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 # Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege,
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 # Belgium
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 #
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 # This program is free software: you can redistribute it and/or
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 # modify it under the terms of the GNU General Public License as
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 # published by the Free Software Foundation, either version 3 of the
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 # License, or (at your option) any later version.
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 #
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 # This program is distributed in the hope that it will be useful, but
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 # General Public License for more details.
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 #
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 # You should have received a copy of the GNU General Public License
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 import argparse
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 import time
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 import os
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 import os.path
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 import RestToolbox
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 parser = argparse.ArgumentParser(
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 description = 'Automated classification of DICOM files from Orthanc.',
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 formatter_class = argparse.ArgumentDefaultsHelpFormatter)
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 parser.add_argument('--host', default = 'localhost',
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 help = 'The host address that runs Orthanc')
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 parser.add_argument('--port', type = int, default = '8042',
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 help = 'The port number to which Orthanc is listening for the REST API')
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 parser.add_argument('--target', default = 'OrthancFiles',
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 help = 'The target directory where to store the DICOM files')
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 parser.add_argument('--all', action = 'store_true',
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 help = 'Replay the entire history on startup (disabled by default)')
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 parser.set_defaults(all = False)
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 parser.add_argument('--remove', action = 'store_true',
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 help = 'Remove DICOM files from Orthanc once classified (disabled by default)')
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 parser.set_defaults(remove = False)
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 # Parse the arguments
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 args = parser.parse_args()
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 URL = 'http://%s:%d' % (args.host, args.port)
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 print 'Connecting to Orthanc on address: %s' % URL
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 # Compute the starting point for the changes loop
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 if args.all:
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 current = 0
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 else:
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 current = RestToolbox.DoGet(URL + '/changes?last')['Last']
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 # Polling loop using the "changes" API of Orthanc, waiting for the
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 # incoming of new DICOM files
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 while True:
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 r = RestToolbox.DoGet(URL + '/changes', {
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 'since' : current,
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 'limit' : 4 # Retrieve at most 4 changes at once
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 })
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 for change in r['Changes']:
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 # We are only interested interested in the arrival of new instances
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 if change['ChangeType'] == 'NewInstance':
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 # Extract the patient, study, series and instance information
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 instance = RestToolbox.DoGet('%s/instances/%s' % (URL, change['ID']))
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 series = RestToolbox.DoGet('%s/series/%s' % (URL, instance['ParentSeries']))
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 study = RestToolbox.DoGet('%s/studies/%s' % (URL, series['ParentStudy']))
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 patient = RestToolbox.DoGet('%s/patients/%s' % (URL, study['ParentPatient']))
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 # Construct a target path
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 a = '%s - %s' % (patient['MainDicomTags']['PatientID'],
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 patient['MainDicomTags']['PatientName'])
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 b = study['MainDicomTags']['StudyDescription']
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 c = '%s - %s' % (series['MainDicomTags']['Modality'],
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 series['MainDicomTags']['SeriesDescription'])
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 d = '%s.dcm' % instance['MainDicomTags']['SOPInstanceUID']
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 p = os.path.join(
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 args.target,
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 a.encode('ascii', 'ignore'),
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 b.encode('ascii', 'ignore'),
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 c.encode('ascii', 'ignore')
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 )
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 f = os.path.join(p, d.encode('ascii', 'ignore'))
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 # Copy the DICOM file to the target path
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 print('Writing new DICOM file: %s' % f)
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 try:
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 os.makedirs(p)
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 except:
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 # Already existing directory, ignore the error
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 pass
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 dcm = RestToolbox.DoGet('%s/instances/%s/file' % (URL, change['ID']))
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 with open(f, 'wb') as g:
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 g.write(dcm)
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 # If requested, remove the instance once it has been copied
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 if args.remove:
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 RestToolbox.DoDelete('%s/instances/%s' % (URL, change['ID']))
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 current = r['Last']
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 if r['Done']:
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 print "Everything has been processed: Waiting..."
d74ac5d0bcaf New sample: Automated classification of DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 time.sleep(1)