Commit 7f9956549b19837f11f10b899f07b8ae2bd9b6df
1 parent
32eb467e
Exists in
master
Using black and isort in some python files
Showing
3 changed files
with
413 additions
and
415 deletions
Show diff stats
invesalius/data/converters.py
1 | -#-------------------------------------------------------------------------- | |
1 | +# -------------------------------------------------------------------------- | |
2 | 2 | # Software: InVesalius - Software de Reconstrucao 3D de Imagens Medicas |
3 | 3 | # Copyright: (C) 2001 Centro de Pesquisas Renato Archer |
4 | 4 | # Homepage: http://www.softwarepublico.gov.br |
5 | 5 | # Contact: invesalius@cti.gov.br |
6 | 6 | # License: GNU - GPL 2 (LICENSE.txt/LICENCA.txt) |
7 | -#-------------------------------------------------------------------------- | |
7 | +# -------------------------------------------------------------------------- | |
8 | 8 | # Este programa e software livre; voce pode redistribui-lo e/ou |
9 | 9 | # modifica-lo sob os termos da Licenca Publica Geral GNU, conforme |
10 | 10 | # publicada pela Free Software Foundation; de acordo com a versao 2 |
... | ... | @@ -15,16 +15,22 @@ |
15 | 15 | # COMERCIALIZACAO ou de ADEQUACAO A QUALQUER PROPOSITO EM |
16 | 16 | # PARTICULAR. Consulte a Licenca Publica Geral GNU para obter mais |
17 | 17 | # detalhes. |
18 | -#-------------------------------------------------------------------------- | |
18 | +# -------------------------------------------------------------------------- | |
19 | 19 | |
20 | 20 | import gdcm |
21 | 21 | import numpy as np |
22 | 22 | import vtk |
23 | - | |
24 | 23 | from vtk.util import numpy_support |
25 | 24 | |
26 | 25 | |
27 | -def to_vtk(n_array, spacing=(1.0, 1.0, 1.0), slice_number=0, orientation='AXIAL', origin=(0, 0, 0), padding=(0, 0, 0)): | |
26 | +def to_vtk( | |
27 | + n_array, | |
28 | + spacing=(1.0, 1.0, 1.0), | |
29 | + slice_number=0, | |
30 | + orientation="AXIAL", | |
31 | + origin=(0, 0, 0), | |
32 | + padding=(0, 0, 0), | |
33 | +): | |
28 | 34 | if orientation == "SAGITTAL": |
29 | 35 | orientation = "SAGITAL" |
30 | 36 | |
... | ... | @@ -38,14 +44,35 @@ def to_vtk(n_array, spacing=(1.0, 1.0, 1.0), slice_number=0, orientation='AXIAL' |
38 | 44 | |
39 | 45 | v_image = numpy_support.numpy_to_vtk(n_array.flat) |
40 | 46 | |
41 | - if orientation == 'AXIAL': | |
42 | - extent = (0 - px , dx -1 - px, 0 - py, dy - 1 - py, slice_number - pz, slice_number + dz - 1 - pz) | |
43 | - elif orientation == 'SAGITAL': | |
47 | + if orientation == "AXIAL": | |
48 | + extent = ( | |
49 | + 0 - px, | |
50 | + dx - 1 - px, | |
51 | + 0 - py, | |
52 | + dy - 1 - py, | |
53 | + slice_number - pz, | |
54 | + slice_number + dz - 1 - pz, | |
55 | + ) | |
56 | + elif orientation == "SAGITAL": | |
44 | 57 | dx, dy, dz = dz, dx, dy |
45 | - extent = (slice_number - px, slice_number + dx - 1 - px, 0 - py, dy - 1 - py, 0 - pz, dz - 1 - pz) | |
46 | - elif orientation == 'CORONAL': | |
58 | + extent = ( | |
59 | + slice_number - px, | |
60 | + slice_number + dx - 1 - px, | |
61 | + 0 - py, | |
62 | + dy - 1 - py, | |
63 | + 0 - pz, | |
64 | + dz - 1 - pz, | |
65 | + ) | |
66 | + elif orientation == "CORONAL": | |
47 | 67 | dx, dy, dz = dx, dz, dy |
48 | - extent = (0 - px, dx - 1 - px, slice_number - py, slice_number + dy - 1 - py, 0 - pz, dz - 1 - pz) | |
68 | + extent = ( | |
69 | + 0 - px, | |
70 | + dx - 1 - px, | |
71 | + slice_number - py, | |
72 | + slice_number + dy - 1 - py, | |
73 | + 0 - pz, | |
74 | + dz - 1 - pz, | |
75 | + ) | |
49 | 76 | |
50 | 77 | # Generating the vtkImageData |
51 | 78 | image = vtk.vtkImageData() |
... | ... | @@ -76,7 +103,7 @@ def to_vtk_mask(n_array, spacing=(1.0, 1.0, 1.0), origin=(0.0, 0.0, 0.0)): |
76 | 103 | oz -= sz |
77 | 104 | |
78 | 105 | v_image = numpy_support.numpy_to_vtk(n_array.flat) |
79 | - extent = (0, dx - 1, 0, dy - 1, 0, dz - 1) | |
106 | + extent = (0, dx - 1, 0, dy - 1, 0, dz - 1) | |
80 | 107 | |
81 | 108 | # Generating the vtkImageData |
82 | 109 | image = vtk.vtkImageData() |
... | ... | @@ -99,9 +126,9 @@ def to_vtk_mask(n_array, spacing=(1.0, 1.0, 1.0), origin=(0.0, 0.0, 0.0)): |
99 | 126 | |
100 | 127 | def np_rgba_to_vtk(n_array, spacing=(1.0, 1.0, 1.0)): |
101 | 128 | dy, dx, dc = n_array.shape |
102 | - v_image = numpy_support.numpy_to_vtk(n_array.reshape(dy*dx, dc)) | |
129 | + v_image = numpy_support.numpy_to_vtk(n_array.reshape(dy * dx, dc)) | |
103 | 130 | |
104 | - extent = (0, dx -1, 0, dy -1, 0, 0) | |
131 | + extent = (0, dx - 1, 0, dy - 1, 0, 0) | |
105 | 132 | |
106 | 133 | # Generating the vtkImageData |
107 | 134 | image = vtk.vtkImageData() |
... | ... | @@ -122,27 +149,34 @@ def np_rgba_to_vtk(n_array, spacing=(1.0, 1.0, 1.0)): |
122 | 149 | # Based on http://gdcm.sourceforge.net/html/ConvertNumpy_8py-example.html |
123 | 150 | def gdcm_to_numpy(image, apply_intercep_scale=True): |
124 | 151 | map_gdcm_np = { |
125 | - gdcm.PixelFormat.UINT8 :np.uint8, | |
126 | - gdcm.PixelFormat.INT8 :np.int8, | |
127 | - gdcm.PixelFormat.UINT12 :np.uint16, | |
128 | - gdcm.PixelFormat.INT12 :np.int16, | |
129 | - gdcm.PixelFormat.UINT16 :np.uint16, | |
130 | - gdcm.PixelFormat.INT16 :np.int16, | |
131 | - gdcm.PixelFormat.UINT32 :np.uint32, | |
132 | - gdcm.PixelFormat.INT32 :np.int32, | |
133 | - #gdcm.PixelFormat.FLOAT16:np.float16, | |
134 | - gdcm.PixelFormat.FLOAT32:np.float32, | |
135 | - gdcm.PixelFormat.FLOAT64:np.float64, | |
152 | + gdcm.PixelFormat.UINT8: np.uint8, | |
153 | + gdcm.PixelFormat.INT8: np.int8, | |
154 | + gdcm.PixelFormat.UINT12: np.uint16, | |
155 | + gdcm.PixelFormat.INT12: np.int16, | |
156 | + gdcm.PixelFormat.UINT16: np.uint16, | |
157 | + gdcm.PixelFormat.INT16: np.int16, | |
158 | + gdcm.PixelFormat.UINT32: np.uint32, | |
159 | + gdcm.PixelFormat.INT32: np.int32, | |
160 | + # gdcm.PixelFormat.FLOAT16:np.float16, | |
161 | + gdcm.PixelFormat.FLOAT32: np.float32, | |
162 | + gdcm.PixelFormat.FLOAT64: np.float64, | |
136 | 163 | } |
137 | 164 | |
138 | 165 | pf = image.GetPixelFormat() |
139 | 166 | if image.GetNumberOfDimensions() == 3: |
140 | - shape = image.GetDimension(2), image.GetDimension(1), image.GetDimension(0), pf.GetSamplesPerPixel() | |
167 | + shape = ( | |
168 | + image.GetDimension(2), | |
169 | + image.GetDimension(1), | |
170 | + image.GetDimension(0), | |
171 | + pf.GetSamplesPerPixel(), | |
172 | + ) | |
141 | 173 | else: |
142 | 174 | shape = image.GetDimension(1), image.GetDimension(0), pf.GetSamplesPerPixel() |
143 | 175 | dtype = map_gdcm_np[pf.GetScalarType()] |
144 | 176 | gdcm_array = image.GetBuffer() |
145 | - np_array = np.frombuffer(gdcm_array.encode('utf-8', errors="surrogateescape"), dtype=dtype) | |
177 | + np_array = np.frombuffer( | |
178 | + gdcm_array.encode("utf-8", errors="surrogateescape"), dtype=dtype | |
179 | + ) | |
146 | 180 | np_array.shape = shape |
147 | 181 | np_array = np_array.squeeze() |
148 | 182 | ... | ... |
invesalius/reader/dicom.py
1 | -#--------------------------------------------------------------------- | |
1 | +# --------------------------------------------------------------------- | |
2 | 2 | # Software: InVesalius Software de Reconstrucao 3D de Imagens Medicas |
3 | 3 | # Copyright: (c) 2001 Centro de Pesquisas Renato Archer |
4 | 4 | # Homepage: http://www.softwarepublico.gov.br |
... | ... | @@ -16,12 +16,14 @@ |
16 | 16 | # COMERCIALIZACAO ou de ADEQUACAO A QUALQUER PROPOSITO EM |
17 | 17 | # PARTICULAR. Consulte a Licenca Publica Geral GNU para obter mais |
18 | 18 | # detalhes. |
19 | -#--------------------------------------------------------------------- | |
20 | -import time | |
21 | -#import gdcm | |
19 | +# --------------------------------------------------------------------- | |
20 | +# import gdcm | |
22 | 21 | import sys |
23 | -import invesalius.utils as utils | |
22 | +import time | |
23 | + | |
24 | 24 | import invesalius.constants as const |
25 | +import invesalius.utils as utils | |
26 | + | |
25 | 27 | # In DICOM file format, if multiple values are present for the |
26 | 28 | # "Window Center" (Level) and "Window Width", both attributes |
27 | 29 | # shall have the same number of values and shall be considered as |
... | ... | @@ -30,48 +32,49 @@ import invesalius.constants as const |
30 | 32 | # one pair per time. This pair is determined by WL_PRESET |
31 | 33 | # constant or might be set by the user when calling GetWindowWidth |
32 | 34 | # and GetWindowLevel. |
33 | -WL_PRESET = 0 # index of selected window and level tuple (if multiple) | |
34 | -WL_MULT = 0 # allow selection of multiple window and level tuples if 1 | |
35 | +WL_PRESET = 0 # index of selected window and level tuple (if multiple) | |
36 | +WL_MULT = 0 # allow selection of multiple window and level tuples if 1 | |
35 | 37 | |
36 | 38 | # dictionary to be used by module functions |
37 | 39 | info = {} |
38 | 40 | # keys to be used to generate dictionary info |
39 | -INFO_KEYS = \ | |
40 | -['AcquisitionDate', | |
41 | - 'AcquisitionGantryTilt', | |
42 | - 'AcquisitionModality', | |
43 | - 'AcquisitionNumber', | |
44 | - 'AcquisionSequence', | |
45 | - 'AcquisitionTime', | |
46 | - 'EquipmentKVP', | |
47 | - 'EquipmentInstitutionName', | |
48 | - 'EquipmentManufacturer', | |
49 | - 'EquipmentXRayTubeCurrent', | |
50 | - 'ImageColumnOrientation', | |
51 | - 'ImageConvolutionKernel', | |
52 | - 'ImageDataType', | |
53 | - 'ImageLocation', | |
54 | - 'ImageNumber', | |
55 | - 'ImagePixelSpacingX', | |
56 | - 'ImagePixelSpacingY', | |
57 | - 'ImagePosition', | |
58 | - 'ImageRowOrientation', | |
59 | - 'ImageSamplesPerPixel', | |
60 | - 'ImageSeriesNumber', | |
61 | - 'ImageThickness', | |
62 | - 'ImageWindowLevel', | |
63 | - 'ImageWindowWidth', | |
64 | - 'PatientAge', | |
65 | - 'PatientBirthDate', | |
66 | - 'PatientGender', | |
67 | - 'PatientName', | |
68 | - 'PhysicianName', | |
69 | - 'StudyID', | |
70 | - 'StudyInstanceUID', | |
71 | - 'StudyAdmittingDiagnosis', | |
72 | - ] | |
73 | - | |
74 | -class Parser(): | |
41 | +INFO_KEYS = [ | |
42 | + "AcquisitionDate", | |
43 | + "AcquisitionGantryTilt", | |
44 | + "AcquisitionModality", | |
45 | + "AcquisitionNumber", | |
46 | + "AcquisionSequence", | |
47 | + "AcquisitionTime", | |
48 | + "EquipmentKVP", | |
49 | + "EquipmentInstitutionName", | |
50 | + "EquipmentManufacturer", | |
51 | + "EquipmentXRayTubeCurrent", | |
52 | + "ImageColumnOrientation", | |
53 | + "ImageConvolutionKernel", | |
54 | + "ImageDataType", | |
55 | + "ImageLocation", | |
56 | + "ImageNumber", | |
57 | + "ImagePixelSpacingX", | |
58 | + "ImagePixelSpacingY", | |
59 | + "ImagePosition", | |
60 | + "ImageRowOrientation", | |
61 | + "ImageSamplesPerPixel", | |
62 | + "ImageSeriesNumber", | |
63 | + "ImageThickness", | |
64 | + "ImageWindowLevel", | |
65 | + "ImageWindowWidth", | |
66 | + "PatientAge", | |
67 | + "PatientBirthDate", | |
68 | + "PatientGender", | |
69 | + "PatientName", | |
70 | + "PhysicianName", | |
71 | + "StudyID", | |
72 | + "StudyInstanceUID", | |
73 | + "StudyAdmittingDiagnosis", | |
74 | +] | |
75 | + | |
76 | + | |
77 | +class Parser: | |
75 | 78 | """ |
76 | 79 | Medical image parser. Used to parse medical image tags. |
77 | 80 | It supports: |
... | ... | @@ -92,13 +95,13 @@ class Parser(): |
92 | 95 | self.encoding = "" |
93 | 96 | self.filepath = "" |
94 | 97 | |
95 | - #def SetFileName(self, filename): | |
98 | + # def SetFileName(self, filename): | |
96 | 99 | """ |
97 | 100 | Set file name to be parsed given its filename (this should |
98 | 101 | include the full path of the file of interest). |
99 | 102 | |
100 | 103 | Return True/False if file could be read. |
101 | - | |
104 | + | |
102 | 105 | import os.path as path |
103 | 106 | |
104 | 107 | |
... | ... | @@ -134,28 +137,26 @@ class Parser(): |
134 | 137 | |
135 | 138 | return False""" |
136 | 139 | |
137 | - #def GetImageData(self): | |
140 | + # def GetImageData(self): | |
138 | 141 | # return None#self.vtkgdcm_reader.GetOutput() |
139 | 142 | |
140 | - | |
141 | 143 | def SetDataImage(self, data_image, filename, thumbnail_path): |
142 | 144 | self.data_image = data_image |
143 | 145 | self.filename = self.filepath = filename |
144 | 146 | self.thumbnail_path = thumbnail_path |
145 | 147 | |
146 | - def __format_time(self,value): | |
148 | + def __format_time(self, value): | |
147 | 149 | sp1 = value.split(".") |
148 | 150 | sp2 = value.split(":") |
149 | 151 | |
150 | - if (len(sp1) == 2) and (len(sp2) == 3): | |
151 | - new_value = str(sp2[0]+sp2[1]+ | |
152 | - str(int(float(sp2[2])))) | |
152 | + if (len(sp1) == 2) and (len(sp2) == 3): | |
153 | + new_value = str(sp2[0] + sp2[1] + str(int(float(sp2[2])))) | |
153 | 154 | data = time.strptime(new_value, "%H%M%S") |
154 | - elif (len(sp1) == 2): | |
155 | + elif len(sp1) == 2: | |
155 | 156 | data = time.gmtime(float(value)) |
156 | - elif (len(sp1) > 2): | |
157 | + elif len(sp1) > 2: | |
157 | 158 | data = time.strptime(value, "%H.%M.%S") |
158 | - elif(len(sp2) > 1): | |
159 | + elif len(sp2) > 1: | |
159 | 160 | data = time.strptime(value, "%H:%M:%S") |
160 | 161 | else: |
161 | 162 | try: |
... | ... | @@ -163,26 +164,26 @@ class Parser(): |
163 | 164 | # If the time is not in a bad format only return it. |
164 | 165 | except ValueError: |
165 | 166 | return value |
166 | - return time.strftime("%H:%M:%S",data) | |
167 | + return time.strftime("%H:%M:%S", data) | |
167 | 168 | |
168 | 169 | def __format_date(self, value): |
169 | 170 | |
170 | 171 | sp1 = value.split(".") |
171 | 172 | try: |
172 | 173 | |
173 | - if (len(sp1) > 1): | |
174 | - if (len(sp1[0]) <= 2): | |
174 | + if len(sp1) > 1: | |
175 | + if len(sp1[0]) <= 2: | |
175 | 176 | data = time.strptime(value, "%D.%M.%Y") |
176 | 177 | else: |
177 | 178 | data = time.strptime(value, "%Y.%M.%d") |
178 | - elif(len(value.split("//")) > 1): | |
179 | + elif len(value.split("//")) > 1: | |
179 | 180 | data = time.strptime(value, "%D/%M/%Y") |
180 | 181 | else: |
181 | 182 | data = time.strptime(value, "%Y%M%d") |
182 | - return time.strftime("%d/%M/%Y",data) | |
183 | + return time.strftime("%d/%M/%Y", data) | |
183 | 184 | |
184 | - except(ValueError): | |
185 | - return "" | |
185 | + except (ValueError): | |
186 | + return "" | |
186 | 187 | |
187 | 188 | def GetImageOrientationLabel(self): |
188 | 189 | """ |
... | ... | @@ -190,9 +191,9 @@ class Parser(): |
190 | 191 | an image. (AXIAL, SAGITTAL, CORONAL, |
191 | 192 | OBLIQUE or UNKNOWN) |
192 | 193 | """ |
193 | - label = self.data_image['invesalius']['orientation_label'] | |
194 | + label = self.data_image["invesalius"]["orientation_label"] | |
194 | 195 | |
195 | - if (label): | |
196 | + if label: | |
196 | 197 | return label |
197 | 198 | else: |
198 | 199 | return "" |
... | ... | @@ -204,11 +205,10 @@ class Parser(): |
204 | 205 | Return "" if not defined. |
205 | 206 | """ |
206 | 207 | data = self.data_image[str(0x028)][str(0x011)] |
207 | - if (data): | |
208 | + if data: | |
208 | 209 | return int(str(data)) |
209 | 210 | return "" |
210 | 211 | |
211 | - | |
212 | 212 | def GetDimensionY(self): |
213 | 213 | """ |
214 | 214 | Return integer associated to Y dimension. This is related |
... | ... | @@ -216,12 +216,11 @@ class Parser(): |
216 | 216 | Return "" if not defined. |
217 | 217 | """ |
218 | 218 | data = self.data_image[str(0x028)][str(0x010)] |
219 | - if (data): | |
219 | + if data: | |
220 | 220 | return int(str(data)) |
221 | 221 | return "" |
222 | 222 | |
223 | - | |
224 | - #def GetDimensionZ(self): | |
223 | + # def GetDimensionZ(self): | |
225 | 224 | # """ |
226 | 225 | # Return float value associated to Z dimension. |
227 | 226 | # Return "" if not defined. |
... | ... | @@ -291,7 +290,6 @@ class Parser(): |
291 | 290 | return spacing[0] |
292 | 291 | return "" |
293 | 292 | |
294 | - | |
295 | 293 | def GetAcquisitionDate(self): |
296 | 294 | """ |
297 | 295 | Return string containing the acquisition date using the |
... | ... | @@ -302,11 +300,11 @@ class Parser(): |
302 | 300 | """ |
303 | 301 | # TODO: internationalize data |
304 | 302 | try: |
305 | - date = self.data_image[str(0x0008)][str(0x0022)] | |
306 | - except(KeyError): | |
303 | + date = self.data_image[str(0x0008)][str(0x0022)] | |
304 | + except (KeyError): | |
307 | 305 | return "" |
308 | 306 | |
309 | - if (date) and (date != ''): | |
307 | + if (date) and (date != ""): | |
310 | 308 | return self.__format_date(str(date)) |
311 | 309 | return "" |
312 | 310 | |
... | ... | @@ -317,8 +315,8 @@ class Parser(): |
317 | 315 | |
318 | 316 | DICOM standard tag (0x0020, 0x0012) was used. |
319 | 317 | """ |
320 | - data = self.data_image[str(0x0020)][str(0x0012)] | |
321 | - if (data): | |
318 | + data = self.data_image[str(0x0020)][str(0x0012)] | |
319 | + if data: | |
322 | 320 | return int(str(data)) |
323 | 321 | return "" |
324 | 322 | |
... | ... | @@ -328,12 +326,12 @@ class Parser(): |
328 | 326 | |
329 | 327 | DICOM standard tag (0x0008, 0x0050) was used. |
330 | 328 | """ |
331 | - #data = self.data_image[0x008][0x050] | |
329 | + # data = self.data_image[0x008][0x050] | |
332 | 330 | return "" |
333 | - if (data): | |
331 | + if data: | |
334 | 332 | try: |
335 | 333 | value = int(str(data)) |
336 | - except(ValueError): #Problem in the other\iCatDanielaProjeto | |
334 | + except (ValueError): # Problem in the other\iCatDanielaProjeto | |
337 | 335 | value = 0 |
338 | 336 | return value |
339 | 337 | return "" |
... | ... | @@ -347,7 +345,7 @@ class Parser(): |
347 | 345 | DICOM standard tag (0x0008,0x0032) was used. |
348 | 346 | """ |
349 | 347 | data = self.data_image[str(0x008)][str(0x032)] |
350 | - if (data) and (data != ''): | |
348 | + if (data) and (data != ""): | |
351 | 349 | return self.__format_time(str(data)) |
352 | 350 | return "" |
353 | 351 | |
... | ... | @@ -363,11 +361,10 @@ class Parser(): |
363 | 361 | sf.SetFile(self.gdcm_reader.GetFile()) |
364 | 362 | res = sf.ToStringPair(tag) |
365 | 363 | |
366 | - if (res[1]): | |
364 | + if res[1]: | |
367 | 365 | return int(res[1]) |
368 | 366 | return "" |
369 | 367 | |
370 | - | |
371 | 368 | def GetImageWindowLevel(self, preset=WL_PRESET, multiple=WL_MULT): |
372 | 369 | """ |
373 | 370 | Return image window center / level (related to brightness). |
... | ... | @@ -382,15 +379,15 @@ class Parser(): |
382 | 379 | """ |
383 | 380 | try: |
384 | 381 | data = self.data_image[str(0x028)][str(0x1050)] |
385 | - except(KeyError): | |
382 | + except (KeyError): | |
386 | 383 | return "300" |
387 | - if (data): | |
384 | + if data: | |
388 | 385 | # Usually 'data' is a number. However, in some DICOM |
389 | 386 | # files, there are several values separated by '\'. |
390 | 387 | # If multiple values are present for the "Window Center" |
391 | 388 | # we choose only one. As this should be paired to "Window |
392 | 389 | # Width", it is set based on WL_PRESET |
393 | - value_list = [float(value) for value in data.split('\\')] | |
390 | + value_list = [float(value) for value in data.split("\\")] | |
394 | 391 | if multiple: |
395 | 392 | return value_list |
396 | 393 | else: |
... | ... | @@ -414,16 +411,16 @@ class Parser(): |
414 | 411 | """ |
415 | 412 | try: |
416 | 413 | data = self.data_image[str(0x028)][str(0x1051)] |
417 | - except(KeyError): | |
414 | + except (KeyError): | |
418 | 415 | return "2000" |
419 | 416 | |
420 | - if (data): | |
417 | + if data: | |
421 | 418 | # Usually 'data' is a number. However, in some DICOM |
422 | 419 | # files, there are several values separated by '\'. |
423 | 420 | # If multiple values are present for the "Window Center" |
424 | 421 | # we choose only one. As this should be paired to "Window |
425 | 422 | # Width", it is set based on WL_PRESET |
426 | - value_list = [float(value) for value in data.split('\\')] | |
423 | + value_list = [float(value) for value in data.split("\\")] | |
427 | 424 | |
428 | 425 | if multiple: |
429 | 426 | return str(value_list) |
... | ... | @@ -443,10 +440,10 @@ class Parser(): |
443 | 440 | """ |
444 | 441 | try: |
445 | 442 | data = self.data_image[str(0x020)][str(0x032)].replace(",", ".") |
446 | - except(KeyError): | |
443 | + except (KeyError): | |
447 | 444 | return "" |
448 | - if (data): | |
449 | - return [eval(value) for value in data.split('\\')] | |
445 | + if data: | |
446 | + return [eval(value) for value in data.split("\\")] | |
450 | 447 | return "" |
451 | 448 | |
452 | 449 | def GetImageLocation(self): |
... | ... | @@ -458,7 +455,7 @@ class Parser(): |
458 | 455 | DICOM standard tag (0x0020, 0x0032) was used. |
459 | 456 | """ |
460 | 457 | data = self.data_image[str(0x020)][str(0x1041)] |
461 | - if (data): | |
458 | + if data: | |
462 | 459 | return eval(data) |
463 | 460 | return "" |
464 | 461 | |
... | ... | @@ -470,15 +467,14 @@ class Parser(): |
470 | 467 | DICOM standard tag (0x7fe0, 0x0010) was used. |
471 | 468 | """ |
472 | 469 | try: |
473 | - data = self.data_image[str(0x7fe0)][str(0x0010)] | |
474 | - except(KeyError): | |
470 | + data = self.data_image[str(0x7FE0)][str(0x0010)] | |
471 | + except (KeyError): | |
475 | 472 | return "" |
476 | 473 | |
477 | - if (data): | |
478 | - return int(data.split(':')[1]) | |
474 | + if data: | |
475 | + return int(data.split(":")[1]) | |
479 | 476 | return "" |
480 | 477 | |
481 | - | |
482 | 478 | def GetImageSeriesNumber(self): |
483 | 479 | """ |
484 | 480 | Return integer related to acquisition series where this |
... | ... | @@ -489,14 +485,13 @@ class Parser(): |
489 | 485 | """ |
490 | 486 | try: |
491 | 487 | data = self.data_image[str(0x020)][str(0x011)] |
492 | - except(KeyError): | |
488 | + except (KeyError): | |
493 | 489 | return "" |
494 | 490 | |
495 | 491 | if (data) and (data != '""') and (data != "None"): |
496 | 492 | return int(data) |
497 | 493 | return "" |
498 | 494 | |
499 | - | |
500 | 495 | def GetPixelSpacing(self): |
501 | 496 | """ |
502 | 497 | Return [x, y] (number list) related to the distance between |
... | ... | @@ -508,7 +503,7 @@ class Parser(): |
508 | 503 | DICOM standard tag (0x0028, 0x0030) was used. |
509 | 504 | """ |
510 | 505 | try: |
511 | - image_helper_spacing = self.data_image["spacing"] | |
506 | + image_helper_spacing = self.data_image["spacing"] | |
512 | 507 | except KeyError: |
513 | 508 | image_helper_spacing = None |
514 | 509 | try: |
... | ... | @@ -516,7 +511,9 @@ class Parser(): |
516 | 511 | except KeyError: |
517 | 512 | tag_spacing = "" |
518 | 513 | |
519 | - # Some dicom images have comma (,) as decimal separation. In this case InVesalius is not using the spacing given by gdcm.ImageHelper but using direct from the tag and replacing the comma with dot. | |
514 | + # Some dicom images have comma (,) as decimal separation. In this case | |
515 | + # InVesalius is not using the spacing given by gdcm.ImageHelper but | |
516 | + # using direct from the tag and replacing the comma with dot. | |
520 | 517 | if image_helper_spacing is not None and "," not in tag_spacing: |
521 | 518 | return image_helper_spacing[:2] |
522 | 519 | else: |
... | ... | @@ -531,10 +528,10 @@ class Parser(): |
531 | 528 | """ |
532 | 529 | try: |
533 | 530 | data = self.data_image[str(0x0010)][str(0x1030)] |
534 | - except(KeyError): | |
531 | + except (KeyError): | |
535 | 532 | return "" |
536 | 533 | |
537 | - if (data): | |
534 | + if data: | |
538 | 535 | return float(data) |
539 | 536 | return "" |
540 | 537 | |
... | ... | @@ -547,10 +544,10 @@ class Parser(): |
547 | 544 | """ |
548 | 545 | try: |
549 | 546 | data = self.data_image[str(0x010)][str(0x1020)] |
550 | - except(KeyError): | |
547 | + except (KeyError): | |
551 | 548 | return "" |
552 | 549 | |
553 | - if (data): | |
550 | + if data: | |
554 | 551 | return float(data) |
555 | 552 | return "" |
556 | 553 | |
... | ... | @@ -562,9 +559,9 @@ class Parser(): |
562 | 559 | """ |
563 | 560 | try: |
564 | 561 | data = self.data_image[str(0x010)][str(0x1040)] |
565 | - except(KeyError): | |
562 | + except (KeyError): | |
566 | 563 | return "" |
567 | - if (data): | |
564 | + if data: | |
568 | 565 | return data |
569 | 566 | return "" |
570 | 567 | |
... | ... | @@ -577,9 +574,9 @@ class Parser(): |
577 | 574 | """ |
578 | 575 | try: |
579 | 576 | data = self.data_image[str(0x010)][str(0x1080)] |
580 | - except(KeyError): | |
577 | + except (KeyError): | |
581 | 578 | return "" |
582 | - if (data): | |
579 | + if data: | |
583 | 580 | return data |
584 | 581 | return "" |
585 | 582 | |
... | ... | @@ -594,9 +591,9 @@ class Parser(): |
594 | 591 | """ |
595 | 592 | try: |
596 | 593 | data = self.data_image[str(0x010)][str(0x1081)] |
597 | - except(KeyError): | |
594 | + except (KeyError): | |
598 | 595 | return "" |
599 | - if (data): | |
596 | + if data: | |
600 | 597 | return data |
601 | 598 | return "" |
602 | 599 | |
... | ... | @@ -610,10 +607,10 @@ class Parser(): |
610 | 607 | """ |
611 | 608 | try: |
612 | 609 | data = self.data_image[str(0x0010)][str(0x2150)] |
613 | - except(KeyError): | |
610 | + except (KeyError): | |
614 | 611 | return "" |
615 | 612 | |
616 | - if (data): | |
613 | + if data: | |
617 | 614 | return data |
618 | 615 | return "" |
619 | 616 | |
... | ... | @@ -627,10 +624,10 @@ class Parser(): |
627 | 624 | """ |
628 | 625 | try: |
629 | 626 | data = self.data_image[str(0x0010)][str(0x2152)] |
630 | - except(KeyError): | |
627 | + except (KeyError): | |
631 | 628 | return "" |
632 | 629 | |
633 | - if (data): | |
630 | + if data: | |
634 | 631 | return data |
635 | 632 | return "" |
636 | 633 | |
... | ... | @@ -643,10 +640,10 @@ class Parser(): |
643 | 640 | """ |
644 | 641 | try: |
645 | 642 | data = self.data_image[str(0x0010)][str(0x2154)] |
646 | - except(KeyError): | |
643 | + except (KeyError): | |
647 | 644 | return "" |
648 | 645 | |
649 | - if (data): | |
646 | + if data: | |
650 | 647 | return data |
651 | 648 | return "" |
652 | 649 | |
... | ... | @@ -660,10 +657,10 @@ class Parser(): |
660 | 657 | """ |
661 | 658 | try: |
662 | 659 | data = self.data_image[str(0x0010)][str(0x2297)] |
663 | - except(KeyError): | |
660 | + except (KeyError): | |
664 | 661 | return "" |
665 | 662 | |
666 | - if (data): | |
663 | + if data: | |
667 | 664 | return data |
668 | 665 | return "" |
669 | 666 | |
... | ... | @@ -676,11 +673,11 @@ class Parser(): |
676 | 673 | DICOM standard tag (0x0010, 0x2298) was used. |
677 | 674 | """ |
678 | 675 | try: |
679 | - data = self.data_image[str(0x0010)][str(0x2298)] | |
680 | - except(KeyError): | |
676 | + data = self.data_image[str(0x0010)][str(0x2298)] | |
677 | + except (KeyError): | |
681 | 678 | return "" |
682 | 679 | |
683 | - if (data): | |
680 | + if data: | |
684 | 681 | return data |
685 | 682 | return "" |
686 | 683 | |
... | ... | @@ -694,10 +691,10 @@ class Parser(): |
694 | 691 | """ |
695 | 692 | try: |
696 | 693 | data = self.data_image[str(0x0010)][str(0x2299)] |
697 | - except(KeyError): | |
694 | + except (KeyError): | |
698 | 695 | return "" |
699 | 696 | |
700 | - if (data): | |
697 | + if data: | |
701 | 698 | return data |
702 | 699 | return "" |
703 | 700 | |
... | ... | @@ -711,10 +708,10 @@ class Parser(): |
711 | 708 | """ |
712 | 709 | try: |
713 | 710 | data = self.data_image[str(0x0010)][str(0x2000)] |
714 | - except(KeyError): | |
711 | + except (KeyError): | |
715 | 712 | return "" |
716 | 713 | |
717 | - if (data): | |
714 | + if data: | |
718 | 715 | return data |
719 | 716 | return "" |
720 | 717 | |
... | ... | @@ -727,15 +724,14 @@ class Parser(): |
727 | 724 | DICOM standard tag (0x0008, 0x2110) was used. |
728 | 725 | """ |
729 | 726 | try: |
730 | - data = self.data_image[str(0x0008)][str(0x2110)] | |
731 | - except(KeyError): | |
727 | + data = self.data_image[str(0x0008)][str(0x2110)] | |
728 | + except (KeyError): | |
732 | 729 | return "" |
733 | 730 | |
734 | - if (data): | |
731 | + if data: | |
735 | 732 | return data |
736 | 733 | return "" |
737 | 734 | |
738 | - | |
739 | 735 | def GetPhysicianReferringName(self): |
740 | 736 | """ |
741 | 737 | Return string containing physician |
... | ... | @@ -746,16 +742,15 @@ class Parser(): |
746 | 742 | """ |
747 | 743 | try: |
748 | 744 | data = self.data_image[str(0x0008)][str(0x0090)] |
749 | - except(KeyError): | |
745 | + except (KeyError): | |
750 | 746 | return "" |
751 | 747 | |
752 | 748 | if data == "None": |
753 | 749 | return "" |
754 | - if (data): | |
750 | + if data: | |
755 | 751 | return data |
756 | 752 | return "" |
757 | 753 | |
758 | - | |
759 | 754 | def GetPhysicianReferringAddress(self): |
760 | 755 | """ |
761 | 756 | Return string containing physician's address. |
... | ... | @@ -765,10 +760,10 @@ class Parser(): |
765 | 760 | """ |
766 | 761 | try: |
767 | 762 | data = self.data_image[str(0x0008)][str(0x0092)] |
768 | - except(KeyError): | |
763 | + except (KeyError): | |
769 | 764 | return "" |
770 | 765 | |
771 | - if (data): | |
766 | + if data: | |
772 | 767 | return data |
773 | 768 | return "" |
774 | 769 | |
... | ... | @@ -781,10 +776,10 @@ class Parser(): |
781 | 776 | """ |
782 | 777 | try: |
783 | 778 | data = self.data_image[str(0x0008)][str(0x0094)] |
784 | - except(KeyError): | |
779 | + except (KeyError): | |
785 | 780 | return "" |
786 | - | |
787 | - if (data): | |
781 | + | |
782 | + if data: | |
788 | 783 | return data |
789 | 784 | return "" |
790 | 785 | |
... | ... | @@ -797,10 +792,10 @@ class Parser(): |
797 | 792 | """ |
798 | 793 | try: |
799 | 794 | data = self.data_image[str(0x0018)][str(0x1030)] |
800 | - except(KeyError): | |
795 | + except (KeyError): | |
801 | 796 | return None |
802 | 797 | |
803 | - if (data): | |
798 | + if data: | |
804 | 799 | return data |
805 | 800 | return None |
806 | 801 | |
... | ... | @@ -815,17 +810,17 @@ class Parser(): |
815 | 810 | """ |
816 | 811 | try: |
817 | 812 | data = self.data_image[str(0x008)][str(0x008)] |
818 | - except(IndexError): | |
813 | + except (IndexError): | |
819 | 814 | return [] |
820 | 815 | # TODO: Check if set image type to empty is the right way of handling |
821 | 816 | # the cases where there is not this tag. |
822 | 817 | except KeyError: |
823 | 818 | return [] |
824 | 819 | |
825 | - if (data): | |
820 | + if data: | |
826 | 821 | try: |
827 | - return data.split('\\') | |
828 | - except(IndexError): | |
822 | + return data.split("\\") | |
823 | + except (IndexError): | |
829 | 824 | return [] |
830 | 825 | return [] |
831 | 826 | |
... | ... | @@ -839,10 +834,10 @@ class Parser(): |
839 | 834 | """ |
840 | 835 | try: |
841 | 836 | data = self.data_image[str(0x0008)][str(0x0016)] |
842 | - except(KeyError): | |
837 | + except (KeyError): | |
843 | 838 | return "" |
844 | 839 | |
845 | - if (data): | |
840 | + if data: | |
846 | 841 | return data |
847 | 842 | return "" |
848 | 843 | |
... | ... | @@ -856,10 +851,10 @@ class Parser(): |
856 | 851 | """ |
857 | 852 | try: |
858 | 853 | data = self.data_image[str(0x0008)][str(0x0018)] |
859 | - except(KeyError): | |
854 | + except (KeyError): | |
860 | 855 | return "" |
861 | 856 | |
862 | - if (data): | |
857 | + if data: | |
863 | 858 | return data |
864 | 859 | return "" |
865 | 860 | |
... | ... | @@ -872,11 +867,11 @@ class Parser(): |
872 | 867 | Critical DICOM Tag (0x0020,0x000D). Cannot be edited. |
873 | 868 | """ |
874 | 869 | try: |
875 | - data = self.data_image[str(0x0020)][str(0x000D)] | |
876 | - except(KeyError): | |
870 | + data = self.data_image[str(0x0020)][str(0x000D)] | |
871 | + except (KeyError): | |
877 | 872 | return "" |
878 | 873 | |
879 | - if (data): | |
874 | + if data: | |
880 | 875 | return data |
881 | 876 | return "" |
882 | 877 | |
... | ... | @@ -893,11 +888,11 @@ class Parser(): |
893 | 888 | """ |
894 | 889 | try: |
895 | 890 | data = self.data_image[str(0x0020)][str(0x0037)].replace(",", ".") |
896 | - except(KeyError): | |
891 | + except (KeyError): | |
897 | 892 | return [1.0, 0.0, 0.0, 0.0, 1.0, 0.0] |
898 | 893 | |
899 | - if (data): | |
900 | - return [float(value) for value in data.split('\\')] | |
894 | + if data: | |
895 | + return [float(value) for value in data.split("\\")] | |
901 | 896 | return [1.0, 0.0, 0.0, 0.0, 1.0, 0.0] |
902 | 897 | |
903 | 898 | def GetImageColumnOrientation(self): |
... | ... | @@ -911,11 +906,11 @@ class Parser(): |
911 | 906 | """ |
912 | 907 | try: |
913 | 908 | data = self.data_image[str(0x0020)][str(0x0037)] |
914 | - except(KeyError): | |
909 | + except (KeyError): | |
915 | 910 | return [0.0, 1.0, 0.0] |
916 | 911 | |
917 | - if (data): | |
918 | - return [float(value) for value in data.split('\\')[3:6]] | |
912 | + if data: | |
913 | + return [float(value) for value in data.split("\\")[3:6]] | |
919 | 914 | return [0.0, 1.0, 0.0] |
920 | 915 | |
921 | 916 | def GetImageRowOrientation(self): |
... | ... | @@ -929,11 +924,11 @@ class Parser(): |
929 | 924 | """ |
930 | 925 | try: |
931 | 926 | data = self.data_image[str(0x0020)][str(0x0037)] |
932 | - except(KeyError): | |
927 | + except (KeyError): | |
933 | 928 | return [1.0, 0.0, 0.0] |
934 | 929 | |
935 | - if (data): | |
936 | - return [float(value) for value in data.split('\\')[0:3]] | |
930 | + if data: | |
931 | + return [float(value) for value in data.split("\\")[0:3]] | |
937 | 932 | return [1.0, 0.0, 0.0] |
938 | 933 | |
939 | 934 | def GetFrameReferenceUID(self): |
... | ... | @@ -945,10 +940,10 @@ class Parser(): |
945 | 940 | """ |
946 | 941 | try: |
947 | 942 | data = self.data_image[str(0x0020)][str(0x0052)] |
948 | - except(KeyError): | |
943 | + except (KeyError): | |
949 | 944 | return "" |
950 | 945 | |
951 | - if (data): | |
946 | + if data: | |
952 | 947 | return data |
953 | 948 | return "" |
954 | 949 | |
... | ... | @@ -980,7 +975,7 @@ class Parser(): |
980 | 975 | sf = gdcm.StringFilter() |
981 | 976 | sf.SetFile(self.gdcm_reader.GetFile()) |
982 | 977 | res = sf.ToStringPair(tag) |
983 | - if (res[1]): | |
978 | + if res[1]: | |
984 | 979 | return res[1] |
985 | 980 | return "" |
986 | 981 | |
... | ... | @@ -996,7 +991,7 @@ class Parser(): |
996 | 991 | sf = gdcm.StringFilter() |
997 | 992 | sf.SetFile(self.gdcm_reader.GetFile()) |
998 | 993 | res = sf.ToStringPair(tag) |
999 | - if (res[1]): | |
994 | + if res[1]: | |
1000 | 995 | return int(res[1]) |
1001 | 996 | return "" |
1002 | 997 | |
... | ... | @@ -1011,7 +1006,7 @@ class Parser(): |
1011 | 1006 | sf = gdcm.StringFilter() |
1012 | 1007 | sf.SetFile(self.gdcm_reader.GetFile()) |
1013 | 1008 | res = sf.ToStringPair(tag) |
1014 | - if (res[1]): | |
1009 | + if res[1]: | |
1015 | 1010 | return int(res[1]) |
1016 | 1011 | return "" |
1017 | 1012 | |
... | ... | @@ -1026,9 +1021,9 @@ class Parser(): |
1026 | 1021 | """ |
1027 | 1022 | try: |
1028 | 1023 | data = self.data_image[str(0x0018)][str(0x1030)] |
1029 | - if (data): | |
1024 | + if data: | |
1030 | 1025 | return data |
1031 | - except(KeyError): | |
1026 | + except (KeyError): | |
1032 | 1027 | return "" |
1033 | 1028 | return "" |
1034 | 1029 | |
... | ... | @@ -1050,10 +1045,10 @@ class Parser(): |
1050 | 1045 | """ |
1051 | 1046 | try: |
1052 | 1047 | data = self.data_image[str(0x0018)][str(0x0020)] |
1053 | - except(KeyError): | |
1048 | + except (KeyError): | |
1054 | 1049 | return "" |
1055 | 1050 | |
1056 | - if (data): | |
1051 | + if data: | |
1057 | 1052 | return data |
1058 | 1053 | return "" |
1059 | 1054 | |
... | ... | @@ -1065,12 +1060,12 @@ class Parser(): |
1065 | 1060 | DICOM standard tag (0x0008, 0x0080) was used. |
1066 | 1061 | """ |
1067 | 1062 | try: |
1068 | - data = self.data_image[str(0x0008)][str(0x0080)] | |
1069 | - except(KeyError): | |
1063 | + data = self.data_image[str(0x0008)][str(0x0080)] | |
1064 | + except (KeyError): | |
1070 | 1065 | return "" |
1071 | 1066 | |
1072 | - if (data): | |
1073 | - return data | |
1067 | + if data: | |
1068 | + return data | |
1074 | 1069 | return "" |
1075 | 1070 | |
1076 | 1071 | def GetInstitutionAddress(self): |
... | ... | @@ -1084,10 +1079,10 @@ class Parser(): |
1084 | 1079 | """ |
1085 | 1080 | try: |
1086 | 1081 | data = self.data_image[str(0x0008)][str(0x0081)] |
1087 | - except(KeyError): | |
1082 | + except (KeyError): | |
1088 | 1083 | return "" |
1089 | 1084 | |
1090 | - if (data): | |
1085 | + if data: | |
1091 | 1086 | return data |
1092 | 1087 | return "" |
1093 | 1088 | |
... | ... | @@ -1101,10 +1096,10 @@ class Parser(): |
1101 | 1096 | """ |
1102 | 1097 | try: |
1103 | 1098 | data = self.data_image[str(0x0020)][str(0x000D)] |
1104 | - except(KeyError): | |
1099 | + except (KeyError): | |
1105 | 1100 | return "" |
1106 | 1101 | |
1107 | - if (data): | |
1102 | + if data: | |
1108 | 1103 | return data |
1109 | 1104 | return "" |
1110 | 1105 | |
... | ... | @@ -1117,10 +1112,10 @@ class Parser(): |
1117 | 1112 | """ |
1118 | 1113 | try: |
1119 | 1114 | data = self.data_image[str(0x0010)][str(0x2180)] |
1120 | - except(KeyError): | |
1115 | + except (KeyError): | |
1121 | 1116 | return "" |
1122 | 1117 | |
1123 | - if (data): | |
1118 | + if data: | |
1124 | 1119 | return data |
1125 | 1120 | return "" |
1126 | 1121 | |
... | ... | @@ -1137,7 +1132,7 @@ class Parser(): |
1137 | 1132 | sf = gdcm.StringFilter() |
1138 | 1133 | sf.SetFile(self.gdcm_reader.GetFile()) |
1139 | 1134 | res = sf.ToStringPair(tag) |
1140 | - if (res[1]): | |
1135 | + if res[1]: | |
1141 | 1136 | return int(res[1]) |
1142 | 1137 | return "" |
1143 | 1138 | |
... | ... | @@ -1149,16 +1144,16 @@ class Parser(): |
1149 | 1144 | |
1150 | 1145 | DICOM standard tag (0x0028, 0x0100) was used. |
1151 | 1146 | """ |
1152 | - #tag = gdcm.Tag(0x0028, 0x0100) | |
1153 | - #sf = gdcm.StringFilter() | |
1154 | - #sf.SetFile(self.gdcm_reader.GetFile()) | |
1155 | - #res = sf.ToStringPair(tag) | |
1147 | + # tag = gdcm.Tag(0x0028, 0x0100) | |
1148 | + # sf = gdcm.StringFilter() | |
1149 | + # sf.SetFile(self.gdcm_reader.GetFile()) | |
1150 | + # res = sf.ToStringPair(tag) | |
1156 | 1151 | try: |
1157 | 1152 | data = self.data_image[str(0x0028)][str(0x0100)] |
1158 | - except(KeyError): | |
1153 | + except (KeyError): | |
1159 | 1154 | return "" |
1160 | 1155 | |
1161 | - if (data): | |
1156 | + if data: | |
1162 | 1157 | return int(data) |
1163 | 1158 | return "" |
1164 | 1159 | |
... | ... | @@ -1174,7 +1169,6 @@ class Parser(): |
1174 | 1169 | return 1 |
1175 | 1170 | return int(data) |
1176 | 1171 | |
1177 | - | |
1178 | 1172 | def GetPatientBirthDate(self): |
1179 | 1173 | """ |
1180 | 1174 | Return string containing the patient's birth date using the |
... | ... | @@ -1186,14 +1180,13 @@ class Parser(): |
1186 | 1180 | # TODO: internationalize data |
1187 | 1181 | try: |
1188 | 1182 | data = self.data_image[str(0x0010)][str(0x0030)] |
1189 | - except(KeyError): | |
1183 | + except (KeyError): | |
1190 | 1184 | return "" |
1191 | 1185 | |
1192 | - if (data) and (data != 'None'): | |
1186 | + if (data) and (data != "None"): | |
1193 | 1187 | return self.__format_date(str(data)) |
1194 | 1188 | return "" |
1195 | 1189 | |
1196 | - | |
1197 | 1190 | def GetStudyID(self): |
1198 | 1191 | """ |
1199 | 1192 | Return string containing the Study ID. |
... | ... | @@ -1203,13 +1196,13 @@ class Parser(): |
1203 | 1196 | """ |
1204 | 1197 | try: |
1205 | 1198 | data = self.data_image[str(0x0020)][str(0x0010)] |
1206 | - except(KeyError): | |
1199 | + except (KeyError): | |
1207 | 1200 | return "" |
1208 | 1201 | |
1209 | - if (data): | |
1202 | + if data: | |
1210 | 1203 | return str(data) |
1211 | 1204 | return "" |
1212 | - | |
1205 | + | |
1213 | 1206 | def GetAcquisitionGantryTilt(self): |
1214 | 1207 | """ |
1215 | 1208 | Return floating point containing nominal angle of |
... | ... | @@ -1220,10 +1213,10 @@ class Parser(): |
1220 | 1213 | """ |
1221 | 1214 | try: |
1222 | 1215 | data = self.data_image[str(0x0018)][str(0x1120)] |
1223 | - except(KeyError): | |
1216 | + except (KeyError): | |
1224 | 1217 | return 0.0 |
1225 | 1218 | |
1226 | - if (data): | |
1219 | + if data: | |
1227 | 1220 | return float(str(data)) |
1228 | 1221 | return 0.0 |
1229 | 1222 | |
... | ... | @@ -1239,16 +1232,16 @@ class Parser(): |
1239 | 1232 | """ |
1240 | 1233 | try: |
1241 | 1234 | data = self.data_image[str(0x0010)][str(0x0040)] |
1242 | - except(KeyError): | |
1235 | + except (KeyError): | |
1243 | 1236 | return "" |
1244 | 1237 | |
1245 | - if (data): | |
1238 | + if data: | |
1246 | 1239 | name = data.strip() |
1247 | 1240 | encoding = self.GetEncoding() |
1248 | 1241 | try: |
1249 | 1242 | # Returns a unicode decoded in the own dicom encoding |
1250 | - return utils.decode(name, encoding, 'replace') | |
1251 | - except(UnicodeEncodeError): | |
1243 | + return utils.decode(name, encoding, "replace") | |
1244 | + except (UnicodeEncodeError): | |
1252 | 1245 | return name |
1253 | 1246 | |
1254 | 1247 | return "" |
... | ... | @@ -1263,11 +1256,11 @@ class Parser(): |
1263 | 1256 | """ |
1264 | 1257 | try: |
1265 | 1258 | data = self.data_image[str(0x0010)][str(0x1010)] |
1266 | - except(KeyError): | |
1259 | + except (KeyError): | |
1267 | 1260 | return "" |
1268 | 1261 | |
1269 | - if (data): | |
1270 | - age = (data.split('Y')[0]) | |
1262 | + if data: | |
1263 | + age = data.split("Y")[0] | |
1271 | 1264 | try: |
1272 | 1265 | return int(age) |
1273 | 1266 | except ValueError: |
... | ... | @@ -1283,7 +1276,7 @@ class Parser(): |
1283 | 1276 | """ |
1284 | 1277 | try: |
1285 | 1278 | data = self.data_image[str(0x0010)][str(0x0010)] |
1286 | - except(KeyError): | |
1279 | + except (KeyError): | |
1287 | 1280 | return "" |
1288 | 1281 | |
1289 | 1282 | encoding = self.GetEncoding() |
... | ... | @@ -1303,19 +1296,18 @@ class Parser(): |
1303 | 1296 | """ |
1304 | 1297 | try: |
1305 | 1298 | data = self.data_image[str(0x0010)][str(0x0020)] |
1306 | - except(KeyError): | |
1299 | + except (KeyError): | |
1307 | 1300 | return "" |
1308 | 1301 | |
1309 | - if (data): | |
1302 | + if data: | |
1310 | 1303 | encoding = self.GetEncoding() |
1311 | 1304 | # Returns a unicode decoded in the own dicom encoding |
1312 | 1305 | try: |
1313 | - return utils.decode(data, encoding, 'replace') | |
1314 | - except(UnicodeEncodeError): | |
1306 | + return utils.decode(data, encoding, "replace") | |
1307 | + except (UnicodeEncodeError): | |
1315 | 1308 | return data |
1316 | 1309 | return "" |
1317 | 1310 | |
1318 | - | |
1319 | 1311 | def GetEquipmentXRayTubeCurrent(self): |
1320 | 1312 | """ |
1321 | 1313 | Return float value associated to the X-ray tube current |
... | ... | @@ -1326,10 +1318,10 @@ class Parser(): |
1326 | 1318 | """ |
1327 | 1319 | try: |
1328 | 1320 | data = self.data_image[str(0x0018)][str(0x1151)] |
1329 | - except(KeyError): | |
1321 | + except (KeyError): | |
1330 | 1322 | return "" |
1331 | 1323 | |
1332 | - if (data): | |
1324 | + if data: | |
1333 | 1325 | return data |
1334 | 1326 | return "" |
1335 | 1327 | |
... | ... | @@ -1343,10 +1335,10 @@ class Parser(): |
1343 | 1335 | """ |
1344 | 1336 | try: |
1345 | 1337 | data = self.data_image[str(0x0018)][str(0x1152)] |
1346 | - except(KeyError): | |
1338 | + except (KeyError): | |
1347 | 1339 | return "" |
1348 | 1340 | |
1349 | - if (data): | |
1341 | + if data: | |
1350 | 1342 | return float(data) |
1351 | 1343 | return "" |
1352 | 1344 | |
... | ... | @@ -1360,10 +1352,10 @@ class Parser(): |
1360 | 1352 | """ |
1361 | 1353 | try: |
1362 | 1354 | data = self.data_image[str(0x0018)][str(0x0060)] |
1363 | - except(KeyError): | |
1355 | + except (KeyError): | |
1364 | 1356 | return "" |
1365 | 1357 | |
1366 | - if (data): | |
1358 | + if data: | |
1367 | 1359 | return float(data) |
1368 | 1360 | return "" |
1369 | 1361 | |
... | ... | @@ -1377,9 +1369,9 @@ class Parser(): |
1377 | 1369 | """ |
1378 | 1370 | try: |
1379 | 1371 | data = self.data_image[str(0x0018)][str(0x0050)].replace(",", ".") |
1380 | - except(KeyError): | |
1372 | + except (KeyError): | |
1381 | 1373 | return 0 |
1382 | - if (data): | |
1374 | + if data: | |
1383 | 1375 | return float(data) |
1384 | 1376 | return 0 |
1385 | 1377 | |
... | ... | @@ -1395,10 +1387,10 @@ class Parser(): |
1395 | 1387 | """ |
1396 | 1388 | try: |
1397 | 1389 | data = self.data_image[str(0x0018)][str(0x1210)] |
1398 | - except(KeyError): | |
1390 | + except (KeyError): | |
1399 | 1391 | return "" |
1400 | 1392 | |
1401 | - if (data): | |
1393 | + if data: | |
1402 | 1394 | return data |
1403 | 1395 | return "" |
1404 | 1396 | |
... | ... | @@ -1412,10 +1404,10 @@ class Parser(): |
1412 | 1404 | """ |
1413 | 1405 | try: |
1414 | 1406 | data = self.data_image[str(0x0008)][str(0x0080)] |
1415 | - except(KeyError): | |
1407 | + except (KeyError): | |
1416 | 1408 | return "" |
1417 | 1409 | |
1418 | - if (data): | |
1410 | + if data: | |
1419 | 1411 | return data |
1420 | 1412 | return "" |
1421 | 1413 | |
... | ... | @@ -1429,10 +1421,10 @@ class Parser(): |
1429 | 1421 | """ |
1430 | 1422 | try: |
1431 | 1423 | data = self.data_image[str(0x0008)][str(0x1010)] |
1432 | - except(KeyError): | |
1424 | + except (KeyError): | |
1433 | 1425 | return "" |
1434 | 1426 | |
1435 | - if (data): | |
1427 | + if data: | |
1436 | 1428 | return data |
1437 | 1429 | return "" |
1438 | 1430 | |
... | ... | @@ -1446,24 +1438,24 @@ class Parser(): |
1446 | 1438 | """ |
1447 | 1439 | try: |
1448 | 1440 | data = self.data_image[str(0x0008)][str(0x1090)] |
1449 | - except(KeyError): | |
1441 | + except (KeyError): | |
1450 | 1442 | return "" |
1451 | 1443 | |
1452 | - if (data): | |
1444 | + if data: | |
1453 | 1445 | return data |
1454 | 1446 | return "" |
1455 | 1447 | |
1456 | 1448 | def GetManufacturerName(self): |
1457 | 1449 | """ |
1458 | - Return Manufacturer of the equipment that produced | |
1450 | + Return Manufacturer of the equipment that produced | |
1459 | 1451 | the composite instances. |
1460 | 1452 | """ |
1461 | 1453 | try: |
1462 | 1454 | data = self.data_image[str(0x0008)][str(0x0070)] |
1463 | - except(KeyError): | |
1455 | + except (KeyError): | |
1464 | 1456 | return "" |
1465 | 1457 | |
1466 | - if (data): | |
1458 | + if data: | |
1467 | 1459 | return data |
1468 | 1460 | return "" |
1469 | 1461 | |
... | ... | @@ -1476,10 +1468,10 @@ class Parser(): |
1476 | 1468 | """ |
1477 | 1469 | try: |
1478 | 1470 | data = self.data_image[str(0x0008)][str(0x1010)] |
1479 | - except(KeyError): | |
1471 | + except (KeyError): | |
1480 | 1472 | return "" |
1481 | 1473 | |
1482 | - if (data): | |
1474 | + if data: | |
1483 | 1475 | return data |
1484 | 1476 | return "" |
1485 | 1477 | |
... | ... | @@ -1494,14 +1486,13 @@ class Parser(): |
1494 | 1486 | """ |
1495 | 1487 | try: |
1496 | 1488 | data = self.data_image[str(0x0008)][str(0x0060)] |
1497 | - except(KeyError): | |
1489 | + except (KeyError): | |
1498 | 1490 | return "" |
1499 | 1491 | |
1500 | - if (data): | |
1492 | + if data: | |
1501 | 1493 | return data |
1502 | 1494 | return "" |
1503 | 1495 | |
1504 | - | |
1505 | 1496 | def GetImageNumber(self): |
1506 | 1497 | """ |
1507 | 1498 | Return slice number (integer). |
... | ... | @@ -1511,10 +1502,10 @@ class Parser(): |
1511 | 1502 | """ |
1512 | 1503 | try: |
1513 | 1504 | data = self.data_image[str(0x0020)][str(0x0013)] |
1514 | - except(KeyError): | |
1505 | + except (KeyError): | |
1515 | 1506 | return 0 |
1516 | 1507 | |
1517 | - if (data): | |
1508 | + if data: | |
1518 | 1509 | return int(data) |
1519 | 1510 | return 0 |
1520 | 1511 | |
... | ... | @@ -1527,10 +1518,10 @@ class Parser(): |
1527 | 1518 | """ |
1528 | 1519 | try: |
1529 | 1520 | data = self.data_image[str(0x0008)][str(0x1030)] |
1530 | - if (data): | |
1521 | + if data: | |
1531 | 1522 | encoding = self.GetEncoding() |
1532 | - return utils.decode(data, encoding, 'replace') | |
1533 | - except(KeyError): | |
1523 | + return utils.decode(data, encoding, "replace") | |
1524 | + except (KeyError): | |
1534 | 1525 | return "" |
1535 | 1526 | |
1536 | 1527 | def GetStudyAdmittingDiagnosis(self): |
... | ... | @@ -1546,11 +1537,10 @@ class Parser(): |
1546 | 1537 | sf.SetFile(self.gdcm_reader.GetFile()) |
1547 | 1538 | res = sf.ToStringPair(tag) |
1548 | 1539 | |
1549 | - if (res[1]): | |
1540 | + if res[1]: | |
1550 | 1541 | return str(res[1]) |
1551 | 1542 | return "" |
1552 | 1543 | |
1553 | - | |
1554 | 1544 | def GetSeriesDescription(self): |
1555 | 1545 | """ |
1556 | 1546 | Return a string with a description of the series. |
... | ... | @@ -1558,7 +1548,7 @@ class Parser(): |
1558 | 1548 | """ |
1559 | 1549 | try: |
1560 | 1550 | data = self.data_image[str(0x0008)][str(0x103E)] |
1561 | - except(KeyError): | |
1551 | + except (KeyError): | |
1562 | 1552 | return _("unnamed") |
1563 | 1553 | |
1564 | 1554 | encoding = self.GetEncoding() |
... | ... | @@ -1581,11 +1571,11 @@ class Parser(): |
1581 | 1571 | """ |
1582 | 1572 | try: |
1583 | 1573 | data = self.data_image[str(0x0008)][str(0x0033)] |
1584 | - except(KeyError): | |
1574 | + except (KeyError): | |
1585 | 1575 | return "" |
1586 | 1576 | |
1587 | - if (data) and (data != 'None'): | |
1588 | - return self.__format_time(data) | |
1577 | + if (data) and (data != "None"): | |
1578 | + return self.__format_time(data) | |
1589 | 1579 | return "" |
1590 | 1580 | |
1591 | 1581 | def GetAcquisitionTime(self): |
... | ... | @@ -1595,10 +1585,10 @@ class Parser(): |
1595 | 1585 | """ |
1596 | 1586 | try: |
1597 | 1587 | data = self.data_image[str(0x0008)][str(0x0032)] |
1598 | - except(KeyError): | |
1588 | + except (KeyError): | |
1599 | 1589 | return "" |
1600 | 1590 | |
1601 | - if (data): | |
1591 | + if data: | |
1602 | 1592 | return self.__format_time(data) |
1603 | 1593 | return "" |
1604 | 1594 | |
... | ... | @@ -1609,10 +1599,10 @@ class Parser(): |
1609 | 1599 | """ |
1610 | 1600 | try: |
1611 | 1601 | data = self.data_image[str(0x0020)][str(0x0011)] |
1612 | - except(KeyError): | |
1602 | + except (KeyError): | |
1613 | 1603 | return "" |
1614 | - | |
1615 | - if (data): | |
1604 | + | |
1605 | + if data: | |
1616 | 1606 | return data |
1617 | 1607 | return "" |
1618 | 1608 | |
... | ... | @@ -1624,8 +1614,8 @@ class Parser(): |
1624 | 1614 | try: |
1625 | 1615 | encoding_value = self.data_image[str(0x0008)][str(0x0005)] |
1626 | 1616 | return const.DICOM_ENCODING_TO_PYTHON[encoding_value] |
1627 | - except(KeyError): | |
1628 | - return 'ISO_IR_100' | |
1617 | + except (KeyError): | |
1618 | + return "ISO_IR_100" | |
1629 | 1619 | |
1630 | 1620 | |
1631 | 1621 | class DicomWriter: |
... | ... | @@ -1665,7 +1655,6 @@ class DicomWriter: |
1665 | 1655 | self.new_dicom = vtkgdcm.vtkGDCMImageWriter() |
1666 | 1656 | reader = self.reader = gdcm.Reader() |
1667 | 1657 | |
1668 | - | |
1669 | 1658 | def SetFileName(self, path): |
1670 | 1659 | """ |
1671 | 1660 | Set Dicom File Name |
... | ... | @@ -1674,19 +1663,16 @@ class DicomWriter: |
1674 | 1663 | |
1675 | 1664 | self.reader.SetFileName(path) |
1676 | 1665 | |
1677 | - if (self.reader.Read()): | |
1666 | + if self.reader.Read(): | |
1678 | 1667 | |
1679 | 1668 | self.anony.SetFile(self.reader.GetFile()) |
1680 | 1669 | |
1681 | - | |
1682 | - | |
1683 | 1670 | def SetInput(self, img_data): |
1684 | 1671 | """ |
1685 | 1672 | Input vtkImageData |
1686 | 1673 | """ |
1687 | 1674 | self.img_data = img_data |
1688 | 1675 | |
1689 | - | |
1690 | 1676 | def __CreateNewDicom(self, img_data): |
1691 | 1677 | """ |
1692 | 1678 | Create New Dicom File, input is |
... | ... | @@ -1697,7 +1683,6 @@ class DicomWriter: |
1697 | 1683 | new_dicom.SetInput(img_data) |
1698 | 1684 | new_dicom.Write() |
1699 | 1685 | |
1700 | - | |
1701 | 1686 | def SaveIsNew(self, img_data): |
1702 | 1687 | """ |
1703 | 1688 | Write Changes in Dicom file or Create |
... | ... | @@ -1706,12 +1691,11 @@ class DicomWriter: |
1706 | 1691 | |
1707 | 1692 | self.__CreateNewDicom(img_data) |
1708 | 1693 | |
1709 | - #Is necessary to create and add | |
1710 | - #information in the dicom tag | |
1694 | + # Is necessary to create and add | |
1695 | + # information in the dicom tag | |
1711 | 1696 | self.SetFileName(self.path) |
1712 | 1697 | self.anony.SetFile(self.reader.GetFile()) |
1713 | 1698 | |
1714 | - | |
1715 | 1699 | def Save(self): |
1716 | 1700 | |
1717 | 1701 | reader = self.reader |
... | ... | @@ -1721,84 +1705,63 @@ class DicomWriter: |
1721 | 1705 | writer.SetFileName(self.path) |
1722 | 1706 | writer.Write() |
1723 | 1707 | |
1724 | - | |
1725 | 1708 | def SetPatientName(self, patient): |
1726 | 1709 | """ |
1727 | 1710 | Set Patient Name requeries string type |
1728 | 1711 | """ |
1729 | - self.anony.Replace(gdcm.Tag(0x0010,0x0010), \ | |
1730 | - str(patient)) | |
1731 | - | |
1712 | + self.anony.Replace(gdcm.Tag(0x0010, 0x0010), str(patient)) | |
1732 | 1713 | |
1733 | 1714 | def SetImageThickness(self, thickness): |
1734 | 1715 | """ |
1735 | 1716 | Set thickness value requeries float type |
1736 | 1717 | """ |
1737 | - self.anony.Replace(gdcm.Tag(0x0018,0x0050), \ | |
1738 | - str(thickness)) | |
1739 | - | |
1718 | + self.anony.Replace(gdcm.Tag(0x0018, 0x0050), str(thickness)) | |
1740 | 1719 | |
1741 | 1720 | def SetImageSeriesNumber(self, number): |
1742 | 1721 | """ |
1743 | 1722 | Set Serie Number value requeries int type |
1744 | 1723 | """ |
1745 | - self.anony.Replace(gdcm.Tag(0x0020,0x0011), \ | |
1746 | - str(number)) | |
1747 | - | |
1724 | + self.anony.Replace(gdcm.Tag(0x0020, 0x0011), str(number)) | |
1748 | 1725 | |
1749 | 1726 | def SetImageNumber(self, number): |
1750 | 1727 | """ |
1751 | 1728 | Set image Number value requeries int type |
1752 | 1729 | """ |
1753 | - self.anony.Replace(gdcm.Tag(0x0020,0x0013), | |
1754 | - str(number)) | |
1755 | - | |
1730 | + self.anony.Replace(gdcm.Tag(0x0020, 0x0013), str(number)) | |
1756 | 1731 | |
1757 | 1732 | def SetImageLocation(self, location): |
1758 | 1733 | """ |
1759 | 1734 | Set slice location value requeries float type |
1760 | 1735 | """ |
1761 | - self.anony.Replace(gdcm.Tag(0x0020,0x1041),\ | |
1762 | - str(number)) | |
1763 | - | |
1736 | + self.anony.Replace(gdcm.Tag(0x0020, 0x1041), str(number)) | |
1764 | 1737 | |
1765 | 1738 | def SetImagePosition(self, position): |
1766 | 1739 | """ |
1767 | 1740 | Set slice position value requeries list |
1768 | 1741 | with three values x, y and z |
1769 | 1742 | """ |
1770 | - self.anony.Replace(gdcm.Tag(0x0020,0x0032), \ | |
1771 | - str(position[0]) + \ | |
1772 | - "\\" + str(position[1]) + \ | |
1773 | - "\\" + str(position[2])) | |
1774 | - | |
1743 | + self.anony.Replace( | |
1744 | + gdcm.Tag(0x0020, 0x0032), | |
1745 | + str(position[0]) + "\\" + str(position[1]) + "\\" + str(position[2]), | |
1746 | + ) | |
1775 | 1747 | |
1776 | 1748 | def SetAcquisitionModality(self, modality): |
1777 | 1749 | """ |
1778 | 1750 | Set modality study CT or RM |
1779 | 1751 | """ |
1780 | - self.anony.Replace(gdcm.Tag(0x0008,0x0060), \ | |
1781 | - str(modality)) | |
1782 | - | |
1752 | + self.anony.Replace(gdcm.Tag(0x0008, 0x0060), str(modality)) | |
1783 | 1753 | |
1784 | 1754 | def SetPixelSpacing(self, spacing): |
1785 | 1755 | """ |
1786 | 1756 | Set pixel spacing x and y |
1787 | 1757 | """ |
1788 | - self.anony.Replace(gdcm.Tag(0x0028,0x0030), \ | |
1789 | - str(spacing)) | |
1790 | - | |
1758 | + self.anony.Replace(gdcm.Tag(0x0028, 0x0030), str(spacing)) | |
1791 | 1759 | |
1792 | 1760 | def SetInstitutionName(self, institution): |
1793 | 1761 | """ |
1794 | 1762 | Set institution name |
1795 | 1763 | """ |
1796 | - self.anony.Replace(gdcm.Tag(0x0008, 0x0080), \ | |
1797 | - str(institution)) | |
1798 | - | |
1799 | - | |
1800 | - | |
1801 | - | |
1764 | + self.anony.Replace(gdcm.Tag(0x0008, 0x0080), str(institution)) | |
1802 | 1765 | |
1803 | 1766 | |
1804 | 1767 | def BuildDictionary(filename): |
... | ... | @@ -1816,10 +1779,11 @@ def BuildDictionary(filename): |
1816 | 1779 | # file, given keys in info_keys list. Example: |
1817 | 1780 | # info["AcquisitionDate"] = dicom.GetAcquisitionDate() |
1818 | 1781 | for key in INFO_KEYS: |
1819 | - info[key] = eval("parser.Get"+key+"()") | |
1782 | + info[key] = eval("parser.Get" + key + "()") | |
1820 | 1783 | |
1821 | 1784 | return info |
1822 | 1785 | |
1786 | + | |
1823 | 1787 | def LoadDictionary(filename): |
1824 | 1788 | """ |
1825 | 1789 | Given a binary file (containing a dictionary), return dictionary |
... | ... | @@ -1844,14 +1808,15 @@ def DumpDictionary(filename, dictionary=info): |
1844 | 1808 | pickle.dump(info, fp) |
1845 | 1809 | fp.close() |
1846 | 1810 | |
1811 | + | |
1847 | 1812 | if __name__ == "__main__": |
1848 | 1813 | |
1849 | 1814 | # Example of how to use Parser |
1850 | 1815 | fail_count = 0 |
1851 | 1816 | total = 48 |
1852 | 1817 | |
1853 | - for i in range(1,total+1): | |
1854 | - filename = "..//data//"+str(i)+".dcm" | |
1818 | + for i in range(1, total + 1): | |
1819 | + filename = "..//data//" + str(i) + ".dcm" | |
1855 | 1820 | |
1856 | 1821 | parser = Parser() |
1857 | 1822 | if parser.SetFileName(filename): |
... | ... | @@ -1865,8 +1830,8 @@ if __name__ == "__main__": |
1865 | 1830 | print("z:", parser.GetDimensionZ()) |
1866 | 1831 | else: |
1867 | 1832 | print("--------------------------------------------------") |
1868 | - total-=1 | |
1869 | - fail_count+=1 | |
1833 | + total -= 1 | |
1834 | + fail_count += 1 | |
1870 | 1835 | |
1871 | 1836 | print("\nREPORT:") |
1872 | 1837 | print("failed: ", fail_count) |
... | ... | @@ -1874,16 +1839,13 @@ if __name__ == "__main__": |
1874 | 1839 | |
1875 | 1840 | # Example of how to use auxiliary functions |
1876 | 1841 | total = 38 |
1877 | - for i in range(1,total+1): | |
1878 | - if (i==8) or (i==9) or (i==13): | |
1842 | + for i in range(1, total + 1): | |
1843 | + if (i == 8) or (i == 9) or (i == 13): | |
1879 | 1844 | pass |
1880 | 1845 | else: |
1881 | - filename = "..//data//"+str(i)+".dcm" | |
1846 | + filename = "..//data//" + str(i) + ".dcm" | |
1882 | 1847 | info = BuildDictionary(filename) |
1883 | - #print info | |
1884 | - | |
1885 | - | |
1886 | - | |
1848 | + # print info | |
1887 | 1849 | |
1888 | 1850 | |
1889 | 1851 | class Dicom(object): |
... | ... | @@ -1896,7 +1858,7 @@ class Dicom(object): |
1896 | 1858 | self.LoadImageInfo() |
1897 | 1859 | self.LoadPatientInfo() |
1898 | 1860 | self.LoadAcquisitionInfo() |
1899 | - #self.LoadStudyInfo() | |
1861 | + # self.LoadStudyInfo() | |
1900 | 1862 | |
1901 | 1863 | def LoadImageInfo(self): |
1902 | 1864 | self.image = Image() |
... | ... | @@ -1911,9 +1873,6 @@ class Dicom(object): |
1911 | 1873 | self.acquisition.SetParser(self.parser) |
1912 | 1874 | |
1913 | 1875 | |
1914 | - | |
1915 | - | |
1916 | - | |
1917 | 1876 | class Patient(object): |
1918 | 1877 | def __init__(self): |
1919 | 1878 | pass |
... | ... | @@ -1928,7 +1887,6 @@ class Patient(object): |
1928 | 1887 | |
1929 | 1888 | |
1930 | 1889 | class Acquisition(object): |
1931 | - | |
1932 | 1890 | def __init__(self): |
1933 | 1891 | pass |
1934 | 1892 | |
... | ... | @@ -1950,7 +1908,6 @@ class Acquisition(object): |
1950 | 1908 | |
1951 | 1909 | |
1952 | 1910 | class Image(object): |
1953 | - | |
1954 | 1911 | def __init__(self): |
1955 | 1912 | pass |
1956 | 1913 | |
... | ... | @@ -1969,14 +1926,14 @@ class Image(object): |
1969 | 1926 | self.time = parser.GetImageTime() |
1970 | 1927 | self.type = parser.GetImageType() |
1971 | 1928 | self.size = (parser.GetDimensionX(), parser.GetDimensionY()) |
1972 | - #self.imagedata = parser.GetImageData() | |
1929 | + # self.imagedata = parser.GetImageData() | |
1973 | 1930 | self.bits_allocad = parser._GetBitsAllocated() |
1974 | 1931 | self.thumbnail_path = parser.thumbnail_path |
1975 | 1932 | |
1976 | 1933 | self.number_of_frames = parser.GetNumberOfFrames() |
1977 | 1934 | self.samples_per_pixel = parser.GetImageSamplesPerPixel() |
1978 | 1935 | |
1979 | - if (parser.GetImageThickness()): | |
1936 | + if parser.GetImageThickness(): | |
1980 | 1937 | self.spacing.append(parser.GetImageThickness()) |
1981 | 1938 | else: |
1982 | 1939 | self.spacing.append(1.0) | ... | ... |
invesalius/reader/dicom_reader.py
1 | -#-------------------------------------------------------------------------- | |
1 | +# -------------------------------------------------------------------------- | |
2 | 2 | # Software: InVesalius - Software de Reconstrucao 3D de Imagens Medicas |
3 | 3 | # Copyright: (C) 2001 Centro de Pesquisas Renato Archer |
4 | 4 | # Homepage: http://www.softwarepublico.gov.br |
5 | 5 | # Contact: invesalius@cti.gov.br |
6 | 6 | # License: GNU - GPL 2 (LICENSE.txt/LICENCA.txt) |
7 | -#-------------------------------------------------------------------------- | |
7 | +# -------------------------------------------------------------------------- | |
8 | 8 | # Este programa e software livre; voce pode redistribui-lo e/ou |
9 | 9 | # modifica-lo sob os termos da Licenca Publica Geral GNU, conforme |
10 | 10 | # publicada pela Free Software Foundation; de acordo com a versao 2 |
... | ... | @@ -15,36 +15,35 @@ |
15 | 15 | # COMERCIALIZACAO ou de ADEQUACAO A QUALQUER PROPOSITO EM |
16 | 16 | # PARTICULAR. Consulte a Licenca Publica Geral GNU para obter mais |
17 | 17 | # detalhes. |
18 | -#-------------------------------------------------------------------------- | |
18 | +# -------------------------------------------------------------------------- | |
19 | 19 | import os |
20 | -import threading | |
21 | -import tempfile | |
22 | 20 | import sys |
23 | - | |
21 | +import tempfile | |
22 | +import threading | |
24 | 23 | from multiprocessing import cpu_count |
25 | 24 | |
26 | -import vtk | |
27 | 25 | import gdcm |
26 | +import vtk | |
27 | + | |
28 | 28 | # Not showing GDCM warning and debug messages |
29 | 29 | gdcm.Trace_DebugOff() |
30 | 30 | gdcm.Trace_WarningOff() |
31 | -from invesalius.pubsub import pub as Publisher | |
31 | +import glob | |
32 | +import plistlib | |
32 | 33 | |
33 | 34 | import invesalius.constants as const |
34 | 35 | import invesalius.reader.dicom as dicom |
35 | 36 | import invesalius.reader.dicom_grouper as dicom_grouper |
36 | 37 | import invesalius.session as session |
37 | -import glob | |
38 | 38 | import invesalius.utils as utils |
39 | - | |
40 | 39 | from invesalius import inv_paths |
41 | 40 | from invesalius.data import imagedata_utils |
41 | +from invesalius.pubsub import pub as Publisher | |
42 | 42 | |
43 | -import plistlib | |
44 | - | |
45 | -if sys.platform == 'win32': | |
43 | +if sys.platform == "win32": | |
46 | 44 | try: |
47 | 45 | import win32api |
46 | + | |
48 | 47 | _has_win32api = True |
49 | 48 | except ImportError: |
50 | 49 | _has_win32api = False |
... | ... | @@ -79,6 +78,7 @@ def SelectLargerDicomGroup(patient_group): |
79 | 78 | |
80 | 79 | return larger_group |
81 | 80 | |
81 | + | |
82 | 82 | def SortFiles(filelist, dicom): |
83 | 83 | # Sort slices |
84 | 84 | # FIXME: Coronal Crash. necessary verify |
... | ... | @@ -89,15 +89,17 @@ def SortFiles(filelist, dicom): |
89 | 89 | sorter.SetZSpacingTolerance(1e-10) |
90 | 90 | sorter.Sort(filelist) |
91 | 91 | |
92 | - #Getting organized image | |
92 | + # Getting organized image | |
93 | 93 | filelist = sorter.GetFilenames() |
94 | 94 | |
95 | 95 | return filelist |
96 | 96 | |
97 | + | |
97 | 98 | tag_labels = {} |
98 | 99 | main_dict = {} |
99 | 100 | dict_file = {} |
100 | 101 | |
102 | + | |
101 | 103 | class LoadDicom: |
102 | 104 | def __init__(self, grouper, filepath): |
103 | 105 | self.grouper = grouper |
... | ... | @@ -109,8 +111,11 @@ class LoadDicom: |
109 | 111 | reader = gdcm.ImageReader() |
110 | 112 | if _has_win32api: |
111 | 113 | try: |
112 | - reader.SetFileName(utils.encode(win32api.GetShortPathName(self.filepath), | |
113 | - const.FS_ENCODE)) | |
114 | + reader.SetFileName( | |
115 | + utils.encode( | |
116 | + win32api.GetShortPathName(self.filepath), const.FS_ENCODE | |
117 | + ) | |
118 | + ) | |
114 | 119 | except TypeError: |
115 | 120 | reader.SetFileName(win32api.GetShortPathName(self.filepath)) |
116 | 121 | else: |
... | ... | @@ -118,7 +123,7 @@ class LoadDicom: |
118 | 123 | reader.SetFileName(utils.encode(self.filepath, const.FS_ENCODE)) |
119 | 124 | except TypeError: |
120 | 125 | reader.SetFileName(self.filepath) |
121 | - if (reader.Read()): | |
126 | + if reader.Read(): | |
122 | 127 | file = reader.GetFile() |
123 | 128 | # Retrieve data set |
124 | 129 | dataSet = file.GetDataSet() |
... | ... | @@ -133,23 +138,23 @@ class LoadDicom: |
133 | 138 | tag = gdcm.Tag(0x0008, 0x0005) |
134 | 139 | ds = reader.GetFile().GetDataSet() |
135 | 140 | image_helper = gdcm.ImageHelper() |
136 | - data_dict['spacing'] = image_helper.GetSpacingValue(reader.GetFile()) | |
141 | + data_dict["spacing"] = image_helper.GetSpacingValue(reader.GetFile()) | |
137 | 142 | if ds.FindDataElement(tag): |
138 | - encoding_value = str(ds.GetDataElement(tag).GetValue()).split('\\')[0] | |
139 | - | |
143 | + encoding_value = str(ds.GetDataElement(tag).GetValue()).split("\\")[0] | |
144 | + | |
140 | 145 | if encoding_value.startswith("Loaded"): |
141 | 146 | encoding = "ISO_IR 100" |
142 | 147 | else: |
143 | 148 | try: |
144 | 149 | encoding = const.DICOM_ENCODING_TO_PYTHON[encoding_value] |
145 | 150 | except KeyError: |
146 | - encoding = 'ISO_IR 100' | |
151 | + encoding = "ISO_IR 100" | |
147 | 152 | else: |
148 | 153 | encoding = "ISO_IR 100" |
149 | 154 | |
150 | 155 | # Iterate through the Header |
151 | 156 | iterator = header.GetDES().begin() |
152 | - while (not iterator.equal(header.GetDES().end())): | |
157 | + while not iterator.equal(header.GetDES().end()): | |
153 | 158 | dataElement = iterator.next() |
154 | 159 | if not dataElement.IsUndefinedLength(): |
155 | 160 | tag = dataElement.GetTag() |
... | ... | @@ -164,20 +169,20 @@ class LoadDicom: |
164 | 169 | if not group in data_dict.keys(): |
165 | 170 | data_dict[group] = {} |
166 | 171 | |
167 | - if not(utils.VerifyInvalidPListCharacter(data[1])): | |
172 | + if not (utils.VerifyInvalidPListCharacter(data[1])): | |
168 | 173 | data_dict[group][field] = utils.decode(data[1], encoding) |
169 | 174 | else: |
170 | 175 | data_dict[group][field] = "Invalid Character" |
171 | 176 | |
172 | 177 | # Iterate through the Data set |
173 | 178 | iterator = dataSet.GetDES().begin() |
174 | - while (not iterator.equal(dataSet.GetDES().end())): | |
179 | + while not iterator.equal(dataSet.GetDES().end()): | |
175 | 180 | dataElement = iterator.next() |
176 | 181 | if not dataElement.IsUndefinedLength(): |
177 | 182 | tag = dataElement.GetTag() |
178 | 183 | # if (tag.GetGroup() == 0x0009 and tag.GetElement() == 0x10e3) \ |
179 | - # or (tag.GetGroup() == 0x0043 and tag.GetElement() == 0x1027): | |
180 | - # continue | |
184 | + # or (tag.GetGroup() == 0x0043 and tag.GetElement() == 0x1027): | |
185 | + # continue | |
181 | 186 | data = stf.ToStringPair(tag) |
182 | 187 | stag = tag.PrintAsPipeSeparatedString() |
183 | 188 | |
... | ... | @@ -189,28 +194,28 @@ class LoadDicom: |
189 | 194 | if not group in data_dict.keys(): |
190 | 195 | data_dict[group] = {} |
191 | 196 | |
192 | - if not(utils.VerifyInvalidPListCharacter(data[1])): | |
193 | - data_dict[group][field] = utils.decode(data[1], encoding, 'replace') | |
197 | + if not (utils.VerifyInvalidPListCharacter(data[1])): | |
198 | + data_dict[group][field] = utils.decode( | |
199 | + data[1], encoding, "replace" | |
200 | + ) | |
194 | 201 | else: |
195 | 202 | data_dict[group][field] = "Invalid Character" |
196 | 203 | |
197 | - | |
198 | 204 | # -------------- To Create DICOM Thumbnail ----------- |
199 | 205 | |
200 | - | |
201 | 206 | try: |
202 | 207 | data = data_dict[str(0x028)][str(0x1050)] |
203 | - level = [float(value) for value in data.split('\\')][0] | |
208 | + level = [float(value) for value in data.split("\\")][0] | |
204 | 209 | data = data_dict[str(0x028)][str(0x1051)] |
205 | - window = [float(value) for value in data.split('\\')][0] | |
206 | - except(KeyError, ValueError): | |
210 | + window = [float(value) for value in data.split("\\")][0] | |
211 | + except (KeyError, ValueError): | |
207 | 212 | level = None |
208 | 213 | window = None |
209 | 214 | |
210 | 215 | img = reader.GetImage() |
211 | 216 | thumbnail_path = imagedata_utils.create_dicom_thumbnails(img, window, level) |
212 | 217 | |
213 | - #------ Verify the orientation -------------------------------- | |
218 | + # ------ Verify the orientation -------------------------------- | |
214 | 219 | |
215 | 220 | direc_cosines = img.GetDirectionCosines() |
216 | 221 | orientation = gdcm.Orientation() |
... | ... | @@ -220,41 +225,43 @@ class LoadDicom: |
220 | 225 | _type = orientation.GetType(direc_cosines) |
221 | 226 | label = orientation.GetLabel(_type) |
222 | 227 | |
223 | - | |
224 | 228 | # ---------- Refactory -------------------------------------- |
225 | - data_dict['invesalius'] = {'orientation_label' : label} | |
229 | + data_dict["invesalius"] = {"orientation_label": label} | |
226 | 230 | |
227 | 231 | # ------------------------------------------------------------- |
228 | 232 | dict_file[self.filepath] = data_dict |
229 | - | |
230 | - #---------- Verify is DICOMDir ------------------------------- | |
233 | + | |
234 | + # ---------- Verify is DICOMDir ------------------------------- | |
231 | 235 | is_dicom_dir = 1 |
232 | - try: | |
233 | - if (data_dict[str(0x002)][str(0x002)] != "1.2.840.10008.1.3.10"): #DICOMDIR | |
234 | - is_dicom_dir = 0 | |
235 | - except(KeyError): | |
236 | + try: | |
237 | + if ( | |
238 | + data_dict[str(0x002)][str(0x002)] != "1.2.840.10008.1.3.10" | |
239 | + ): # DICOMDIR | |
236 | 240 | is_dicom_dir = 0 |
237 | - | |
238 | - if not(is_dicom_dir): | |
241 | + except (KeyError): | |
242 | + is_dicom_dir = 0 | |
243 | + | |
244 | + if not (is_dicom_dir): | |
239 | 245 | parser = dicom.Parser() |
240 | - parser.SetDataImage(dict_file[self.filepath], self.filepath, thumbnail_path) | |
241 | - | |
246 | + parser.SetDataImage( | |
247 | + dict_file[self.filepath], self.filepath, thumbnail_path | |
248 | + ) | |
249 | + | |
242 | 250 | dcm = dicom.Dicom() |
243 | - #self.l.acquire() | |
251 | + # self.l.acquire() | |
244 | 252 | dcm.SetParser(parser) |
245 | 253 | grouper.AddFile(dcm) |
246 | 254 | |
247 | - #self.l.release() | |
248 | - | |
255 | + # self.l.release() | |
249 | 256 | |
250 | - #========== used in test ======================================= | |
251 | - #print dict_file | |
252 | - #main_dict = dict( | |
257 | + # ========== used in test ======================================= | |
258 | + # print dict_file | |
259 | + # main_dict = dict( | |
253 | 260 | # data = dict_file, |
254 | 261 | # labels = tag_labels) |
255 | - #print main_dict | |
256 | - #print "\n" | |
257 | - #plistlib.writePlist(main_dict, ".//teste.plist") | |
262 | + # print main_dict | |
263 | + # print "\n" | |
264 | + # plistlib.writePlist(main_dict, ".//teste.plist") | |
258 | 265 | |
259 | 266 | |
260 | 267 | def yGetDicomGroups(directory, recursive=True, gui=True): |
... | ... | @@ -271,11 +278,11 @@ def yGetDicomGroups(directory, recursive=True, gui=True): |
271 | 278 | nfiles = len(filenames) |
272 | 279 | |
273 | 280 | counter = 0 |
274 | - grouper = dicom_grouper.DicomPatientGrouper() | |
275 | - #q = Queue.Queue() | |
276 | - #l = threading.Lock() | |
277 | - #threads = [] | |
278 | - #for i in xrange(cpu_count()): | |
281 | + grouper = dicom_grouper.DicomPatientGrouper() | |
282 | + # q = Queue.Queue() | |
283 | + # l = threading.Lock() | |
284 | + # threads = [] | |
285 | + # for i in xrange(cpu_count()): | |
279 | 286 | # t = LoadDicom(grouper, q, l) |
280 | 287 | # t.start() |
281 | 288 | # threads.append(t) |
... | ... | @@ -286,7 +293,7 @@ def yGetDicomGroups(directory, recursive=True, gui=True): |
286 | 293 | filepath = os.path.join(dirpath, name) |
287 | 294 | counter += 1 |
288 | 295 | if gui: |
289 | - yield (counter,nfiles) | |
296 | + yield (counter, nfiles) | |
290 | 297 | LoadDicom(grouper, filepath) |
291 | 298 | else: |
292 | 299 | dirpath, dirnames, filenames = os.walk(directory) |
... | ... | @@ -294,17 +301,17 @@ def yGetDicomGroups(directory, recursive=True, gui=True): |
294 | 301 | filepath = str(os.path.join(dirpath, name)) |
295 | 302 | counter += 1 |
296 | 303 | if gui: |
297 | - yield (counter,nfiles) | |
298 | - #q.put(filepath) | |
304 | + yield (counter, nfiles) | |
305 | + # q.put(filepath) | |
299 | 306 | |
300 | - #for t in threads: | |
307 | + # for t in threads: | |
301 | 308 | # q.put(0) |
302 | 309 | |
303 | - #for t in threads: | |
310 | + # for t in threads: | |
304 | 311 | # t.join() |
305 | 312 | |
306 | - #TODO: Is this commented update necessary? | |
307 | - #grouper.Update() | |
313 | + # TODO: Is this commented update necessary? | |
314 | + # grouper.Update() | |
308 | 315 | yield grouper.GetPatientsGroups() |
309 | 316 | |
310 | 317 | |
... | ... | @@ -321,14 +328,14 @@ class ProgressDicomReader: |
321 | 328 | self.stoped = True |
322 | 329 | |
323 | 330 | def SetWindowEvent(self, frame): |
324 | - self.frame = frame | |
331 | + self.frame = frame | |
325 | 332 | |
326 | - def SetDirectoryPath(self, path,recursive=True): | |
333 | + def SetDirectoryPath(self, path, recursive=True): | |
327 | 334 | self.running = True |
328 | 335 | self.stoped = False |
329 | - self.GetDicomGroups(path,recursive) | |
336 | + self.GetDicomGroups(path, recursive) | |
330 | 337 | |
331 | - def UpdateLoadFileProgress(self,cont_progress): | |
338 | + def UpdateLoadFileProgress(self, cont_progress): | |
332 | 339 | Publisher.sendMessage("Update dicom load", data=cont_progress) |
333 | 340 | |
334 | 341 | def EndLoadFile(self, patient_list): |
... | ... | @@ -337,7 +344,9 @@ class ProgressDicomReader: |
337 | 344 | def GetDicomGroups(self, path, recursive): |
338 | 345 | |
339 | 346 | if not const.VTK_WARNING: |
340 | - log_path = utils.encode(str(inv_paths.USER_LOG_DIR.joinpath('vtkoutput.txt')), const.FS_ENCODE) | |
347 | + log_path = utils.encode( | |
348 | + str(inv_paths.USER_LOG_DIR.joinpath("vtkoutput.txt")), const.FS_ENCODE | |
349 | + ) | |
341 | 350 | fow = vtk.vtkFileOutputWindow() |
342 | 351 | fow.SetFileName(log_path) |
343 | 352 | ow = vtk.vtkOutputWindow() |
... | ... | @@ -354,10 +363,8 @@ class ProgressDicomReader: |
354 | 363 | self.EndLoadFile(value_progress) |
355 | 364 | self.UpdateLoadFileProgress(None) |
356 | 365 | |
357 | - #Is necessary in the case user cancel | |
358 | - #the load, ensure that dicomdialog is closed | |
359 | - if(self.stoped): | |
366 | + # Is necessary in the case user cancel | |
367 | + # the load, ensure that dicomdialog is closed | |
368 | + if self.stoped: | |
360 | 369 | self.UpdateLoadFileProgress(None) |
361 | - self.stoped = False | |
362 | - | |
363 | - | |
370 | + self.stoped = False | ... | ... |