From 0a8d35b25865f6fde0c25f7d1f0c407baf292f62 Mon Sep 17 00:00:00 2001 From: tatiana Date: Thu, 15 Oct 2009 16:57:00 +0000 Subject: [PATCH] ENC: Changed import panel's implementation --- invesalius/control.py | 52 +++------------------------------------------------- invesalius/gui/import_panel.py | 85 ++++++++++++++++++++++++++++++++++--------------------------------------------------- invesalius/reader/dicom_grouper.py | 21 +++++++++++++++++++-- invesalius/reader/dicom_reader.py | 11 ----------- 4 files changed, 56 insertions(+), 113 deletions(-) diff --git a/invesalius/control.py b/invesalius/control.py index 349c282..b1520ab 100755 --- a/invesalius/control.py +++ b/invesalius/control.py @@ -35,56 +35,10 @@ class Controller(): path = pubsub_evt.data # retrieve DICOM files splited into groups - dicom_series = dcm.GetSeries(path) - - # create dictionary with DICOM files' groups with - # information as it will be shown on import panel - dict = {} - series_preview = [] - - for key in dicom_series: - patient_name = key[0] - dicom = dicom_series[key][0] - - # Compute how many images per series: - n_images = str(len(dicom_series[key])) - # Add date and time in a single field: - date_time = "%s %s"%(dicom.acquisition.date, - dicom.acquisition.time) - - # Data per series - exam_data = [dicom.acquisition.series_description, - dicom.patient.id, - str(dicom.patient.age), - dicom.patient.gender, - dicom.acquisition.study_description, # per patient - dicom.acquisition.modality, - date_time, - n_images, - dicom.acquisition.institution, - dicom.patient.birthdate, - dicom.acquisition.accession_number, - dicom.patient.physician, - dicom.acquisition.protocol_name] # per series - - # Preview data - series_preview.append((dicom.image.file, # Filename - dicom.image.level, # Window level - dicom.image.window, # Window width - dicom.acquisition.series_description, # Title - "%s Images" %(n_images), # Subtitle - )) - - try: - dict[patient_name].append(exam_data) - except KeyError: - dict[patient_name] = [exam_data] - print dict - - + dicom_series = dcm.GetDicomGroups(path) + ps.Publisher().sendMessage("Load import panel", dicom_series) - ps.Publisher().sendMessage("Load import panel", dict) - ps.Publisher().sendMessage("Load dicom preview", series_preview) + #ps.Publisher().sendMessage("Load dicom preview", series_preview) def ImportDirectory(self, pubsub_evt=None, dir_=None): diff --git a/invesalius/gui/import_panel.py b/invesalius/gui/import_panel.py index d8b68e2..1395c9d 100644 --- a/invesalius/gui/import_panel.py +++ b/invesalius/gui/import_panel.py @@ -40,8 +40,8 @@ class InnerPanel(wx.Panel): ps.Publisher().subscribe(self.ShowDicomPreview, "Load import panel") def ShowDicomPreview(self, pubsub_evt): - dict = pubsub_evt.data - self.text_panel.Populate(dict) + dicom_groups = pubsub_evt.data + self.text_panel.Populate(dicom_groups) class TextPanel(wx.Panel): @@ -92,64 +92,47 @@ class TextPanel(wx.Panel): self.root = tree.AddRoot("InVesalius Database") self.tree = tree - def Populate(self, dict): + def Populate(self, patient_list): tree = self.tree - i = 0 + for patient in patient_list: + ngroups = patient.ngroups + dicom = patient.GetDicomSample() + title = dicom.patient.name + " (%d series)"%(ngroups) + date_time = "%s %s"%(dicom.acquisition.date, + dicom.acquisition.time) - # For each patient on dictionary - for patient_name in dict: - # In spite of the patient name, we'll show number of - # series also - title = patient_name + " (%d series)"%(len(dict[patient_name])) parent = tree.AppendItem(self.root, title) - patient_data = dict[patient_name] - - # Row background colour - if i%2: - tree.SetItemBackgroundColour(parent, (242,246,254)) - - # Insert patient data into columns based on first series - for item in xrange(1, len(patient_data[0])-1): - value = patient_data[0][item] - # Sum slices of all patient's series - if (item == 7): - value = 0 - for series in xrange(len(patient_data)): - value += int(patient_data[series][7]) - tree.SetItemText(parent, str(value), item) # ID - - # For each series on patient - j = 0 - for series in xrange(len(patient_data)): - series_title = patient_data[series][0] - - child = self.tree.AppendItem(parent, series_title) - if not j%2: - tree.SetItemBackgroundColour(child, (242,246,254)) - - # TODO: change description "protocol_name" - description = patient_data[series][-1] - modality = patient_data[series][5] - # TODO: add to date the time - date = patient_data[series][6] - nimages = patient_data[series][7] - - tree.SetItemText(child, series_title, 0) - tree.SetItemText(child, description, 4) - tree.SetItemText(child, modality, 5) - tree.SetItemText(child, date, 6) - tree.SetItemText(child, nimages, 7) - - j += 1 - i += 1 - + tree.SetItemText(parent, str(dicom.patient.id), 1) + tree.SetItemText(parent, str(dicom.patient.age), 2) + tree.SetItemText(parent, str(dicom.patient.gender), 3) + tree.SetItemText(parent, str(dicom.acquisition.study_description), 4) + tree.SetItemText(parent, str(dicom.acquisition.modality), 5) + tree.SetItemText(parent, str(date_time), 6) + tree.SetItemText(parent, str(patient.nslices), 7) + tree.SetItemText(parent, str(dicom.acquisition.institution), 8) + tree.SetItemText(parent, str(dicom.patient.birthdate), 9) + tree.SetItemText(parent, str(dicom.acquisition.accession_number), 10) + tree.SetItemText(parent, str(dicom.patient.physician), 11) + + group_list = patient.GetGroups() + for group in group_list: + dicom = group.GetDicomSample() + group_title = dicom.acquisition.series_description + + child = self.tree.AppendItem(parent, group_title) + + tree.SetItemText(child, str(group_title), 0) + tree.SetItemText(child, str(dicom.acquisition.protocol_name), 4) + tree.SetItemText(child, str(dicom.acquisition.modality), 5) + tree.SetItemText(child, str(date_time), 6) + tree.SetItemText(child, str(group.nslices), 7) + tree.Expand(self.root) tree.GetMainWindow().Bind(wx.EVT_RIGHT_UP, self.OnRightUp) tree.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.OnActivate) - def OnActivate(self, evt): print 'OnActivate: %s' % self.tree.GetItemText(evt.GetItem()) diff --git a/invesalius/reader/dicom_grouper.py b/invesalius/reader/dicom_grouper.py index 672a4de..f8f2624 100644 --- a/invesalius/reader/dicom_grouper.py +++ b/invesalius/reader/dicom_grouper.py @@ -66,11 +66,13 @@ class DicomGroup: # externally as list self.nslices = 0 self.zspacing = 1 + self.dicom = None def AddSlice(self, dicom): + if not self.dicom: + self.dicom = dicom pos = tuple(dicom.image.position) if pos not in self.slices_dict.keys(): - self.slices_dict[pos] = dicom self.nslices += 1 return True @@ -106,6 +108,9 @@ class DicomGroup: self.zspacing = abs(p1 - p2) else: self.zspacing = 1 + + def GetDicomSample(self): + return self.dicom class PatientGroup: def __init__(self): @@ -114,6 +119,8 @@ class PatientGroup: self.key = () self.groups_dict = {} # group_key: DicomGroup self.nslices = 0 + self.ngroups = 0 + self.dicom = None def AddFile(self, dicom, index=0): # Given general DICOM information, we group slices according @@ -129,10 +136,14 @@ class PatientGroup: dicom.acquisition.serie_number, dicom.image.orientation_label, index) # This will be used to deal with Problem 2 + if not self.dicom: + self.dicom = dicom + self.nslices += 1 # Does this group exist? Best case ;) if group_key not in self.groups_dict.keys(): group = DicomGroup() + self.ngroups += 1 group.AddSlice(dicom) self.groups_dict[group_key] = group # Group exists... Lets try to add slice @@ -168,6 +179,9 @@ class PatientGroup: def GetGroups(self): return self.groups_dict.values() + def GetDicomSample(self): + return self.dicom + def FixProblem1(self, dict): """ Merge multiple DICOM groups in case Problem 1 (description @@ -268,6 +282,7 @@ class DicomPatientGrouper: # Does this patient exist? if patient_key not in self.patients_dict.keys(): patient = PatientGroup() + patient.key = patient_key patient.AddFile(dicom) self.patients_dict[patient_key] = patient # Patient exists... Lets add group to it @@ -290,5 +305,7 @@ class DicomPatientGrouper: # :) you've got a list of dicom.Dicom # of the same series """ - return self.patients_dict.values() + plist = self.patients_dict.values() + plist = sorted(plist, key = lambda patient:patient.key[0]) + return plist diff --git a/invesalius/reader/dicom_reader.py b/invesalius/reader/dicom_reader.py index 13c49fd..1be99a4 100644 --- a/invesalius/reader/dicom_reader.py +++ b/invesalius/reader/dicom_reader.py @@ -118,17 +118,6 @@ def CreateImageData(filelist, zspacing): imagedata.Update() return imagedata -def GetSeries(path): - """ - Return DICOM group of files inside given directory. - """ - dcm_files = GetDicomFiles(path) - - dcm_series = dicom_grouper.DicomGroups() - dcm_series.SetFileList(dcm_files) - dcm_series.Update() - - return dcm_series.GetOutput() def GetDicomGroups(directory, recursive=True): """ -- libgit2 0.21.2