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