Commit 75348ea4ec0a625f865d08024aa18e655c755741

Authored by Paulo Henrique Junqueira Amorim
1 parent fb59d2bd

ADD: Part of the implementation new DICOM Class

invesalius/reader/dicom_grouper.py
@@ -51,7 +51,7 @@ @@ -51,7 +51,7 @@
51 # <dicom.image.number> and <dicom.acquisition.series_number> 51 # <dicom.image.number> and <dicom.acquisition.series_number>
52 # were swapped 52 # were swapped
53 53
54 -ORIENT_MAP = {"SAGITTAL":0, "AXIAL":1, "CORONAL":2} 54 +ORIENT_MAP = {"SAGITTAL":0, "CORONAL":1, "AXIAL":2}
55 55
56 56
57 class DicomGroup: 57 class DicomGroup:
@@ -65,10 +65,12 @@ class DicomGroup: @@ -65,10 +65,12 @@ class DicomGroup:
65 # IDEA (13/10): Represent internally as dictionary, 65 # IDEA (13/10): Represent internally as dictionary,
66 # externally as list 66 # externally as list
67 self.nslices = 0 67 self.nslices = 0
68 - 68 + self.spacing = 0
  69 +
69 def AddSlice(self, dicom): 70 def AddSlice(self, dicom):
70 - pos = dicom.image.position 71 + pos = tuple(dicom.image.position)
71 if pos not in self.slices_dict.keys(): 72 if pos not in self.slices_dict.keys():
  73 +
72 self.slices_dict[pos] = dicom 74 self.slices_dict[pos] = dicom
73 self.nslices += 1 75 self.nslices += 1
74 return True 76 return True
@@ -84,12 +86,27 @@ class DicomGroup: @@ -84,12 +86,27 @@ class DicomGroup:
84 def GetSortedList(self): 86 def GetSortedList(self):
85 # This will be used to fix problem 1, after merging 87 # This will be used to fix problem 1, after merging
86 # single DicomGroups of same study_id and orientation 88 # single DicomGroups of same study_id and orientation
87 - list_ = self.slices_dict_values()  
88 - axis = ORIENT_MAP[self.key[3]] 89 + list_ = self.slices_dict.values()
  90 + dicom = list_[0]
  91 + axis = ORIENT_MAP[dicom.image.orientation_label]
89 list_ = sorted(list_, key = lambda dicom:dicom.image.position[axis]) 92 list_ = sorted(list_, key = lambda dicom:dicom.image.position[axis])
90 return list_ 93 return list_
91 94
92 - 95 + def GetSpacing(self):
  96 + list_ = self.GetSortedList()
  97 + if (len(list_) > 1):
  98 + dicom = list_[0]
  99 + axis = ORIENT_MAP[dicom.image.orientation_label]
  100 + #print dicom.image.orientation_label
  101 + p1 = dicom.image.position[axis]
  102 +
  103 + dicom = list_[1]
  104 + p2 = dicom.image.position[axis]
  105 +
  106 + self.spacing = abs(p1 - p2)
  107 + else:
  108 + self.spacing = 1
  109 +
