Commit 61d6ebfa72727b21617892f7b14ef792c3bcb050
1 parent
d6839add
Exists in
master
and in
5 other branches
ENH: Turned thread support in importation
Showing
2 changed files
with
158 additions
and
203 deletions
Show diff stats
invesalius/control.py
@@ -120,12 +120,13 @@ class Controller(): | @@ -120,12 +120,13 @@ class Controller(): | ||
120 | answer = dialog.SaveChangesDialog2(filename) | 120 | answer = dialog.SaveChangesDialog2(filename) |
121 | if answer: | 121 | if answer: |
122 | self.ShowDialogSaveProject() | 122 | self.ShowDialogSaveProject() |
123 | - | ||
124 | # Import project | 123 | # Import project |
125 | dirpath = dialog.ShowImportDirDialog() | 124 | dirpath = dialog.ShowImportDirDialog() |
126 | if dirpath and not os.listdir(dirpath): | 125 | if dirpath and not os.listdir(dirpath): |
126 | + print "IFFFFFFFfff" | ||
127 | dialog.ImportEmptyDirectory(dirpath) | 127 | dialog.ImportEmptyDirectory(dirpath) |
128 | elif dirpath: | 128 | elif dirpath: |
129 | + print "ELSE..........." | ||
129 | self.StartImportPanel(dirpath) | 130 | self.StartImportPanel(dirpath) |
130 | ps.Publisher().sendMessage("Load data to import panel", dirpath) | 131 | ps.Publisher().sendMessage("Load data to import panel", dirpath) |
131 | 132 |
invesalius/reader/dicom_reader.py
@@ -79,60 +79,132 @@ def SortFiles(filelist, dicom): | @@ -79,60 +79,132 @@ def SortFiles(filelist, dicom): | ||
79 | 79 | ||
80 | return filelist | 80 | return filelist |
81 | 81 | ||
82 | -""" | 82 | +tag_labels = {} |
83 | +main_dict = {} | ||
84 | +dict_file = {} | ||
85 | + | ||
83 | class LoadDicom(threading.Thread): | 86 | class LoadDicom(threading.Thread): |
87 | + | ||
84 | def __init__(self, grouper, q, l): | 88 | def __init__(self, grouper, q, l): |
85 | threading.Thread.__init__(self) | 89 | threading.Thread.__init__(self) |
86 | self.grouper = grouper | 90 | self.grouper = grouper |
87 | self.q = q | 91 | self.q = q |
88 | self.l = l | 92 | self.l = l |
93 | + | ||
89 | def run(self): | 94 | def run(self): |
95 | + | ||
90 | grouper = self.grouper | 96 | grouper = self.grouper |
91 | q = self.q | 97 | q = self.q |
98 | + | ||
92 | while 1: | 99 | while 1: |
100 | + | ||
93 | filepath = q.get() | 101 | filepath = q.get() |
94 | if not filepath: | 102 | if not filepath: |
95 | break | 103 | break |
96 | - parser = dicom.Parser() | ||
97 | - #if parser.SetFileName(filepath): | ||
98 | - dcm = dicom.Dicom() | ||
99 | - self.l.acquire() | ||
100 | - dcm.SetParser(parser) | ||
101 | - grouper.AddFile(dcm) | ||
102 | - self.l.release() | ||
103 | -""" | ||
104 | - | ||
105 | -""" | ||
106 | -class LoadDicom(): | ||
107 | - def __init__(self, grouper, dicom_dict): | ||
108 | - self.grouper = grouper | ||
109 | - self.dicom_dict = dicom_dict | 104 | + |
105 | + reader = gdcm.Reader() | ||
106 | + reader.SetFileName(filepath) | ||
107 | + | ||
108 | + if (reader.Read()): | ||
109 | + | ||
110 | + file = reader.GetFile() | ||
111 | + | ||
112 | + # Retrieve data set | ||
113 | + dataSet = file.GetDataSet() | ||
114 | + | ||
115 | + # Retrieve header | ||
116 | + header = file.GetHeader() | ||
117 | + stf = gdcm.StringFilter() | ||
118 | + | ||
119 | + field_dict = {} | ||
120 | + data_dict = {} | ||
121 | + | ||
122 | + | ||
123 | + tag = gdcm.Tag(0x0008, 0x0005) | ||
124 | + ds = reader.GetFile().GetDataSet() | ||
125 | + if ds.FindDataElement(tag): | ||
126 | + encoding = str(ds.GetDataElement(tag).GetValue()) | ||
127 | + if not(encoding != None and encoding != "None" and encoding != "Loaded"): | ||
128 | + encoding = "ISO_IR 100" | ||
129 | + else: | ||
130 | + encoding = "ISO_IR_100" | ||
131 | + # Iterate through the Header | ||
132 | + iterator = header.GetDES().begin() | ||
133 | + while (not iterator.equal(header.GetDES().end())): | ||
134 | + dataElement = iterator.next() | ||
135 | + stf.SetFile(file) | ||
136 | + tag = dataElement.GetTag() | ||
137 | + data = stf.ToStringPair(tag) | ||
138 | + stag = tag.PrintAsPipeSeparatedString() | ||
139 | + | ||
140 | + group = stag.split("|")[0][1:] | ||
141 | + field = stag.split("|")[1][:-1] | ||
142 | + tag_labels[stag] = data[0] | ||
143 | + | ||
144 | + if not group in data_dict.keys(): | ||
145 | + data_dict[group] = {} | ||
146 | + | ||
147 | + if not(utils.VerifyInvalidPListCharacter(data[1])): | ||
148 | + data_dict[group][field] = data[1].decode(encoding) | ||
149 | + else: | ||
150 | + data_dict[group][field] = "Invalid Character" | ||
110 | 151 | ||
111 | - def Start(self): | ||
112 | - images = self.dicom_dict['data'] | ||
113 | 152 | ||
114 | - grouper = self.grouper | ||
115 | - | ||
116 | - for x in images.keys(): | ||
117 | - data_image = images[x] | ||
118 | - parser = dicom.Parser() | ||
119 | - parser.SetDataImage(data_image) | ||
120 | - | ||
121 | - dcm = dicom.Dicom() | ||
122 | - dcm.SetParser(parser) | ||
123 | - grouper.AddFile(dcm) | ||
124 | - | ||
125 | - | ||
126 | - #while 1: | ||
127 | - #filepath = path | ||
128 | - #if not filepath: | ||
129 | - # break | ||
130 | - #parser = dicom.Parser() | ||
131 | - #if parser.SetFileName(filepath): | ||
132 | - # dcm = dicom.Dicom() | ||
133 | - # dcm.SetParser(parser) | ||
134 | - # grouper.AddFile(dcm) | ||
135 | -""" | 153 | + # Iterate through the Data set |
154 | + iterator = dataSet.GetDES().begin() | ||
155 | + while (not iterator.equal(dataSet.GetDES().end())): | ||
156 | + dataElement = iterator.next() | ||
157 | + | ||
158 | + stf.SetFile(file) | ||
159 | + tag = dataElement.GetTag() | ||
160 | + data = stf.ToStringPair(tag) | ||
161 | + stag = tag.PrintAsPipeSeparatedString() | ||
162 | + | ||
163 | + group = stag.split("|")[0][1:] | ||
164 | + field = stag.split("|")[1][:-1] | ||
165 | + tag_labels[stag] = data[0] | ||
166 | + | ||
167 | + if not group in data_dict.keys(): | ||
168 | + data_dict[group] = {} | ||
169 | + | ||
170 | + if not(utils.VerifyInvalidPListCharacter(data[1])): | ||
171 | + data_dict[group][field] = data[1].decode(encoding) | ||
172 | + else: | ||
173 | + data_dict[group][field] = "Invalid Character" | ||
174 | + | ||
175 | + | ||
176 | + | ||
177 | + # ---------- Refactory -------------------------------------- | ||
178 | + data_dict['invesalius'] = {'orientation_label' : GetImageOrientationLabel(filepath)} | ||
179 | + | ||
180 | + # ------------------------------------------------------------- | ||
181 | + dict_file[filepath] = data_dict | ||
182 | + | ||
183 | + #---------- Verify is DICOMDir ------------------------------- | ||
184 | + is_dicom_dir = 1 | ||
185 | + try: | ||
186 | + if (data_dict['0002']['0002'] != "1.2.840.10008.1.3.10"): #DICOMDIR | ||
187 | + is_dicom_dir = 0 | ||
188 | + except(KeyError): | ||
189 | + is_dicom_dir = 0 | ||
190 | + | ||
191 | + if not(is_dicom_dir): | ||
192 | + parser = dicom.Parser() | ||
193 | + parser.SetDataImage(dict_file[filepath], filepath) | ||
194 | + | ||
195 | + dcm = dicom.Dicom() | ||
196 | + self.l.acquire() | ||
197 | + dcm.SetParser(parser) | ||
198 | + grouper.AddFile(dcm) | ||
199 | + | ||
200 | + self.l.release() | ||
201 | + | ||
202 | + #========== used in test ======================================= | ||
203 | + #main_dict = dict( | ||
204 | + # data = dict_file, | ||
205 | + # labels = tag_labels) | ||
206 | + | ||
207 | + #plistlib.writePlist(main_dict, ".//teste.plist")""" | ||
136 | 208 | ||
137 | 209 | ||
138 | def GetImageOrientationLabel(filename): | 210 | def GetImageOrientationLabel(filename): |
@@ -158,180 +230,60 @@ def GetImageOrientationLabel(filename): | @@ -158,180 +230,60 @@ def GetImageOrientationLabel(filename): | ||
158 | else: | 230 | else: |
159 | return "" | 231 | return "" |
160 | 232 | ||
161 | -grouper = dicom_grouper.DicomPatientGrouper() | ||
162 | 233 | ||
163 | -def GetDicomGroups(directory, recursive=True, gui=True): | 234 | +def yGetDicomGroups(directory, recursive=True, gui=True): |
164 | """ | 235 | """ |
165 | Return all full paths to DICOM files inside given directory. | 236 | Return all full paths to DICOM files inside given directory. |
166 | """ | 237 | """ |
167 | - | ||
168 | nfiles = 0 | 238 | nfiles = 0 |
169 | # Find total number of files | 239 | # Find total number of files |
170 | if recursive: | 240 | if recursive: |
171 | - | ||
172 | - folders = [f[0] for f in os.walk(directory)] | ||
173 | - | ||
174 | - tag_labels = {} | ||
175 | - main_dict = {} | ||
176 | - dict_file = {} | ||
177 | - | ||
178 | - for folder in folders: | ||
179 | - files = glob.glob(os.path.join(folder, "*")) | ||
180 | - | ||
181 | - for filename in files: | ||
182 | - | ||
183 | - # Read file | ||
184 | - reader = gdcm.Reader() | ||
185 | - reader.SetFileName(filename) | ||
186 | - | ||
187 | - if (reader.Read()): | ||
188 | - | ||
189 | - file = reader.GetFile() | ||
190 | - | ||
191 | - # Retrieve data set | ||
192 | - dataSet = file.GetDataSet() | ||
193 | - | ||
194 | - # Retrieve header | ||
195 | - header = file.GetHeader() | ||
196 | - stf = gdcm.StringFilter() | ||
197 | - | ||
198 | - field_dict = {} | ||
199 | - data_dict = {} | ||
200 | - | 241 | + for dirpath, dirnames, filenames in os.walk(directory): |
242 | + nfiles += len(filenames) | ||
243 | + else: | ||
244 | + dirpath, dirnames, filenames = os.walk(directory) | ||
245 | + nfiles = len(filenames) | ||
246 | + | ||
247 | + counter = 0 | ||
248 | + grouper = dicom_grouper.DicomPatientGrouper() | ||
249 | + q = Queue.Queue() | ||
250 | + l = threading.Lock() | ||
251 | + threads = [] | ||
252 | + for i in xrange(cpu_count()): | ||
253 | + t = LoadDicom(grouper, q, l) | ||
254 | + t.start() | ||
255 | + threads.append(t) | ||
256 | + # Retrieve only DICOM files, splited into groups | ||
257 | + if recursive: | ||
258 | + for dirpath, dirnames, filenames in os.walk(directory): | ||
259 | + for name in filenames: | ||
260 | + filepath = os.path.join(dirpath, name) | ||
261 | + counter += 1 | ||
262 | + if gui: | ||
263 | + yield (counter,nfiles) | ||
264 | + q.put(filepath) | ||
265 | + else: | ||
266 | + dirpath, dirnames, filenames = os.walk(directory) | ||
267 | + for name in filenames: | ||
268 | + filepath = str(os.path.join(dirpath, name)) | ||
269 | + counter += 1 | ||
270 | + if gui: | ||
271 | + yield (counter,nfiles) | ||
272 | + q.put(filepath) | ||
201 | 273 | ||
202 | - tag = gdcm.Tag(0x0008, 0x0005) | ||
203 | - ds = reader.GetFile().GetDataSet() | ||
204 | - if ds.FindDataElement(tag): | ||
205 | - encoding = str(ds.GetDataElement(tag).GetValue()) | ||
206 | - if not(encoding != None and encoding != "None" and encoding != "Loaded"): | ||
207 | - encoding = "ISO_IR 100" | ||
208 | - else: | ||
209 | - encoding = "ISO_IR_100" | ||
210 | - # Iterate through the Header | ||
211 | - iterator = header.GetDES().begin() | ||
212 | - while (not iterator.equal(header.GetDES().end())): | ||
213 | - dataElement = iterator.next() | ||
214 | - stf.SetFile(file) | ||
215 | - tag = dataElement.GetTag() | ||
216 | - data = stf.ToStringPair(tag) | ||
217 | - stag = tag.PrintAsPipeSeparatedString() | ||
218 | - | ||
219 | - group = stag.split("|")[0][1:] | ||
220 | - field = stag.split("|")[1][:-1] | ||
221 | - tag_labels[stag] = data[0] | ||
222 | - | ||
223 | - if not group in data_dict.keys(): | ||
224 | - data_dict[group] = {} | ||
225 | - | ||
226 | - if not(utils.VerifyInvalidPListCharacter(data[1])): | ||
227 | - data_dict[group][field] = data[1].decode(encoding) | ||
228 | - else: | ||
229 | - data_dict[group][field] = "Invalid Character" | 274 | + for t in threads: |
275 | + q.put(0) | ||
230 | 276 | ||
231 | - | ||
232 | - # Iterate through the Data set | ||
233 | - iterator = dataSet.GetDES().begin() | ||
234 | - while (not iterator.equal(dataSet.GetDES().end())): | ||
235 | - dataElement = iterator.next() | ||
236 | - | ||
237 | - stf.SetFile(file) | ||
238 | - tag = dataElement.GetTag() | ||
239 | - data = stf.ToStringPair(tag) | ||
240 | - stag = tag.PrintAsPipeSeparatedString() | ||
241 | - | ||
242 | - group = stag.split("|")[0][1:] | ||
243 | - field = stag.split("|")[1][:-1] | ||
244 | - tag_labels[stag] = data[0] | ||
245 | - | ||
246 | - if not group in data_dict.keys(): | ||
247 | - data_dict[group] = {} | ||
248 | - | ||
249 | - if not(utils.VerifyInvalidPListCharacter(data[1])): | ||
250 | - data_dict[group][field] = data[1].decode(encoding) | ||
251 | - else: | ||
252 | - data_dict[group][field] = "Invalid Character" | ||
253 | - | 277 | + for t in threads: |
278 | + t.join() | ||
254 | 279 | ||
255 | - | ||
256 | - # ---------- Refactory -------------------------------------- | ||
257 | - data_dict['invesalius'] = {'orientation_label' : GetImageOrientationLabel(filename)} | 280 | + #TODO: Is this commented update necessary? |
281 | + #grouper.Update() | ||
282 | + yield grouper.GetPatientsGroups() | ||
258 | 283 | ||
259 | - # ------------------------------------------------------------- | ||
260 | - filepath = os.path.abspath(filename) | ||
261 | - dict_file[filepath] = data_dict | ||
262 | - | ||
263 | - #---------- Verify is DICOMDir ------------------------------- | ||
264 | - is_dicom_dir = 1 | ||
265 | - try: | ||
266 | - if (data_dict['0002']['0002'] != "1.2.840.10008.1.3.10"): #DICOMDIR | ||
267 | - is_dicom_dir = 0 | ||
268 | - except(KeyError): | ||
269 | - is_dicom_dir = 0 | ||
270 | - | ||
271 | - if not(is_dicom_dir): | ||
272 | - parser = dicom.Parser() | ||
273 | - parser.SetDataImage(dict_file[filepath], filepath) | ||
274 | - | ||
275 | - dcm = dicom.Dicom() | ||
276 | - dcm.SetParser(parser) | ||
277 | - grouper.AddFile(dcm) | ||
278 | 284 | ||
279 | - | ||
280 | - #------- to test ------------------------------------- | ||
281 | - #main_dict = dict( | ||
282 | - # data = dict_file, | ||
283 | - # labels = tag_labels) | ||
284 | - | ||
285 | - #plistlib.writePlist(main_dict, ".//teste.plist") | ||
286 | - | ||
287 | - grouper.Update() | ||
288 | - | ||
289 | - return grouper.GetPatientsGroups() | ||
290 | - | ||
291 | - else: | ||
292 | - pass | ||
293 | - #dirpath, dirnames, filenames = os.walk(directory) | ||
294 | - #nfiles = len(filenames) | ||
295 | - | ||
296 | - #counter = 0 | ||
297 | - #grouper = dicom_grouper.DicomPatientGrouper() | ||
298 | - #q = Queue.Queue() | ||
299 | - #l = threading.Lock() | ||
300 | - #threads = [] | ||
301 | - #for i in xrange(cpu_count()): | ||
302 | - # t = LoadDicom(grouper, q, l) | ||
303 | - # t.start() | ||
304 | - # threads.append(t) | ||
305 | - ## Retrieve only DICOM files, splited into groups | ||
306 | - #if recursive: | ||
307 | - # for dirpath, dirnames, filenames in os.walk(directory): | ||
308 | - # for name in filenames: | ||
309 | - # filepath = os.path.join(dirpath, name) | ||
310 | - # counter += 1 | ||
311 | - # if gui: | ||
312 | - # yield (counter,nfiles) | ||
313 | - # q.put(filepath) | ||
314 | - #else: | ||
315 | - # dirpath, dirnames, filenames = os.walk(directory) | ||
316 | - # for name in filenames: | ||
317 | - # filepath = str(os.path.join(dirpath, name)) | ||
318 | - # counter += 1 | ||
319 | - # if gui: | ||
320 | - # yield (counter,nfiles) | ||
321 | - # q.put(filepath) | ||
322 | - | ||
323 | - #for t in threads: | ||
324 | - # q.put(0) | ||
325 | - | ||
326 | - #for t in threads: | ||
327 | - # t.join() | ||
328 | - | ||
329 | - ##TODO: Is this commented update necessary? | ||
330 | - ##grouper.Update() | ||
331 | - #yield grouper.GetPatientsGroups() | ||
332 | - | ||
333 | -#def GetDicomGroups(directory, recursive=True): | ||
334 | -# return yGetDicomGroups(directory, recursive, gui=False).next() | 285 | +def GetDicomGroups(directory, recursive=True): |
286 | + return yGetDicomGroups(directory, recursive, gui=False).next() | ||
335 | 287 | ||
336 | 288 | ||
337 | class ProgressDicomReader: | 289 | class ProgressDicomReader: |
@@ -364,7 +316,9 @@ class ProgressDicomReader: | @@ -364,7 +316,9 @@ class ProgressDicomReader: | ||
364 | fow.SetFileName(log_path) | 316 | fow.SetFileName(log_path) |
365 | ow = vtk.vtkOutputWindow() | 317 | ow = vtk.vtkOutputWindow() |
366 | ow.SetInstance(fow) | 318 | ow.SetInstance(fow) |
367 | - | 319 | + print "=====>>> Progress... dicom_reader.py 367" |
320 | + | ||
321 | + | ||
368 | y = yGetDicomGroups(path, recursive) | 322 | y = yGetDicomGroups(path, recursive) |
369 | for value_progress in y: | 323 | for value_progress in y: |
370 | if not self.running: | 324 | if not self.running: |