Commit b09b522f3091ec4ebd338deda75b4fa821a5c022

Authored by Paulo Henrique Junqueira Amorim
1 parent 3a7f3445

ENH: Restructuring of the class group DICOM

invesalius/reader/dicom.py
... ... @@ -1620,3 +1620,79 @@ if __name__ == "__main__":
1620 1620 #print info
1621 1621  
1622 1622  
  1623 +
  1624 +
  1625 +
  1626 +class Dicom(object):
  1627 + def __init__(self):
  1628 + pass
  1629 +
  1630 + def SetParser(self, parser):
  1631 + self.parser = parser
  1632 +
  1633 + self.LoadImageInfo()
  1634 + self.LoadPatientInfo()
  1635 + self.LoadAcquisitionInfo()
  1636 + #self.LoadStudyInfo()
  1637 +
  1638 + def LoadImageInfo(self):
  1639 + self.image = Image()
  1640 + self.image.SetParser(self.parser)
  1641 +
  1642 + def LoadPatientInfo(self):
  1643 + self.patient = Patient()
  1644 + self.patient.SetParser(self.parser)
  1645 +
  1646 + def LoadAcquisitionInfo(self):
  1647 + self.acquisition = Acquisition()
  1648 + self.acquisition.SetParser(self.parser)
  1649 +
  1650 +
  1651 +
  1652 +class Patient(object):
  1653 + def __init__(self):
  1654 + pass
  1655 +
  1656 + def SetParser(self, parser):
  1657 + self.name = parser.GetPatientName()
  1658 + self.physician = parser.GetPhysicianName()
  1659 +
  1660 +class Acquisition(object):
  1661 +
  1662 + def __init__(self):
  1663 + pass
  1664 +
  1665 + def SetParser(self, parser):
  1666 + self.patient_orientation = parser.GetImagePatientOrientation()
  1667 + self.series_description = parser.GetSeriesDescrition()
  1668 + self.tilt = parser.GetAcquisitionGantryTilt()
  1669 + self.serie_number = parser.GetImageSeriesNumber()
  1670 + self.id_study = parser.GetStudyID()
  1671 +
  1672 +class Image(object):
  1673 +
  1674 + def __init__(self):
  1675 + pass
  1676 +
  1677 + def SetParser(self, parser):
  1678 + self.level = parser.GetImageWindowLevel()
  1679 + self.window = parser.GetImageWindowWidth()
  1680 + self.position = parser.GetImagePosition()
  1681 + self.number = parser.GetImageNumber()
  1682 + self.spacing = spacing = parser.GetPixelSpacing()
  1683 + self.orientation_label = parser.GetImageOrientationLabel()
  1684 + self.file = parser.filename
  1685 +
  1686 + if (parser.GetImageThickness()):
  1687 + spacing.append(parser.GetImageThickness())
  1688 + else:
  1689 + spacing.append(1.5)
  1690 +
  1691 + spacing[0] = round(spacing[0],2)
  1692 + spacing[1] = round(spacing[1],2)
  1693 + spacing[2] = round(spacing[2],2)
  1694 +
  1695 + try:
  1696 + self.type = parser.GetImageType()[2]
  1697 + except(IndexError):
  1698 + self.type = None
... ...
invesalius/reader/dicom_grouper.py
... ... @@ -19,23 +19,23 @@
19 19 # detalhes.
20 20 #---------------------------------------------------------------------
21 21  
22   -import dicom as ivDicom
  22 +import dicom
23 23  
24   -class ivDicomGroups:
  24 +class DicomGroups:
25 25 """
26 26 It is possible to separate sets of a set
27 27 of files dicom.
28 28  
29 29 To use:
30 30 list_dicoms = [c:\a.dcm, c:\a1.gdcm]
31   - dicom_splitter = ivDicomGroups()
  31 + dicom_splitter = DicomGroups()
32 32 dicom_splitter.SetFileList(list_dicoms)
33 33 dicom_splitter.Update()
34 34 splitted = dicom_splitter.GetOutput()
35 35 """
36 36  
37 37 def __init__(self):
38   - self.parser = ivDicom.Parser()
  38 + self.parser = dicom.Parser()
