|
a |
|
b/.ipynb_checkpoints/3d-ircadb-01_util-checkpoint.ipynb |
|
|
1 |
{ |
|
|
2 |
"cells": [ |
|
|
3 |
{ |
|
|
4 |
"cell_type": "code", |
|
|
5 |
"execution_count": 1, |
|
|
6 |
"metadata": {}, |
|
|
7 |
"outputs": [], |
|
|
8 |
"source": [ |
|
|
9 |
"import os, glob\n", |
|
|
10 |
"import sys\n", |
|
|
11 |
"import copy \n", |
|
|
12 |
"import pydicom\n", |
|
|
13 |
"import scipy\n", |
|
|
14 |
"import scipy.misc\n", |
|
|
15 |
"import numpy as np\n", |
|
|
16 |
"import cv2\n", |
|
|
17 |
"import imageio\n", |
|
|
18 |
"from scipy.ndimage import rotate\n", |
|
|
19 |
"from PIL import Image\n", |
|
|
20 |
"from zipfile import ZipFile" |
|
|
21 |
] |
|
|
22 |
}, |
|
|
23 |
{ |
|
|
24 |
"cell_type": "markdown", |
|
|
25 |
"metadata": {}, |
|
|
26 |
"source": [ |
|
|
27 |
"### Download the 3D-IRCADb-01 dataset from https://www.ircad.fr/research/3d-ircadb-01/ and into a directory in the root of the repo and name it Dataset" |
|
|
28 |
] |
|
|
29 |
}, |
|
|
30 |
{ |
|
|
31 |
"cell_type": "code", |
|
|
32 |
"execution_count": null, |
|
|
33 |
"metadata": {}, |
|
|
34 |
"outputs": [], |
|
|
35 |
"source": [ |
|
|
36 |
"dataset_path = \"Dataset\"" |
|
|
37 |
] |
|
|
38 |
}, |
|
|
39 |
{ |
|
|
40 |
"cell_type": "code", |
|
|
41 |
"execution_count": null, |
|
|
42 |
"metadata": {}, |
|
|
43 |
"outputs": [], |
|
|
44 |
"source": [ |
|
|
45 |
"#ONLY DO THIS STEP IF YOU'VE DOWNLOADED THE ENTIRE DATASET AT ONCE NOT PATIENT BY PATIENT\n", |
|
|
46 |
"with ZipFile(os.path.join(dataset_path,\"3Dircadb1.zip\"), 'r') as zipObj:\n", |
|
|
47 |
" try:\n", |
|
|
48 |
" zipObj.extractall(dataset_path)\n", |
|
|
49 |
" except Exception as error:\n", |
|
|
50 |
" print(error)" |
|
|
51 |
] |
|
|
52 |
}, |
|
|
53 |
{ |
|
|
54 |
"cell_type": "code", |
|
|
55 |
"execution_count": null, |
|
|
56 |
"metadata": {}, |
|
|
57 |
"outputs": [], |
|
|
58 |
"source": [ |
|
|
59 |
"dataset_path = \"Dataset\" # You can change the path of the directory if you have the dataset elsewhere\n", |
|
|
60 |
"# EXTRACT EVERY PATIENT'S .zip FILE\n", |
|
|
61 |
"\n", |
|
|
62 |
"for file in os.listdir(dataset_path):\n", |
|
|
63 |
" file_path = os.path.join(dataset_path, file)\n", |
|
|
64 |
" if file_path.endswith(\".zip\"):\n", |
|
|
65 |
" with ZipFile(file_path, 'r') as zipObj:\n", |
|
|
66 |
" try:\n", |
|
|
67 |
" zipObj.extractall(dataset_path)\n", |
|
|
68 |
" except Exception as error:\n", |
|
|
69 |
" print(error)\n", |
|
|
70 |
" \n", |
|
|
71 |
"\n", |
|
|
72 |
"# GO INTO THE JUST EXTRACTED DIRECTORIES AND EXTRACT THE .zip FILES THAT CONTAIN THE PATIENT SLICES AND MASKS\n", |
|
|
73 |
"\n", |
|
|
74 |
"for dir_path in os.listdir(dataset_path):\n", |
|
|
75 |
" dir_full_path = os.path.join(dataset_path, dir_path)\n", |
|
|
76 |
" if os.path.isdir(dir_full_path):\n", |
|
|
77 |
" if os.path.isfile(os.path.join(dir_full_path,\"PATIENT_DICOM.zip\")):\n", |
|
|
78 |
" with ZipFile(os.path.join(dir_full_path,\"PATIENT_DICOM.zip\"), 'r') as zipObj:\n", |
|
|
79 |
" zipObj.extractall(dir_full_path) \n", |
|
|
80 |
"\n", |
|
|
81 |
" if os.path.isfile(os.path.join(dir_full_path,\"MASKS_DICOM.zip\")):\n", |
|
|
82 |
" with ZipFile(os.path.join(dir_full_path,\"MASKS_DICOM.zip\"), 'r') as zipObj:\n", |
|
|
83 |
" zipObj.extractall(dir_full_path) " |
|
|
84 |
] |
|
|
85 |
}, |
|
|
86 |
{ |
|
|
87 |
"cell_type": "code", |
|
|
88 |
"execution_count": null, |
|
|
89 |
"metadata": {}, |
|
|
90 |
"outputs": [], |
|
|
91 |
"source": [ |
|
|
92 |
"# CHANGE DATASET STRUCTURE\n", |
|
|
93 |
"# PREFIX FILES IN THE DIRECTORY WITH THE PATIENT'S ID\n", |
|
|
94 |
"for folder in os.listdir(\"Dataset\"):\n", |
|
|
95 |
" x = folder.split('db')\n", |
|
|
96 |
" folder_path = os.path.join('Dataset', folder)\n", |
|
|
97 |
" if os.path.isdir(folder_path):\n", |
|
|
98 |
" for subfolder in os.listdir(folder_path):\n", |
|
|
99 |
" subfolder_path = os.path.join(folder_path,subfolder)\n", |
|
|
100 |
" if os.path.isdir(subfolder_path):\n", |
|
|
101 |
" for sub_sub in os.listdir(subfolder_path):\n", |
|
|
102 |
" if os.path.isdir(os.path.join(subfolder_path, sub_sub)):\n", |
|
|
103 |
" for image in os.listdir(os.path.join(subfolder_path, sub_sub)):\n", |
|
|
104 |
" src= os.path.join(subfolder_path, sub_sub, image)\n", |
|
|
105 |
" dst = os.path.join(subfolder_path, sub_sub, x[1]+'_'+image)\n", |
|
|
106 |
" os.rename(src, dst)\n", |
|
|
107 |
" \n", |
|
|
108 |
" src= os.path.join(subfolder_path, sub_sub)\n", |
|
|
109 |
" dst = os.path.join(subfolder_path, x[1]+'_'+sub_sub)\n", |
|
|
110 |
" os.rename(src, dst)" |
|
|
111 |
] |
|
|
112 |
}, |
|
|
113 |
{ |
|
|
114 |
"cell_type": "code", |
|
|
115 |
"execution_count": null, |
|
|
116 |
"metadata": {}, |
|
|
117 |
"outputs": [], |
|
|
118 |
"source": [ |
|
|
119 |
"directory_to_move = \"PATIENT_DICOM\"\n", |
|
|
120 |
"desired_scans_path = os.path.join(\"train\", \"patients\")\n", |
|
|
121 |
"desired_masks_path = os.path.join(\"train\", \"masks\")\n", |
|
|
122 |
"try: \n", |
|
|
123 |
" os.mkdir(\"train\") \n", |
|
|
124 |
"except OSError as error: \n", |
|
|
125 |
" print(error)\n", |
|
|
126 |
"\n", |
|
|
127 |
"try: \n", |
|
|
128 |
" os.mkdir(desired_scans_path) \n", |
|
|
129 |
"except OSError as error: \n", |
|
|
130 |
" print(error)\n", |
|
|
131 |
"\n", |
|
|
132 |
"for patient_path in os.listdir(dataset_path):\n", |
|
|
133 |
" patient_full_path = os.path.join(dataset_path, patient_path)\n", |
|
|
134 |
" if os.path.isdir(patient_full_path):\n", |
|
|
135 |
" patient_scans_path = os.path.join(patient_full_path, \"PATIENT_DICOM\")\n", |
|
|
136 |
" if os.path.isdir(patient_scans_path):\n", |
|
|
137 |
" for scan in os.listdir(patient_scans_path):\n", |
|
|
138 |
" os.rename(os.path.join(patient_scans_path, scan), os.path.join(desired_scans_path, scan))\n", |
|
|
139 |
"\n", |
|
|
140 |
"\n", |
|
|
141 |
"\n", |
|
|
142 |
"try: \n", |
|
|
143 |
" os.mkdir(desired_masks_path) \n", |
|
|
144 |
"except OSError as error: \n", |
|
|
145 |
" print(error)\n", |
|
|
146 |
"\n", |
|
|
147 |
"for patient_path in os.listdir(dataset_path):\n", |
|
|
148 |
" patient_full_path = os.path.join(dataset_path, patient_path)\n", |
|
|
149 |
" if(os.path.isdir(patient_full_path)):\n", |
|
|
150 |
" patient_masks_path = os.path.join(patient_full_path, \"MASKS_DICOM\")\n", |
|
|
151 |
" if os.path.isdir(patient_masks_path):\n", |
|
|
152 |
" for scan in os.listdir(patient_masks_path):\n", |
|
|
153 |
" os.replace(os.path.join(patient_masks_path, scan), os.path.join(desired_masks_path, scan))\n", |
|
|
154 |
"\n", |
|
|
155 |
"try: \n", |
|
|
156 |
" os.mkdir(os.path.join(desired_masks_path, \"merged_livertumors\")) \n", |
|
|
157 |
"except OSError as error: \n", |
|
|
158 |
" print(error)" |
|
|
159 |
] |
|
|
160 |
}, |
|
|
161 |
{ |
|
|
162 |
"cell_type": "code", |
|
|
163 |
"execution_count": 3, |
|
|
164 |
"metadata": {}, |
|
|
165 |
"outputs": [], |
|
|
166 |
"source": [ |
|
|
167 |
"# COUNT THE NUMBER OF SCANS FOR 1 PATIENT TO APPEND ON THEM\n", |
|
|
168 |
"def count_scans_startwith(directory, prefix):\n", |
|
|
169 |
" count = 0\n", |
|
|
170 |
" for file in os.listdir(directory):\n", |
|
|
171 |
" if file.startswith(prefix+'_'):\n", |
|
|
172 |
" count+=1\n", |
|
|
173 |
" return count" |
|
|
174 |
] |
|
|
175 |
}, |
|
|
176 |
{ |
|
|
177 |
"cell_type": "code", |
|
|
178 |
"execution_count": 4, |
|
|
179 |
"metadata": {}, |
|
|
180 |
"outputs": [], |
|
|
181 |
"source": [ |
|
|
182 |
"# MERGE ALL LIVERTUMORS IN 1 MASK\n", |
|
|
183 |
"def merge_livertumors(scans_path, masks_path):\n", |
|
|
184 |
" for scan in os.listdir(scans_path):\n", |
|
|
185 |
" patient_id = scan.split('_')[0]\n", |
|
|
186 |
" tumor_volume = None\n", |
|
|
187 |
" for mask_dir in os.listdir(masks_path):\n", |
|
|
188 |
" if mask_dir.startswith(patient_id+'_livertumor'):\n", |
|
|
189 |
" current_tumor = pydicom.dcmread(os.path.join(masks_path, mask_dir, scan)).pixel_array\n", |
|
|
190 |
" current_tumor = current_tumor/255.0\n", |
|
|
191 |
" current_tumor = np.clip(current_tumor,0,1)\n", |
|
|
192 |
" tumor_volume = current_tumor if tumor_volume is None else np.logical_or(tumor_volume,current_tumor)\n", |
|
|
193 |
" \n", |
|
|
194 |
" if tumor_volume is None:\n", |
|
|
195 |
" tumor_volume = np.zeros((512,512))\n", |
|
|
196 |
" tumor_volume = [[j*255 for j in i] for i in tumor_volume]\n", |
|
|
197 |
" \n", |
|
|
198 |
" \n", |
|
|
199 |
" tumor_volume = np.array(tumor_volume)\n", |
|
|
200 |
" tumor_volume = tumor_volume.astype(np.uint8)\n", |
|
|
201 |
" im = Image.fromarray(tumor_volume)\n", |
|
|
202 |
" im.save(os.path.join(masks_path, 'merged_livertumors', scan+'.jpg'))\n", |
|
|
203 |
"# imageio.imwrite(os.path.join(masks_path, 'merged_livertumors', scan+'.jpg'), tumor_volume)" |
|
|
204 |
] |
|
|
205 |
}, |
|
|
206 |
{ |
|
|
207 |
"cell_type": "code", |
|
|
208 |
"execution_count": 5, |
|
|
209 |
"metadata": {}, |
|
|
210 |
"outputs": [], |
|
|
211 |
"source": [ |
|
|
212 |
"# REFLECT IMAGE AND MASK TO AUGMENT DATA\n", |
|
|
213 |
"def reflect_dicom(src_img, src_mask, src_liver_mask, train_path, masks_path, liver_mask_path, patient_id, patient_imgs_count):\n", |
|
|
214 |
" reflected_img = copy.deepcopy(src_img)\n", |
|
|
215 |
" reflected_img.PixelData = np.fliplr(reflected_img.pixel_array).tobytes()\n", |
|
|
216 |
" \n", |
|
|
217 |
"\n", |
|
|
218 |
" reflected_liver_mask = copy.deepcopy(src_liver_mask)\n", |
|
|
219 |
" reflected_liver_mask.PixelData = np.fliplr(reflected_liver_mask.pixel_array).tobytes()\n", |
|
|
220 |
" reflected_liver_mask.save_as(os.path.join(liver_mask_path, patient_id+'_image_'+str(patient_imgs_count)+'_augref'))\n", |
|
|
221 |
" \n", |
|
|
222 |
" reflected_mask = np.fliplr(src_mask)\n", |
|
|
223 |
" \n", |
|
|
224 |
" \n", |
|
|
225 |
" reflected_img.save_as(os.path.join(train_path, patient_id+'_image_'+str(patient_imgs_count)+'_augref'))\n", |
|
|
226 |
"\n", |
|
|
227 |
" reflected_mask = reflected_mask.astype(np.uint8)\n", |
|
|
228 |
" imageio.imwrite(os.path.join(masks_path, 'merged_livertumors', patient_id+'_image_'+str(patient_imgs_count)+'_augref.jpg'), reflected_mask)\n", |
|
|
229 |
" \n", |
|
|
230 |
" \n", |
|
|
231 |
"# ROTATE IMAGE AND MASK TO AUGMENT DATA\n", |
|
|
232 |
"def rotate_dicom(src_img, src_mask, src_liver_mask, train_path, masks_path, liver_mask_path, patient_id, patient_imgs_count, angle):\n", |
|
|
233 |
" rotated_img = copy.deepcopy(src_img)\n", |
|
|
234 |
" rotated_img.PixelData = rotate(rotated_img.pixel_array, angle, reshape=False).tobytes()\n", |
|
|
235 |
" \n", |
|
|
236 |
"\n", |
|
|
237 |
" rotated_liver_mask = copy.deepcopy(src_liver_mask)\n", |
|
|
238 |
" rotated_liver_mask.PixelData = rotate(rotated_liver_mask.pixel_array, angle, reshape=False).tobytes()\n", |
|
|
239 |
" rotated_liver_mask.save_as(os.path.join(liver_mask_path, patient_id+'_image_'+str(patient_imgs_count)+'_augrot'))\n", |
|
|
240 |
" \n", |
|
|
241 |
" rotated_mask = rotate(src_mask, angle, reshape=False)\n", |
|
|
242 |
" \n", |
|
|
243 |
"\n", |
|
|
244 |
" rotated_img.save_as(os.path.join(train_path, patient_id+'_image_'+str(patient_imgs_count)+'_augrot'))\n", |
|
|
245 |
" \n", |
|
|
246 |
" rotated_mask = rotated_mask.astype(np.uint8)\n", |
|
|
247 |
" imageio.imwrite(os.path.join(masks_path, 'merged_livertumors', patient_id+'_image_'+str(patient_imgs_count)+'_augrot.jpg'), rotated_mask) \n", |
|
|
248 |
" " |
|
|
249 |
] |
|
|
250 |
}, |
|
|
251 |
{ |
|
|
252 |
"cell_type": "code", |
|
|
253 |
"execution_count": 6, |
|
|
254 |
"metadata": {}, |
|
|
255 |
"outputs": [], |
|
|
256 |
"source": [ |
|
|
257 |
"# AUGMENT THE MASKS WITH TUMORS TO FIX CLASS IMBALANCING\n", |
|
|
258 |
"def augment_dicom(train_path, masks_path):\n", |
|
|
259 |
" rotation_angles = [x * 10 for x in range(36) if x > 0]\n", |
|
|
260 |
" train_files = copy.deepcopy(os.listdir(train_path))\n", |
|
|
261 |
" \n", |
|
|
262 |
" for scan in train_files:\n", |
|
|
263 |
" mask_path = os.path.join(masks_path, 'merged_livertumors', scan+'.jpg')\n", |
|
|
264 |
" tumor_mask = cv2.imread(mask_path)\n", |
|
|
265 |
" if 1 in tumor_mask:\n", |
|
|
266 |
" patient_id = scan.split('_')[0]\n", |
|
|
267 |
" patient_imgs_count = count_scans_startwith(train_path, patient_id)\n", |
|
|
268 |
" original_img = pydicom.dcmread(os.path.join(train_path, scan)) \n", |
|
|
269 |
" liver_mask_path = os.path.join(masks_path, patient_id+'_liver')\n", |
|
|
270 |
" liver_mask = pydicom.dcmread(os.path.join(liver_mask_path, scan))\n", |
|
|
271 |
" reflect_dicom(original_img, tumor_mask, liver_mask, train_path, masks_path, liver_mask_path, patient_id, patient_imgs_count)\n", |
|
|
272 |
" \n", |
|
|
273 |
" for angle in rotation_angles:\n", |
|
|
274 |
" patient_imgs_count += 1 \n", |
|
|
275 |
" rotate_dicom(original_img, tumor_mask, liver_mask, train_path, masks_path, liver_mask_path, patient_id, patient_imgs_count, angle)\n", |
|
|
276 |
" " |
|
|
277 |
] |
|
|
278 |
}, |
|
|
279 |
{ |
|
|
280 |
"cell_type": "code", |
|
|
281 |
"execution_count": 7, |
|
|
282 |
"metadata": {}, |
|
|
283 |
"outputs": [ |
|
|
284 |
{ |
|
|
285 |
"name": "stdout", |
|
|
286 |
"output_type": "stream", |
|
|
287 |
"text": [ |
|
|
288 |
"done\n" |
|
|
289 |
] |
|
|
290 |
} |
|
|
291 |
], |
|
|
292 |
"source": [ |
|
|
293 |
"masks_path = os.path.join('train','masks')\n", |
|
|
294 |
"training_path = os.path.join('train','patients')\n", |
|
|
295 |
"\n", |
|
|
296 |
"augment_dicom(training_path, masks_path)\n", |
|
|
297 |
"# merge_livertumors(training_path, masks_path)\n", |
|
|
298 |
"print('done')" |
|
|
299 |
] |
|
|
300 |
}, |
|
|
301 |
{ |
|
|
302 |
"cell_type": "code", |
|
|
303 |
"execution_count": 8, |
|
|
304 |
"metadata": {}, |
|
|
305 |
"outputs": [ |
|
|
306 |
{ |
|
|
307 |
"name": "stdout", |
|
|
308 |
"output_type": "stream", |
|
|
309 |
"text": [ |
|
|
310 |
"20974\n" |
|
|
311 |
] |
|
|
312 |
} |
|
|
313 |
], |
|
|
314 |
"source": [ |
|
|
315 |
"count = 0\n", |
|
|
316 |
"for mask in os.listdir(os.path.join(masks_path,'merged_livertumors')):\n", |
|
|
317 |
" tumor_mask = cv2.imread(os.path.join(masks_path,'merged_livertumors', mask))\n", |
|
|
318 |
" if 1 in tumor_mask:\n", |
|
|
319 |
" count += 1\n", |
|
|
320 |
"print(count)\n", |
|
|
321 |
"# 568" |
|
|
322 |
] |
|
|
323 |
}, |
|
|
324 |
{ |
|
|
325 |
"cell_type": "code", |
|
|
326 |
"execution_count": 4, |
|
|
327 |
"metadata": {}, |
|
|
328 |
"outputs": [ |
|
|
329 |
{ |
|
|
330 |
"name": "stdout", |
|
|
331 |
"output_type": "stream", |
|
|
332 |
"text": [ |
|
|
333 |
"done\n" |
|
|
334 |
] |
|
|
335 |
} |
|
|
336 |
], |
|
|
337 |
"source": [ |
|
|
338 |
"# REMOVE AUGMENTED DATA\n", |
|
|
339 |
"for scan in os.listdir(training_path):\n", |
|
|
340 |
" if scan.endswith('_augref') or scan.endswith('_augrot'):\n", |
|
|
341 |
" os.remove(os.path.join(training_path,scan))\n", |
|
|
342 |
"\n", |
|
|
343 |
"for mask_dir in os.listdir(masks_path):\n", |
|
|
344 |
" if mask_dir.endswith('liver'):\n", |
|
|
345 |
" mask_dir_path = os.path.join(masks_path, mask_dir)\n", |
|
|
346 |
" for liver_mask in os.listdir(mask_dir_path):\n", |
|
|
347 |
" if liver_mask.endswith('augref') or liver_mask.endswith('augrot') or liver_mask.endswith('aug'):\n", |
|
|
348 |
" os.remove(os.path.join(mask_dir_path, liver_mask))\n", |
|
|
349 |
"\n", |
|
|
350 |
"for mask in os.listdir(os.path.join(masks_path, 'merged_livertumors')):\n", |
|
|
351 |
" if mask.endswith('_augref.jpg') or mask.endswith('augrot.jpg'):\n", |
|
|
352 |
" os.remove(os.path.join(masks_path, 'merged_livertumors', mask))\n", |
|
|
353 |
"print('done')" |
|
|
354 |
] |
|
|
355 |
} |
|
|
356 |
], |
|
|
357 |
"metadata": { |
|
|
358 |
"kernelspec": { |
|
|
359 |
"display_name": "Python 3", |
|
|
360 |
"language": "python", |
|
|
361 |
"name": "python3" |
|
|
362 |
}, |
|
|
363 |
"language_info": { |
|
|
364 |
"codemirror_mode": { |
|
|
365 |
"name": "ipython", |
|
|
366 |
"version": 3 |
|
|
367 |
}, |
|
|
368 |
"file_extension": ".py", |
|
|
369 |
"mimetype": "text/x-python", |
|
|
370 |
"name": "python", |
|
|
371 |
"nbconvert_exporter": "python", |
|
|
372 |
"pygments_lexer": "ipython3", |
|
|
373 |
"version": "3.7.5" |
|
|
374 |
} |
|
|
375 |
}, |
|
|
376 |
"nbformat": 4, |
|
|
377 |
"nbformat_minor": 2 |
|
|
378 |
} |