Commit d9ef2963d41d5c340adaf26c1e32d2ef4555ff30

Authored by tatiana
1 parent f4bb8776

FIX: Enabled recursion while reading DICOM directory

Showing 1 changed file with 44 additions and 12 deletions   Show diff stats
invesalius/reader/dicom_reader.py
@@ -32,6 +32,7 @@ def LoadImages(dir_): @@ -32,6 +32,7 @@ def LoadImages(dir_):
32 # TODO!!! SUPER GAMBIARRA!!! DO THIS BETTER 32 # TODO!!! SUPER GAMBIARRA!!! DO THIS BETTER
33 33
34 patient_group = GetDicomFiles(dir_) 34 patient_group = GetDicomFiles(dir_)
  35 + print "1111111111111"
35 #select the series with the largest 36 #select the series with the largest
36 #number of slices. 37 #number of slices.
37 nslices_old = 0 38 nslices_old = 0
@@ -39,15 +40,19 @@ def LoadImages(dir_): @@ -39,15 +40,19 @@ def LoadImages(dir_):
39 group_list = patient.GetGroups() 40 group_list = patient.GetGroups()
40 for group in group_list: 41 for group in group_list:
41 nslices = group.nslices 42 nslices = group.nslices
  43 + print "nslices:", nslices
42 if (nslices >= nslices_old): 44 if (nslices >= nslices_old):
43 dicoms = group.GetList() 45 dicoms = group.GetList()
44 spacing = group.spacing 46 spacing = group.spacing
45 nslices_old = nslices 47 nslices_old = nslices
  48 + print "22222222222222"
46 49
47 file_list = [] 50 file_list = []
48 for dicom in dicoms: 51 for dicom in dicoms:
49 file_list.append(dicom.image.file) 52 file_list.append(dicom.image.file)
50 53
  54 +
  55 +
51 #Coronal Crash. necessary verify 56 #Coronal Crash. necessary verify
52 if (dicom.image.orientation_label <> "CORONAL"): 57 if (dicom.image.orientation_label <> "CORONAL"):
53 #Organize reversed image 58 #Organize reversed image
@@ -59,12 +64,15 @@ def LoadImages(dir_): @@ -59,12 +64,15 @@ def LoadImages(dir_):
59 #Getting organized image 64 #Getting organized image
60 files = sorter.GetFilenames() 65 files = sorter.GetFilenames()
61 66
  67 + print "33333333333"
  68 +
62 array = vtk.vtkStringArray() 69 array = vtk.vtkStringArray()
63 70
64 img_app = vtk.vtkImageAppend() 71 img_app = vtk.vtkImageAppend()
65 img_app.SetAppendAxis(2) #Define Stack in Z 72 img_app.SetAppendAxis(2) #Define Stack in Z
66 73
67 if not(const.REDUCE_IMAGEDATA_QUALITY): 74 if not(const.REDUCE_IMAGEDATA_QUALITY):
  75 + print "not reduce"
68 for x in xrange(len(files)): 76 for x in xrange(len(files)):
69 array.InsertValue(x,files[x]) 77 array.InsertValue(x,files[x])
70 78
@@ -77,6 +85,7 @@ def LoadImages(dir_): @@ -77,6 +85,7 @@ def LoadImages(dir_):
77 85
78 image_data.SetSpacing(spacing) 86 image_data.SetSpacing(spacing)
79 else: 87 else:
  88 + print "reduce"
80 for x in xrange(len(files)): 89 for x in xrange(len(files)):
81 #SIf the resolution of the 90 #SIf the resolution of the
82 #matrix is very large 91 #matrix is very large
@@ -91,12 +100,16 @@ def LoadImages(dir_): @@ -91,12 +100,16 @@ def LoadImages(dir_):
91 img_app.AddInput(img) 100 img_app.AddInput(img)
92 img_app.Update() 101 img_app.Update()
93 102
  103 +
  104 + print "444444444444444"
94 image_data = vtk.vtkImageData() 105 image_data = vtk.vtkImageData()
95 image_data.DeepCopy(img_app.GetOutput()) 106 image_data.DeepCopy(img_app.GetOutput())
96 image_data.SetSpacing(image_data.GetSpacing()[0],\ 107 image_data.SetSpacing(image_data.GetSpacing()[0],\
97 image_data.GetSpacing()[1], spacing) 108 image_data.GetSpacing()[1], spacing)
  109 + print "555555555"
98 110
99 image_data.Update() 111 image_data.Update()
  112 + print "66666666666666"
100 113
101 return image_data, dicom 114 return image_data, dicom
102 115
@@ -144,24 +157,43 @@ def GetSeries(path): @@ -144,24 +157,43 @@ def GetSeries(path):
144 157
145 return dcm_series.GetOutput() 158 return dcm_series.GetOutput()
146 159
147 -def GetDicomFiles(path): 160 +def GetDicomFiles(directory, recursive=True):
148 """ 161 """
149 Return all full paths to DICOM files inside given directory. 162 Return all full paths to DICOM files inside given directory.
150 """ 163 """
151 - list_paths = os.walk(path)  
152 -  
153 - # FIXME: Currently recursion is not working  
154 - # Recursivelly, find all files inside this folder  
155 grouper = dicom_grouper.DicomPatientGrouper() 164 grouper = dicom_grouper.DicomPatientGrouper()
156 - for p in list_paths:  
157 - p_file_list = p[-1]  
158 - file_path = p[0]  
159 - for filename in p_file_list:  
160 - file = str(os.path.join(file_path,filename)) 165 +
  166 + nfiles = 0
  167 + # Find total number of files
  168 + if 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 + print "TOTAL FILES:", nfiles
  175 +
  176 + # Retrieve only DICOM files, splited into groups
  177 + if recursive:
  178 + for dirpath, dirnames, filenames in os.walk(directory):
  179 + print "@: ",dirpath
  180 + for name in filenames:
  181 + filepath = str(os.path.join(dirpath, name))
  182 + parser = dicom.Parser()
  183 + if parser.SetFileName(filepath):
  184 + dcm = dicom.Dicom()
  185 + dcm.SetParser(parser)
  186 + grouper.AddFile(dcm)
  187 + else:
  188 + dirpath, dirnames, filenames = os.walk(directory)
  189 + print "@: ",dirpath
  190 + for name in filenames:
  191 + filepath = str(os.path.join(dirpath, name))
161 parser = dicom.Parser() 192 parser = dicom.Parser()
162 - if (parser.SetFileName(file)): 193 + if parser.SetFileName(filepath):
163 dcm = dicom.Dicom() 194 dcm = dicom.Dicom()
164 dcm.SetParser(parser) 195 dcm.SetParser(parser)
165 grouper.AddFile(dcm) 196 grouper.AddFile(dcm)
166 - 197 +
167 return grouper.GetPatientsGroups() 198 return grouper.GetPatientsGroups()
  199 +