Mercurial > hg > orthanc
annotate OrthancServer/Resources/Samples/Python/ArchiveStudiesInTimeRange.py @ 5150:6ff13c95f62f
ThreadedSetOfInstancesJob: reduce cpu consumption of job main thread
author | Alain Mazy <am@osimis.io> |
---|---|
date | Mon, 30 Jan 2023 11:31:12 +0100 |
parents | 43e613a7756b |
children | 0ea402b4d901 |
rev | line source |
---|---|
1348
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
1 #!/usr/bin/python |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
3 |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
4 # Orthanc - A Lightweight, RESTful DICOM Store |
1900 | 5 # Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
1348
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
6 # Department, University Hospital of Liege, Belgium |
4870
43e613a7756b
upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4855
diff
changeset
|
7 # Copyright (C) 2017-2022 Osimis S.A., Belgium |
43e613a7756b
upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4855
diff
changeset
|
8 # Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
1348
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
9 # |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
10 # This program is free software: you can redistribute it and/or |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
11 # modify it under the terms of the GNU General Public License as |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
12 # published by the Free Software Foundation, either version 3 of the |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
13 # License, or (at your option) any later version. |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
14 # |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
15 # This program is distributed in the hope that it will be useful, but |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
16 # WITHOUT ANY WARRANTY; without even the implied warranty of |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
18 # General Public License for more details. |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
19 # |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
20 # You should have received a copy of the GNU General Public License |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
21 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
22 |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
23 |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
24 |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
25 import os |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
26 import os.path |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
27 import sys |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
28 import RestToolbox |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
29 |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
30 def PrintHelp(): |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
31 print('Download ZIP archives for all the studies generated ' |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
32 'during a given time range (according to the StudyDate tag)\n') |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
33 print('Usage: %s <URL> <StartDate> <EndDate> <TargetFolder>\n' % sys.argv[0]) |
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
|
34 print('Example: %s http://127.0.0.1:8042/ 20150101 20151231 /tmp/\n' % sys.argv[0]) |
1348
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
35 exit(-1) |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
36 |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
37 def CheckIsDate(date): |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
38 if len(date) != 8 or not date.isdigit(): |
4855
ffc03a1d60c3
fix sample scripts for Python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4831
diff
changeset
|
39 print('"%s" is not a valid date!\n' % date) |
1348
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
40 exit(-1) |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
41 |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
42 |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
43 if len(sys.argv) != 5: |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
44 PrintHelp() |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
45 |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
46 URL = sys.argv[1] |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
47 START = sys.argv[2] |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
48 END = sys.argv[3] |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
49 TARGET = sys.argv[4] |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
50 |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
51 CheckIsDate(START) |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
52 CheckIsDate(END) |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
53 |
2070
7e6afa0beaf6
samples: improved handling of special characters in paths
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2032
diff
changeset
|
54 def GetTag(tags, key): |
7e6afa0beaf6
samples: improved handling of special characters in paths
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2032
diff
changeset
|
55 if key in tags: |
7e6afa0beaf6
samples: improved handling of special characters in paths
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2032
diff
changeset
|
56 return tags[key] |
7e6afa0beaf6
samples: improved handling of special characters in paths
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2032
diff
changeset
|
57 else: |
7e6afa0beaf6
samples: improved handling of special characters in paths
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2032
diff
changeset
|
58 return 'No%s' % key |
7e6afa0beaf6
samples: improved handling of special characters in paths
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2032
diff
changeset
|
59 |
1348
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
60 # Loop over the studies |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
61 for studyId in RestToolbox.DoGet('%s/studies' % URL): |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
62 # Retrieve the DICOM tags of the current study |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
63 study = RestToolbox.DoGet('%s/studies/%s' % (URL, studyId))['MainDicomTags'] |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
64 |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
65 # Retrieve the DICOM tags of the parent patient of this study |
4089
a2060a76ed6a
note in ArchiveStudiesInTimeRange.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
66 |
a2060a76ed6a
note in ArchiveStudiesInTimeRange.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
67 # Case 1: Baseline version |
1348
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
68 patient = RestToolbox.DoGet('%s/studies/%s/patient' % (URL, studyId))['MainDicomTags'] |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
69 |
4089
a2060a76ed6a
note in ArchiveStudiesInTimeRange.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
70 # Case 2: Tweaked version that can be used if several patients |
a2060a76ed6a
note in ArchiveStudiesInTimeRange.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
71 # share the same "Patient ID", but have different "Patient Name" |
a2060a76ed6a
note in ArchiveStudiesInTimeRange.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
72 # (which is invalid according to the DICOM standard). |
a2060a76ed6a
note in ArchiveStudiesInTimeRange.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
73 # https://groups.google.com/d/msg/orthanc-users/58AxIkxFbZs/N6Knub8MAgAJ |
a2060a76ed6a
note in ArchiveStudiesInTimeRange.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
74 # patient = RestToolbox.DoGet('%s/studies/%s' % (URL, studyId)) ['PatientMainDicomTags'] |
a2060a76ed6a
note in ArchiveStudiesInTimeRange.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
75 |
1348
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
76 # Check that the StudyDate tag lies within the given range |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
77 studyDate = study['StudyDate'][:8] |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
78 if studyDate >= START and studyDate <= END: |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
79 # Create a filename |
2070
7e6afa0beaf6
samples: improved handling of special characters in paths
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2032
diff
changeset
|
80 filename = '%s - %s %s - %s.zip' % (GetTag(study, 'StudyDate'), |
7e6afa0beaf6
samples: improved handling of special characters in paths
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2032
diff
changeset
|
81 GetTag(patient, 'PatientID'), |
7e6afa0beaf6
samples: improved handling of special characters in paths
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2032
diff
changeset
|
82 GetTag(patient, 'PatientName'), |
7e6afa0beaf6
samples: improved handling of special characters in paths
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2032
diff
changeset
|
83 GetTag(study, 'StudyDescription')) |
1348
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
84 |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
85 # Remove any non-ASCII character in the filename |
4855
ffc03a1d60c3
fix sample scripts for Python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4831
diff
changeset
|
86 filename = filename.encode('ascii', errors = 'replace') |
ffc03a1d60c3
fix sample scripts for Python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4831
diff
changeset
|
87 filename = filename.translate(None, b"'\/:*?\"<>|!=") |
ffc03a1d60c3
fix sample scripts for Python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4831
diff
changeset
|
88 filename = filename.decode('ascii').strip() |
1348
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
89 |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
90 # Download the ZIP archive of the study |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
91 print('Downloading %s' % filename) |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
92 zipContent = RestToolbox.DoGet('%s/studies/%s/archive' % (URL, studyId)) |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
93 |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
94 # Write the ZIP archive at the proper location |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
95 with open(os.path.join(TARGET, filename), 'wb') as f: |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
96 f.write(zipContent) |