dicom_reader.py
6.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#--------------------------------------------------------------------------
# Software: InVesalius - Software de Reconstrucao 3D de Imagens Medicas
# Copyright: (C) 2001 Centro de Pesquisas Renato Archer
# Homepage: http://www.softwarepublico.gov.br
# Contact: invesalius@cti.gov.br
# License: GNU - GPL 2 (LICENSE.txt/LICENCA.txt)
#--------------------------------------------------------------------------
# Este programa e software livre; voce pode redistribui-lo e/ou
# modifica-lo sob os termos da Licenca Publica Geral GNU, conforme
# publicada pela Free Software Foundation; de acordo com a versao 2
# da Licenca.
#
# Este programa eh distribuido na expectativa de ser util, mas SEM
# QUALQUER GARANTIA; sem mesmo a garantia implicita de
# COMERCIALIZACAO ou de ADEQUACAO A QUALQUER PROPOSITO EM
# PARTICULAR. Consulte a Licenca Publica Geral GNU para obter mais
# detalhes.
#--------------------------------------------------------------------------
import os
import Queue
import threading
from multiprocessing import cpu_count
import vtk
import gdcm
import wx.lib.pubsub as ps
import constants as const
import dicom
import dicom_grouper
import session
def ReadDicomGroup(dir_):
patient_group = GetDicomGroups(dir_)
if len(patient_group) > 0:
filelist, dicom, zspacing = SelectLargerDicomGroup(patient_group)
filelist = SortFiles(filelist, dicom)
size = dicom.image.size
bits = dicom.image.bits_allocad
imagedata = CreateImageData(filelist, zspacing, size, bits)
session.Session().project_status = const.NEW_PROJECT
return imagedata, dicom
else:
return False
def SelectLargerDicomGroup(patient_group):
maxslices = 0
for patient in patient_group:
group_list = patient.GetGroups()
for group in group_list:
if group.nslices > maxslices:
maxslices = group.nslices
larger_group = group
return larger_group
def SortFiles(filelist, dicom):
# Sort slices
# FIXME: Coronal Crash. necessary verify
if (dicom.image.orientation_label <> "CORONAL"):
#Organize reversed image
sorter = gdcm.IPPSorter()
sorter.SetComputeZSpacing(True)
sorter.SetZSpacingTolerance(1e-10)
sorter.Sort(filelist)
#Getting organized image
filelist = sorter.GetFilenames()
return filelist
class LoadDicom(threading.Thread):
def __init__(self, grouper, q, l):
threading.Thread.__init__(self)
self.grouper = grouper
self.q = q
self.l = l
def run(self):
grouper = self.grouper
q = self.q
while 1:
filepath = q.get()
if not filepath:
break
parser = dicom.Parser()
if parser.SetFileName(filepath):
dcm = dicom.Dicom()
self.l.acquire()
dcm.SetParser(parser)
grouper.AddFile(dcm)
self.l.release()
def yGetDicomGroups(directory, recursive=True, gui=True):
"""
Return all full paths to DICOM files inside given directory.
"""
nfiles = 0
# Find total number of files
if recursive:
for dirpath, dirnames, filenames in os.walk(directory):
nfiles += len(filenames)
else:
dirpath, dirnames, filenames = os.walk(directory)
nfiles = len(filenames)
counter = 0
grouper = dicom_grouper.DicomPatientGrouper()
q = Queue.Queue()
l = threading.Lock()
threads = []
for i in xrange(cpu_count()):
t = LoadDicom(grouper, q, l)
t.start()
threads.append(t)
# Retrieve only DICOM files, splited into groups
if recursive:
for dirpath, dirnames, filenames in os.walk(directory):
for name in filenames:
filepath = str(os.path.join(dirpath, name))
counter += 1
if gui:
yield (counter,nfiles)
q.put(filepath)
else:
dirpath, dirnames, filenames = os.walk(directory)
for name in filenames:
filepath = str(os.path.join(dirpath, name))
counter += 1
if gui:
yield (counter,nfiles)
q.put(filepath)
for t in threads:
q.put(0)
for t in threads:
t.join()
#TODO: Is this commented update necessary?
#grouper.Update()
yield grouper.GetPatientsGroups()
def GetDicomGroups(directory, recursive=True):
return yGetDicomGroups(directory, recursive, gui=False).next()
class ProgressDicomReader:
def __init__(self):
ps.Publisher().subscribe(self.CancelLoad, "Cancel DICOM load")
def CancelLoad(self, evt_pubsub):
self.running = False
self.stoped = True
def SetWindowEvent(self, frame):
self.frame = frame
def SetDirectoryPath(self, path,recursive=True):
self.running = True
self.stoped = False
self.GetDicomGroups(path,recursive)
def UpdateLoadFileProgress(self,cont_progress):
ps.Publisher().sendMessage("Update dicom load", cont_progress)
def EndLoadFile(self, patient_list):
ps.Publisher().sendMessage("End dicom load", patient_list)
def GetDicomGroups(self, path, recursive):
if not const.VTK_WARNING:
log_path = os.path.join(const.LOG_FOLDER, 'vtkoutput.txt')
fow = vtk.vtkFileOutputWindow()
fow.SetFileName(log_path)
ow = vtk.vtkOutputWindow()
ow.SetInstance(fow)
y = yGetDicomGroups(path, recursive)
for value_progress in y:
if not self.running:
break
if isinstance(value_progress, tuple):
self.UpdateLoadFileProgress(value_progress)
else:
self.EndLoadFile(value_progress)
#Is necessary in the case user cancel
#the load, ensure that dicomdialog is closed
if(self.stoped):
self.UpdateLoadFileProgress(None)
self.stoped = False