Commit 69febb4211ba18f2dafb8f536e64505f6cdaade2

Authored by Paulo Henrique Junqueira Amorim
1 parent 3174c58f

ENH: Add thread to read dicom

invesalius/control.py
... ... @@ -22,6 +22,7 @@ class Controller():
22 22 self.surface_manager = surface.SurfaceManager()
23 23 self.volume = volume.Volume()
24 24 self.__bind_events()
  25 + self.frame = frame
25 26  
26 27 def __bind_events(self):
27 28 ps.Publisher().subscribe(self.OnImportMedicalImages, 'Import directory')
... ... @@ -36,9 +37,25 @@ class Controller():
36 37 def StartImportPanel(self, pubsub_evt):
37 38 # path to directory
38 39 path = pubsub_evt.data
39   -
40 40 # retrieve DICOM files splited into groups
41   - patient_series = dcm.GetDicomGroups(path)
  41 +
  42 + reader = dcm.ProgressDicomReader()
  43 + reader.SetWindowEvent(self.frame)
  44 + reader.SetDirectoryPath(str(path))
  45 +
  46 + self.frame.Bind(dicomgroups.evt_update_progress, self.Progress)
  47 + self.frame.Bind(dicomgroups.evt_end_load_file, self.LoadPanel)
  48 + #thread.start_new_thread(t.GetDicomGroups, (path,True, lock))
  49 +
  50 + def Progress(self, evt):
  51 + print evt.progress
  52 + print "AAAAA"
  53 + #ps.Publisher().sendMessage("Progress Import")
  54 +
  55 + def LoadPanel(self,evt):
  56 + print "LoadPanel"
  57 + print evt.value
  58 + patient_series = evt.value
42 59 if patient_series:
43 60 ps.Publisher().sendMessage("Load import panel", patient_series)
44 61 first_patient = patient_series[0]
... ... @@ -46,6 +63,8 @@ class Controller():
46 63 else:
47 64 print "No DICOM files on directory"
48 65  
  66 +
  67 +
49 68 def OnImportMedicalImages(self, pubsub_evt):
50 69 directory = pubsub_evt.data
51 70 self.ImportMedicalImages(directory)
... ... @@ -53,6 +72,7 @@ class Controller():
53 72 def ImportMedicalImages(self, directory):
54 73 # OPTION 1: DICOM?
55 74 patients_groups = dcm.GetDicomGroups(directory)
  75 +
56 76 if len(patients_groups):
57 77 group = dcm.SelectLargerDicomGroup(patients_groups)
58 78 imagedata, dicom = self.OpenDicomGroup(group, gui=False)
... ...
invesalius/reader/dicom_reader.py
... ... @@ -16,12 +16,16 @@
16 16 # PARTICULAR. Consulte a Licenca Publica Geral GNU para obter mais
17 17 # detalhes.
18 18 #--------------------------------------------------------------------------
  19 +from vtk.util.colors import yellow
19 20 import glob
20 21 import os
21 22  
22 23 import vtk
23 24 import vtkgdcm
24 25 import gdcm
  26 +import thread
  27 +import wx
  28 +import wx.lib.pubsub as ps
25 29  
26 30 import constants as const
27 31 import dicom
... ... @@ -29,8 +33,9 @@ import dicom_grouper
29 33 import data.imagedata_utils as iu
30 34  
31 35 def ReadDicomGroup(dir_):
32   -
  36 +
33 37 patient_group = GetDicomGroups(dir_)
  38 +
34 39 if len(patient_group) > 0:
35 40 filelist, dicom, zspacing = SelectLargerDicomGroup(patient_group)
36 41 filelist = SortFiles(filelist, dicom)
... ... @@ -65,13 +70,10 @@ def SortFiles(filelist, dicom):
65 70 return filelist
66 71  
67 72  
68   -
69   -def GetDicomGroups(directory, recursive=True):
  73 +def yGetDicomGroups(directory, recursive=True, gui=True):
70 74 """
71 75 Return all full paths to DICOM files inside given directory.
72 76 """
73   - grouper = dicom_grouper.DicomPatientGrouper()
74   -
75 77 nfiles = 0
76 78 # Find total number of files
77 79 if recursive:
... ... @@ -80,8 +82,10 @@ def GetDicomGroups(directory, recursive=True):
80 82 else:
81 83 dirpath, dirnames, filenames = os.walk(directory)
82 84 nfiles = len(filenames)
  85 +
83 86 print "TOTAL FILES:", nfiles
84   -
  87 + counter = 0.0
  88 + grouper = dicom_grouper.DicomPatientGrouper()
