Commit 0a8d35b25865f6fde0c25f7d1f0c407baf292f62

Authored by tatiana
1 parent 3dfaec66

ENC: Changed import panel's implementation

invesalius/control.py
@@ -35,56 +35,10 @@ class Controller(): @@ -35,56 +35,10 @@ class Controller():
35 path = pubsub_evt.data 35 path = pubsub_evt.data
36 36
37 # retrieve DICOM files splited into groups 37 # retrieve DICOM files splited into groups
38 - dicom_series = dcm.GetSeries(path)  
39 -  
40 - # create dictionary with DICOM files' groups with  
41 - # information as it will be shown on import panel  
42 - dict = {}  
43 - series_preview = []  
44 -  
45 - for key in dicom_series:  
46 - patient_name = key[0]  
47 - dicom = dicom_series[key][0]  
48 -  
49 - # Compute how many images per series:  
50 - n_images = str(len(dicom_series[key]))  
51 - # Add date and time in a single field:  
52 - date_time = "%s %s"%(dicom.acquisition.date,  
53 - dicom.acquisition.time)  
54 -  
55 - # Data per series  
56 - exam_data = [dicom.acquisition.series_description,  
57 - dicom.patient.id,  
58 - str(dicom.patient.age),  
59 - dicom.patient.gender,  
60 - dicom.acquisition.study_description, # per patient  
61 - dicom.acquisition.modality,  
62 - date_time,  
63 - n_images,  
64 - dicom.acquisition.institution,  
65 - dicom.patient.birthdate,  
66 - dicom.acquisition.accession_number,  
67 - dicom.patient.physician,  
68 - dicom.acquisition.protocol_name] # per series  
69 -  
70 - # Preview data  
71 - series_preview.append((dicom.image.file, # Filename  
72 - dicom.image.level, # Window level  
73 - dicom.image.window, # Window width  
74 - dicom.acquisition.series_description, # Title  
75 - "%s Images" %(n_images), # Subtitle  
76 - ))  
77 -  
78 - try:  
79 - dict[patient_name].append(exam_data)  
80 - except KeyError:  
81 - dict[patient_name] = [exam_data]  
82 - print dict  
83 -  
84 - 38 + dicom_series = dcm.GetDicomGroups(path)
  39 + ps.Publisher().sendMessage("Load import panel", dicom_series)
85 40
86 - ps.Publisher().sendMessage("Load import panel", dict)  
87 - ps.Publisher().sendMessage("Load dicom preview", series_preview) 41 + #ps.Publisher().sendMessage("Load dicom preview", series_preview)
88 42
89 43
90 def ImportDirectory(self, pubsub_evt=None, dir_=None): 44 def ImportDirectory(self, pubsub_evt=None, dir_=None):
invesalius/gui/import_panel.py
@@ -40,8 +40,8 @@ class InnerPanel(wx.Panel): @@ -40,8 +40,8 @@ class InnerPanel(wx.Panel):
40 ps.Publisher().subscribe(self.ShowDicomPreview, "Load import panel") 40 ps.Publisher().subscribe(self.ShowDicomPreview, "Load import panel")
41 41
42 def ShowDicomPreview(self, pubsub_evt): 42 def ShowDicomPreview(self, pubsub_evt):
43 - dict = pubsub_evt.data  
44 - self.text_panel.Populate(dict) 43 + dicom_groups = pubsub_evt.data
  44 + self.text_panel.Populate(dicom_groups)
45 45
46 46
47 class TextPanel(wx.Panel): 47 class TextPanel(wx.Panel):
@@ -92,64 +92,47 @@ class TextPanel(wx.Panel): @@ -92,64 +92,47 @@ class TextPanel(wx.Panel):
92 self.root = tree.AddRoot("InVesalius Database") 92 self.root = tree.AddRoot("InVesalius Database")
93 self.tree = tree 93 self.tree = tree
94 94
95 - def Populate(self, dict): 95 + def Populate(self, patient_list):
96 tree = self.tree 96 tree = self.tree
97 97
98 - i = 0 98 + for patient in patient_list:
  99 + ngroups = patient.ngroups
  100 + dicom = patient.GetDicomSample()
  101 + title = dicom.patient.name + " (%d series)"%(ngroups)
  102 + date_time = "%s %s"%(dicom.acquisition.date,
  103 + dicom.acquisition.time)
