Commit b94711ad85011589d49f7c067eb2e03e75661156

Authored by Paulo Henrique Junqueira Amorim
1 parent 3e89f362

ENH: Removed thread to load DICOM

Showing 1 changed file with 149 additions and 150 deletions   Show diff stats
invesalius/reader/dicom_reader.py
@@ -85,162 +85,161 @@ tag_labels = {} @@ -85,162 +85,161 @@ tag_labels = {}
85 main_dict = {} 85 main_dict = {}
86 dict_file = {} 86 dict_file = {}
87 87
88 -class LoadDicom(threading.Thread): 88 +class LoadDicom:#(threading.Thread):
89 89
90 - def __init__(self, grouper, q, l):  
91 - threading.Thread.__init__(self) 90 + def __init__(self, grouper, filepath):
  91 + #threading.Thread.__init__(self)
92 self.grouper = grouper 92 self.grouper = grouper
93 - self.q = q  
94 - self.l = l 93 + self.filepath = filepath
  94 + self.run()
95 95
96 def run(self): 96 def run(self):
97 97
98 grouper = self.grouper 98 grouper = self.grouper
99 - q = self.q  
100 99
101 - while 1: 100 + #while 1:
102 101
103 - filepath = q.get()  
104 - if not filepath:  
105 - break  
106 -  
107 - reader = gdcm.Reader()  
108 - reader.SetFileName(filepath.encode(utils.get_system_encoding()))  
109 -  
110 - if (reader.Read()):  
111 - file = reader.GetFile()  
112 -  
113 - # Retrieve data set  
114 - dataSet = file.GetDataSet()  
115 -  
116 - # Retrieve header  
117 - header = file.GetHeader()  
118 - stf = gdcm.StringFilter()  
119 -  
120 - field_dict = {}  
121 - data_dict = {} 102 + #filepath = q.get()
  103 + #if not filepath:
  104 + # break
  105 +
  106 + reader = gdcm.Reader()
  107 + reader.SetFileName(self.filepath.encode(utils.get_system_encoding()))
  108 +
  109 + if (reader.Read()):
  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()
122 118
  119 + field_dict = {}
  120 + data_dict = {}
123 121
124 - tag = gdcm.Tag(0x0008, 0x0005)  
125 - ds = reader.GetFile().GetDataSet()  
126 - if ds.FindDataElement(tag):  
127 - encoding = str(ds.GetDataElement(tag).GetValue())  
128 - if not(encoding != None and encoding != "None" and encoding != "Loaded"):  
129 - encoding = "ISO_IR 100"  
130 - else:  
131 - encoding = "ISO_IR_100"  
132 - # Iterate through the Header  
133 - iterator = header.GetDES().begin()  
134 - while (not iterator.equal(header.GetDES().end())):  
135 - dataElement = iterator.next()  
136 - stf.SetFile(file)  
137 - tag = dataElement.GetTag()  
138 - data = stf.ToStringPair(tag)  
139 - stag = tag.PrintAsPipeSeparatedString()  
140 -  
141 - group = stag.split("|")[0][1:]  
142 - field = stag.split("|")[1][:-1]  
143 - tag_labels[stag] = data[0]  
144 -  
145 - if not group in data_dict.keys():  
146 - data_dict[group] = {}  
147 -  
148 - if not(utils.VerifyInvalidPListCharacter(data[1])):  
149 - data_dict[group][field] = data[1].decode(encoding)  
150 - else:  
151 - data_dict[group][field] = "Invalid Character"  
152 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()
153 139
154 - # Iterate through the Data set  
155 - iterator = dataSet.GetDES().begin()  
156 - while (not iterator.equal(dataSet.GetDES().end())):  
157 - dataElement = iterator.next()  
158 -  
159 - stf.SetFile(file)  
160 - tag = dataElement.GetTag()  
161 - data = stf.ToStringPair(tag)  
162 - stag = tag.PrintAsPipeSeparatedString()  
163 -  
164 - group = stag.split("|")[0][1:]  
165 - field = stag.split("|")[1][:-1]  
166 - tag_labels[stag] = data[0]  
167 -  
168 - if not group in data_dict.keys():  
169 - data_dict[group] = {}  
170 -  
171 - if not(utils.VerifyInvalidPListCharacter(data[1])):  
172 - data_dict[group][field] = data[1].decode(encoding)  
173 - else:  
174 - data_dict[group][field] = "Invalid Character" 140 + group = stag.split("|")[0][1:]
  141 + field = stag.split("|")[1][:-1]
  142 + tag_labels[stag] = data[0]
175 143
  144 + if not group in data_dict.keys():
  145 + data_dict[group] = {}
