Commit 07f7b52a73260f1bb6a8247eec4644c3d768f2f0
1 parent
61106a5f
Exists in
master
iSort and Black to improve source code format in bitmap reader and imagedata_utils
Showing
2 changed files
with
239 additions
and
196 deletions
Show diff stats
invesalius/data/imagedata_utils.py
1 | -#-------------------------------------------------------------------------- | 1 | +# -------------------------------------------------------------------------- |
2 | # Software: InVesalius - Software de Reconstrucao 3D de Imagens Medicas | 2 | # Software: InVesalius - Software de Reconstrucao 3D de Imagens Medicas |
3 | # Copyright: (C) 2001 Centro de Pesquisas Renato Archer | 3 | # Copyright: (C) 2001 Centro de Pesquisas Renato Archer |
4 | # Homepage: http://www.softwarepublico.gov.br | 4 | # Homepage: http://www.softwarepublico.gov.br |
5 | # Contact: invesalius@cti.gov.br | 5 | # Contact: invesalius@cti.gov.br |
6 | # License: GNU - GPL 2 (LICENSE.txt/LICENCA.txt) | 6 | # License: GNU - GPL 2 (LICENSE.txt/LICENCA.txt) |
7 | -#-------------------------------------------------------------------------- | 7 | +# -------------------------------------------------------------------------- |
8 | # Este programa e software livre; voce pode redistribui-lo e/ou | 8 | # Este programa e software livre; voce pode redistribui-lo e/ou |
9 | # modifica-lo sob os termos da Licenca Publica Geral GNU, conforme | 9 | # modifica-lo sob os termos da Licenca Publica Geral GNU, conforme |
10 | # publicada pela Free Software Foundation; de acordo com a versao 2 | 10 | # publicada pela Free Software Foundation; de acordo com a versao 2 |
@@ -15,7 +15,7 @@ | @@ -15,7 +15,7 @@ | ||
15 | # COMERCIALIZACAO ou de ADEQUACAO A QUALQUER PROPOSITO EM | 15 | # COMERCIALIZACAO ou de ADEQUACAO A QUALQUER PROPOSITO EM |
16 | # PARTICULAR. Consulte a Licenca Publica Geral GNU para obter mais | 16 | # PARTICULAR. Consulte a Licenca Publica Geral GNU para obter mais |
17 | # detalhes. | 17 | # detalhes. |
18 | -#-------------------------------------------------------------------------- | 18 | +# -------------------------------------------------------------------------- |
19 | 19 | ||
20 | import math | 20 | import math |
21 | import os | 21 | import os |
@@ -28,21 +28,19 @@ import numpy | @@ -28,21 +28,19 @@ import numpy | ||
28 | import numpy as np | 28 | import numpy as np |
29 | import vtk | 29 | import vtk |
30 | from pubsub import pub as Publisher | 30 | from pubsub import pub as Publisher |
31 | - | ||
32 | from scipy.ndimage import shift, zoom | 31 | from scipy.ndimage import shift, zoom |
33 | from vtk.util import numpy_support | 32 | from vtk.util import numpy_support |
34 | 33 | ||
35 | import invesalius.constants as const | 34 | import invesalius.constants as const |
36 | -from invesalius.data import vtk_utils as vtk_utils | ||
37 | -import invesalius.reader.bitmap_reader as bitmap_reader | ||
38 | -import invesalius.utils as utils | ||
39 | import invesalius.data.converters as converters | 35 | import invesalius.data.converters as converters |
40 | import invesalius.data.slice_ as sl | 36 | import invesalius.data.slice_ as sl |
41 | import invesalius.data.transformations as tr | 37 | import invesalius.data.transformations as tr |
42 | - | 38 | +import invesalius.reader.bitmap_reader as bitmap_reader |
39 | +import invesalius.utils as utils | ||
43 | from invesalius import inv_paths | 40 | from invesalius import inv_paths |
41 | +from invesalius.data import vtk_utils as vtk_utils | ||
44 | 42 | ||
45 | -if sys.platform == 'win32': | 43 | +if sys.platform == "win32": |
46 | try: | 44 | try: |
47 | import win32api | 45 | import win32api |
48 | _has_win32api = True | 46 | _has_win32api = True |
@@ -54,6 +52,7 @@ else: | @@ -54,6 +52,7 @@ else: | ||
54 | # TODO: Test cases which are originally in sagittal/coronal orientation | 52 | # TODO: Test cases which are originally in sagittal/coronal orientation |
55 | # and have gantry | 53 | # and have gantry |
56 | 54 | ||
55 | + | ||
57 | def ResampleImage3D(imagedata, value): | 56 | def ResampleImage3D(imagedata, value): |
58 | """ | 57 | """ |
59 | Resample vtkImageData matrix. | 58 | Resample vtkImageData matrix. |
@@ -63,9 +62,9 @@ def ResampleImage3D(imagedata, value): | @@ -63,9 +62,9 @@ def ResampleImage3D(imagedata, value): | ||
63 | size = imagedata.GetDimensions() | 62 | size = imagedata.GetDimensions() |
64 | 63 | ||
65 | width = float(size[0]) | 64 | width = float(size[0]) |
66 | - height = float(size[1]/value) | 65 | + height = float(size[1] / value) |
67 | 66 | ||
68 | - resolution = (height/(extent[1]-extent[0])+1)*spacing[1] | 67 | + resolution = (height / (extent[1] - extent[0]) + 1) * spacing[1] |
69 | 68 | ||
70 | resample = vtk.vtkImageResample() | 69 | resample = vtk.vtkImageResample() |
71 | resample.SetInput(imagedata) | 70 | resample.SetInput(imagedata) |
@@ -74,8 +73,10 @@ def ResampleImage3D(imagedata, value): | @@ -74,8 +73,10 @@ def ResampleImage3D(imagedata, value): | ||
74 | 73 | ||
75 | return resample.GetOutput() | 74 | return resample.GetOutput() |
76 | 75 | ||
77 | -def ResampleImage2D(imagedata, px=None, py=None, resolution_percentage = None, | ||
78 | - update_progress = None): | 76 | + |
77 | +def ResampleImage2D( | ||
78 | + imagedata, px=None, py=None, resolution_percentage=None, update_progress=None | ||
79 | +): | ||
79 | """ | 80 | """ |
80 | Resample vtkImageData matrix. | 81 | Resample vtkImageData matrix. |
81 | """ | 82 | """ |
@@ -88,30 +89,30 @@ def ResampleImage2D(imagedata, px=None, py=None, resolution_percentage = None, | @@ -88,30 +89,30 @@ def ResampleImage2D(imagedata, px=None, py=None, resolution_percentage = None, | ||
88 | factor_x = resolution_percentage | 89 | factor_x = resolution_percentage |
89 | factor_y = resolution_percentage | 90 | factor_y = resolution_percentage |
90 | else: | 91 | else: |
91 | - if abs(extent[1]-extent[3]) < abs(extent[3]-extent[5]): | 92 | + if abs(extent[1] - extent[3]) < abs(extent[3] - extent[5]): |
92 | f = extent[1] | 93 | f = extent[1] |
93 | - elif abs(extent[1]-extent[5]) < abs(extent[1] - extent[3]): | 94 | + elif abs(extent[1] - extent[5]) < abs(extent[1] - extent[3]): |
94 | f = extent[1] | 95 | f = extent[1] |
95 | - elif abs(extent[3]-extent[5]) < abs(extent[1] - extent[3]): | 96 | + elif abs(extent[3] - extent[5]) < abs(extent[1] - extent[3]): |
96 | f = extent[3] | 97 | f = extent[3] |
97 | else: | 98 | else: |
98 | f = extent[1] | 99 | f = extent[1] |
99 | 100 | ||
100 | - factor_x = px/float(f+1) | ||
101 | - factor_y = py/float(f+1) | 101 | + factor_x = px / float(f + 1) |
102 | + factor_y = py / float(f + 1) | ||
102 | 103 | ||
103 | resample = vtk.vtkImageResample() | 104 | resample = vtk.vtkImageResample() |
104 | resample.SetInputData(imagedata) | 105 | resample.SetInputData(imagedata) |
105 | resample.SetAxisMagnificationFactor(0, factor_x) | 106 | resample.SetAxisMagnificationFactor(0, factor_x) |
106 | resample.SetAxisMagnificationFactor(1, factor_y) | 107 | resample.SetAxisMagnificationFactor(1, factor_y) |
107 | # resample.SetOutputSpacing(spacing[0] * factor_x, spacing[1] * factor_y, spacing[2]) | 108 | # resample.SetOutputSpacing(spacing[0] * factor_x, spacing[1] * factor_y, spacing[2]) |
108 | - if (update_progress): | 109 | + if update_progress: |
109 | message = _("Generating multiplanar visualization...") | 110 | message = _("Generating multiplanar visualization...") |
110 | - resample.AddObserver("ProgressEvent", lambda obj, | ||
111 | - evt:update_progress(resample,message)) | 111 | + resample.AddObserver( |
112 | + "ProgressEvent", lambda obj, evt: update_progress(resample, message) | ||
113 | + ) | ||
112 | resample.Update() | 114 | resample.Update() |
113 | 115 | ||
114 | - | ||
115 | return resample.GetOutput() | 116 | return resample.GetOutput() |
116 | 117 | ||
117 | 118 | ||
@@ -131,7 +132,7 @@ def resize_image_array(image, resolution_percentage, as_mmap=False): | @@ -131,7 +132,7 @@ def resize_image_array(image, resolution_percentage, as_mmap=False): | ||
131 | out = zoom(image, resolution_percentage, image.dtype, order=2) | 132 | out = zoom(image, resolution_percentage, image.dtype, order=2) |
132 | if as_mmap: | 133 | if as_mmap: |
133 | fname = tempfile.mktemp(suffix="_resized") | 134 | fname = tempfile.mktemp(suffix="_resized") |
134 | - out_mmap = np.memmap(fname, shape=out.shape, dtype=out.dtype, mode='w+') | 135 | + out_mmap = np.memmap(fname, shape=out.shape, dtype=out.dtype, mode="w+") |
135 | out_mmap[:] = out | 136 | out_mmap[:] = out |
136 | return out_mmap | 137 | return out_mmap |
137 | return out | 138 | return out |
@@ -159,7 +160,7 @@ def FixGantryTilt(matrix, spacing, tilt): | @@ -159,7 +160,7 @@ def FixGantryTilt(matrix, spacing, tilt): | ||
159 | 160 | ||
160 | for n, slice_ in enumerate(matrix): | 161 | for n, slice_ in enumerate(matrix): |
161 | offset = gntan * n * spacing[2] | 162 | offset = gntan * n * spacing[2] |
162 | - matrix[n] = shift(slice_, (-offset/spacing[1], 0), cval=matrix.min()) | 163 | + matrix[n] = shift(slice_, (-offset / spacing[1], 0), cval=matrix.min()) |
163 | 164 | ||
164 | 165 | ||
165 | def BuildEditedImage(imagedata, points): | 166 | def BuildEditedImage(imagedata, points): |
@@ -175,28 +176,28 @@ def BuildEditedImage(imagedata, points): | @@ -175,28 +176,28 @@ def BuildEditedImage(imagedata, points): | ||
175 | imagedata.SetScalarComponentFromDouble(x, y, z, 0, colour) | 176 | imagedata.SetScalarComponentFromDouble(x, y, z, 0, colour) |
176 | imagedata.Update() | 177 | imagedata.Update() |
177 | 178 | ||
178 | - if not(init_values): | ||
179 | - xi = x | ||
180 | - xf = x | ||
181 | - yi = y | ||
182 | - yf = y | ||
183 | - zi = z | ||
184 | - zf = z | ||
185 | - init_values = 1 | 179 | + if not (init_values): |
180 | + xi = x | ||
181 | + xf = x | ||
182 | + yi = y | ||
183 | + yf = y | ||
184 | + zi = z | ||
185 | + zf = z | ||
186 | + init_values = 1 | ||
186 | 187 | ||
187 | - if (xi > x): | 188 | + if xi > x: |
188 | xi = x | 189 | xi = x |
189 | - elif(xf < x): | 190 | + elif xf < x: |
190 | xf = x | 191 | xf = x |
191 | 192 | ||
192 | - if (yi > y): | 193 | + if yi > y: |
193 | yi = y | 194 | yi = y |
194 | - elif(yf < y): | 195 | + elif yf < y: |
195 | yf = y | 196 | yf = y |
196 | 197 | ||
197 | - if (zi > z): | 198 | + if zi > z: |
198 | zi = z | 199 | zi = z |
199 | - elif(zf < z): | 200 | + elif zf < z: |
200 | zf = z | 201 | zf = z |
201 | 202 | ||
202 | clip = vtk.vtkImageClip() | 203 | clip = vtk.vtkImageClip() |
@@ -226,8 +227,9 @@ def Export(imagedata, filename, bin=False): | @@ -226,8 +227,9 @@ def Export(imagedata, filename, bin=False): | ||
226 | writer.SetDataModeToBinary() | 227 | writer.SetDataModeToBinary() |
227 | else: | 228 | else: |
228 | writer.SetDataModeToAscii() | 229 | writer.SetDataModeToAscii() |
229 | - #writer.SetInput(imagedata) | ||
230 | - #writer.Write() | 230 | + # writer.SetInput(imagedata) |
231 | + # writer.Write() | ||
232 | + | ||
231 | 233 | ||
232 | def Import(filename): | 234 | def Import(filename): |
233 | reader = vtk.vtkXMLImageDataReader() | 235 | reader = vtk.vtkXMLImageDataReader() |
@@ -238,6 +240,7 @@ def Import(filename): | @@ -238,6 +240,7 @@ def Import(filename): | ||
238 | 240 | ||
239 | return reader.GetOutput() | 241 | return reader.GetOutput() |
240 | 242 | ||
243 | + | ||
241 | def View(imagedata): | 244 | def View(imagedata): |
242 | viewer = vtk.vtkImageViewer() | 245 | viewer = vtk.vtkImageViewer() |
243 | viewer.SetInput(imagedata) | 246 | viewer.SetInput(imagedata) |
@@ -246,16 +249,17 @@ def View(imagedata): | @@ -246,16 +249,17 @@ def View(imagedata): | ||
246 | viewer.Render() | 249 | viewer.Render() |
247 | 250 | ||
248 | import time | 251 | import time |
252 | + | ||
249 | time.sleep(10) | 253 | time.sleep(10) |
250 | 254 | ||
251 | 255 | ||
252 | -def ExtractVOI(imagedata,xi,xf,yi,yf,zi,zf): | 256 | +def ExtractVOI(imagedata, xi, xf, yi, yf, zi, zf): |
253 | """ | 257 | """ |
254 | Cropping the vtkImagedata according | 258 | Cropping the vtkImagedata according |
255 | with values. | 259 | with values. |
256 | """ | 260 | """ |
257 | voi = vtk.vtkExtractVOI() | 261 | voi = vtk.vtkExtractVOI() |
258 | - voi.SetVOI(xi,xf,yi,yf,zi,zf) | 262 | + voi.SetVOI(xi, xf, yi, yf, zi, zf) |
259 | voi.SetInputData(imagedata) | 263 | voi.SetInputData(imagedata) |
260 | voi.SetSampleRate(1, 1, 1) | 264 | voi.SetSampleRate(1, 1, 1) |
261 | voi.Update() | 265 | voi.Update() |
@@ -274,27 +278,30 @@ def create_dicom_thumbnails(image, window=None, level=None): | @@ -274,27 +278,30 @@ def create_dicom_thumbnails(image, window=None, level=None): | ||
274 | thumbnail_paths = [] | 278 | thumbnail_paths = [] |
275 | for i in range(np_image.shape[0]): | 279 | for i in range(np_image.shape[0]): |
276 | thumb_image = zoom(np_image[i], 0.25) | 280 | thumb_image = zoom(np_image[i], 0.25) |
277 | - thumb_image = np.array(get_LUT_value_255(thumb_image, window, level), dtype=np.uint8) | ||
278 | - thumbnail_path = tempfile.mktemp(prefix='thumb_', suffix='.png') | 281 | + thumb_image = np.array( |
282 | + get_LUT_value_255(thumb_image, window, level), dtype=np.uint8 | ||
283 | + ) | ||
284 | + thumbnail_path = tempfile.mktemp(prefix="thumb_", suffix=".png") | ||
279 | imageio.imsave(thumbnail_path, thumb_image) | 285 | imageio.imsave(thumbnail_path, thumb_image) |
280 | thumbnail_paths.append(thumbnail_path) | 286 | thumbnail_paths.append(thumbnail_path) |
281 | return thumbnail_paths | 287 | return thumbnail_paths |
282 | else: | 288 | else: |
283 | - thumbnail_path = tempfile.mktemp(prefix='thumb_', suffix='.png') | 289 | + thumbnail_path = tempfile.mktemp(prefix="thumb_", suffix=".png") |
284 | if pf.GetSamplesPerPixel() == 1: | 290 | if pf.GetSamplesPerPixel() == 1: |
285 | thumb_image = zoom(np_image, 0.25) | 291 | thumb_image = zoom(np_image, 0.25) |
286 | - thumb_image = np.array(get_LUT_value_255(thumb_image, window, level), dtype=np.uint8) | 292 | + thumb_image = np.array( |
293 | + get_LUT_value_255(thumb_image, window, level), dtype=np.uint8 | ||
294 | + ) | ||
287 | else: | 295 | else: |
288 | thumb_image = zoom(np_image, (0.25, 0.25, 1)) | 296 | thumb_image = zoom(np_image, (0.25, 0.25, 1)) |
289 | imageio.imsave(thumbnail_path, thumb_image) | 297 | imageio.imsave(thumbnail_path, thumb_image) |
290 | return thumbnail_path | 298 | return thumbnail_path |
291 | 299 | ||
292 | 300 | ||
293 | - | ||
294 | def array2memmap(arr, filename=None): | 301 | def array2memmap(arr, filename=None): |
295 | if filename is None: | 302 | if filename is None: |
296 | - filename = tempfile.mktemp(prefix='inv3_', suffix='.dat') | ||
297 | - matrix = numpy.memmap(filename, mode='w+', dtype=arr.dtype, shape=arr.shape) | 303 | + filename = tempfile.mktemp(prefix="inv3_", suffix=".dat") |
304 | + matrix = numpy.memmap(filename, mode="w+", dtype=arr.dtype, shape=arr.shape) | ||
298 | matrix[:] = arr[:] | 305 | matrix[:] = arr[:] |
299 | matrix.flush() | 306 | matrix.flush() |
300 | return matrix | 307 | return matrix |
@@ -307,34 +314,44 @@ def bitmap2memmap(files, slice_size, orientation, spacing, resolution_percentage | @@ -307,34 +314,44 @@ def bitmap2memmap(files, slice_size, orientation, spacing, resolution_percentage | ||
307 | """ | 314 | """ |
308 | message = _("Generating multiplanar visualization...") | 315 | message = _("Generating multiplanar visualization...") |
309 | if len(files) > 1: | 316 | if len(files) > 1: |
310 | - update_progress= vtk_utils.ShowProgress(len(files) - 1, dialog_type = "ProgressDialog") | 317 | + update_progress = vtk_utils.ShowProgress( |
318 | + len(files) - 1, dialog_type="ProgressDialog" | ||
319 | + ) | ||
311 | 320 | ||
312 | temp_file = tempfile.mktemp() | 321 | temp_file = tempfile.mktemp() |
313 | 322 | ||
314 | - if orientation == 'SAGITTAL': | 323 | + if orientation == "SAGITTAL": |
315 | if resolution_percentage == 1.0: | 324 | if resolution_percentage == 1.0: |
316 | shape = slice_size[1], slice_size[0], len(files) | 325 | shape = slice_size[1], slice_size[0], len(files) |
317 | else: | 326 | else: |
318 | - shape = math.ceil(slice_size[1]*resolution_percentage),\ | ||
319 | - math.ceil(slice_size[0]*resolution_percentage), len(files) | 327 | + shape = ( |
328 | + math.ceil(slice_size[1] * resolution_percentage), | ||
329 | + math.ceil(slice_size[0] * resolution_percentage), | ||
330 | + len(files), | ||
331 | + ) | ||
320 | 332 | ||
321 | - elif orientation == 'CORONAL': | 333 | + elif orientation == "CORONAL": |
322 | if resolution_percentage == 1.0: | 334 | if resolution_percentage == 1.0: |
323 | shape = slice_size[1], len(files), slice_size[0] | 335 | shape = slice_size[1], len(files), slice_size[0] |
324 | else: | 336 | else: |
325 | - shape = math.ceil(slice_size[1]*resolution_percentage), len(files),\ | ||
326 | - math.ceil(slice_size[0]*resolution_percentage) | 337 | + shape = ( |
338 | + math.ceil(slice_size[1] * resolution_percentage), | ||
339 | + len(files), | ||
340 | + math.ceil(slice_size[0] * resolution_percentage), | ||
341 | + ) | ||
327 | else: | 342 | else: |
328 | if resolution_percentage == 1.0: | 343 | if resolution_percentage == 1.0: |
329 | shape = len(files), slice_size[1], slice_size[0] | 344 | shape = len(files), slice_size[1], slice_size[0] |
330 | else: | 345 | else: |
331 | - shape = len(files), math.ceil(slice_size[1]*resolution_percentage),\ | ||
332 | - math.ceil(slice_size[0]*resolution_percentage) | ||
333 | - | 346 | + shape = ( |
347 | + len(files), | ||
348 | + math.ceil(slice_size[1] * resolution_percentage), | ||
349 | + math.ceil(slice_size[0] * resolution_percentage), | ||
350 | + ) | ||
334 | 351 | ||
335 | if resolution_percentage == 1.0: | 352 | if resolution_percentage == 1.0: |
336 | - matrix = numpy.memmap(temp_file, mode='w+', dtype='int16', shape=shape) | ||
337 | - | 353 | + matrix = numpy.memmap(temp_file, mode="w+", dtype="int16", shape=shape) |
354 | + | ||
338 | cont = 0 | 355 | cont = 0 |
339 | max_scalar = None | 356 | max_scalar = None |
340 | min_scalar = None | 357 | min_scalar = None |
@@ -347,21 +364,31 @@ def bitmap2memmap(files, slice_size, orientation, spacing, resolution_percentage | @@ -347,21 +364,31 @@ def bitmap2memmap(files, slice_size, orientation, spacing, resolution_percentage | ||
347 | 364 | ||
348 | print(image_as_array.dtype) | 365 | print(image_as_array.dtype) |
349 | 366 | ||
350 | - image = converters.to_vtk(image_as_array, spacing=spacing,\ | ||
351 | - slice_number=1, orientation=orientation.upper()) | 367 | + image = converters.to_vtk( |
368 | + image_as_array, | ||
369 | + spacing=spacing, | ||
370 | + slice_number=1, | ||
371 | + orientation=orientation.upper(), | ||
372 | + ) | ||
352 | 373 | ||
353 | if resolution_percentage != 1.0: | 374 | if resolution_percentage != 1.0: |
354 | - | ||
355 | - | ||
356 | - image_resized = ResampleImage2D(image, px=None, py=None,\ | ||
357 | - resolution_percentage = resolution_percentage, update_progress = None) | ||
358 | 375 | ||
359 | - yx_shape = image_resized.GetDimensions()[1], image_resized.GetDimensions()[0] | ||
360 | - | ||
361 | - | ||
362 | - if not(first_resample_entry): | ||
363 | - shape = shape[0], yx_shape[0], yx_shape[1] | ||
364 | - matrix = numpy.memmap(temp_file, mode='w+', dtype='int16', shape=shape) | 376 | + image_resized = ResampleImage2D( |
377 | + image, | ||
378 | + px=None, | ||
379 | + py=None, | ||
380 | + resolution_percentage=resolution_percentage, | ||
381 | + update_progress=None, | ||
382 | + ) | ||
383 | + | ||
384 | + yx_shape = ( | ||
385 | + image_resized.GetDimensions()[1], | ||
386 | + image_resized.GetDimensions()[0], | ||
387 | + ) | ||
388 | + | ||
389 | + if not (first_resample_entry): | ||
390 | + shape = shape[0], yx_shape[0], yx_shape[1] | ||
391 | + matrix = numpy.memmap(temp_file, mode="w+", dtype="int16", shape=shape) | ||
365 | first_resample_entry = True | 392 | first_resample_entry = True |
366 | 393 | ||
367 | image = image_resized | 394 | image = image_resized |
@@ -375,26 +402,26 @@ def bitmap2memmap(files, slice_size, orientation, spacing, resolution_percentage | @@ -375,26 +402,26 @@ def bitmap2memmap(files, slice_size, orientation, spacing, resolution_percentage | ||
375 | 402 | ||
376 | array = numpy_support.vtk_to_numpy(image.GetPointData().GetScalars()) | 403 | array = numpy_support.vtk_to_numpy(image.GetPointData().GetScalars()) |
377 | 404 | ||
378 | - if array.dtype == 'uint16': | 405 | + if array.dtype == "uint16": |
379 | new_array = np.empty_like(array, dtype=np.int16) | 406 | new_array = np.empty_like(array, dtype=np.int16) |
380 | new_array = array - 32768 | 407 | new_array = array - 32768 |
381 | array = new_array | 408 | array = new_array |
382 | 409 | ||
383 | - if orientation == 'CORONAL': | 410 | + if orientation == "CORONAL": |
384 | array.shape = matrix.shape[0], matrix.shape[2] | 411 | array.shape = matrix.shape[0], matrix.shape[2] |
385 | - matrix[:, n, :] = array[:,::-1] | ||
386 | - elif orientation == 'SAGITTAL': | 412 | + matrix[:, n, :] = array[:, ::-1] |
413 | + elif orientation == "SAGITTAL": | ||
387 | array.shape = matrix.shape[0], matrix.shape[1] | 414 | array.shape = matrix.shape[0], matrix.shape[1] |
388 | # TODO: Verify if it's necessary to add the slices swapped only in | 415 | # TODO: Verify if it's necessary to add the slices swapped only in |
389 | # sagittal rmi or only in # Rasiane's case or is necessary in all | 416 | # sagittal rmi or only in # Rasiane's case or is necessary in all |
390 | # sagittal cases. | 417 | # sagittal cases. |
391 | - matrix[:, :, n] = array[:,::-1] | 418 | + matrix[:, :, n] = array[:, ::-1] |
392 | else: | 419 | else: |
393 | array.shape = matrix.shape[1], matrix.shape[2] | 420 | array.shape = matrix.shape[1], matrix.shape[2] |
394 | matrix[n] = array | 421 | matrix[n] = array |
395 | - | 422 | + |
396 | if len(files) > 1: | 423 | if len(files) > 1: |
397 | - update_progress(cont,message) | 424 | + update_progress(cont, message) |
398 | cont += 1 | 425 | cont += 1 |
399 | 426 | ||
400 | matrix.flush() | 427 | matrix.flush() |
@@ -411,27 +438,29 @@ def dcm2memmap(files, slice_size, orientation, resolution_percentage): | @@ -411,27 +438,29 @@ def dcm2memmap(files, slice_size, orientation, resolution_percentage): | ||
411 | """ | 438 | """ |
412 | if len(files) > 1: | 439 | if len(files) > 1: |
413 | message = _("Generating multiplanar visualization...") | 440 | message = _("Generating multiplanar visualization...") |
414 | - update_progress= vtk_utils.ShowProgress(len(files) - 1, dialog_type = "ProgressDialog") | 441 | + update_progress = vtk_utils.ShowProgress( |
442 | + len(files) - 1, dialog_type="ProgressDialog" | ||
443 | + ) | ||
415 | 444 | ||
416 | first_slice = read_dcm_slice_as_np2(files[0], resolution_percentage) | 445 | first_slice = read_dcm_slice_as_np2(files[0], resolution_percentage) |
417 | slice_size = first_slice.shape[::-1] | 446 | slice_size = first_slice.shape[::-1] |
418 | 447 | ||
419 | temp_file = tempfile.mktemp() | 448 | temp_file = tempfile.mktemp() |
420 | 449 | ||
421 | - if orientation == 'SAGITTAL': | 450 | + if orientation == "SAGITTAL": |
422 | shape = slice_size[0], slice_size[1], len(files) | 451 | shape = slice_size[0], slice_size[1], len(files) |
423 | - elif orientation == 'CORONAL': | 452 | + elif orientation == "CORONAL": |
424 | shape = slice_size[1], len(files), slice_size[0] | 453 | shape = slice_size[1], len(files), slice_size[0] |
425 | else: | 454 | else: |
426 | shape = len(files), slice_size[1], slice_size[0] | 455 | shape = len(files), slice_size[1], slice_size[0] |
427 | 456 | ||
428 | - matrix = numpy.memmap(temp_file, mode='w+', dtype='int16', shape=shape) | 457 | + matrix = numpy.memmap(temp_file, mode="w+", dtype="int16", shape=shape) |
429 | for n, f in enumerate(files): | 458 | for n, f in enumerate(files): |
430 | im_array = read_dcm_slice_as_np2(f, resolution_percentage)[::-1] | 459 | im_array = read_dcm_slice_as_np2(f, resolution_percentage)[::-1] |
431 | 460 | ||
432 | - if orientation == 'CORONAL': | 461 | + if orientation == "CORONAL": |
433 | matrix[:, shape[1] - n - 1, :] = im_array | 462 | matrix[:, shape[1] - n - 1, :] = im_array |
434 | - elif orientation == 'SAGITTAL': | 463 | + elif orientation == "SAGITTAL": |
435 | # TODO: Verify if it's necessary to add the slices swapped only in | 464 | # TODO: Verify if it's necessary to add the slices swapped only in |
436 | # sagittal rmi or only in # Rasiane's case or is necessary in all | 465 | # sagittal rmi or only in # Rasiane's case or is necessary in all |
437 | # sagittal cases. | 466 | # sagittal cases. |
@@ -456,15 +485,15 @@ def dcmmf2memmap(dcm_file, orientation): | @@ -456,15 +485,15 @@ def dcmmf2memmap(dcm_file, orientation): | ||
456 | pf = image.GetPixelFormat() | 485 | pf = image.GetPixelFormat() |
457 | np_image = converters.gdcm_to_numpy(image, pf.GetSamplesPerPixel() == 1) | 486 | np_image = converters.gdcm_to_numpy(image, pf.GetSamplesPerPixel() == 1) |
458 | temp_file = tempfile.mktemp() | 487 | temp_file = tempfile.mktemp() |
459 | - matrix = numpy.memmap(temp_file, mode='w+', dtype='int16', shape=np_image.shape) | 488 | + matrix = numpy.memmap(temp_file, mode="w+", dtype="int16", shape=np_image.shape) |
460 | print("Number of dimensions", np_image.shape) | 489 | print("Number of dimensions", np_image.shape) |
461 | z, y, x = np_image.shape | 490 | z, y, x = np_image.shape |
462 | - if orientation == 'CORONAL': | 491 | + if orientation == "CORONAL": |
463 | spacing = xs, zs, ys | 492 | spacing = xs, zs, ys |
464 | matrix.shape = y, z, x | 493 | matrix.shape = y, z, x |
465 | for n in range(z): | 494 | for n in range(z): |
466 | matrix[:, n, :] = np_image[n][::-1] | 495 | matrix[:, n, :] = np_image[n][::-1] |
467 | - elif orientation == 'SAGITTAL': | 496 | + elif orientation == "SAGITTAL": |
468 | spacing = zs, ys, xs | 497 | spacing = zs, ys, xs |
469 | matrix.shape = y, x, z | 498 | matrix.shape = y, x, z |
470 | for n in range(z): | 499 | for n in range(z): |
@@ -495,7 +524,7 @@ def img2memmap(group): | @@ -495,7 +524,7 @@ def img2memmap(group): | ||
495 | data = numpy.swapaxes(data, 0, 2) | 524 | data = numpy.swapaxes(data, 0, 2) |
496 | data = numpy.fliplr(data) | 525 | data = numpy.fliplr(data) |
497 | 526 | ||
498 | - matrix = numpy.memmap(temp_file, mode='w+', dtype=np.int16, shape=data.shape) | 527 | + matrix = numpy.memmap(temp_file, mode="w+", dtype=np.int16, shape=data.shape) |
499 | matrix[:] = data[:] | 528 | matrix[:] = data[:] |
500 | matrix.flush() | 529 | matrix.flush() |
501 | 530 | ||
@@ -507,10 +536,14 @@ def img2memmap(group): | @@ -507,10 +536,14 @@ def img2memmap(group): | ||
507 | def get_LUT_value_255(data, window, level): | 536 | def get_LUT_value_255(data, window, level): |
508 | shape = data.shape | 537 | shape = data.shape |
509 | data_ = data.ravel() | 538 | data_ = data.ravel() |
510 | - data = np.piecewise(data_, | ||
511 | - [data_ <= (level - 0.5 - (window-1)/2), | ||
512 | - data_ > (level - 0.5 + (window-1)/2)], | ||
513 | - [0, 255, lambda data_: ((data_ - (level - 0.5))/(window-1) + 0.5)*(255)]) | 539 | + data = np.piecewise( |
540 | + data_, | ||
541 | + [ | ||
542 | + data_ <= (level - 0.5 - (window - 1) / 2), | ||
543 | + data_ > (level - 0.5 + (window - 1) / 2), | ||
544 | + ], | ||
545 | + [0, 255, lambda data_: ((data_ - (level - 0.5)) / (window - 1) + 0.5) * (255)], | ||
546 | + ) | ||
514 | data.shape = shape | 547 | data.shape = shape |
515 | return data | 548 | return data |
516 | 549 | ||
@@ -534,7 +567,9 @@ def world2invspace(affine=None): | @@ -534,7 +567,9 @@ def world2invspace(affine=None): | ||
534 | 567 | ||
535 | # remove scaling factor for non-unitary voxel dimensions | 568 | # remove scaling factor for non-unitary voxel dimensions |
536 | scale, shear, angs, trans, persp = tr.decompose_matrix(affine) | 569 | scale, shear, angs, trans, persp = tr.decompose_matrix(affine) |
537 | - affine_noscale = tr.compose_matrix(scale=None, shear=shear, angles=angs, translate=trans, perspective=persp) | 570 | + affine_noscale = tr.compose_matrix( |
571 | + scale=None, shear=shear, angles=angs, translate=trans, perspective=persp | ||
572 | + ) | ||
538 | # repos_img = [0.] * 6 | 573 | # repos_img = [0.] * 6 |
539 | # repos_img[1] = -float(shape[1]) | 574 | # repos_img[1] = -float(shape[1]) |
540 | # | 575 | # |
@@ -579,7 +614,7 @@ def convert_world_to_voxel(xyz, affine): | @@ -579,7 +614,7 @@ def convert_world_to_voxel(xyz, affine): | ||
579 | 614 | ||
580 | # print("xyz: ", xyz, "\naffine", affine) | 615 | # print("xyz: ", xyz, "\naffine", affine) |
581 | # convert xyz coordinate to 1x4 homogeneous coordinates array | 616 | # convert xyz coordinate to 1x4 homogeneous coordinates array |
582 | - xyz_homo = np.hstack((xyz, 1.)).reshape([4, 1]) | 617 | + xyz_homo = np.hstack((xyz, 1.0)).reshape([4, 1]) |
583 | ijk_homo = np.linalg.inv(affine) @ xyz_homo | 618 | ijk_homo = np.linalg.inv(affine) @ xyz_homo |
584 | ijk = ijk_homo.T[np.newaxis, 0, :3] | 619 | ijk = ijk_homo.T[np.newaxis, 0, :3] |
585 | 620 | ||
@@ -587,13 +622,13 @@ def convert_world_to_voxel(xyz, affine): | @@ -587,13 +622,13 @@ def convert_world_to_voxel(xyz, affine): | ||
587 | 622 | ||
588 | 623 | ||
589 | def create_grid(xy_range, z_range, z_offset, spacing): | 624 | def create_grid(xy_range, z_range, z_offset, spacing): |
590 | - x = np.arange(xy_range[0], xy_range[1]+1, spacing) | ||
591 | - y = np.arange(xy_range[0], xy_range[1]+1, spacing) | ||
592 | - z = z_offset + np.arange(z_range[0], z_range[1]+1, spacing) | 625 | + x = np.arange(xy_range[0], xy_range[1] + 1, spacing) |
626 | + y = np.arange(xy_range[0], xy_range[1] + 1, spacing) | ||
627 | + z = z_offset + np.arange(z_range[0], z_range[1] + 1, spacing) | ||
593 | xv, yv, zv = np.meshgrid(x, y, -z) | 628 | xv, yv, zv = np.meshgrid(x, y, -z) |
594 | coord_grid = np.array([xv, yv, zv]) | 629 | coord_grid = np.array([xv, yv, zv]) |
595 | # create grid of points | 630 | # create grid of points |
596 | - grid_number = x.shape[0]*y.shape[0]*z.shape[0] | 631 | + grid_number = x.shape[0] * y.shape[0] * z.shape[0] |
597 | coord_grid = coord_grid.reshape([3, grid_number]).T | 632 | coord_grid = coord_grid.reshape([3, grid_number]).T |
598 | # sort grid from distance to the origin/coil center | 633 | # sort grid from distance to the origin/coil center |
599 | coord_list = coord_grid[np.argsort(np.linalg.norm(coord_grid, axis=1)), :] | 634 | coord_list = coord_grid[np.argsort(np.linalg.norm(coord_grid, axis=1)), :] |
@@ -604,11 +639,11 @@ def create_grid(xy_range, z_range, z_offset, spacing): | @@ -604,11 +639,11 @@ def create_grid(xy_range, z_range, z_offset, spacing): | ||
604 | 639 | ||
605 | 640 | ||
606 | def create_spherical_grid(radius=10, subdivision=1): | 641 | def create_spherical_grid(radius=10, subdivision=1): |
607 | - x = np.linspace(-radius, radius, int(2*radius/subdivision)+1) | 642 | + x = np.linspace(-radius, radius, int(2 * radius / subdivision) + 1) |
608 | xv, yv, zv = np.meshgrid(x, x, x) | 643 | xv, yv, zv = np.meshgrid(x, x, x) |
609 | coord_grid = np.array([xv, yv, zv]) | 644 | coord_grid = np.array([xv, yv, zv]) |
610 | # create grid of points | 645 | # create grid of points |
611 | - grid_number = x.shape[0]**3 | 646 | + grid_number = x.shape[0] ** 3 |
612 | coord_grid = coord_grid.reshape([3, grid_number]).T | 647 | coord_grid = coord_grid.reshape([3, grid_number]).T |
613 | 648 | ||
614 | sph_grid = coord_grid[np.linalg.norm(coord_grid, axis=1) < radius, :] | 649 | sph_grid = coord_grid[np.linalg.norm(coord_grid, axis=1) < radius, :] |
invesalius/reader/bitmap_reader.py
1 | -#-------------------------------------------------------------------------- | 1 | +# -------------------------------------------------------------------------- |
2 | # Software: InVesalius - Software de Reconstrucao 3D de Imagens Medicas | 2 | # Software: InVesalius - Software de Reconstrucao 3D de Imagens Medicas |
3 | # Copyright: (C) 2001 Centro de Pesquisas Renato Archer | 3 | # Copyright: (C) 2001 Centro de Pesquisas Renato Archer |
4 | # Homepage: http://www.softwarepublico.gov.br | 4 | # Homepage: http://www.softwarepublico.gov.br |
5 | # Contact: invesalius@cti.gov.br | 5 | # Contact: invesalius@cti.gov.br |
6 | # License: GNU - GPL 2 (LICENSE.txt/LICENCA.txt) | 6 | # License: GNU - GPL 2 (LICENSE.txt/LICENCA.txt) |
7 | -#-------------------------------------------------------------------------- | 7 | +# -------------------------------------------------------------------------- |
8 | # Este programa e software livre; voce pode redistribui-lo e/ou | 8 | # Este programa e software livre; voce pode redistribui-lo e/ou |
9 | # modifica-lo sob os termos da Licenca Publica Geral GNU, conforme | 9 | # modifica-lo sob os termos da Licenca Publica Geral GNU, conforme |
10 | # publicada pela Free Software Foundation; de acordo com a versao 2 | 10 | # publicada pela Free Software Foundation; de acordo com a versao 2 |
@@ -15,56 +15,55 @@ | @@ -15,56 +15,55 @@ | ||
15 | # COMERCIALIZACAO ou de ADEQUACAO A QUALQUER PROPOSITO EM | 15 | # COMERCIALIZACAO ou de ADEQUACAO A QUALQUER PROPOSITO EM |
16 | # PARTICULAR. Consulte a Licenca Publica Geral GNU para obter mais | 16 | # PARTICULAR. Consulte a Licenca Publica Geral GNU para obter mais |
17 | # detalhes. | 17 | # detalhes. |
18 | -#-------------------------------------------------------------------------- | 18 | +# -------------------------------------------------------------------------- |
19 | +import imghdr | ||
19 | import os | 20 | import os |
20 | -import threading | ||
21 | -import tempfile | 21 | +import re |
22 | import sys | 22 | import sys |
23 | +import tempfile | ||
24 | +import threading | ||
25 | +from multiprocessing import cpu_count | ||
26 | + | ||
27 | +import numpy | ||
23 | import vtk | 28 | import vtk |
24 | -import re | ||
25 | -import invesalius.constants as const | ||
26 | import wx | 29 | import wx |
27 | - | 30 | +from imageio import imread |
28 | from pubsub import pub as Publisher | 31 | from pubsub import pub as Publisher |
29 | -from multiprocessing import cpu_count | ||
30 | - | ||
31 | from vtk.util import numpy_support | 32 | from vtk.util import numpy_support |
32 | -from imageio import imread | ||
33 | -import numpy | ||
34 | -import imghdr | ||
35 | 33 | ||
36 | -import invesalius.utils as utils | 34 | +import invesalius.constants as const |
37 | import invesalius.data.converters as converters | 35 | import invesalius.data.converters as converters |
36 | +import invesalius.utils as utils | ||
38 | from invesalius import inv_paths | 37 | from invesalius import inv_paths |
39 | 38 | ||
40 | - | ||
41 | -#flag to control vtk error in read files | ||
42 | -no_error = True | 39 | +# flag to control vtk error in read files |
40 | +no_error = True | ||
43 | vtk_error = False | 41 | vtk_error = False |
44 | 42 | ||
45 | -if sys.platform == 'win32': | 43 | +if sys.platform == "win32": |
46 | try: | 44 | try: |
47 | import win32api | 45 | import win32api |
46 | + | ||
48 | _has_win32api = True | 47 | _has_win32api = True |
49 | except ImportError: | 48 | except ImportError: |
50 | _has_win32api = False | 49 | _has_win32api = False |
51 | else: | 50 | else: |
52 | _has_win32api = False | 51 | _has_win32api = False |
53 | 52 | ||
54 | -class Singleton: | ||
55 | 53 | ||
56 | - def __init__(self,klass): | 54 | +class Singleton: |
55 | + def __init__(self, klass): | ||
57 | self.klass = klass | 56 | self.klass = klass |
58 | self.instance = None | 57 | self.instance = None |
59 | - | ||
60 | - def __call__(self,*args,**kwds): | 58 | + |
59 | + def __call__(self, *args, **kwds): | ||
61 | if self.instance == None: | 60 | if self.instance == None: |
62 | - self.instance = self.klass(*args,**kwds) | 61 | + self.instance = self.klass(*args, **kwds) |
63 | return self.instance | 62 | return self.instance |
64 | 63 | ||
64 | + | ||
65 | @Singleton | 65 | @Singleton |
66 | class BitmapData: | 66 | class BitmapData: |
67 | - | ||
68 | def __init__(self): | 67 | def __init__(self): |
69 | self.data = None | 68 | self.data = None |
70 | 69 | ||
@@ -86,7 +85,7 @@ class BitmapData: | @@ -86,7 +85,7 @@ class BitmapData: | ||
86 | 85 | ||
87 | k = {} | 86 | k = {} |
88 | for v in sizes: | 87 | for v in sizes: |
89 | - k[v] = '' | 88 | + k[v] = "" |
90 | 89 | ||
91 | if len(k.keys()) > 1: | 90 | if len(k.keys()) > 1: |
92 | return False | 91 | return False |
@@ -94,10 +93,10 @@ class BitmapData: | @@ -94,10 +93,10 @@ class BitmapData: | ||
94 | return True | 93 | return True |
95 | 94 | ||
96 | def GetFirstPixelSize(self): | 95 | def GetFirstPixelSize(self): |
97 | - | ||
98 | - path = self.data[0][0] | 96 | + |
97 | + path = self.data[0][0] | ||
99 | size = ReadBitmap(path).dtype.itemsize * 8 | 98 | size = ReadBitmap(path).dtype.itemsize * 8 |
100 | - | 99 | + |
101 | return size | 100 | return size |
102 | 101 | ||
103 | def RemoveFileByPath(self, path): | 102 | def RemoveFileByPath(self, path): |
@@ -110,8 +109,8 @@ class BitmapData: | @@ -110,8 +109,8 @@ class BitmapData: | ||
110 | if path.encode(const.FS_ENCODE) in v: | 109 | if path.encode(const.FS_ENCODE) in v: |
111 | return i | 110 | return i |
112 | 111 | ||
113 | -class BitmapFiles: | ||
114 | 112 | ||
113 | +class BitmapFiles: | ||
115 | def __init__(self): | 114 | def __init__(self): |
116 | self.bitmapfiles = [] | 115 | self.bitmapfiles = [] |
117 | 116 | ||
@@ -119,7 +118,7 @@ class BitmapFiles: | @@ -119,7 +118,7 @@ class BitmapFiles: | ||
119 | self.bitmapfiles.append(bmp) | 118 | self.bitmapfiles.append(bmp) |
120 | 119 | ||
121 | def Sort(self, x): | 120 | def Sort(self, x): |
122 | - c_re = re.compile('\d+') | 121 | + c_re = re.compile("\d+") |
123 | if len(c_re.findall(x[6])) > 0: | 122 | if len(c_re.findall(x[6])) > 0: |
124 | return [int(i) for i in c_re.findall(x[6])] | 123 | return [int(i) for i in c_re.findall(x[6])] |
125 | else: | 124 | else: |
@@ -127,37 +126,37 @@ class BitmapFiles: | @@ -127,37 +126,37 @@ class BitmapFiles: | ||
127 | 126 | ||
128 | def GetValues(self): | 127 | def GetValues(self): |
129 | bmpfile = self.bitmapfiles | 128 | bmpfile = self.bitmapfiles |
130 | - bmpfile.sort(key = self.Sort) | 129 | + bmpfile.sort(key=self.Sort) |
131 | 130 | ||
132 | bmp_data = BitmapData() | 131 | bmp_data = BitmapData() |
133 | bmp_data.data = bmpfile | 132 | bmp_data.data = bmpfile |
134 | 133 | ||
135 | return bmpfile | 134 | return bmpfile |
136 | 135 | ||
137 | -class LoadBitmap: | ||
138 | 136 | ||
137 | +class LoadBitmap: | ||
139 | def __init__(self, bmp_file, filepath): | 138 | def __init__(self, bmp_file, filepath): |
140 | self.bmp_file = bmp_file | 139 | self.bmp_file = bmp_file |
141 | self.filepath = utils.decode(filepath, const.FS_ENCODE) | 140 | self.filepath = utils.decode(filepath, const.FS_ENCODE) |
142 | - | 141 | + |
143 | self.run() | 142 | self.run() |
144 | - | 143 | + |
145 | def run(self): | 144 | def run(self): |
146 | global vtk_error | 145 | global vtk_error |
147 | 146 | ||
148 | - #----- verify extension ------------------ | 147 | + # ----- verify extension ------------------ |
149 | extension = VerifyDataType(self.filepath) | 148 | extension = VerifyDataType(self.filepath) |
150 | 149 | ||
151 | file_name = self.filepath.split(os.path.sep)[-1] | 150 | file_name = self.filepath.split(os.path.sep)[-1] |
152 | 151 | ||
153 | n_array = ReadBitmap(self.filepath) | 152 | n_array = ReadBitmap(self.filepath) |
154 | - | ||
155 | - if not(isinstance(n_array, numpy.ndarray)): | 153 | + |
154 | + if not (isinstance(n_array, numpy.ndarray)): | ||
156 | return False | 155 | return False |
157 | - | ||
158 | - image = converters.to_vtk(n_array, spacing=(1,1,1),\ | ||
159 | - slice_number=1, orientation="AXIAL") | ||
160 | 156 | ||
157 | + image = converters.to_vtk( | ||
158 | + n_array, spacing=(1, 1, 1), slice_number=1, orientation="AXIAL" | ||
159 | + ) | ||
161 | 160 | ||
162 | dim = image.GetDimensions() | 161 | dim = image.GetDimensions() |
163 | x = dim[0] | 162 | x = dim[0] |
@@ -165,16 +164,16 @@ class LoadBitmap: | @@ -165,16 +164,16 @@ class LoadBitmap: | ||
165 | 164 | ||
166 | img = vtk.vtkImageResample() | 165 | img = vtk.vtkImageResample() |
167 | img.SetInputData(image) | 166 | img.SetInputData(image) |
168 | - img.SetAxisMagnificationFactor ( 0, 0.25 ) | ||
169 | - img.SetAxisMagnificationFactor ( 1, 0.25 ) | ||
170 | - img.SetAxisMagnificationFactor ( 2, 1 ) | 167 | + img.SetAxisMagnificationFactor(0, 0.25) |
168 | + img.SetAxisMagnificationFactor(1, 0.25) | ||
169 | + img.SetAxisMagnificationFactor(2, 1) | ||
171 | img.Update() | 170 | img.Update() |
172 | 171 | ||
173 | tp = img.GetOutput().GetScalarTypeAsString() | 172 | tp = img.GetOutput().GetScalarTypeAsString() |
174 | 173 | ||
175 | image_copy = vtk.vtkImageData() | 174 | image_copy = vtk.vtkImageData() |
176 | image_copy.DeepCopy(img.GetOutput()) | 175 | image_copy.DeepCopy(img.GetOutput()) |
177 | - | 176 | + |
178 | thumbnail_path = tempfile.mktemp() | 177 | thumbnail_path = tempfile.mktemp() |
179 | 178 | ||
180 | write_png = vtk.vtkPNGWriter() | 179 | write_png = vtk.vtkPNGWriter() |
@@ -187,20 +186,28 @@ class LoadBitmap: | @@ -187,20 +186,28 @@ class LoadBitmap: | ||
187 | img = vtk.vtkImageCast() | 186 | img = vtk.vtkImageCast() |
188 | img.SetInputData(image_copy) | 187 | img.SetInputData(image_copy) |
189 | img.SetOutputScalarTypeToUnsignedShort() | 188 | img.SetOutputScalarTypeToUnsignedShort() |
190 | - #img.SetClampOverflow(1) | 189 | + # img.SetClampOverflow(1) |
191 | img.Update() | 190 | img.Update() |
192 | 191 | ||
193 | write_png = vtk.vtkPNGWriter() | 192 | write_png = vtk.vtkPNGWriter() |
194 | write_png.SetInputConnection(img.GetOutputPort()) | 193 | write_png.SetInputConnection(img.GetOutputPort()) |
195 | write_png.SetFileName(thumbnail_path) | 194 | write_png.SetFileName(thumbnail_path) |
196 | write_png.Write() | 195 | write_png.Write() |
197 | - | 196 | + |
198 | vtk_error = False | 197 | vtk_error = False |
199 | 198 | ||
200 | id = wx.NewId() | 199 | id = wx.NewId() |
201 | 200 | ||
202 | - bmp_item = [self.filepath, thumbnail_path, extension, x, y,\ | ||
203 | - str(x) + ' x ' + str(y), file_name, id] | 201 | + bmp_item = [ |
202 | + self.filepath, | ||
203 | + thumbnail_path, | ||
204 | + extension, | ||
205 | + x, | ||
206 | + y, | ||
207 | + str(x) + " x " + str(y), | ||
208 | + file_name, | ||
209 | + id, | ||
210 | + ] | ||
204 | self.bmp_file.Add(bmp_item) | 211 | self.bmp_file.Add(bmp_item) |
205 | 212 | ||
206 | 213 | ||
@@ -217,7 +224,6 @@ def yGetBitmaps(directory, recursive=True, gui=True): | @@ -217,7 +224,6 @@ def yGetBitmaps(directory, recursive=True, gui=True): | ||
217 | dirpath, dirnames, filenames = os.walk(directory) | 224 | dirpath, dirnames, filenames = os.walk(directory) |
218 | nfiles = len(filenames) | 225 | nfiles = len(filenames) |
219 | 226 | ||
220 | - | ||
221 | counter = 0 | 227 | counter = 0 |
222 | bmp_file = BitmapFiles() | 228 | bmp_file = BitmapFiles() |
223 | 229 | ||
@@ -228,7 +234,7 @@ def yGetBitmaps(directory, recursive=True, gui=True): | @@ -228,7 +234,7 @@ def yGetBitmaps(directory, recursive=True, gui=True): | ||
228 | filepath = os.path.join(dirpath, name).encode(const.FS_ENCODE) | 234 | filepath = os.path.join(dirpath, name).encode(const.FS_ENCODE) |
229 | counter += 1 | 235 | counter += 1 |
230 | if gui: | 236 | if gui: |
231 | - yield (counter,nfiles) | 237 | + yield (counter, nfiles) |
232 | LoadBitmap(bmp_file, filepath) | 238 | LoadBitmap(bmp_file, filepath) |
233 | else: | 239 | else: |
234 | dirpath, dirnames, filenames = os.walk(directory) | 240 | dirpath, dirnames, filenames = os.walk(directory) |
@@ -236,7 +242,7 @@ def yGetBitmaps(directory, recursive=True, gui=True): | @@ -236,7 +242,7 @@ def yGetBitmaps(directory, recursive=True, gui=True): | ||
236 | filepath = str(os.path.join(dirpath, name)).encode(const.FS_ENCODE) | 242 | filepath = str(os.path.join(dirpath, name)).encode(const.FS_ENCODE) |
237 | counter += 1 | 243 | counter += 1 |
238 | if gui: | 244 | if gui: |
239 | - yield (counter,nfiles) | 245 | + yield (counter, nfiles) |
240 | 246 | ||
241 | yield bmp_file.GetValues() | 247 | yield bmp_file.GetValues() |
242 | 248 | ||
@@ -252,12 +258,12 @@ class ProgressBitmapReader: | @@ -252,12 +258,12 @@ class ProgressBitmapReader: | ||
252 | def SetWindowEvent(self, frame): | 258 | def SetWindowEvent(self, frame): |
253 | self.frame = frame | 259 | self.frame = frame |
254 | 260 | ||
255 | - def SetDirectoryPath(self, path,recursive=True): | 261 | + def SetDirectoryPath(self, path, recursive=True): |
256 | self.running = True | 262 | self.running = True |
257 | self.stoped = False | 263 | self.stoped = False |
258 | - self.GetBitmaps(path,recursive) | 264 | + self.GetBitmaps(path, recursive) |
259 | 265 | ||
260 | - def UpdateLoadFileProgress(self,cont_progress): | 266 | + def UpdateLoadFileProgress(self, cont_progress): |
261 | Publisher.sendMessage("Update bitmap load", data=cont_progress) | 267 | Publisher.sendMessage("Update bitmap load", data=cont_progress) |
262 | 268 | ||
263 | def EndLoadFile(self, bitmap_list): | 269 | def EndLoadFile(self, bitmap_list): |
@@ -279,29 +285,31 @@ class ProgressBitmapReader: | @@ -279,29 +285,31 @@ class ProgressBitmapReader: | ||
279 | self.UpdateLoadFileProgress(None) | 285 | self.UpdateLoadFileProgress(None) |
280 | self.stoped = False | 286 | self.stoped = False |
281 | 287 | ||
288 | + | ||
282 | def VtkErrorPNGWriter(obj, f): | 289 | def VtkErrorPNGWriter(obj, f): |
283 | global vtk_error | 290 | global vtk_error |
284 | vtk_error = True | 291 | vtk_error = True |
285 | 292 | ||
293 | + | ||
286 | def ScipyRead(filepath): | 294 | def ScipyRead(filepath): |
287 | try: | 295 | try: |
288 | r = imread(filepath, flatten=True) | 296 | r = imread(filepath, flatten=True) |
289 | - dt = r.dtype | ||
290 | - if dt == "float" or dt == "float16"\ | ||
291 | - or dt == "float32" or dt == "float64": | ||
292 | - shift=-r.max()/2 | ||
293 | - simage = numpy.zeros_like(r, dtype='int16') | ||
294 | - simage[:] = r.astype('int32') + shift | 297 | + dt = r.dtype |
298 | + if dt == "float" or dt == "float16" or dt == "float32" or dt == "float64": | ||
299 | + shift = -r.max() / 2 | ||
300 | + simage = numpy.zeros_like(r, dtype="int16") | ||
301 | + simage[:] = r.astype("int32") + shift | ||
295 | 302 | ||
296 | return simage | 303 | return simage |
297 | else: | 304 | else: |
298 | return r | 305 | return r |
299 | - except(IOError): | 306 | + except (IOError): |
300 | return False | 307 | return False |
301 | 308 | ||
309 | + | ||
302 | def VtkRead(filepath, t): | 310 | def VtkRead(filepath, t): |
303 | if not const.VTK_WARNING: | 311 | if not const.VTK_WARNING: |
304 | - log_path = os.path.join(inv_paths.USER_LOG_DIR, 'vtkoutput.txt') | 312 | + log_path = os.path.join(inv_paths.USER_LOG_DIR, "vtkoutput.txt") |
305 | fow = vtk.vtkFileOutputWindow() | 313 | fow = vtk.vtkFileOutputWindow() |
306 | fow.SetFileName(log_path.encode(const.FS_ENCODE)) | 314 | fow.SetFileName(log_path.encode(const.FS_ENCODE)) |
307 | ow = vtk.vtkOutputWindow() | 315 | ow = vtk.vtkOutputWindow() |
@@ -317,7 +325,7 @@ def VtkRead(filepath, t): | @@ -317,7 +325,7 @@ def VtkRead(filepath, t): | ||
317 | 325 | ||
318 | elif t == "png": | 326 | elif t == "png": |
319 | reader = vtk.vtkPNGReader() | 327 | reader = vtk.vtkPNGReader() |
320 | - | 328 | + |
321 | elif t == "jpeg" or t == "jpg": | 329 | elif t == "jpeg" or t == "jpg": |
322 | reader = vtk.vtkJPEGReader() | 330 | reader = vtk.vtkJPEGReader() |
323 | 331 | ||
@@ -327,11 +335,11 @@ def VtkRead(filepath, t): | @@ -327,11 +335,11 @@ def VtkRead(filepath, t): | ||
327 | reader.AddObserver("ErrorEvent", VtkErrorToPy) | 335 | reader.AddObserver("ErrorEvent", VtkErrorToPy) |
328 | reader.SetFileName(filepath) | 336 | reader.SetFileName(filepath) |
329 | reader.Update() | 337 | reader.Update() |
330 | - | 338 | + |
331 | if no_error: | 339 | if no_error: |
332 | image = reader.GetOutput() | 340 | image = reader.GetOutput() |
333 | dim = image.GetDimensions() | 341 | dim = image.GetDimensions() |
334 | - | 342 | + |
335 | if reader.GetNumberOfScalarComponents() > 1: | 343 | if reader.GetNumberOfScalarComponents() > 1: |
336 | luminanceFilter = vtk.vtkImageLuminance() | 344 | luminanceFilter = vtk.vtkImageLuminance() |
337 | luminanceFilter.SetInputData(image) | 345 | luminanceFilter.SetInputData(image) |
@@ -349,7 +357,7 @@ def VtkRead(filepath, t): | @@ -349,7 +357,7 @@ def VtkRead(filepath, t): | ||
349 | return False | 357 | return False |
350 | 358 | ||
351 | 359 | ||
352 | -def ReadBitmap(filepath): | 360 | +def ReadBitmap(filepath): |
353 | t = VerifyDataType(filepath) | 361 | t = VerifyDataType(filepath) |
354 | 362 | ||
355 | if _has_win32api: | 363 | if _has_win32api: |
@@ -361,65 +369,65 @@ def ReadBitmap(filepath): | @@ -361,65 +369,65 @@ def ReadBitmap(filepath): | ||
361 | except UnicodeDecodeError: | 369 | except UnicodeDecodeError: |
362 | measures_info = False | 370 | measures_info = False |
363 | if measures_info: | 371 | if measures_info: |
364 | - Publisher.sendMessage('Set bitmap spacing', spacing=measures_info) | 372 | + Publisher.sendMessage("Set bitmap spacing", spacing=measures_info) |
365 | 373 | ||
366 | return False | 374 | return False |
367 | 375 | ||
368 | img_array = VtkRead(filepath, t) | 376 | img_array = VtkRead(filepath, t) |
369 | - | ||
370 | - if not(isinstance(img_array, numpy.ndarray)): | ||
371 | - | 377 | + |
378 | + if not (isinstance(img_array, numpy.ndarray)): | ||
379 | + | ||
372 | no_error = True | 380 | no_error = True |
373 | - | 381 | + |
374 | img_array = ScipyRead(filepath) | 382 | img_array = ScipyRead(filepath) |
375 | - | ||
376 | - if not(isinstance(img_array, numpy.ndarray)): | 383 | + |
384 | + if not (isinstance(img_array, numpy.ndarray)): | ||
377 | return False | 385 | return False |
378 | 386 | ||
379 | return img_array | 387 | return img_array |
380 | - | 388 | + |
381 | 389 | ||
382 | def GetPixelSpacingFromInfoFile(filepath): | 390 | def GetPixelSpacingFromInfoFile(filepath): |
383 | filepath = utils.decode(filepath, const.FS_ENCODE) | 391 | filepath = utils.decode(filepath, const.FS_ENCODE) |
384 | - if filepath.endswith('.DS_Store'): | 392 | + if filepath.endswith(".DS_Store"): |
385 | return False | 393 | return False |
386 | - fi = open(filepath, 'r') | 394 | + fi = open(filepath, "r") |
387 | lines = fi.readlines() | 395 | lines = fi.readlines() |
388 | - measure_scale = 'mm' | 396 | + measure_scale = "mm" |
389 | values = [] | 397 | values = [] |
390 | 398 | ||
391 | if len(lines) > 0: | 399 | if len(lines) > 0: |
392 | - #info text from avizo | ||
393 | - if '# Avizo Stacked Slices' in lines[0]: | ||
394 | - value = lines[2].split(' ') | 400 | + # info text from avizo |
401 | + if "# Avizo Stacked Slices" in lines[0]: | ||
402 | + value = lines[2].split(" ") | ||
395 | spx = float(value[1]) | 403 | spx = float(value[1]) |
396 | spy = float(value[2]) | 404 | spy = float(value[2]) |
397 | - value = lines[5].split(' ') | 405 | + value = lines[5].split(" ") |
398 | spz = float(value[1]) | 406 | spz = float(value[1]) |
399 | 407 | ||
400 | return [spx * 0.001, spy * 0.001, spz * 0.001] | 408 | return [spx * 0.001, spy * 0.001, spz * 0.001] |
401 | else: | 409 | else: |
402 | - #info text from skyscan | 410 | + # info text from skyscan |
403 | for l in lines: | 411 | for l in lines: |
404 | - if 'Pixel Size' in l: | ||
405 | - if 'um' in l: | ||
406 | - measure_scale = 'um' | ||
407 | - | 412 | + if "Pixel Size" in l: |
413 | + if "um" in l: | ||
414 | + measure_scale = "um" | ||
415 | + | ||
408 | value = l.split("=")[-1] | 416 | value = l.split("=")[-1] |
409 | values.append(value) | 417 | values.append(value) |
410 | 418 | ||
411 | if len(values) > 0: | 419 | if len(values) > 0: |
412 | value = values[-1] | 420 | value = values[-1] |
413 | - | ||
414 | - value = value.replace('\n','') | ||
415 | - value = value.replace('\r','') | ||
416 | 421 | ||
417 | - #convert um to mm (InVesalius default) | ||
418 | - if measure_scale == 'um': | 422 | + value = value.replace("\n", "") |
423 | + value = value.replace("\r", "") | ||
424 | + | ||
425 | + # convert um to mm (InVesalius default) | ||
426 | + if measure_scale == "um": | ||
419 | value = float(value) * 0.001 | 427 | value = float(value) * 0.001 |
420 | - measure_scale = 'mm' | 428 | + measure_scale = "mm" |
421 | 429 | ||
422 | - elif measure_scale == 'nm': | 430 | + elif measure_scale == "nm": |
423 | value = float(value) * 0.000001 | 431 | value = float(value) * 0.000001 |
424 | 432 | ||
425 | return [value, value, value] | 433 | return [value, value, value] |
@@ -428,6 +436,7 @@ def GetPixelSpacingFromInfoFile(filepath): | @@ -428,6 +436,7 @@ def GetPixelSpacingFromInfoFile(filepath): | ||
428 | else: | 436 | else: |
429 | return False | 437 | return False |
430 | 438 | ||
439 | + | ||
431 | def VtkErrorToPy(obj, evt): | 440 | def VtkErrorToPy(obj, evt): |
432 | global no_error | 441 | global no_error |
433 | no_error = False | 442 | no_error = False |
@@ -442,4 +451,3 @@ def VerifyDataType(filepath): | @@ -442,4 +451,3 @@ def VerifyDataType(filepath): | ||
442 | return False | 451 | return False |
443 | except IOError: | 452 | except IOError: |
444 | return False | 453 | return False |
445 | - |