99 104
100 - # For each patient on dictionary  
101 - for patient_name in dict:  
102 - # In spite of the patient name, we'll show number of  
103 - # series also  
104 - title = patient_name + " (%d series)"%(len(dict[patient_name]))  
105 parent = tree.AppendItem(self.root, title) 105 parent = tree.AppendItem(self.root, title)
106 - patient_data = dict[patient_name]  
107 -  
108 - # Row background colour  
109 - if i%2:  
110 - tree.SetItemBackgroundColour(parent, (242,246,254))  
111 -  
112 - # Insert patient data into columns based on first series  
113 - for item in xrange(1, len(patient_data[0])-1):  
114 - value = patient_data[0][item]  
115 - # Sum slices of all patient's series  
116 - if (item == 7):  
117 - value = 0  
118 - for series in xrange(len(patient_data)):  
119 - value += int(patient_data[series][7])  
120 - tree.SetItemText(parent, str(value), item) # ID  
121 -  
122 - # For each series on patient  
123 - j = 0  
124 - for series in xrange(len(patient_data)):  
125 - series_title = patient_data[series][0]  
126 -  
127 - child = self.tree.AppendItem(parent, series_title)  
128 - if not j%2:  
129 - tree.SetItemBackgroundColour(child, (242,246,254))  
130 -  
131 - # TODO: change description "protocol_name"  
132 - description = patient_data[series][-1]  
133 - modality = patient_data[series][5]  
134 - # TODO: add to date the time  
135 - date = patient_data[series][6]  
136 - nimages = patient_data[series][7]  
137 -  
138 - tree.SetItemText(child, series_title, 0)  
139 - tree.SetItemText(child, description, 4)  
140 - tree.SetItemText(child, modality, 5)  
141 - tree.SetItemText(child, date, 6)  
142 - tree.SetItemText(child, nimages, 7)  
143 -  
144 - j += 1  
145 - i += 1  
146 - 106 + tree.SetItemText(parent, str(dicom.patient.id), 1)
  107 + tree.SetItemText(parent, str(dicom.patient.age), 2)
  108 + tree.SetItemText(parent, str(dicom.patient.gender), 3)
  109 + tree.SetItemText(parent, str(dicom.acquisition.study_description), 4)
  110 + tree.SetItemText(parent, str(dicom.acquisition.modality), 5)
  111 + tree.SetItemText(parent, str(date_time), 6)
  112 + tree.SetItemText(parent, str(patient.nslices), 7)
  113 + tree.SetItemText(parent, str(dicom.acquisition.institution), 8)
  114 + tree.SetItemText(parent, str(dicom.patient.birthdate), 9)
  115 + tree.SetItemText(parent, str(dicom.acquisition.accession_number), 10)
  116 + tree.SetItemText(parent, str(dicom.patient.physician), 11)
  117 +
  118 + group_list = patient.GetGroups()
  119 + for group in group_list:
  120 + dicom = group.GetDicomSample()
  121 + group_title = dicom.acquisition.series_description
  122 +
  123 + child = self.tree.AppendItem(parent, group_title)
  124 +
  125 + tree.SetItemText(child, str(group_title), 0)
  126 + tree.SetItemText(child, str(dicom.acquisition.protocol_name), 4)
  127 + tree.SetItemText(child, str(dicom.acquisition.modality), 5)
  128 + tree.SetItemText(child, str(date_time), 6)
  129 + tree.SetItemText(child, str(group.nslices), 7)
  130 +
147 tree.Expand(self.root) 131 tree.Expand(self.root)
148 132
149 tree.GetMainWindow().Bind(wx.EVT_RIGHT_UP, self.OnRightUp) 133 tree.GetMainWindow().Bind(wx.EVT_RIGHT_UP, self.OnRightUp)
150 tree.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.OnActivate) 134 tree.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.OnActivate)
151 135
152 -  
153 def OnActivate(self, evt): 136 def OnActivate(self, evt):
154 print 'OnActivate: %s' % self.tree.GetItemText(evt.GetItem()) 137 print 'OnActivate: %s' % self.tree.GetItemText(evt.GetItem())
155 138
invesalius/reader/dicom_grouper.py
@@ -66,11 +66,13 @@ class DicomGroup: @@ -66,11 +66,13 @@ class DicomGroup:
66 # externally as list 66 # externally as list
67 self.nslices = 0 67 self.nslices = 0
68 self.zspacing = 1 68 self.zspacing = 1
  69 + self.dicom = None
69 70
70 def AddSlice(self, dicom): 71 def AddSlice(self, dicom):
  72 + if not self.dicom:
  73 + self.dicom = dicom
71 pos = tuple(dicom.image.position) 74 pos = tuple(dicom.image.position)
72 if pos not in self.slices_dict.keys(): 75 if pos not in self.slices_dict.keys():
73 -  
74 self.slices_dict[pos] = dicom 76 self.slices_dict[pos] = dicom
75 self.nslices += 1 77 self.nslices += 1
76 return True 78 return True
@@ -106,6 +108,9 @@ class DicomGroup: @@ -106,6 +108,9 @@ class DicomGroup:
106 self.zspacing = abs(p1 - p2) 108 self.zspacing = abs(p1 - p2)
107 else: 109 else:
108 self.zspacing = 1 110 self.zspacing = 1
  111 +
  112 + def GetDicomSample(self):
  113 + return self.dicom