39 39 #List of DICOM from Directory
40 40 self.filenamelist = []
41 41 # List of DICOM with Informations
... ... @@ -62,12 +62,13 @@ class ivDicomGroups:
62 62 self.__Split1()
63 63  
64 64 if (len(self.GetOutput().keys()) == len(self.filenamelist)):
  65 + print "Vai entrar na 2"
65 66 self.__Split2()
66 67 self.split_type = 1
67 68  
68 69 self.__Split3()
69 70  
70   - self.__Info()
  71 + self.__UpdateZSpacing()
71 72  
72 73  
73 74 def GetOutput(self):
... ... @@ -103,45 +104,13 @@ class ivDicomGroups:
103 104 if not parser.SetFileName(filenamelist[x]):
104 105 return None
105 106  
106   - file = filenamelist[x]
107   - patient_name = parser.GetPatientName()
108   - serie_number = parser.GetImageSeriesNumber()
109   -
110   - image_position = parser.GetImagePosition()
111   - image_number = parser.GetImageNumber()
112   -
113   - try:
114   - image_type = parser.GetImageType()[2]
115   - except(IndexError):
116   - image_type = None
117   -
118   - patient_position = parser.GetImagePatientOrientation()
119   - image_orientation_label = parser.GetImageOrientationLabel()
120   - series_description = parser.GetSeriesDescrition()
121   - spacing = parser.GetPixelSpacing()
122   - id_study = parser.GetStudyID()
123   - tilt = parser.GetAcquisitionGantryTilt()
124   - window = parser.GetImageWindowWidth()
125   - level = parser.GetImageWindowLevel()
126   -
127   - if (parser.GetImageThickness()):
128   - spacing.append(parser.GetImageThickness())
129   - else:
130   - spacing.append(1.5)
131   -
132   - spacing[0] = round(spacing[0],2)
133   - spacing[1] = round(spacing[1],2)
134   - spacing[2] = round(spacing[2],2)
135   -
136   - filelist.append([image_number, serie_number, spacing,
137   - image_position, patient_position,
138   - image_type,patient_name,
139   - image_orientation_label, file,
140   - series_description, id_study, tilt,
141   - window, level])
  107 + information = dicom.Dicom()
  108 + information.SetParser(parser)
142 109  
  110 + self.filelist.append(information)
143 111 self.filelist = filelist
144   -
  112 +
  113 +
145 114 def __GetInformations(self, ind):
146 115 """
147 116 Return a list referring to a specific DICOM
... ... @@ -149,32 +118,7 @@ class ivDicomGroups:
149 118 to pass only the index
150 119 """
151 120 filelist = self.filelist
152   -
153   - self.filelist.sort()
154   -
155   - image_number = filelist[ind][0]
156   - serie_number = filelist[ind][1]
157   - spacing = filelist[ind][2]
158   - image_position = filelist[ind][3]
159   - patient_position = filelist[ind][4]
160   - image_type = filelist[ind][5]
161   - patient_name = filelist[ind][6]
162   - image_orientation_label = filelist[ind][7]
163   - file = filelist[ind][8]
164   - series_description = filelist[ind][9]
165   - id_study = filelist[ind][10]
166   - tilt = filelist[ind][11]
167   - window = filelist[ind][12]
168   - level = filelist[ind][13]
169   -
170   - list = [image_number, serie_number, spacing,
171   - image_position, patient_position,
172   - image_type,patient_name,
173   - image_orientation_label, file,
174   - series_description, id_study, tilt,
175   - window, level]
176   -
177   - return list
  121 + return filelist[ind]