176 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"
177 151
178 - # -------------- To Create DICOM Thumbnail -----------  
179 - rvtk = vtkgdcm.vtkGDCMImageReader()  
180 - rvtk.SetFileName(filepath.encode(utils.get_system_encoding()))  
181 - rvtk.Update()  
182 -  
183 - try:  
184 - data = data_dict['0028']['1050']  
185 - level = [float(value) for value in data.split('\\')][0]  
186 - data = data_dict['0028']['1051']  
187 - window = [float(value) for value in data.split('\\')][0]  
188 - except(KeyError):  
189 - level = 300.0  
190 - window = 2000.0  
191 -  
192 - colorer = vtk.vtkImageMapToWindowLevelColors()  
193 - colorer.SetInput(rvtk.GetOutput())  
194 - colorer.SetWindow(float(window))  
195 - colorer.SetLevel(float(level))  
196 - colorer.SetOutputFormatToRGB()  
197 - colorer.Update() 152 +
  153 + # Iterate through the Data set
  154 + iterator = dataSet.GetDES().begin()
  155 + while (not iterator.equal(dataSet.GetDES().end())):
  156 + dataElement = iterator.next()
198 157
199 - resample = vtk.vtkImageResample()  
200 - resample.SetInput(colorer.GetOutput())  
201 - resample.SetAxisMagnificationFactor ( 0, 0.25 )  
202 - resample.SetAxisMagnificationFactor ( 1, 0.25 )  
203 - resample.SetAxisMagnificationFactor ( 2, 1 )  
204 - resample.Update()  
205 -  
206 - thumbnail_path = tempfile.mktemp() 158 + stf.SetFile(file)
  159 + tag = dataElement.GetTag()
  160 + data = stf.ToStringPair(tag)
  161 + stag = tag.PrintAsPipeSeparatedString()
207 162
208 - write_png = vtk.vtkPNGWriter()  
209 - write_png.SetInput(resample.GetOutput())  
210 - write_png.SetFileName(thumbnail_path)  
211 - write_png.Write()  
212 -  
213 - # ---------- Refactory --------------------------------------  
214 - data_dict['invesalius'] = {'orientation_label' : GetImageOrientationLabel(filepath.encode(utils.get_system_encoding()))} 163 + group = stag.split("|")[0][1:]
  164 + field = stag.split("|")[1][:-1]
  165 + tag_labels[stag] = data[0]
215 166
216 - # -------------------------------------------------------------  
217 - dict_file[filepath] = data_dict  
218 -  
219 - #---------- Verify is DICOMDir -------------------------------  
220 - is_dicom_dir = 1  
221 - try:  
222 - if (data_dict['0002']['0002'] != "1.2.840.10008.1.3.10"): #DICOMDIR  
223 - is_dicom_dir = 0  
224 - except(KeyError):  
225 - is_dicom_dir = 0  
226 -  
227 - if not(is_dicom_dir):  
228 - parser = dicom.Parser()  
229 - parser.SetDataImage(dict_file[filepath], filepath, thumbnail_path)  
230 -  
231 - dcm = dicom.Dicom()  
232 - self.l.acquire()  
233 - dcm.SetParser(parser)  
234 - grouper.AddFile(dcm)  
235 -  
236 - self.l.release() 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 + # -------------- To Create DICOM Thumbnail -----------
  178 + rvtk = vtkgdcm.vtkGDCMImageReader()
  179 + rvtk.SetFileName(self.filepath.encode(utils.get_system_encoding()))
  180 + rvtk.Update()
  181 +
  182 + try:
  183 + data = data_dict['0028']['1050']
  184 + level = [float(value) for value in data.split('\\')][0]
  185 + data = data_dict['0028']['1051']
  186 + window = [float(value) for value in data.split('\\')][0]
  187 + except(KeyError):
  188 + level = 300.0
  189 + window = 2000.0
  190 +
  191 + colorer = vtk.vtkImageMapToWindowLevelColors()
  192 + colorer.SetInput(rvtk.GetOutput())
  193 + colorer.SetWindow(float(window))
  194 + colorer.SetLevel(float(level))
  195 + colorer.SetOutputFormatToRGB()
  196 + colorer.Update()
  197 +
  198 + resample = vtk.vtkImageResample()
  199 + resample.SetInput(colorer.GetOutput())
  200 + resample.SetAxisMagnificationFactor ( 0, 0.25 )
  201 + resample.SetAxisMagnificationFactor ( 1, 0.25 )
  202 + resample.SetAxisMagnificationFactor ( 2, 1 )
  203 + resample.Update()
  204 +
  205 + thumbnail_path = tempfile.mktemp()
  206 +
  207 + write_png = vtk.vtkPNGWriter()
  208 + write_png.SetInput(resample.GetOutput())
  209 + write_png.SetFileName(thumbnail_path)
  210 + write_png.Write()
  211 +
  212 + # ---------- Refactory --------------------------------------
  213 + data_dict['invesalius'] = {'orientation_label' : GetImageOrientationLabel(self.filepath.encode(utils.get_system_encoding()))}
  214 +
  215 + # -------------------------------------------------------------
  216 + dict_file[self.filepath] = data_dict
  217 +
  218 + #---------- Verify is DICOMDir -------------------------------
  219 + is_dicom_dir = 1
  220 + try:
  221 + if (data_dict['0002']['0002'] != "1.2.840.10008.1.3.10"): #DICOMDIR
  222 + is_dicom_dir = 0
  223 + except(KeyError):
  224 + is_dicom_dir = 0
  225 +
  226 + if not(is_dicom_dir):
  227 + parser = dicom.Parser()
  228 + parser.SetDataImage(dict_file[self.filepath], self.filepath, thumbnail_path)
