Mercurial > hg > orthanc-tests
annotate Plugins/WSI/Run.py @ 310:65d5c591fece c-get
closing c-get
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 20 May 2020 16:35:50 +0200 |
parents | 943166deebcb |
children | bb01670b3b79 |
rev | line source |
---|---|
251 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 | |
5 # Orthanc - A Lightweight, RESTful DICOM Store | |
6 # Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
7 # Department, University Hospital of Liege, Belgium | |
260
943166deebcb
upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
253
diff
changeset
|
8 # Copyright (C) 2017-2020 Osimis S.A., Belgium |
251 | 9 # |
10 # This program is free software: you can redistribute it and/or | |
11 # modify it under the terms of the GNU General Public License as | |
12 # published by the Free Software Foundation, either version 3 of the | |
13 # License, or (at your option) any later version. | |
14 # | |
15 # This program is distributed in the hope that it will be useful, but | |
16 # WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
18 # General Public License for more details. | |
19 # | |
20 # You should have received a copy of the GNU General Public License | |
21 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
22 | |
23 | |
24 import argparse | |
25 import os | |
26 import pprint | |
27 import re | |
28 import subprocess | |
29 import sys | |
30 import tempfile | |
31 import unittest | |
32 from shutil import copyfile | |
33 | |
34 sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'Tests')) | |
35 from Toolbox import * | |
36 | |
37 | |
38 ## | |
39 ## Parse the command-line arguments | |
40 ## | |
41 | |
42 parser = argparse.ArgumentParser(description = 'Run the integration tests for the WSI Dicomizer.') | |
43 | |
44 parser.add_argument('--server', | |
45 default = 'localhost', | |
46 help = 'Address of the Orthanc server to test') | |
47 parser.add_argument('--rest', | |
48 type = int, | |
49 default = 8042, | |
50 help = 'Port to the REST API') | |
51 parser.add_argument('--username', | |
52 default = 'alice', | |
53 help = 'Username to the REST API') | |
54 parser.add_argument('--password', | |
55 default = 'orthanctest', | |
56 help = 'Password to the REST API') | |
57 parser.add_argument('--dicomizer', | |
58 default = '/home/jodogne/Subversion/orthanc-wsi/Applications/i/OrthancWSIDicomizer', | |
59 help = 'Password to the REST API') | |
60 parser.add_argument('--to-tiff', | |
61 default = '/home/jodogne/Subversion/orthanc-wsi/Applications/i/OrthancWSIDicomToTiff', | |
62 help = 'Password to the REST API') | |
252 | 63 parser.add_argument('--valgrind', help = 'Use valgrind while running the DICOM-izer', |
64 action = 'store_true') | |
251 | 65 parser.add_argument('--force', help = 'Do not warn the user', |
66 action = 'store_true') | |
67 parser.add_argument('options', metavar = 'N', nargs = '*', | |
68 help='Arguments to Python unittest') | |
69 | |
70 args = parser.parse_args() | |
71 | |
72 if not args.force: | |
73 print(""" | |
74 WARNING: This test will remove all the content of your | |
75 Orthanc instance running on %s! | |
76 | |
77 Are you sure ["yes" to go on]?""" % args.server) | |
78 | |
79 if sys.stdin.readline().strip() != 'yes': | |
80 print('Aborting...') | |
81 exit(0) | |
82 | |
83 | |
84 | |
85 | |
86 ## | |
87 ## The tests | |
88 ## | |
89 | |
90 ORTHANC = DefineOrthanc(server = args.server, | |
91 username = args.username, | |
92 password = args.password, | |
93 restPort = args.rest) | |
94 | |
253 | 95 def CallCommand(command): |
252 | 96 prefix = [] |
97 if args.valgrind: | |
98 prefix = [ 'valgrind' ] | |
99 | |
253 | 100 log = subprocess.check_output(prefix + command, |
252 | 101 stderr=subprocess.STDOUT) |
253 | 102 |
252 | 103 # If using valgrind, only print the lines from the log starting |
104 # with '==' (they contain the report from valgrind) | |
105 if args.valgrind: | |
106 print('\n'.join(filter(lambda x: x.startswith('=='), log.splitlines()))) | |
251 | 107 |
253 | 108 |
109 def CallDicomizer(suffix): | |
110 CallCommand([ args.dicomizer, | |
111 '--username=%s' % args.username, | |
112 '--password=%s' % args.password ] + suffix) | |
113 | |
114 | |
115 def CallDicomToTiff(suffix): | |
116 CallCommand([ args.to_tiff ] + suffix) | |
117 | |
118 | |
119 def CallTiffInfoOnSeries(series): | |
120 with tempfile.NamedTemporaryFile(delete = False) as temp: | |
121 temp.close() | |
122 CallDicomToTiff([ series, temp.name ]) | |
123 tiff = subprocess.check_output([ 'tiffinfo', temp.name ]) | |
124 os.unlink(temp.name) | |
125 | |
126 return tiff | |
127 | |
251 | 128 |
129 class Orthanc(unittest.TestCase): | |
130 def setUp(self): | |
131 if (sys.version_info >= (3, 0)): | |
132 # Remove annoying warnings about unclosed socket in Python 3 | |
133 import warnings | |
134 warnings.simplefilter("ignore", ResourceWarning) | |
135 | |
136 DropOrthanc(ORTHANC) | |
137 | |
138 | |
139 def test_single(self): | |
140 CallDicomizer([ GetDatabasePath('Lena.jpg') ]) | |
141 | |
142 i = DoGet(ORTHANC, '/instances') | |
143 self.assertEqual(1, len(i)) | |
144 | |
145 tags = DoGet(ORTHANC, '/instances/%s/tags?simplify' % i[0]) | |
146 self.assertEqual(1, int(tags['NumberOfFrames'])) | |
147 self.assertEqual(512, int(tags['Columns'])) | |
148 self.assertEqual(512, int(tags['Rows'])) | |
149 self.assertEqual('YBR_FULL_422', tags['PhotometricInterpretation']) | |
150 | |
151 s = DoGet(ORTHANC, '/series') | |
152 self.assertEqual(1, len(s)) | |
153 | |
154 pyramid = DoGet(ORTHANC, '/wsi/pyramids/%s' % s[0]) | |
155 self.assertEqual(s[0], pyramid['ID']) | |
156 self.assertEqual(1, len(pyramid['Resolutions'])) | |
157 self.assertEqual(1, len(pyramid['Sizes'])) | |
158 self.assertEqual(1, len(pyramid['TilesCount'])) | |
159 self.assertEqual(1, pyramid['Resolutions'][0]) | |
160 self.assertEqual(512, pyramid['Sizes'][0][0]) | |
161 self.assertEqual(512, pyramid['Sizes'][0][1]) | |
162 self.assertEqual(512, pyramid['TileWidth']) | |
163 self.assertEqual(512, pyramid['TileHeight']) | |
164 self.assertEqual(512, pyramid['TotalWidth']) | |
165 self.assertEqual(512, pyramid['TotalHeight']) | |
166 self.assertEqual(1, pyramid['TilesCount'][0][0]) | |
167 self.assertEqual(1, pyramid['TilesCount'][0][1]) | |
168 | |
253 | 169 tiff = CallTiffInfoOnSeries(s[0]) |
170 p = filter(lambda x: 'Photometric Interpretation' in x, tiff.splitlines()) | |
171 self.assertEqual(1, len(p)) | |
172 self.assertTrue('YCbCr' in p[0]) | |
173 | |
251 | 174 |
175 def test_grayscale_pyramid(self): | |
176 CallDicomizer([ GetDatabasePath('LenaGrayscale.png'), '--tile-width=64', '--tile-height=64' ]) | |
177 | |
178 i = DoGet(ORTHANC, '/instances') | |
179 self.assertEqual(4, len(i)) | |
180 | |
181 for j in range(4): | |
182 tags = DoGet(ORTHANC, '/instances/%s/tags?simplify' % i[j]) | |
183 self.assertEqual(64, int(tags['Columns'])) | |
184 self.assertEqual(64, int(tags['Rows'])) | |
185 self.assertEqual('MONOCHROME2', tags['PhotometricInterpretation']) | |
186 | |
187 s = DoGet(ORTHANC, '/series') | |
188 self.assertEqual(1, len(s)) | |
189 | |
190 pyramid = DoGet(ORTHANC, '/wsi/pyramids/%s' % s[0]) | |
191 self.assertEqual(s[0], pyramid['ID']) | |
192 self.assertEqual(4, len(pyramid['Resolutions'])) | |
193 self.assertEqual(4, len(pyramid['Sizes'])) | |
194 self.assertEqual(4, len(pyramid['TilesCount'])) | |
195 self.assertEqual(1, pyramid['Resolutions'][0]) | |
196 self.assertEqual(2, pyramid['Resolutions'][1]) | |
197 self.assertEqual(4, pyramid['Resolutions'][2]) | |
198 self.assertEqual(8, pyramid['Resolutions'][3]) | |
199 self.assertEqual(512, pyramid['Sizes'][0][0]) | |
200 self.assertEqual(512, pyramid['Sizes'][0][1]) | |
201 self.assertEqual(256, pyramid['Sizes'][1][0]) | |
202 self.assertEqual(256, pyramid['Sizes'][1][1]) | |
203 self.assertEqual(128, pyramid['Sizes'][2][0]) | |
204 self.assertEqual(128, pyramid['Sizes'][2][1]) | |
205 self.assertEqual(64, pyramid['Sizes'][3][0]) | |
206 self.assertEqual(64, pyramid['Sizes'][3][1]) | |
207 self.assertEqual(64, pyramid['TileWidth']) | |
208 self.assertEqual(64, pyramid['TileHeight']) | |
209 self.assertEqual(512, pyramid['TotalWidth']) | |
210 self.assertEqual(512, pyramid['TotalHeight']) | |
211 self.assertEqual(8, pyramid['TilesCount'][0][0]) | |
212 self.assertEqual(8, pyramid['TilesCount'][0][1]) | |
213 self.assertEqual(4, pyramid['TilesCount'][1][0]) | |
214 self.assertEqual(4, pyramid['TilesCount'][1][1]) | |
215 self.assertEqual(2, pyramid['TilesCount'][2][0]) | |
216 self.assertEqual(2, pyramid['TilesCount'][2][1]) | |
217 self.assertEqual(1, pyramid['TilesCount'][3][0]) | |
218 self.assertEqual(1, pyramid['TilesCount'][3][1]) | |
253 | 219 |
220 tiff = CallTiffInfoOnSeries(s[0]) | |
221 p = filter(lambda x: 'Photometric Interpretation' in x, tiff.splitlines()) | |
222 self.assertEqual(4, len(p)) | |
223 for j in range(4): | |
224 self.assertTrue('min-is-black' in p[j]) | |
225 | |
226 | |
227 def test_import_tiff_grayscale(self): | |
228 CallDicomizer([ GetDatabasePath('WSI/LenaGrayscaleJpeg.tiff') ]) | |
229 | |
230 s = DoGet(ORTHANC, '/series') | |
231 self.assertEqual(1, len(s)) | |
232 | |
233 pyramid = DoGet(ORTHANC, '/wsi/pyramids/%s' % s[0]) | |
234 self.assertEqual(4, len(pyramid['Resolutions'])) | |
235 | |
236 tiff = CallTiffInfoOnSeries(s[0]) | |
237 p = filter(lambda x: 'Photometric Interpretation' in x, tiff.splitlines()) | |
238 self.assertEqual(4, len(p)) | |
239 for j in range(4): | |
240 self.assertTrue('min-is-black' in p[j]) | |
241 | |
242 | |
243 def test_import_tiff_ycbcr(self): | |
244 CallDicomizer([ GetDatabasePath('WSI/LenaColorJpegYCbCr.tiff') ]) | |
245 | |
246 s = DoGet(ORTHANC, '/series') | |
247 self.assertEqual(1, len(s)) | |
248 | |
249 pyramid = DoGet(ORTHANC, '/wsi/pyramids/%s' % s[0]) | |
250 self.assertEqual(4, len(pyramid['Resolutions'])) | |
251 | |
252 tiff = CallTiffInfoOnSeries(s[0]) | |
253 p = filter(lambda x: 'Photometric Interpretation' in x, tiff.splitlines()) | |
254 self.assertEqual(4, len(p)) | |
255 for j in range(4): | |
256 self.assertTrue('YCbCr' in p[j]) | |
257 | |
258 | |
259 def test_import_tiff_rgb(self): | |
260 CallDicomizer([ GetDatabasePath('WSI/LenaColorJpegRGB.tiff') ]) | |
261 | |
262 s = DoGet(ORTHANC, '/series') | |
263 self.assertEqual(1, len(s)) | |
264 | |
265 pyramid = DoGet(ORTHANC, '/wsi/pyramids/%s' % s[0]) | |
266 self.assertEqual(4, len(pyramid['Resolutions'])) | |
267 | |
268 tiff = CallTiffInfoOnSeries(s[0]) | |
269 p = filter(lambda x: 'Photometric Interpretation' in x, tiff.splitlines()) | |
270 self.assertEqual(4, len(p)) | |
271 for j in range(4): | |
272 self.assertTrue('RGB' in p[j]) | |
273 | |
251 | 274 |
275 try: | |
276 print('\nStarting the tests...') | |
277 unittest.main(argv = [ sys.argv[0] ] + args.options) | |
278 | |
279 finally: | |
280 print('\nDone') |