178 122  
179 123  
180 124 def __Split1(self):
... ... @@ -185,22 +129,16 @@ class ivDicomGroups:
185 129 """
186 130 groups_dcm = self.groups_dcm
187 131  
188   - for x in xrange(len(self.filelist)):
189   -
190   - list = self.__GetInformations(x)
  132 + for x in xrange(len(self.filelist)):
  133 + information = self.__GetInformations(x)
191 134  
192   - patient_name = list[6]
193   - serie_number = list[1]
194   - id_study = list[10]
195   - image_orientation_label = list[7]
196   -
197   - key = (patient_name, id_study, serie_number,
198   - image_orientation_label)
  135 + key = (information.patient.name, information.acquisition.id_study,\
  136 + information.acquisition.serie_number, information.image.orientation_label)
199 137  
200 138 if (key in groups_dcm.keys()):
201   - groups_dcm[key][0].append(list)
  139 + groups_dcm[key][0].append(information)
202 140 else:
203   - groups_dcm[key] = [[list]]
  141 + groups_dcm[key] = [[information]]
204 142  
205 143 self.groups_dcm = groups_dcm
206 144  
... ... @@ -219,17 +157,8 @@ class ivDicomGroups:
219 157 #Through in the series (index of the dictionary)
220 158 for x in xrange(len(self.filelist)):
221 159 #Slices through in the serie
222   - list = self.__GetInformations(x)
223   -
224   - spacing = list[2]
225   - image_position = list[3]
226   - image_orientation_label = list[7]
227   - patient_name = list[6]
228   - serie_number = list[1]
229   - id_study = list[10]
230   - image_orientation_label = list[7]
231   -
232   - key = (patient_name, cont_series)
  160 + information = self.__GetInformations(x)
  161 + key = (information.patient.name, cont_series)
233 162  
234 163 #If there exists only one slice.
235 164 if (len(self.filelist) > 1):
... ... @@ -262,19 +191,19 @@ class ivDicomGroups:
262 191 #only adds value. Otherwise this key create in the
263 192 #dictionary and add value
264 193 if (key in groups_dcm.keys()):
265   - groups_dcm[key][0].append(list)
  194 + groups_dcm[key][0].append(information)
266 195 else:
267   - groups_dcm[key] = [[list]]
  196 + groups_dcm[key] = [[information]]
268 197 else:
269 198 cont_series = cont_series + 1
270   - groups_dcm[key] = [[list]]
  199 + groups_dcm[key] = [[information]]
271 200  
272 201 else:
273 202  
274 203 if (cont_series in groups_dcm.keys()):
275   - groups_dcm[key].append(list)
  204 + groups_dcm[key].append(information)
276 205 else:
277   - groups_dcm[key] = [[list]]
  206 + groups_dcm[key] = [[information]]
278 207  
279 208 cont_series = cont_series + 1
280 209  
... ... @@ -305,39 +234,21 @@ class ivDicomGroups:
305 234 for y in xrange(size_list):
306 235  
307 236 #Slices walks in the series
308   - image_pos = groups_dcm[key][0][y][3]
309   - image_number = groups_dcm[key][0][y][0]
310   - serie_number = groups_dcm[key][0][y][1]
311   - spacing = groups_dcm[key][0][y][2]
312   - image_position = groups_dcm[key][0][y][3]
313   - patient_position = groups_dcm[key][0][y][4]
314   - image_type = groups_dcm[key][0][y][5]
315   - patient_name = groups_dcm[key][0][y][6]
316   - image_orientation_label = groups_dcm[key][0][y][7]
317   - file = groups_dcm[key][0][y][8]
318   - series_description = groups_dcm[key][0][y][9]
319   - id_study = groups_dcm[key][0][y][10]
320   - tilt = groups_dcm[key][0][y][11]
321   - window = groups_dcm[key][0][y][12]
322   - level = groups_dcm[key][0][y][13]
  237 + information = groups_dcm[key][0][y]
323 238  
324 239 #Generate new key to dictionary
  240 + image_pos = information.image.position
325 241 key_ = (image_pos[0], image_pos[1], image_pos[2])
326 242  
327 243 #Add informations in the list
328   - list = [image_number, serie_number, spacing,
329   - image_position, patient_position,
330   - image_type,patient_name,
331   - image_orientation_label, file,
332   - series_description, id_study, tilt,
333   - window, level]
  244 + list = [information]
334 245  
335 246 #If list Null, create dictionary
336 247 #and add list with information
337 248 #after add in a temporary list
338 249 if (tmp_list == []):
339 250 tmp = {}
340   - tmp[key_] = list
  251 + tmp[key_] = information
341 252 tmp_list.append(tmp)
342 253  
343 254 else:
... ... @@ -353,13 +264,13 @@ class ivDicomGroups:
353 264 #dictionary
354 265  
355 266 if not (key_ in (tmp_list[a]).keys()):
356   - (tmp_list[a])[key_] = list
  267 + (tmp_list[a])[key_] = information
357 268 flag = 1
358 269 a = a + 1
359 270  
360 271 if (flag == 0):
361 272 tmp = {}
362   - tmp[key_] = list
  273 + tmp[key_] = information
363 274  
364 275 tmp_list.append(tmp)
365 276  
... ... @@ -375,37 +286,16 @@ class ivDicomGroups:
375 286 for m in xrange(len(tmp1.keys())):
376 287  
377 288 key = tmp1.keys()[m]
  289 + information = tmp1[key]
  290 + new_key = (information.patient.name, None, x, information.image.orientation_label)
378 291  
379   - image_pos = tmp1[key][3]
380   - image_number = tmp1[key][0]
381   - serie_number = tmp1[key][1]
382   - spacing = tmp1[key][2]
383   - image_position = tmp1[key][3]
384   - patient_position = tmp1[key][4]
385   - image_type = tmp1[key][5]
386   - patient_name = tmp1[key][6]
387   - image_orientation_label = tmp1[key][7]
388   - file = tmp1[key][8]
389   - series_description = tmp1[key][9]
390   - id_study = tmp1[key][10]
391   - tilt = tmp1[key][11]
392   - window = tmp1[key][12]
393   - level = tmp1[key][13]
394   -
395   - new_key = (patient_name, None, x, image_orientation_label)
396   -
397   -
398   - list = [image_number, serie_number, spacing,
399   - image_position, patient_position,
400   - image_type,patient_name,
401   - image_orientation_label, file,
402   - series_description, id_study, tilt,
403   - window, level]
  292 +
  293 + list = [information]
404 294  
405 295 if (new_key in groups_dcm_.keys()):
406   - groups_dcm_[new_key][0].append(list)
  296 + groups_dcm_[new_key][0].append(information)
407 297 else:
408   - groups_dcm_[new_key] = [[list]]
  298 + groups_dcm_[new_key] = [[information]]
409 299  
410 300 for j in xrange(len(groups_dcm_.keys())):
411 301 key = groups_dcm_.keys()[j]
... ... @@ -417,97 +307,30 @@ class ivDicomGroups:
417 307 self.groups_dcm = groups_dcm_
418 308  
419 309  
420   - def __Info(self):
  310 + def __UpdateZSpacing(self):
421 311 """
422   - This INFO is used in InVesalius 2 to learn the
423   - characteristics of the test. Add a list at the end
424   - of each series.
  312 + Calculate Z spacing from slices
