From b09b522f3091ec4ebd338deda75b4fa821a5c022 Mon Sep 17 00:00:00 2001 From: paulojamorim Date: Tue, 1 Sep 2009 14:48:30 +0000 Subject: [PATCH] ENH: Restructuring of the class group DICOM --- invesalius/reader/dicom.py | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ invesalius/reader/dicom_grouper.py | 294 +++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- invesalius/reader/dicom_reader.py | 50 ++++++++++++++++++++++++++------------------------ 3 files changed, 155 insertions(+), 265 deletions(-) diff --git a/invesalius/reader/dicom.py b/invesalius/reader/dicom.py index 1b9f225..1f02f5c 100644 --- a/invesalius/reader/dicom.py +++ b/invesalius/reader/dicom.py @@ -1620,3 +1620,79 @@ if __name__ == "__main__": #print info + + + +class Dicom(object): + def __init__(self): + pass + + def SetParser(self, parser): + self.parser = parser + + self.LoadImageInfo() + self.LoadPatientInfo() + self.LoadAcquisitionInfo() + #self.LoadStudyInfo() + + def LoadImageInfo(self): + self.image = Image() + self.image.SetParser(self.parser) + + def LoadPatientInfo(self): + self.patient = Patient() + self.patient.SetParser(self.parser) + + def LoadAcquisitionInfo(self): + self.acquisition = Acquisition() + self.acquisition.SetParser(self.parser) + + + +class Patient(object): + def __init__(self): + pass + + def SetParser(self, parser): + self.name = parser.GetPatientName() + self.physician = parser.GetPhysicianName() + +class Acquisition(object): + + def __init__(self): + pass + + def SetParser(self, parser): + self.patient_orientation = parser.GetImagePatientOrientation() + self.series_description = parser.GetSeriesDescrition() + self.tilt = parser.GetAcquisitionGantryTilt() + self.serie_number = parser.GetImageSeriesNumber() + self.id_study = parser.GetStudyID() + +class Image(object): + + def __init__(self): + pass + + def SetParser(self, parser): + self.level = parser.GetImageWindowLevel() + self.window = parser.GetImageWindowWidth() + self.position = parser.GetImagePosition() + self.number = parser.GetImageNumber() + self.spacing = spacing = parser.GetPixelSpacing() + self.orientation_label = parser.GetImageOrientationLabel() + self.file = parser.filename + + if (parser.GetImageThickness()): + spacing.append(parser.GetImageThickness()) + else: + spacing.append(1.5) + + spacing[0] = round(spacing[0],2) + spacing[1] = round(spacing[1],2) + spacing[2] = round(spacing[2],2) + + try: + self.type = parser.GetImageType()[2] + except(IndexError): + self.type = None diff --git a/invesalius/reader/dicom_grouper.py b/invesalius/reader/dicom_grouper.py index 0dd397b..cc40791 100644 --- a/invesalius/reader/dicom_grouper.py +++ b/invesalius/reader/dicom_grouper.py @@ -19,23 +19,23 @@ # detalhes. #--------------------------------------------------------------------- -import dicom as ivDicom +import dicom -class ivDicomGroups: +class DicomGroups: """ It is possible to separate sets of a set of files dicom. To use: list_dicoms = [c:\a.dcm, c:\a1.gdcm] - dicom_splitter = ivDicomGroups() + dicom_splitter = DicomGroups() dicom_splitter.SetFileList(list_dicoms) dicom_splitter.Update() splitted = dicom_splitter.GetOutput() """ def __init__(self): - self.parser = ivDicom.Parser() + self.parser = dicom.Parser() #List of DICOM from Directory self.filenamelist = [] # List of DICOM with Informations @@ -62,12 +62,13 @@ class ivDicomGroups: self.__Split1() if (len(self.GetOutput().keys()) == len(self.filenamelist)): + print "Vai entrar na 2" self.__Split2() self.split_type = 1 self.__Split3() - self.__Info() + self.__UpdateZSpacing() def GetOutput(self): @@ -103,45 +104,13 @@ class ivDicomGroups: if not parser.SetFileName(filenamelist[x]): return None - file = filenamelist[x] - patient_name = parser.GetPatientName() - serie_number = parser.GetImageSeriesNumber() - - image_position = parser.GetImagePosition() - image_number = parser.GetImageNumber() - - try: - image_type = parser.GetImageType()[2] - except(IndexError): - image_type = None - - patient_position = parser.GetImagePatientOrientation() - image_orientation_label = parser.GetImageOrientationLabel() - series_description = parser.GetSeriesDescrition() - spacing = parser.GetPixelSpacing() - id_study = parser.GetStudyID() - tilt = parser.GetAcquisitionGantryTilt() - window = parser.GetImageWindowWidth() - level = parser.GetImageWindowLevel() - - if (parser.GetImageThickness()): - spacing.append(parser.GetImageThickness()) - else: - spacing.append(1.5) - - spacing[0] = round(spacing[0],2) - spacing[1] = round(spacing[1],2) - spacing[2] = round(spacing[2],2) - - filelist.append([image_number, serie_number, spacing, - image_position, patient_position, - image_type,patient_name, - image_orientation_label, file, - series_description, id_study, tilt, - window, level]) + information = dicom.Dicom() + information.SetParser(parser) + self.filelist.append(information) self.filelist = filelist - + + def __GetInformations(self, ind): """ Return a list referring to a specific DICOM @@ -149,32 +118,7 @@ class ivDicomGroups: to pass only the index """ filelist = self.filelist - - self.filelist.sort() - - image_number = filelist[ind][0] - serie_number = filelist[ind][1] - spacing = filelist[ind][2] - image_position = filelist[ind][3] - patient_position = filelist[ind][4] - image_type = filelist[ind][5] - patient_name = filelist[ind][6] - image_orientation_label = filelist[ind][7] - file = filelist[ind][8] - series_description = filelist[ind][9] - id_study = filelist[ind][10] - tilt = filelist[ind][11] - window = filelist[ind][12] - level = filelist[ind][13] - - list = [image_number, serie_number, spacing, - image_position, patient_position, - image_type,patient_name, - image_orientation_label, file, - series_description, id_study, tilt, - window, level] - - return list + return filelist[ind] def __Split1(self): @@ -185,22 +129,16 @@ class ivDicomGroups: """ groups_dcm = self.groups_dcm - for x in xrange(len(self.filelist)): - - list = self.__GetInformations(x) + for x in xrange(len(self.filelist)): + information = self.__GetInformations(x) - patient_name = list[6] - serie_number = list[1] - id_study = list[10] - image_orientation_label = list[7] - - key = (patient_name, id_study, serie_number, - image_orientation_label) + key = (information.patient.name, information.acquisition.id_study,\ + information.acquisition.serie_number, information.image.orientation_label) if (key in groups_dcm.keys()): - groups_dcm[key][0].append(list) + groups_dcm[key][0].append(information) else: - groups_dcm[key] = [[list]] + groups_dcm[key] = [[information]] self.groups_dcm = groups_dcm @@ -219,17 +157,8 @@ class ivDicomGroups: #Through in the series (index of the dictionary) for x in xrange(len(self.filelist)): #Slices through in the serie - list = self.__GetInformations(x) - - spacing = list[2] - image_position = list[3] - image_orientation_label = list[7] - patient_name = list[6] - serie_number = list[1] - id_study = list[10] - image_orientation_label = list[7] - - key = (patient_name, cont_series) + information = self.__GetInformations(x) + key = (information.patient.name, cont_series) #If there exists only one slice. if (len(self.filelist) > 1): @@ -262,19 +191,19 @@ class ivDicomGroups: #only adds value. Otherwise this key create in the #dictionary and add value if (key in groups_dcm.keys()): - groups_dcm[key][0].append(list) + groups_dcm[key][0].append(information) else: - groups_dcm[key] = [[list]] + groups_dcm[key] = [[information]] else: cont_series = cont_series + 1 - groups_dcm[key] = [[list]] + groups_dcm[key] = [[information]] else: if (cont_series in groups_dcm.keys()): - groups_dcm[key].append(list) + groups_dcm[key].append(information) else: - groups_dcm[key] = [[list]] + groups_dcm[key] = [[information]] cont_series = cont_series + 1 @@ -305,39 +234,21 @@ class ivDicomGroups: for y in xrange(size_list): #Slices walks in the series - image_pos = groups_dcm[key][0][y][3] - image_number = groups_dcm[key][0][y][0] - serie_number = groups_dcm[key][0][y][1] - spacing = groups_dcm[key][0][y][2] - image_position = groups_dcm[key][0][y][3] - patient_position = groups_dcm[key][0][y][4] - image_type = groups_dcm[key][0][y][5] - patient_name = groups_dcm[key][0][y][6] - image_orientation_label = groups_dcm[key][0][y][7] - file = groups_dcm[key][0][y][8] - series_description = groups_dcm[key][0][y][9] - id_study = groups_dcm[key][0][y][10] - tilt = groups_dcm[key][0][y][11] - window = groups_dcm[key][0][y][12] - level = groups_dcm[key][0][y][13] + information = groups_dcm[key][0][y] #Generate new key to dictionary + image_pos = information.image.position key_ = (image_pos[0], image_pos[1], image_pos[2]) #Add informations in the list - list = [image_number, serie_number, spacing, - image_position, patient_position, - image_type,patient_name, - image_orientation_label, file, - series_description, id_study, tilt, - window, level] + list = [information] #If list Null, create dictionary #and add list with information #after add in a temporary list if (tmp_list == []): tmp = {} - tmp[key_] = list + tmp[key_] = information tmp_list.append(tmp) else: @@ -353,13 +264,13 @@ class ivDicomGroups: #dictionary if not (key_ in (tmp_list[a]).keys()): - (tmp_list[a])[key_] = list + (tmp_list[a])[key_] = information flag = 1 a = a + 1 if (flag == 0): tmp = {} - tmp[key_] = list + tmp[key_] = information tmp_list.append(tmp) @@ -375,37 +286,16 @@ class ivDicomGroups: for m in xrange(len(tmp1.keys())): key = tmp1.keys()[m] + information = tmp1[key] + new_key = (information.patient.name, None, x, information.image.orientation_label) - image_pos = tmp1[key][3] - image_number = tmp1[key][0] - serie_number = tmp1[key][1] - spacing = tmp1[key][2] - image_position = tmp1[key][3] - patient_position = tmp1[key][4] - image_type = tmp1[key][5] - patient_name = tmp1[key][6] - image_orientation_label = tmp1[key][7] - file = tmp1[key][8] - series_description = tmp1[key][9] - id_study = tmp1[key][10] - tilt = tmp1[key][11] - window = tmp1[key][12] - level = tmp1[key][13] - - new_key = (patient_name, None, x, image_orientation_label) - - - list = [image_number, serie_number, spacing, - image_position, patient_position, - image_type,patient_name, - image_orientation_label, file, - series_description, id_study, tilt, - window, level] + + list = [information] if (new_key in groups_dcm_.keys()): - groups_dcm_[new_key][0].append(list) + groups_dcm_[new_key][0].append(information) else: - groups_dcm_[new_key] = [[list]] + groups_dcm_[new_key] = [[information]] for j in xrange(len(groups_dcm_.keys())): key = groups_dcm_.keys()[j] @@ -417,97 +307,30 @@ class ivDicomGroups: self.groups_dcm = groups_dcm_ - def __Info(self): + def __UpdateZSpacing(self): """ - This INFO is used in InVesalius 2 to learn the - characteristics of the test. Add a list at the end - of each series. + Calculate Z spacing from slices """ - INFO_KEYS = \ - ['AcquisitionDate', - 'AcquisitionGantryTilt', - 'AcquisitionModality', - 'AcquisitionNumber', - 'AcquisionSequence', - 'AcquisitionTime', - 'EquipmentKVP', - 'EquipmentInstitutionName', - 'EquipmentManufacturer', - 'EquipmentXRayTubeCurrent', - 'ImageColumnOrientation', - 'ImageConvolutionKernel', - 'ImageDataType', - 'ImageLocation', - 'ImageNumber', - 'ImagePixelSpacingX', - 'ImagePixelSpacingY', - 'ImagePosition', - 'ImageRowOrientation', - 'ImageSamplesPerPixel', - 'ImageSeriesNumber', - 'ImageThickness', - 'ImageWindowLevel', - 'ImageWindowWidth', - 'PatientAge', - 'PatientBirthDate', - 'PatientGender', - 'PatientName', - 'PhysicianName', - 'StudyID', - 'StudyInstanceUID', - 'StudyAdmittingDiagnosis', - ] for x in xrange(len(self.groups_dcm.keys())): key = self.groups_dcm.keys()[x] - - file = self.groups_dcm[key][0][0][8] - - self.parser.SetFileName(file) - - acquisition_date = self.parser.GetAcquisitionDate() - acquisition_gantry_tilt = self.parser.GetAcquisitionGantryTilt() - acquisition_number = self.parser.GetAcquisitionNumber() - acquision_sequence = self.parser.GetAcquisionSequence() - acquisition_time = self.parser.GetAcquisitionTime() - equipment_kvp = self.parser.GetEquipmentKVP() - equipment_institution_name = self.parser.GetEquipmentInstitutionName() - equipment_manufacturer = self.parser.GetEquipmentManufacturer() - equipmentxraytubecurrent = self.parser.GetEquipmentXRayTubeCurrent() - image_column_orientation = self.parser.GetImageColumnOrientation() - image_convolution_kernel = self.parser.GetImageConvolutionKernel() - image_data_type = self.parser.GetImageDataType() - image_location = self.parser.GetImageLocation() - image_number = self.parser.GetImageNumber() - image_pixel_spacing_x = self.parser.GetImagePixelSpacingX() - image_pixel_spacing_y = self.parser.GetImagePixelSpacingY() - image_position = self.parser.GetImagePosition() - image_row_orientation = self.parser.GetImageRowOrientation() - image_samples_perpixel = self.parser.GetImageSamplesPerPixel() - image_series_number = self.parser.GetImageSeriesNumber() - image_thickness = self.parser.GetImageThickness() - image_window_level = self.parser.GetImageWindowLevel() - image_windowWidth = self.parser.GetImageWindowWidth() - patient_age = self.parser.GetPatientAge() - patient_birth_date = self.parser.GetPatientBirthDate() - patient_gender = self.parser.GetPatientGender() - patient_name = self.parser.GetPatientName() - study_id = self.parser.GetStudyID() - study_instance_UID = self.parser.GetStudyInstanceUID() - study_admitting_diagnosis = self.parser.GetStudyAdmittingDiagnosis() - image_dimension = (self.parser.GetDimensionX(), self.parser.GetDimensionY()) - - if (len(self.groups_dcm[key][0]) > 1 ): + information = self.groups_dcm[key][0] + + if (len(self.groups_dcm[key][0]) > 1): #Catch a slice of middle and the next to find the spacing. center = len(self.groups_dcm[key][0])/2 if (center == 1): - center = 0 - current_position = self.groups_dcm[key][0][center][3] - next_position = self.groups_dcm[key][0][center + 1][3] + center = 0 + + information = self.groups_dcm[key][0][center] + current_position = information.image.position + + information = self.groups_dcm[key][0][center + 1] + next_position = information.image.position try: - image_orientation_label = self.groups_dcm.keys()[x][3] + image_orientation_label = self.groups_dcm.keys()[3] except(IndexError): image_orientation_label = None @@ -522,17 +345,6 @@ class ivDicomGroups: else: spacing = None - - info = [acquisition_date, acquisition_gantry_tilt, acquisition_number, - acquision_sequence, acquisition_time, equipment_kvp, - equipment_institution_name, equipment_manufacturer, - equipmentxraytubecurrent, image_column_orientation, - image_convolution_kernel, image_data_type, image_location, - image_number, image_pixel_spacing_x, image_pixel_spacing_y, - image_position, image_row_orientation, image_samples_perpixel, - image_series_number, image_thickness, image_window_level, - image_windowWidth,patient_age, patient_birth_date,patient_gender, - patient_name, study_id, study_instance_UID, study_admitting_diagnosis, - spacing, image_dimension] - - self.groups_dcm[key].append(info) + + for information in self.groups_dcm[key][0]: + information.image.spacing[2] = spacing diff --git a/invesalius/reader/dicom_reader.py b/invesalius/reader/dicom_reader.py index ac57133..5cb10c2 100644 --- a/invesalius/reader/dicom_reader.py +++ b/invesalius/reader/dicom_reader.py @@ -39,7 +39,7 @@ def LoadImages(dir_): dcm_files, acquisition_modality = GetDicomFiles(dir_) - dcm_series = dicom_grouper.ivDicomGroups() + dcm_series = dicom_grouper.DicomGroups() dcm_series.SetFileList(dcm_files) dcm_series.Update() @@ -52,8 +52,8 @@ def LoadImages(dir_): key = groups.keys()[x] for y in xrange(len(groups[key][0])): - - file = groups[key][0][y][8] + dicom = groups[key][0][y] + file = dicom.image.file tmp_list.append(file) list_files.append([len(tmp_list), key]) @@ -66,19 +66,22 @@ def LoadImages(dir_): file_list = [] for x in xrange(len(groups[key][0])): - file_list.append(groups[key][0][x][8]) - - tilt = groups[key][0][x][11] - spacing = groups[key][1][14] - spacing_z = groups[key][1][30] - orientation = groups[key][0][x][7] - window = groups[key][0][x][12] - level = groups[key][0][x][13] - + dicom = groups[key][0][x] + file_list.append(dicom.image.file) + + information = groups[key][0][x] + + tilt = dicom.acquisition.tilt#groups[key][0][x][11] + spacing = dicom.image.spacing#groups[key][1][14] + #spacing_z = #groups[key][1][30] + orientation = dicom.image.orientation_label#groups[key][0][x][7] + window = dicom.image.window#groups[key][0][x][12] + level = dicom.image.level#groups[key][0][x][13] files = file_list + print dicom.image.orientation_label #Coronal Crash. necessary verify - if (orientation <> "CORONAL"): + if (dicom.image.orientation_label <> "CORONAL"): #Organize reversed image sorter = gdcm.IPPSorter() sorter.SetComputeZSpacing(True) @@ -100,9 +103,9 @@ def LoadImages(dir_): read.SetFileNames(array) read.Update() - img_axial = vtk.vtkImageData() - img_axial.DeepCopy(read.GetOutput()) - img_axial.SetSpacing(spacing, spacing, spacing_z) + image_data = vtk.vtkImageData() + image_data.DeepCopy(read.GetOutput()) + image_data.SetSpacing(spacing, spacing, spacing_z) else: for x in xrange(len(files)): #SIf the resolution of the @@ -118,16 +121,15 @@ def LoadImages(dir_): img_app.AddInput(img) img_app.Update() - img_axial = vtk.vtkImageData() - img_axial.DeepCopy(img_app.GetOutput()) - img_axial.SetSpacing(img_axial.GetSpacing()[0],\ - img_axial.GetSpacing()[1],\ - spacing_z) - + 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]) - img_axial.Update() + image_data.Update() - return img_axial, acquisition_modality, tilt, orientation, window, level + return image_data, acquisition_modality, tilt, orientation, window, level def GetDicomFiles(path, recursive = False): # TODO!!! SUPER GAMBIARRA!!! DO THIS BETTER -- libgit2 0.21.2