Commit 75348ea4ec0a625f865d08024aa18e655c755741
1 parent
fb59d2bd
Exists in
master
and in
68 other branches
ADD: Part of the implementation new DICOM Class
Showing
2 changed files
with
58 additions
and
62 deletions
Show diff stats
invesalius/reader/dicom_grouper.py
... | ... | @@ -51,7 +51,7 @@ |
51 | 51 | # <dicom.image.number> and <dicom.acquisition.series_number> |
52 | 52 | # were swapped |
53 | 53 | |
54 | -ORIENT_MAP = {"SAGITTAL":0, "AXIAL":1, "CORONAL":2} | |
54 | +ORIENT_MAP = {"SAGITTAL":0, "CORONAL":1, "AXIAL":2} | |
55 | 55 | |
56 | 56 | |
57 | 57 | class DicomGroup: |
... | ... | @@ -65,10 +65,12 @@ class DicomGroup: |
65 | 65 | # IDEA (13/10): Represent internally as dictionary, |
66 | 66 | # externally as list |
67 | 67 | self.nslices = 0 |
68 | - | |
68 | + self.spacing = 0 | |
69 | + | |
69 | 70 | def AddSlice(self, dicom): |
70 | - pos = dicom.image.position | |
71 | + pos = tuple(dicom.image.position) | |
71 | 72 | if pos not in self.slices_dict.keys(): |
73 | + | |
72 | 74 | self.slices_dict[pos] = dicom |
73 | 75 | self.nslices += 1 |
74 | 76 | return True |
... | ... | @@ -84,12 +86,27 @@ class DicomGroup: |
84 | 86 | def GetSortedList(self): |
85 | 87 | # This will be used to fix problem 1, after merging |
86 | 88 | # single DicomGroups of same study_id and orientation |
87 | - list_ = self.slices_dict_values() | |
88 | - axis = ORIENT_MAP[self.key[3]] | |
89 | + list_ = self.slices_dict.values() | |
90 | + dicom = list_[0] | |
91 | + axis = ORIENT_MAP[dicom.image.orientation_label] | |
89 | 92 | list_ = sorted(list_, key = lambda dicom:dicom.image.position[axis]) |
90 | 93 | return list_ |
91 | 94 | |
92 | - | |
95 | + def GetSpacing(self): | |
96 | + list_ = self.GetSortedList() | |
97 | + if (len(list_) > 1): | |
98 | + dicom = list_[0] | |
99 | + axis = ORIENT_MAP[dicom.image.orientation_label] | |
100 | + #print dicom.image.orientation_label | |
101 | + p1 = dicom.image.position[axis] | |
102 | + | |
103 | + dicom = list_[1] | |
104 | + p2 = dicom.image.position[axis] | |
105 | + | |
106 | + self.spacing = abs(p1 - p2) | |
107 | + else: | |
108 | + self.spacing = 1 | |
109 | + | |
93 | 110 | class PatientGroup: |
94 | 111 | def __init__(self): |
95 | 112 | # key: |
... | ... | @@ -109,7 +126,7 @@ class PatientGroup: |
109 | 126 | # implemented, dinamically during new dicom's addition |
110 | 127 | group_key = (dicom.patient.name, |
111 | 128 | dicom.acquisition.id_study, |
112 | - dicom.acquisition.series_number, | |
129 | + dicom.acquisition.serie_number, | |
113 | 130 | dicom.image.orientation_label, |
114 | 131 | index) # This will be used to deal with Problem 2 |
115 | 132 | |
... | ... | @@ -120,13 +137,15 @@ class PatientGroup: |
120 | 137 | self.groups_dict[group_key] = group |
121 | 138 | # Group exists... Lets try to add slice |
122 | 139 | else: |
123 | - group = self.groups_dicom[group_key] | |
140 | + group = self.groups_dict[group_key] | |
124 | 141 | slice_added = group.AddSlice(dicom) |
125 | 142 | if not slice_added: |
126 | 143 | # If we're here, then Problem 2 occured |
127 | 144 | # TODO: Optimize recursion |
128 | 145 | self.AddFile(file_path, index+1) |
129 | - | |
146 | + | |
147 | + group.GetSpacing() | |
148 | + | |
130 | 149 | def Update(self): |
131 | 150 | # Ideally, AddFile would be sufficient for splitting DICOM |
132 | 151 | # files into groups (series). However, this does not work for |
... | ... | @@ -144,7 +163,7 @@ class PatientGroup: |
144 | 163 | # Fix Problem 1 |
145 | 164 | if is_there_problem_1: |
146 | 165 | self.groups_dict = self.FixProblem1(self.groups_dict) |
147 | - | |
166 | + | |
148 | 167 | def GetGroups(self): |
149 | 168 | return self.groups_dict.values() |
150 | 169 | |
... | ... | @@ -231,18 +250,18 @@ class DicomPatientGrouper: |
231 | 250 | # ... (repeat to all files on folder) |
232 | 251 | # grouper.Update() |
233 | 252 | # groups = GetPatientGroups() |
234 | - | |
253 | + | |
235 | 254 | def __init__(self): |
236 | 255 | self.patients_dict = {} |
237 | - | |
256 | + | |
238 | 257 | def AddFile(self, dicom): |
239 | 258 | patient_key = (dicom.patient.name, |
240 | 259 | dicom.patient.id) |
241 | - | |
260 | + | |
242 | 261 | # Does this patient exist? |
243 | 262 | if patient_key not in self.patients_dict.keys(): |
244 | 263 | patient = PatientGroup() |
245 | - patient.AddSlice(dicom) | |
264 | + patient.AddFile(dicom) | |
246 | 265 | self.patients_dict[patient_key] = patient |
247 | 266 | # Patient exists... Lets add group to it |
248 | 267 | else: |
... | ... | @@ -252,7 +271,7 @@ class DicomPatientGrouper: |
252 | 271 | def Update(self): |
253 | 272 | for patient in self.patients_dict.values(): |
254 | 273 | patient.Update() |
255 | - | |
274 | + | |
256 | 275 | def GetPatientsGroups(self): |
257 | 276 | """ |
258 | 277 | How to use: | ... | ... |
invesalius/reader/dicom_reader.py
... | ... | @@ -31,41 +31,22 @@ from data.imagedata_utils import ResampleImage2D |
31 | 31 | def LoadImages(dir_): |
32 | 32 | # TODO!!! SUPER GAMBIARRA!!! DO THIS BETTER |
33 | 33 | |
34 | - dcm_files = GetDicomFiles(dir_) | |
35 | - | |
36 | - dcm_series = dicom_grouper.DicomGroups() | |
37 | - dcm_series.SetFileList(dcm_files) | |
38 | - dcm_series.Update() | |
39 | - | |
40 | - groups = dcm_series.GetOutput() | |
41 | - | |
42 | - tmp_list = [] | |
43 | - list_files = [] | |
44 | - | |
45 | - for x in xrange(len(groups.keys())): | |
46 | - key = groups.keys()[x] | |
47 | - | |
48 | - for y in xrange(len(groups[key])): | |
49 | - dicom = groups[key][y] | |
50 | - file = dicom.image.file | |
51 | - tmp_list.append(file) | |
52 | - | |
53 | - list_files.append([len(tmp_list), key]) | |
54 | - tmp_list = [] | |
55 | - | |
56 | - if list_files: | |
57 | - key = max(list_files)[1] | |
58 | - else: | |
59 | - return None | |
60 | - | |
34 | + patient_group = GetDicomFiles(dir_) | |
35 | + | |
36 | + for patient in patient_group: | |
37 | + group_list = patient.GetGroups() | |
38 | + for group in group_list: | |
39 | + d = group.GetList() | |
40 | + spacing = group.spacing | |
41 | + | |
42 | + #dcm_series = dicom_grouper.DicomGroup() | |
43 | + #dcm_series.SetFileList(dcm_files) | |
44 | + #dcm_series.Update() | |
45 | + | |
61 | 46 | file_list = [] |
62 | - for x in xrange(len(groups[key])): | |
63 | - dicom = groups[key][x] | |
47 | + for dicom in d: | |
64 | 48 | file_list.append(dicom.image.file) |
65 | 49 | |
66 | - files = file_list | |
67 | - spacing = dicom.image.spacing | |
68 | - | |
69 | 50 | #Coronal Crash. necessary verify |
70 | 51 | if (dicom.image.orientation_label <> "CORONAL"): |
71 | 52 | #Organize reversed image |
... | ... | @@ -92,7 +73,7 @@ def LoadImages(dir_): |
92 | 73 | |
93 | 74 | image_data = vtk.vtkImageData() |
94 | 75 | image_data.DeepCopy(read.GetOutput()) |
95 | - spacing = dicom.image.spacing | |
76 | + | |
96 | 77 | image_data.SetSpacing(spacing) |
97 | 78 | else: |
98 | 79 | for x in xrange(len(files)): |
... | ... | @@ -112,8 +93,7 @@ def LoadImages(dir_): |
112 | 93 | image_data = vtk.vtkImageData() |
113 | 94 | image_data.DeepCopy(img_app.GetOutput()) |
114 | 95 | image_data.SetSpacing(image_data.GetSpacing()[0],\ |
115 | - image_data.GetSpacing()[1],\ | |
116 | - dicom.image.spacing[2]) | |
96 | + image_data.GetSpacing()[1], spacing) | |
117 | 97 | |
118 | 98 | image_data.Update() |
119 | 99 | |
... | ... | @@ -171,19 +151,16 @@ def GetDicomFiles(path): |
171 | 151 | |
172 | 152 | # FIXME: Currently recursion is not working |
173 | 153 | # Recursivelly, find all files inside this folder |
174 | - file_list = [] | |
154 | + grouper = dicom_grouper.DicomPatientGrouper() | |
175 | 155 | for p in list_paths: |
176 | 156 | p_file_list = p[-1] |
177 | 157 | file_path = p[0] |
178 | 158 | for filename in p_file_list: |
179 | - file_list.append(str(os.path.join(file_path,filename))) | |
180 | - | |
181 | - # Check if given file is in DICOM format | |
182 | - dicom_files = [] | |
183 | - for file in file_list: | |
184 | - read_dicom = dicom.Parser() | |
185 | - if (read_dicom.SetFileName(file)): | |
186 | - dicom_files.append(file) | |
187 | - | |
188 | - | |
189 | - return dicom_files | |
159 | + file = str(os.path.join(file_path,filename)) | |
160 | + parser = dicom.Parser() | |
161 | + if (parser.SetFileName(file)): | |
162 | + dcm = dicom.Dicom() | |
163 | + dcm.SetParser(parser) | |
164 | + grouper.AddFile(dcm) | |
165 | + | |
166 | + return grouper.GetPatientsGroups() | ... | ... |