237 229
238 - #========== used in test =======================================  
239 - #main_dict = dict(  
240 - # data = dict_file,  
241 - # labels = tag_labels)  
242 -  
243 - #plistlib.writePlist(main_dict, ".//teste.plist")""" 230 + dcm = dicom.Dicom()
  231 + #self.l.acquire()
  232 + dcm.SetParser(parser)
  233 + grouper.AddFile(dcm)
  234 +
  235 + #self.l.release()
  236 +
  237 + #========== used in test =======================================
  238 + #main_dict = dict(
  239 + # data = dict_file,
  240 + # labels = tag_labels)
  241 +
  242 + #plistlib.writePlist(main_dict, ".//teste.plist")"""
244 243
245 244
246 def GetImageOrientationLabel(filename): 245 def GetImageOrientationLabel(filename):
@@ -282,13 +281,13 @@ def yGetDicomGroups(directory, recursive=True, gui=True): @@ -282,13 +281,13 @@ def yGetDicomGroups(directory, recursive=True, gui=True):
282 281
283 counter = 0 282 counter = 0
284 grouper = dicom_grouper.DicomPatientGrouper() 283 grouper = dicom_grouper.DicomPatientGrouper()
285 - q = Queue.Queue()  
286 - l = threading.Lock()  
287 - threads = []  
288 - for i in xrange(cpu_count()):  
289 - t = LoadDicom(grouper, q, l)  
290 - t.start()  
291 - threads.append(t) 284 + #q = Queue.Queue()
  285 + #l = threading.Lock()
  286 + #threads = []
  287 + #for i in xrange(cpu_count()):
  288 + # t = LoadDicom(grouper, q, l)
  289 + # t.start()
  290 + # threads.append(t)
292 # Retrieve only DICOM files, splited into groups 291 # Retrieve only DICOM files, splited into groups
293 if recursive: 292 if recursive:
294 for dirpath, dirnames, filenames in os.walk(directory): 293 for dirpath, dirnames, filenames in os.walk(directory):
@@ -297,7 +296,7 @@ def yGetDicomGroups(directory, recursive=True, gui=True): @@ -297,7 +296,7 @@ def yGetDicomGroups(directory, recursive=True, gui=True):
297 counter += 1 296 counter += 1
298 if gui: 297 if gui:
299 yield (counter,nfiles) 298 yield (counter,nfiles)
300 - q.put(filepath) 299 + LoadDicom(grouper, filepath)
301 else: 300 else:
302 dirpath, dirnames, filenames = os.walk(directory) 301 dirpath, dirnames, filenames = os.walk(directory)
303 for name in filenames: 302 for name in filenames:
@@ -305,13 +304,13 @@ def yGetDicomGroups(directory, recursive=True, gui=True): @@ -305,13 +304,13 @@ def yGetDicomGroups(directory, recursive=True, gui=True):
305 counter += 1 304 counter += 1
306 if gui: 305 if gui:
307 yield (counter,nfiles) 306 yield (counter,nfiles)
308 - q.put(filepath) 307 + #q.put(filepath)
309 308
310 - for t in threads:  
311 - q.put(0) 309 + #for t in threads:
  310 + # q.put(0)
312 311
313 - for t in threads:  
314 - t.join() 312 + #for t in threads:
  313 + # t.join()
315 314
316 #TODO: Is this commented update necessary? 315 #TODO: Is this commented update necessary?
317 #grouper.Update() 316 #grouper.Update()