85 89 # Retrieve only DICOM files, splited into groups
86 90 if recursive:
87 91 for dirpath, dirnames, filenames in os.walk(directory):
... ... @@ -89,6 +93,9 @@ def GetDicomGroups(directory, recursive=True):
89 93 for name in filenames:
90 94 filepath = str(os.path.join(dirpath, name))
91 95 parser = dicom.Parser()
  96 + counter += 1
  97 + if gui:
  98 + yield counter/nfiles*100
92 99 if parser.SetFileName(filepath):
93 100 dcm = dicom.Dicom()
94 101 dcm.SetParser(parser)
... ... @@ -99,10 +106,100 @@ def GetDicomGroups(directory, recursive=True):
99 106 for name in filenames:
100 107 filepath = str(os.path.join(dirpath, name))
101 108 parser = dicom.Parser()
  109 + counter += 1
  110 + if gui:
  111 + yield counter/nfiles*100
102 112 if parser.SetFileName(filepath):
103 113 dcm = dicom.Dicom()
104 114 dcm.SetParser(parser)
105 115 grouper.AddFile(dcm)
106 116  
107   - return grouper.GetPatientsGroups()
  117 + yield grouper.GetPatientsGroups()
  118 +
  119 +def GetDicomGroups(directory, recursive=True):
  120 + return yGetDicomGroups(directory, recursive, gui=False).next()
  121 +
  122 +class ProgressDicomReader:
  123 +
  124 + def __init__(self):
  125 + (self.LoadFilesProgress, EVT_LOAD_FILE_PROGRESS) = wx.lib.newevent.NewEvent()
  126 + (self.EndLoadFiles, EVT_END_LOAD_FILE) = wx.lib.newevent.NewEvent()
  127 +
  128 + self.evt_update_progress = EVT_LOAD_FILE_PROGRESS
  129 + self.evt_end_load_file = EVT_END_LOAD_FILE
  130 +
  131 + def SetWindowEvent(self, frame):
  132 + self.frame = frame
  133 +
  134 + def SetDirectoryPath(self, path,recursive=True):
  135 + self.running = True
  136 + print "1",path,recursive
  137 + thread.start_new_thread(self.GetDicomGroupsIntern,(path,recursive))
  138 +
  139 + def UpdateLoadFileProgress(self,cont_progress):
  140 + evt = self.LoadFilesProgress(progress = cont_progress)
  141 + wx.PostEvent(self.frame, evt)
  142 +
  143 + def EndLoadFile(self, grouper):
  144 + evt = self.EndLoadFiles(value = grouper)
  145 + wx.PostEvent(self.frame, evt)
  146 +
  147 + def GetDicomGroups(self, path, recursive):
  148 + y = yGetDicomGroups(path, recursive)
  149 + while self.running:
  150 + value_progress = y.next()
  151 + if isinstance(value_progress, float):
  152 + self.UpdateLoadFileProgress(value_progress)
  153 + else:
  154 + print "____________________"
  155 + print value_progress
  156 + self.EndLoadFile(value_progress)
  157 + self.running = False
  158 +
  159 + def GetDicomGroupsOld(self):
  160 + """
  161 + Return all full paths to DICOM files inside given directory.
  162 + """
  163 + while self.running:
  164 + grouper = dicom_grouper.DicomPatientGrouper()
  165 + dirpath = directory = self.dirpath
  166 + nfiles = 0
  167 + # Find total number of files
  168 + if self.recursive:
  169 + for dirpath, dirnames, filenames in os.walk(directory):
  170 + nfiles += len(filenames)
  171 + else:
  172 + dirpath, dirnames, filenames = os.walk(directory)
  173 + nfiles = len(filenames)
  174 +
  175 + print "TOTAL FILES:", nfiles
  176 + cont_progress = 0
  177 + # Retrieve only DICOM files, splited into groups
  178 + if self.recursive:
  179 + for dirpath, dirnames, filenames in os.walk(directory):
  180 + print "@: ",dirpath
  181 + for name in filenames:
  182 + filepath = str(os.path.join(dirpath, name))
  183 + parser = dicom.Parser()
  184 +
  185 + cont_progress += 1
  186 + self.UpdateLoadFileProgress(cont_progress, nfiles)
  187 +
  188 + if parser.SetFileName(filepath):
  189 + dcm = dicom.Dicom()
  190 + dcm.SetParser(parser)
  191 + grouper.AddFile(dcm)
  192 + else:
  193 + dirpath, dirnames, filenames = os.walk(directory)
  194 + print "@: ",dirpath
  195 + for name in filenames:
  196 + filepath = str(os.path.join(dirpath, name))
  197 + parser = dicom.Parser()
  198 + if parser.SetFileName(filepath):
  199 + dcm = dicom.Dicom()
  200 + dcm.SetParser(parser)
  201 + grouper.AddFile(dcm)
  202 + self.running = False
  203 + self.EndLoadFile(grouper)
  204 + #return grouper.GetPatientsGroups()
108 205  
... ...