diff --git a/invesalius/reader/dicom_grouper.py b/invesalius/reader/dicom_grouper.py index 1e21ef5..ddda602 100644 --- a/invesalius/reader/dicom_grouper.py +++ b/invesalius/reader/dicom_grouper.py @@ -51,7 +51,7 @@ # and # were swapped -ORIENT_MAP = {"SAGITTAL":0, "AXIAL":1, "CORONAL":2} +ORIENT_MAP = {"SAGITTAL":0, "CORONAL":1, "AXIAL":2} class DicomGroup: @@ -65,10 +65,12 @@ class DicomGroup: # IDEA (13/10): Represent internally as dictionary, # externally as list self.nslices = 0 - + self.spacing = 0 + def AddSlice(self, dicom): - pos = dicom.image.position + pos = tuple(dicom.image.position) if pos not in self.slices_dict.keys(): + self.slices_dict[pos] = dicom self.nslices += 1 return True @@ -84,12 +86,27 @@ class DicomGroup: def GetSortedList(self): # This will be used to fix problem 1, after merging # single DicomGroups of same study_id and orientation - list_ = self.slices_dict_values() - axis = ORIENT_MAP[self.key[3]] + list_ = self.slices_dict.values() + dicom = list_[0] + axis = ORIENT_MAP[dicom.image.orientation_label] list_ = sorted(list_, key = lambda dicom:dicom.image.position[axis]) return list_ - + def GetSpacing(self): + list_ = self.GetSortedList() + if (len(list_) > 1): + dicom = list_[0] + axis = ORIENT_MAP[dicom.image.orientation_label] + #print dicom.image.orientation_label + p1 = dicom.image.position[axis] + + dicom = list_[1] + p2 = dicom.image.position[axis] + + self.spacing = abs(p1 - p2) + else: + self.spacing = 1 + class PatientGroup: def __init__(self): # key: @@ -109,7 +126,7 @@ class PatientGroup: # implemented, dinamically during new dicom's addition group_key = (dicom.patient.name, dicom.acquisition.id_study, - dicom.acquisition.series_number, + dicom.acquisition.serie_number, dicom.image.orientation_label, index) # This will be used to deal with Problem 2 @@ -120,13 +137,15 @@ class PatientGroup: self.groups_dict[group_key] = group # Group exists... Lets try to add slice else: - group = self.groups_dicom[group_key] + group = self.groups_dict[group_key] slice_added = group.AddSlice(dicom) if not slice_added: # If we're here, then Problem 2 occured # TODO: Optimize recursion self.AddFile(file_path, index+1) - + + group.GetSpacing() + def Update(self): # Ideally, AddFile would be sufficient for splitting DICOM # files into groups (series). However, this does not work for @@ -144,7 +163,7 @@ class PatientGroup: # Fix Problem 1 if is_there_problem_1: self.groups_dict = self.FixProblem1(self.groups_dict) - + def GetGroups(self): return self.groups_dict.values() @@ -231,18 +250,18 @@ class DicomPatientGrouper: # ... (repeat to all files on folder) # grouper.Update() # groups = GetPatientGroups() - + def __init__(self): self.patients_dict = {} - + def AddFile(self, dicom): patient_key = (dicom.patient.name, dicom.patient.id) - + # Does this patient exist? if patient_key not in self.patients_dict.keys(): patient = PatientGroup() - patient.AddSlice(dicom) + patient.AddFile(dicom) self.patients_dict[patient_key] = patient # Patient exists... Lets add group to it else: @@ -252,7 +271,7 @@ class DicomPatientGrouper: def Update(self): for patient in self.patients_dict.values(): patient.Update() - + def GetPatientsGroups(self): """ How to use: diff --git a/invesalius/reader/dicom_reader.py b/invesalius/reader/dicom_reader.py index 11f61a9..4c3f960 100644 --- a/invesalius/reader/dicom_reader.py +++ b/invesalius/reader/dicom_reader.py @@ -31,41 +31,22 @@ from data.imagedata_utils import ResampleImage2D def LoadImages(dir_): # TODO!!! SUPER GAMBIARRA!!! DO THIS BETTER - dcm_files = GetDicomFiles(dir_) - - dcm_series = dicom_grouper.DicomGroups() - dcm_series.SetFileList(dcm_files) - dcm_series.Update() - - groups = dcm_series.GetOutput() - - tmp_list = [] - list_files = [] - - for x in xrange(len(groups.keys())): - key = groups.keys()[x] - - for y in xrange(len(groups[key])): - dicom = groups[key][y] - file = dicom.image.file - tmp_list.append(file) - - list_files.append([len(tmp_list), key]) - tmp_list = [] - - if list_files: - key = max(list_files)[1] - else: - return None - + patient_group = GetDicomFiles(dir_) + + for patient in patient_group: + group_list = patient.GetGroups() + for group in group_list: + d = group.GetList() + spacing = group.spacing + + #dcm_series = dicom_grouper.DicomGroup() + #dcm_series.SetFileList(dcm_files) + #dcm_series.Update() + file_list = [] - for x in xrange(len(groups[key])): - dicom = groups[key][x] + for dicom in d: file_list.append(dicom.image.file) - files = file_list - spacing = dicom.image.spacing - #Coronal Crash. necessary verify if (dicom.image.orientation_label <> "CORONAL"): #Organize reversed image @@ -92,7 +73,7 @@ def LoadImages(dir_): image_data = vtk.vtkImageData() image_data.DeepCopy(read.GetOutput()) - spacing = dicom.image.spacing + image_data.SetSpacing(spacing) else: for x in xrange(len(files)): @@ -112,8 +93,7 @@ def LoadImages(dir_): image_data = vtk.vtkImageData() image_data.DeepCopy(img_app.GetOutput()) image_data.SetSpacing(image_data.GetSpacing()[0],\ - image_data.GetSpacing()[1],\ - dicom.image.spacing[2]) + image_data.GetSpacing()[1], spacing) image_data.Update() @@ -171,19 +151,16 @@ def GetDicomFiles(path): # FIXME: Currently recursion is not working # Recursivelly, find all files inside this folder - file_list = [] + grouper = dicom_grouper.DicomPatientGrouper() for p in list_paths: p_file_list = p[-1] file_path = p[0] for filename in p_file_list: - file_list.append(str(os.path.join(file_path,filename))) - - # Check if given file is in DICOM format - dicom_files = [] - for file in file_list: - read_dicom = dicom.Parser() - if (read_dicom.SetFileName(file)): - dicom_files.append(file) - - - return dicom_files + file = str(os.path.join(file_path,filename)) + parser = dicom.Parser() + if (parser.SetFileName(file)): + dcm = dicom.Dicom() + dcm.SetParser(parser) + grouper.AddFile(dcm) + + return grouper.GetPatientsGroups() -- libgit2 0.21.2