93 class PatientGroup: 110 class PatientGroup:
94 def __init__(self): 111 def __init__(self):
95 # key: 112 # key:
@@ -109,7 +126,7 @@ class PatientGroup: @@ -109,7 +126,7 @@ class PatientGroup:
109 # implemented, dinamically during new dicom's addition 126 # implemented, dinamically during new dicom's addition
110 group_key = (dicom.patient.name, 127 group_key = (dicom.patient.name,
111 dicom.acquisition.id_study, 128 dicom.acquisition.id_study,
112 - dicom.acquisition.series_number, 129 + dicom.acquisition.serie_number,
113 dicom.image.orientation_label, 130 dicom.image.orientation_label,
114 index) # This will be used to deal with Problem 2 131 index) # This will be used to deal with Problem 2
115 132
@@ -120,13 +137,15 @@ class PatientGroup: @@ -120,13 +137,15 @@ class PatientGroup:
120 self.groups_dict[group_key] = group 137 self.groups_dict[group_key] = group
121 # Group exists... Lets try to add slice 138 # Group exists... Lets try to add slice
122 else: 139 else:
123 - group = self.groups_dicom[group_key] 140 + group = self.groups_dict[group_key]
124 slice_added = group.AddSlice(dicom) 141 slice_added = group.AddSlice(dicom)
125 if not slice_added: 142 if not slice_added:
126 # If we're here, then Problem 2 occured 143 # If we're here, then Problem 2 occured
127 # TODO: Optimize recursion 144 # TODO: Optimize recursion
128 self.AddFile(file_path, index+1) 145 self.AddFile(file_path, index+1)
129 - 146 +
  147 + group.GetSpacing()
  148 +
130 def Update(self): 149 def Update(self):
131 # Ideally, AddFile would be sufficient for splitting DICOM 150 # Ideally, AddFile would be sufficient for splitting DICOM
132 # files into groups (series). However, this does not work for 151 # files into groups (series). However, this does not work for
@@ -144,7 +163,7 @@ class PatientGroup: @@ -144,7 +163,7 @@ class PatientGroup:
144 # Fix Problem 1 163 # Fix Problem 1
145 if is_there_problem_1: 164 if is_there_problem_1:
146 self.groups_dict = self.FixProblem1(self.groups_dict) 165 self.groups_dict = self.FixProblem1(self.groups_dict)
147 - 166 +
148 def GetGroups(self): 167 def GetGroups(self):
149 return self.groups_dict.values() 168 return self.groups_dict.values()
150 169
@@ -231,18 +250,18 @@ class DicomPatientGrouper: @@ -231,18 +250,18 @@ class DicomPatientGrouper:
231 # ... (repeat to all files on folder) 250 # ... (repeat to all files on folder)
232 # grouper.Update() 251 # grouper.Update()
233 # groups = GetPatientGroups() 252 # groups = GetPatientGroups()
234 - 253 +
235 def __init__(self): 254 def __init__(self):
236 self.patients_dict = {} 255 self.patients_dict = {}
237 - 256 +
238 def AddFile(self, dicom): 257 def AddFile(self, dicom):
239 patient_key = (dicom.patient.name, 258 patient_key = (dicom.patient.name,
240 dicom.patient.id) 259 dicom.patient.id)
241 - 260 +
242 # Does this patient exist? 261 # Does this patient exist?
243 if patient_key not in self.patients_dict.keys(): 262 if patient_key not in self.patients_dict.keys():
244 patient = PatientGroup() 263 patient = PatientGroup()
245 - patient.AddSlice(dicom) 264 + patient.AddFile(dicom)
246 self.patients_dict[patient_key] = patient 265 self.patients_dict[patient_key] = patient
247 # Patient exists... Lets add group to it 266 # Patient exists... Lets add group to it
248 else: 267 else:
@@ -252,7 +271,7 @@ class DicomPatientGrouper: @@ -252,7 +271,7 @@ class DicomPatientGrouper:
252 def Update(self): 271 def Update(self):
253 for patient in self.patients_dict.values(): 272 for patient in self.patients_dict.values():
254 patient.Update() 273 patient.Update()
255 - 274 +
256 def GetPatientsGroups(self): 275 def GetPatientsGroups(self):
257 """ 276 """
258 How to use: 277 How to use:
invesalius/reader/dicom_reader.py
@@ -31,41 +31,22 @@ from data.imagedata_utils import ResampleImage2D @@ -31,41 +31,22 @@ from data.imagedata_utils import ResampleImage2D
31 def LoadImages(dir_): 31 def LoadImages(dir_):
32 # TODO!!! SUPER GAMBIARRA!!! DO THIS BETTER 32 # TODO!!! SUPER GAMBIARRA!!! DO THIS BETTER
33 33
34 - dcm_files = GetDicomFiles(dir_)  
35 -  
36 - dcm_series = dicom_grouper.DicomGroups()  
37 - dcm_series.SetFileList(dcm_files)  
38 - dcm_series.Update()  
39 -  
40 - groups = dcm_series.GetOutput()  
41 -  
42 - tmp_list = []  
43 - list_files = []  
44 -  
45 - for x in xrange(len(groups.keys())):  
46 - key = groups.keys()[x]  
47 -  
48 - for y in xrange(len(groups[key])):  
49 - dicom = groups[key][y]  
50 - file = dicom.image.file  
51 - tmp_list.append(file)  
52 -  
53 - list_files.append([len(tmp_list), key])  
54 - tmp_list = []  
55 -  
56 - if list_files:  
57 - key = max(list_files)[1]  
58 - else:  
59 - return None  
60 - 34 + patient_group = GetDicomFiles(dir_)
  35 +
  36 + for patient in patient_group:
  37 + group_list = patient.GetGroups()
  38 + for group in group_list:
  39 + d = group.GetList()
  40 + spacing = group.spacing
  41 +
  42 + #dcm_series = dicom_grouper.DicomGroup()
  43 + #dcm_series.SetFileList(dcm_files)
  44 + #dcm_series.Update()
  45 +
61 file_list = [] 46 file_list = []
62 - for x in xrange(len(groups[key])):  
63 - dicom = groups[key][x] 47 + for dicom in d:
64 file_list.append(dicom.image.file) 48 file_list.append(dicom.image.file)
65 49
66 - files = file_list  
67 - spacing = dicom.image.spacing  
68 -  
69 #Coronal Crash. necessary verify 50 #Coronal Crash. necessary verify
70 if (dicom.image.orientation_label <> "CORONAL"): 51 if (dicom.image.orientation_label <> "CORONAL"):
71 #Organize reversed image 52 #Organize reversed image
@@ -92,7 +73,7 @@ def LoadImages(dir_): @@ -92,7 +73,7 @@ def LoadImages(dir_):
92 73
93 image_data = vtk.vtkImageData() 74 image_data = vtk.vtkImageData()
94 image_data.DeepCopy(read.GetOutput()) 75 image_data.DeepCopy(read.GetOutput())
95 - spacing = dicom.image.spacing 76 +
96 image_data.SetSpacing(spacing) 77 image_data.SetSpacing(spacing)
97 else: 78 else:
98 for x in xrange(len(files)): 79 for x in xrange(len(files)):
@@ -112,8 +93,7 @@ def LoadImages(dir_): @@ -112,8 +93,7 @@ def LoadImages(dir_):
112 image_data = vtk.vtkImageData() 93 image_data = vtk.vtkImageData()
113 image_data.DeepCopy(img_app.GetOutput()) 94 image_data.DeepCopy(img_app.GetOutput())
114 image_data.SetSpacing(image_data.GetSpacing()[0],\ 95 image_data.SetSpacing(image_data.GetSpacing()[0],\
115 - image_data.GetSpacing()[1],\  
116 - dicom.image.spacing[2]) 96 + image_data.GetSpacing()[1], spacing)
117 97
118 image_data.Update() 98 image_data.Update()
119 99
@@ -171,19 +151,16 @@ def GetDicomFiles(path): @@ -171,19 +151,16 @@ def GetDicomFiles(path):
171 151
172 # FIXME: Currently recursion is not working 152 # FIXME: Currently recursion is not working
173 # Recursivelly, find all files inside this folder 153 # Recursivelly, find all files inside this folder
174 - file_list = [] 154 + grouper = dicom_grouper.DicomPatientGrouper()
175 for p in list_paths: 155 for p in list_paths:
176 p_file_list = p[-1] 156 p_file_list = p[-1]
177 file_path = p[0] 157 file_path = p[0]
178 for filename in p_file_list: 158 for filename in p_file_list:
179 - file_list.append(str(os.path.join(file_path,filename)))  
180 -  
181 - # Check if given file is in DICOM format  
182 - dicom_files = []  
183 - for file in file_list:  
184 - read_dicom = dicom.Parser()  
185 - if (read_dicom.SetFileName(file)):  
186 - dicom_files.append(file)  
187 -  
188 -  
189 - return dicom_files 159 + file = str(os.path.join(file_path,filename))
  160 + parser = dicom.Parser()
  161 + if (parser.SetFileName(file)):
  162 + dcm = dicom.Dicom()
  163 + dcm.SetParser(parser)
  164 + grouper.AddFile(dcm)
  165 +
  166 + return grouper.GetPatientsGroups()