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 32 # TODO!!! SUPER GAMBIARRA!!! DO THIS BETTER
33 33  
34 34 patient_group = GetDicomFiles(dir_)
  35 + print "1111111111111"
35 36 #select the series with the largest
36 37 #number of slices.
37 38 nslices_old = 0
... ... @@ -39,15 +40,19 @@ def LoadImages(dir_):
39 40 group_list = patient.GetGroups()
40 41 for group in group_list:
41 42 nslices = group.nslices
  43 + print "nslices:", nslices
42 44 if (nslices >= nslices_old):
43 45 dicoms = group.GetList()
44 46 spacing = group.spacing
45 47 nslices_old = nslices
  48 + print "22222222222222"
46 49  
47 50 file_list = []
48 51 for dicom in dicoms:
49 52 file_list.append(dicom.image.file)
50 53  
  54 +
  55 +
51 56 #Coronal Crash. necessary verify
52 57 if (dicom.image.orientation_label <> "CORONAL"):
53 58 #Organize reversed image
... ... @@ -59,12 +64,15 @@ def LoadImages(dir_):
59 64 #Getting organized image
60 65 files = sorter.GetFilenames()
61 66  
  67 + print "33333333333"
  68 +
62 69 array = vtk.vtkStringArray()
63 70  
64 71 img_app = vtk.vtkImageAppend()
65 72 img_app.SetAppendAxis(2) #Define Stack in Z
66 73  
67 74 if not(const.REDUCE_IMAGEDATA_QUALITY):
  75 + print "not reduce"
68 76 for x in xrange(len(files)):
69 77 array.InsertValue(x,files[x])
70 78  
... ... @@ -77,6 +85,7 @@ def LoadImages(dir_):
77 85  
78 86 image_data.SetSpacing(spacing)
79 87 else:
  88 + print "reduce"
80 89 for x in xrange(len(files)):
81 90 #SIf the resolution of the
82 91 #matrix is very large
... ... @@ -91,12 +100,16 @@ def LoadImages(dir_):
91 100 img_app.AddInput(img)
92 101 img_app.Update()
93 102  
  103 +
  104 + print "444444444444444"
94 105 image_data = vtk.vtkImageData()
95 106 image_data.DeepCopy(img_app.GetOutput())
96 107 image_data.SetSpacing(image_data.GetSpacing()[0],\
97 108 image_data.GetSpacing()[1], spacing)
  109 + print "555555555"
98 110  
99 111 image_data.Update()
  112 + print "66666666666666"
100 113  
101 114 return image_data, dicom
102 115  
... ... @@ -144,24 +157,43 @@ def GetSeries(path):
144 157  
145 158 return dcm_series.GetOutput()
146 159  
147   -def GetDicomFiles(path):
  160 +def GetDicomFiles(directory, recursive=True):
148 161 """
149 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 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 192 parser = dicom.Parser()
162   - if (parser.SetFileName(file)):
  193 + if parser.SetFileName(filepath):
163 194 dcm = dicom.Dicom()
164 195 dcm.SetParser(parser)
165 196 grouper.AddFile(dcm)
166   -
  197 +
167 198 return grouper.GetPatientsGroups()
  199 +
... ...