425 313 """
426   - INFO_KEYS = \
427   - ['AcquisitionDate',
428   - 'AcquisitionGantryTilt',
429   - 'AcquisitionModality',
430   - 'AcquisitionNumber',
431   - 'AcquisionSequence',
432   - 'AcquisitionTime',
433   - 'EquipmentKVP',
434   - 'EquipmentInstitutionName',
435   - 'EquipmentManufacturer',
436   - 'EquipmentXRayTubeCurrent',
437   - 'ImageColumnOrientation',
438   - 'ImageConvolutionKernel',
439   - 'ImageDataType',
440   - 'ImageLocation',
441   - 'ImageNumber',
442   - 'ImagePixelSpacingX',
443   - 'ImagePixelSpacingY',
444   - 'ImagePosition',
445   - 'ImageRowOrientation',
446   - 'ImageSamplesPerPixel',
447   - 'ImageSeriesNumber',
448   - 'ImageThickness',
449   - 'ImageWindowLevel',
450   - 'ImageWindowWidth',
451   - 'PatientAge',
452   - 'PatientBirthDate',
453   - 'PatientGender',
454   - 'PatientName',
455   - 'PhysicianName',
456   - 'StudyID',
457   - 'StudyInstanceUID',
458   - 'StudyAdmittingDiagnosis',
459   - ]
460 314  
461 315 for x in xrange(len(self.groups_dcm.keys())):
462 316  
463 317 key = self.groups_dcm.keys()[x]
464   -
465   - file = self.groups_dcm[key][0][0][8]
466   -
467   - self.parser.SetFileName(file)
468   -
469   - acquisition_date = self.parser.GetAcquisitionDate()
470   - acquisition_gantry_tilt = self.parser.GetAcquisitionGantryTilt()
471   - acquisition_number = self.parser.GetAcquisitionNumber()
472   - acquision_sequence = self.parser.GetAcquisionSequence()
473   - acquisition_time = self.parser.GetAcquisitionTime()
474   - equipment_kvp = self.parser.GetEquipmentKVP()
475   - equipment_institution_name = self.parser.GetEquipmentInstitutionName()
476   - equipment_manufacturer = self.parser.GetEquipmentManufacturer()
477   - equipmentxraytubecurrent = self.parser.GetEquipmentXRayTubeCurrent()
478   - image_column_orientation = self.parser.GetImageColumnOrientation()
479   - image_convolution_kernel = self.parser.GetImageConvolutionKernel()
480   - image_data_type = self.parser.GetImageDataType()
481   - image_location = self.parser.GetImageLocation()
482   - image_number = self.parser.GetImageNumber()
483   - image_pixel_spacing_x = self.parser.GetImagePixelSpacingX()
484   - image_pixel_spacing_y = self.parser.GetImagePixelSpacingY()
485   - image_position = self.parser.GetImagePosition()
486   - image_row_orientation = self.parser.GetImageRowOrientation()
487   - image_samples_perpixel = self.parser.GetImageSamplesPerPixel()
488   - image_series_number = self.parser.GetImageSeriesNumber()
489   - image_thickness = self.parser.GetImageThickness()
490   - image_window_level = self.parser.GetImageWindowLevel()
491   - image_windowWidth = self.parser.GetImageWindowWidth()
492   - patient_age = self.parser.GetPatientAge()
493   - patient_birth_date = self.parser.GetPatientBirthDate()
494   - patient_gender = self.parser.GetPatientGender()
495   - patient_name = self.parser.GetPatientName()
496   - study_id = self.parser.GetStudyID()
497   - study_instance_UID = self.parser.GetStudyInstanceUID()
498   - study_admitting_diagnosis = self.parser.GetStudyAdmittingDiagnosis()
499   - image_dimension = (self.parser.GetDimensionX(), self.parser.GetDimensionY())
500   -
501   - if (len(self.groups_dcm[key][0]) > 1 ):
  318 + information = self.groups_dcm[key][0]
  319 +
  320 + if (len(self.groups_dcm[key][0]) > 1):
502 321 #Catch a slice of middle and the next to find the spacing.
503 322 center = len(self.groups_dcm[key][0])/2
504 323 if (center == 1):
505   - center = 0
506   - current_position = self.groups_dcm[key][0][center][3]
507   - next_position = self.groups_dcm[key][0][center + 1][3]
  324 + center = 0
  325 +
  326 + information = self.groups_dcm[key][0][center]
  327 + current_position = information.image.position
  328 +
  329 + information = self.groups_dcm[key][0][center + 1]
  330 + next_position = information.image.position
508 331  
509 332 try:
510   - image_orientation_label = self.groups_dcm.keys()[x][3]
  333 + image_orientation_label = self.groups_dcm.keys()[3]
511 334 except(IndexError):
512 335 image_orientation_label = None
513 336  
... ... @@ -522,17 +345,6 @@ class ivDicomGroups:
522 345  
523 346 else:
524 347 spacing = None
525   -
526   - info = [acquisition_date, acquisition_gantry_tilt, acquisition_number,
527   - acquision_sequence, acquisition_time, equipment_kvp,
528   - equipment_institution_name, equipment_manufacturer,
529   - equipmentxraytubecurrent, image_column_orientation,
530   - image_convolution_kernel, image_data_type, image_location,
531   - image_number, image_pixel_spacing_x, image_pixel_spacing_y,
532   - image_position, image_row_orientation, image_samples_perpixel,
533   - image_series_number, image_thickness, image_window_level,
534   - image_windowWidth,patient_age, patient_birth_date,patient_gender,
535   - patient_name, study_id, study_instance_UID, study_admitting_diagnosis,
536   - spacing, image_dimension]
537   -
538   - self.groups_dcm[key].append(info)
  348 +
  349 + for information in self.groups_dcm[key][0]:
  350 + information.image.spacing[2] = spacing
... ...
invesalius/reader/dicom_reader.py
... ... @@ -39,7 +39,7 @@ def LoadImages(dir_):
39 39  
40 40 dcm_files, acquisition_modality = GetDicomFiles(dir_)
41 41  
42   - dcm_series = dicom_grouper.ivDicomGroups()
  42 + dcm_series = dicom_grouper.DicomGroups()
43 43 dcm_series.SetFileList(dcm_files)
44 44 dcm_series.Update()
45 45  
... ... @@ -52,8 +52,8 @@ def LoadImages(dir_):
52 52 key = groups.keys()[x]
53 53  
54 54 for y in xrange(len(groups[key][0])):
55   -
56   - file = groups[key][0][y][8]
  55 + dicom = groups[key][0][y]
  56 + file = dicom.image.file
57 57 tmp_list.append(file)
58 58  
59 59 list_files.append([len(tmp_list), key])
... ... @@ -66,19 +66,22 @@ def LoadImages(dir_):
66 66  
67 67 file_list = []
68 68 for x in xrange(len(groups[key][0])):
69   - file_list.append(groups[key][0][x][8])
70   -
71   - tilt = groups[key][0][x][11]
72   - spacing = groups[key][1][14]
73   - spacing_z = groups[key][1][30]
74   - orientation = groups[key][0][x][7]
75   - window = groups[key][0][x][12]
76   - level = groups[key][0][x][13]
77   -
  69 + dicom = groups[key][0][x]
  70 + file_list.append(dicom.image.file)
  71 +
  72 + information = groups[key][0][x]
  73 +
  74 + tilt = dicom.acquisition.tilt#groups[key][0][x][11]
  75 + spacing = dicom.image.spacing#groups[key][1][14]
  76 + #spacing_z = #groups[key][1][30]
  77 + orientation = dicom.image.orientation_label#groups[key][0][x][7]
  78 + window = dicom.image.window#groups[key][0][x][12]
  79 + level = dicom.image.level#groups[key][0][x][13]
78 80  
79 81 files = file_list
  82 + print dicom.image.orientation_label
80 83 #Coronal Crash. necessary verify
81   - if (orientation <> "CORONAL"):
  84 + if (dicom.image.orientation_label <> "CORONAL"):
82 85 #Organize reversed image
83 86 sorter = gdcm.IPPSorter()
84 87 sorter.SetComputeZSpacing(True)
... ... @@ -100,9 +103,9 @@ def LoadImages(dir_):
100 103 read.SetFileNames(array)
101 104 read.Update()
102 105  
103   - img_axial = vtk.vtkImageData()
104   - img_axial.DeepCopy(read.GetOutput())
105   - img_axial.SetSpacing(spacing, spacing, spacing_z)
  106 + image_data = vtk.vtkImageData()
  107 + image_data.DeepCopy(read.GetOutput())
  108 + image_data.SetSpacing(spacing, spacing, spacing_z)
106 109 else:
107 110 for x in xrange(len(files)):
108 111 #SIf the resolution of the
... ... @@ -118,16 +121,15 @@ def LoadImages(dir_):
118 121 img_app.AddInput(img)
119 122 img_app.Update()
120 123  
121   - img_axial = vtk.vtkImageData()
122   - img_axial.DeepCopy(img_app.GetOutput())
123   - img_axial.SetSpacing(img_axial.GetSpacing()[0],\
124   - img_axial.GetSpacing()[1],\
125   - spacing_z)
126   -
  124 + image_data = vtk.vtkImageData()
  125 + image_data.DeepCopy(img_app.GetOutput())
  126 + image_data.SetSpacing(image_data.GetSpacing()[0],\
  127 + image_data.GetSpacing()[1],\
  128 + dicom.image.spacing[2])
127 129  
128   - img_axial.Update()
  130 + image_data.Update()
129 131  
130   - return img_axial, acquisition_modality, tilt, orientation, window, level
  132 + return image_data, acquisition_modality, tilt, orientation, window, level
131 133  
132 134 def GetDicomFiles(path, recursive = False):
133 135 # TODO!!! SUPER GAMBIARRA!!! DO THIS BETTER
... ...