109 114
110 class PatientGroup: 115 class PatientGroup:
111 def __init__(self): 116 def __init__(self):
@@ -114,6 +119,8 @@ class PatientGroup: @@ -114,6 +119,8 @@ class PatientGroup:
114 self.key = () 119 self.key = ()
115 self.groups_dict = {} # group_key: DicomGroup 120 self.groups_dict = {} # group_key: DicomGroup
116 self.nslices = 0 121 self.nslices = 0
  122 + self.ngroups = 0
  123 + self.dicom = None
117 124
118 def AddFile(self, dicom, index=0): 125 def AddFile(self, dicom, index=0):
119 # Given general DICOM information, we group slices according 126 # Given general DICOM information, we group slices according
@@ -129,10 +136,14 @@ class PatientGroup: @@ -129,10 +136,14 @@ class PatientGroup:
129 dicom.acquisition.serie_number, 136 dicom.acquisition.serie_number,
130 dicom.image.orientation_label, 137 dicom.image.orientation_label,
131 index) # This will be used to deal with Problem 2 138 index) # This will be used to deal with Problem 2
  139 + if not self.dicom:
  140 + self.dicom = dicom
  141 +
132 self.nslices += 1 142 self.nslices += 1
133 # Does this group exist? Best case ;) 143 # Does this group exist? Best case ;)
134 if group_key not in self.groups_dict.keys(): 144 if group_key not in self.groups_dict.keys():
135 group = DicomGroup() 145 group = DicomGroup()
  146 + self.ngroups += 1
136 group.AddSlice(dicom) 147 group.AddSlice(dicom)
137 self.groups_dict[group_key] = group 148 self.groups_dict[group_key] = group
138 # Group exists... Lets try to add slice 149 # Group exists... Lets try to add slice
@@ -168,6 +179,9 @@ class PatientGroup: @@ -168,6 +179,9 @@ class PatientGroup:
168 def GetGroups(self): 179 def GetGroups(self):
169 return self.groups_dict.values() 180 return self.groups_dict.values()
170 181
  182 + def GetDicomSample(self):
  183 + return self.dicom
  184 +
171 def FixProblem1(self, dict): 185 def FixProblem1(self, dict):
172 """ 186 """
173 Merge multiple DICOM groups in case Problem 1 (description 187 Merge multiple DICOM groups in case Problem 1 (description
@@ -268,6 +282,7 @@ class DicomPatientGrouper: @@ -268,6 +282,7 @@ class DicomPatientGrouper:
268 # Does this patient exist? 282 # Does this patient exist?
269 if patient_key not in self.patients_dict.keys(): 283 if patient_key not in self.patients_dict.keys():
270 patient = PatientGroup() 284 patient = PatientGroup()
  285 + patient.key = patient_key
271 patient.AddFile(dicom) 286 patient.AddFile(dicom)
272 self.patients_dict[patient_key] = patient 287 self.patients_dict[patient_key] = patient
273 # Patient exists... Lets add group to it 288 # Patient exists... Lets add group to it
@@ -290,5 +305,7 @@ class DicomPatientGrouper: @@ -290,5 +305,7 @@ class DicomPatientGrouper:
290 # :) you've got a list of dicom.Dicom 305 # :) you've got a list of dicom.Dicom
291 # of the same series 306 # of the same series
292 """ 307 """
293 - return self.patients_dict.values() 308 + plist = self.patients_dict.values()
  309 + plist = sorted(plist, key = lambda patient:patient.key[0])
  310 + return plist
294 311
invesalius/reader/dicom_reader.py
@@ -118,17 +118,6 @@ def CreateImageData(filelist, zspacing): @@ -118,17 +118,6 @@ def CreateImageData(filelist, zspacing):
118 imagedata.Update() 118 imagedata.Update()
119 return imagedata 119 return imagedata
120 120
121 -def GetSeries(path):  
122 - """  
123 - Return DICOM group of files inside given directory.  
124 - """  
125 - dcm_files = GetDicomFiles(path)  
126 -  
127 - dcm_series = dicom_grouper.DicomGroups()  
128 - dcm_series.SetFileList(dcm_files)  
129 - dcm_series.Update()  
130 -  
131 - return dcm_series.GetOutput()  
132 121
133 def GetDicomGroups(directory, recursive=True): 122 def GetDicomGroups(directory, recursive=True):
134 """ 123 """