|
a |
|
b/utils/PP.py |
|
|
1 |
#Preprocessing file |
|
|
2 |
import os |
|
|
3 |
import numpy as np |
|
|
4 |
import nibabel as nib |
|
|
5 |
import time |
|
|
6 |
import math |
|
|
7 |
import random |
|
|
8 |
import glob |
|
|
9 |
import collections |
|
|
10 |
import sys |
|
|
11 |
import random |
|
|
12 |
from random import randint |
|
|
13 |
import time |
|
|
14 |
import datetime |
|
|
15 |
import augmentations as AUG |
|
|
16 |
|
|
|
17 |
#------------------------------------------------------------------------------ |
|
|
18 |
#------------------------------------------------------------------------------ |
|
|
19 |
#Basic Utils |
|
|
20 |
#------------------------------------------------------------------------------ |
|
|
21 |
#------------------------------------------------------------------------------ |
|
|
22 |
|
|
|
23 |
def getTime(): |
|
|
24 |
return datetime.datetime.now().strftime("%Y-%m-%d-%H-%M") |
|
|
25 |
|
|
|
26 |
def printDimensions(img_path = 'pre/FLAIR.nii.gz', segm_path = 'wmh.nii.gz', data_folder = '../Data/MS2017a/'): |
|
|
27 |
scan_folders = glob.glob(data_folder + 'scans/*') |
|
|
28 |
|
|
|
29 |
for sf in scan_folders: |
|
|
30 |
file_num = os.path.basename(sf) |
|
|
31 |
img = nib.load(os.path.join(sf, img_path)) |
|
|
32 |
print(file_num, img.shape) |
|
|
33 |
|
|
|
34 |
def extractMeanDataStats(size = [200, 200, 100], |
|
|
35 |
postfix = '_200x200x100orig', |
|
|
36 |
main_folder_path = '../../Data/MS2017b/', |
|
|
37 |
): |
|
|
38 |
scan_folders = glob.glob(main_folder_path + 'scans/*') |
|
|
39 |
img_path = 'pre/FLAIR' + postfix + '.nii.gz' |
|
|
40 |
segm_path = 'wmh' + postfix + '.nii.gz' |
|
|
41 |
|
|
|
42 |
shape_ = [len(scan_folders), size[0], size[1], size[2]] |
|
|
43 |
arr = np.zeros(shape_) |
|
|
44 |
|
|
|
45 |
for i, sf in enumerate(scan_folders): |
|
|
46 |
arr[i, :,:,:] = numpyFromScan(os.path.join(sf,img_path)).squeeze() |
|
|
47 |
|
|
|
48 |
arr /= len(scan_folders) |
|
|
49 |
|
|
|
50 |
means = np.mean(arr) |
|
|
51 |
stds = np.std(arr, axis = 0) |
|
|
52 |
|
|
|
53 |
np.save(main_folder_path + 'extra_data/std' + postfix, stds) |
|
|
54 |
np.save(main_folder_path + 'extra_data/mean' + postfix, means) |
|
|
55 |
|
|
|
56 |
def getExperimentInfo(experiment_str): |
|
|
57 |
exp_arr = experiment_str.split('_') |
|
|
58 |
isPriv = bool(int(exp_arr[1])) |
|
|
59 |
withASPP = bool(int(exp_arr[2])) |
|
|
60 |
dilations_str = exp_arr[0] |
|
|
61 |
dilation_arr = [int(i) for i in dilations_str.split('x')] |
|
|
62 |
|
|
|
63 |
return dilation_arr, isPriv, withASPP |
|
|
64 |
|
|
|
65 |
#------------------------------------------------------------------------------ |
|
|
66 |
#------------------------------------------------------------------------------ |
|
|
67 |
##GENERAL PREPROCESSING |
|
|
68 |
#------------------------------------------------------------------------------ |
|
|
69 |
#------------------------------------------------------------------------------ |
|
|
70 |
|
|
|
71 |
#convert .nii.gz file to scan |
|
|
72 |
def numpyFromScan(path, get_affine = False, makebin = False): |
|
|
73 |
img = nib.load(path) |
|
|
74 |
img_np = img.get_data() |
|
|
75 |
#reshape to size1 x size2 - > size1 x size2 x 1 |
|
|
76 |
img_np = np.expand_dims(img_np, axis=len(img_np.shape)) |
|
|
77 |
#img_np = img_np.reshape([img_np.shape[0], img_np.shape[1], 1]) |
|
|
78 |
if makebin: |
|
|
79 |
img_np[img_np == 2] = 0 |
|
|
80 |
|
|
|
81 |
if get_affine: |
|
|
82 |
return img_np, img.get_affine() |
|
|
83 |
return img_np |
|
|
84 |
|
|
|
85 |
def saveScan(img_np, affine, save_path, header = None): |
|
|
86 |
if header: |
|
|
87 |
nft_img = nib.Nifti1Image(img_np, affine, header = header) |
|
|
88 |
else: |
|
|
89 |
nft_img = nib.Nifti1Image(img_np, affine) |
|
|
90 |
nib.save(nft_img, save_path) |
|
|
91 |
|
|
|
92 |
#get list of validation/train sets |
|
|
93 |
def splitTrainVal(train_fraction, data_folder = '../Data/MS2017a/'): |
|
|
94 |
scan_folders = glob.glob(data_folder + 'scans/*') |
|
|
95 |
num_scans = len(scan_folders) |
|
|
96 |
|
|
|
97 |
indices = np.random.permutation(num_scans) |
|
|
98 |
train_indices = indices[0:int(num_scans*train_fraction)] |
|
|
99 |
val_indices = indices[int(num_scans*train_fraction):] |
|
|
100 |
|
|
|
101 |
train_scan_folders = [scan_folders[i] for i in train_indices] |
|
|
102 |
val_scan_folders = [scan_folders[i] for i in val_indices] |
|
|
103 |
|
|
|
104 |
return train_scan_folders, val_scan_folders |
|
|
105 |
|
|
|
106 |
#call this once to split training data |
|
|
107 |
def generateTrainValFile(train_fraction, main_folder = '../Data/MS2017a/', postfix = ''): |
|
|
108 |
train_folders, val_folders = splitTrainVal(0.8, data_folder=main_folder) |
|
|
109 |
|
|
|
110 |
img_path = '/pre/FLAIR' + postfix + '.nii.gz' |
|
|
111 |
train_folder_names = [train_folders[i].split(main_folder)[1] + img_path for i in range(len(train_folders))] |
|
|
112 |
val_folder_names = [val_folders[i].split(main_folder)[1] + img_path for i in range(len(val_folders))] |
|
|
113 |
|
|
|
114 |
f_train = open(main_folder + 'train' + postfix + '.txt', 'w+') |
|
|
115 |
f_val = open(main_folder + 'val' + postfix + '.txt', 'w+') |
|
|
116 |
|
|
|
117 |
for fn in train_folder_names: |
|
|
118 |
f_train.write(fn + '\n') |
|
|
119 |
|
|
|
120 |
for fn in val_folder_names: |
|
|
121 |
f_val.write(fn + '\n') |
|
|
122 |
|
|
|
123 |
f_train.close() |
|
|
124 |
f_val.close() |
|
|
125 |
|
|
|
126 |
def read_file(path_to_file, pretext = ''): |
|
|
127 |
with open(path_to_file) as f: |
|
|
128 |
img_list = [] |
|
|
129 |
for line in f: |
|
|
130 |
img_list.append(pretext + line[:-1]) |
|
|
131 |
return img_list |
|
|
132 |
|
|
|
133 |
|
|
|
134 |
def generateTestFile(folder): |
|
|
135 |
pass |
|
|
136 |
|
|
|
137 |
#------------------------------------------------------------------------------ |
|
|
138 |
#------------------------------------------------------------------------------ |
|
|
139 |
##END GENERAL PREPROCESSING |
|
|
140 |
#------------------------------------------------------------------------------ |
|
|
141 |
#------------------------------------------------------------------------------ |
|
|
142 |
|
|
|
143 |
|
|
|
144 |
|
|
|
145 |
#func1() |
|
|
146 |
#simpleSplitTrainVal(0.8) |
|
|
147 |
#generateTrainValFile(0.8) |
|
|
148 |
|
|
|
149 |
|
|
|
150 |
#------------------------------------------------------------------------------ |
|
|
151 |
#------------------------------------------------------------------------------ |
|
|
152 |
##SLICES PREPROCESSING |
|
|
153 |
#------------------------------------------------------------------------------ |
|
|
154 |
#------------------------------------------------------------------------------ |
|
|
155 |
|
|
|
156 |
|
|
|
157 |
#generate a slices folder containing all slices |
|
|
158 |
def generateImgSlicesFolder(data_folder = '../Data/MS2017a/scans/'): |
|
|
159 |
scan_folders = glob.glob(data_folder + '*') |
|
|
160 |
|
|
|
161 |
for sf in scan_folders: |
|
|
162 |
slice_dir_path = os.path.join(sf, 'slices/') |
|
|
163 |
if not os.path.exists(slice_dir_path): |
|
|
164 |
print('Creating directory at:' , slice_dir_path) |
|
|
165 |
os.makedirs(slice_dir_path) |
|
|
166 |
|
|
|
167 |
img = nib.load(os.path.join(sf, 'pre/FLAIR.nii.gz')) |
|
|
168 |
img_np = img.get_data() |
|
|
169 |
img_affine = img.affine |
|
|
170 |
print(sf) |
|
|
171 |
print('The img shape', img_np.shape[2]) |
|
|
172 |
for i in range(img_np.shape[2]): |
|
|
173 |
slice_img_np = img_np[:,:,i] |
|
|
174 |
nft_img = nib.Nifti1Image(slice_img_np, img_affine) |
|
|
175 |
nib.save(nft_img, slice_dir_path + 'FLAIR_' + str(i) + '.nii.gz') |
|
|
176 |
|
|
|
177 |
if os.path.basename(sf) == '0': |
|
|
178 |
slice_img = nib.load(slice_dir_path + 'FLAIR_' + str(i) + '.nii.gz').get_data() / 5 |
|
|
179 |
print('DID I GET HERE?') |
|
|
180 |
print('Writing to', str(i) + '.jpg') |
|
|
181 |
|
|
|
182 |
def generateGTSlicesFolder(data_folder = '../Data/MS2017a/scans/'): |
|
|
183 |
scan_folders = glob.glob(data_folder + '*') |
|
|
184 |
|
|
|
185 |
for sf in scan_folders: |
|
|
186 |
slice_dir_path = os.path.join(sf, 'gt_slices/') |
|
|
187 |
if not os.path.exists(slice_dir_path): |
|
|
188 |
print('Creating directory at:' , slice_dir_path) |
|
|
189 |
os.makedirs(slice_dir_path) |
|
|
190 |
|
|
|
191 |
img = nib.load(os.path.join(sf, 'wmh.nii.gz')) |
|
|
192 |
img_np = img.get_data() |
|
|
193 |
img_affine = img.affine |
|
|
194 |
print(sf) |
|
|
195 |
print('The img shape', img_np.shape[2]) |
|
|
196 |
for i in range(img_np.shape[2]): |
|
|
197 |
slice_img_np = img_np[:,:,i] |
|
|
198 |
nft_img = nib.Nifti1Image(slice_img_np, img_affine) |
|
|
199 |
nib.save(nft_img, slice_dir_path + 'wmh_' + str(i) + '.nii.gz') |
|
|
200 |
|
|
|
201 |
if os.path.basename(sf) == '0': |
|
|
202 |
slice_img = nib.load(slice_dir_path + 'wmh_' + str(i) + '.nii.gz').get_data() * 256 |
|
|
203 |
#cv2.imwrite('temp/' + str(i) + '.jpg', slice_img) |
|
|
204 |
|
|
|
205 |
def splitTrainVal_Slices(train_fraction, data_folder = '../Data/MS2017a/scans/'): |
|
|
206 |
scan_folders = glob.glob(data_folder + '/*/slices/*') |
|
|
207 |
num_scans = len(scan_folders) |
|
|
208 |
|
|
|
209 |
indices = np.random.permutation(num_scans) |
|
|
210 |
train_indices = indices[0:int(num_scans*train_fraction)] |
|
|
211 |
val_indices = indices[int(num_scans*train_fraction):] |
|
|
212 |
|
|
|
213 |
train_scan_folders = [scan_folders[i] for i in train_indices] |
|
|
214 |
val_scan_folders = [scan_folders[i] for i in val_indices] |
|
|
215 |
|
|
|
216 |
return train_scan_folders, val_scan_folders |
|
|
217 |
|
|
|
218 |
def generateTrainValFile_Slices(train_fraction, main_folder = '../Data/MS2017a/'): |
|
|
219 |
train_folders, val_folders = splitTrainVal_Slices(0.8) |
|
|
220 |
|
|
|
221 |
train_folder_names = [train_folders[i].split(main_folder)[1] for i in range(len(train_folders))] |
|
|
222 |
val_folder_names = [val_folders[i].split(main_folder)[1] for i in range(len(val_folders))] |
|
|
223 |
|
|
|
224 |
f_train = open(main_folder + 'train_slices.txt', 'w+') |
|
|
225 |
f_val = open(main_folder + 'val_slices.txt', 'w+') |
|
|
226 |
|
|
|
227 |
for fn in train_folder_names: |
|
|
228 |
f_train.write(fn + '\n') |
|
|
229 |
|
|
|
230 |
for fn in val_folder_names: |
|
|
231 |
f_val.write(fn + '\n') |
|
|
232 |
|
|
|
233 |
f_train.close() |
|
|
234 |
f_val.close() |
|
|
235 |
|
|
|
236 |
#Use this to save the images quickly (for testing purposes) |
|
|
237 |
def quickSave(img, wmh, gif, n): |
|
|
238 |
nft_img = nib.Nifti1Image(img.squeeze(), np.eye(4)) |
|
|
239 |
nib.save(nft_img, n + '_img.nii.gz') |
|
|
240 |
nft_img = nib.Nifti1Image(wmh.squeeze(), np.eye(4)) |
|
|
241 |
nib.save(nft_img, n + '_wmh.nii.gz') |
|
|
242 |
if gif is not None: |
|
|
243 |
nft_img = nib.Nifti1Image(gif.squeeze(), np.eye(4)) |
|
|
244 |
nib.save(nft_img, n + '_gif.nii.gz') |
|
|
245 |
|
|
|
246 |
#------------------------------------------------------------------------------ |
|
|
247 |
#END OF SLICES PREPROCESSING |
|
|
248 |
#------------------------------------------------------------------------------ |
|
|
249 |
|
|
|
250 |
#------------------------------------------------------------------------------ |
|
|
251 |
#3D PREPROCESSING |
|
|
252 |
#------------------------------------------------------------------------------ |
|
|
253 |
#go through every 3D object from training set and every patch of size NxNxN |
|
|
254 |
#save resulting 3D volumes in one of the two folders based on what the center pixel of the image is |
|
|
255 |
|
|
|
256 |
def extractCenterPixelPatches(N = 33, main_folder = '../Data/MS2017b/', postfix = ''): |
|
|
257 |
if N % 2 != 1: |
|
|
258 |
print('N must be odd') |
|
|
259 |
sys.exit() |
|
|
260 |
|
|
|
261 |
img_path = 'pre/FLAIR' + postfix + '.nii.gz' |
|
|
262 |
segm_path = 'wmh' + postfix + '.nii.gz' |
|
|
263 |
|
|
|
264 |
folders = ['lesion', 'other'] |
|
|
265 |
patch_folder_path = os.path.join(main_folder, 'centerPixelPatches' + postfix + '_' + str(N)) |
|
|
266 |
if not os.path.exists(patch_folder_path): |
|
|
267 |
for f in folders: |
|
|
268 |
os.makedirs(os.path.join(main_folder, patch_folder_path, f)) |
|
|
269 |
|
|
|
270 |
scan_folders = glob.glob(main_folder + 'scans/*') |
|
|
271 |
|
|
|
272 |
counter = 0 |
|
|
273 |
|
|
|
274 |
f_lesion_txt = open(os.path.join(patch_folder_path, 'lesion', 'center_locs.txt'), 'w+') |
|
|
275 |
f_other_txt = open(os.path.join(patch_folder_path, 'other', 'center_locs.txt'), 'w+') |
|
|
276 |
|
|
|
277 |
#This is only for training data |
|
|
278 |
img_list = read_file(main_folder + 'train' + postfix + '.txt', pretext = main_folder) |
|
|
279 |
print('Gathering training images from ' + main_folder + 'train' + postfix + '.txt') |
|
|
280 |
#remove pre/FLAIR_s.nii.gz from path. Only want up to folder name |
|
|
281 |
scan_folders = [img_list[i][:-len(img_path)] for i in range(len(img_list))] |
|
|
282 |
|
|
|
283 |
num_lesion = 0 |
|
|
284 |
num_other = 0 |
|
|
285 |
num_background = 0 |
|
|
286 |
|
|
|
287 |
for sf in scan_folders: |
|
|
288 |
folder_num = sf.split('/')[-2] |
|
|
289 |
#read the FLAIR img |
|
|
290 |
img = nib.load(os.path.join(sf, img_path)) |
|
|
291 |
img_affine = img.affine |
|
|
292 |
img_np = img.get_data() |
|
|
293 |
|
|
|
294 |
#read the wmh img |
|
|
295 |
wmh = nib.load(os.path.join(sf, segm_path)) |
|
|
296 |
wmh_affine = wmh.affine |
|
|
297 |
wmh_np = wmh.get_data() |
|
|
298 |
|
|
|
299 |
#reshape to size1 x size2 -> size1 x size2 x 1 |
|
|
300 |
img_np = img_np.reshape([img_np.shape[0], img_np.shape[1], img_np.shape[2], 1]) |
|
|
301 |
wmh_np = wmh_np.reshape([wmh_np.shape[0], wmh_np.shape[1], wmh_np.shape[2], 1]) |
|
|
302 |
|
|
|
303 |
#loop through every size |
|
|
304 |
for x in range(img_np.shape[0] - N + 1): |
|
|
305 |
for y in range(img_np.shape[1] - N + 1): |
|
|
306 |
for z in range(img_np.shape[2] - N + 1): |
|
|
307 |
wmh_patch = wmh_np[x:x+N, y:y+N, z:z+N] |
|
|
308 |
M = (N + 1) / 2 |
|
|
309 |
center_pixel = wmh_patch[M,M,M] |
|
|
310 |
|
|
|
311 |
#folder_num | x | y | z |
|
|
312 |
location_name = str(folder_num) + '|' + str(x) + '|' + str(y) + '|' + str(z) |
|
|
313 |
if center_pixel == 1: |
|
|
314 |
num_lesion += 1 |
|
|
315 |
f_lesion_txt.write(location_name + '\n') |
|
|
316 |
elif center_pixel == 2: |
|
|
317 |
num_other += 1 |
|
|
318 |
f_other_txt.write(location_name + '\n') |
|
|
319 |
counter += 1 |
|
|
320 |
print(str(counter) + ' / ' + str(len(scan_folders))) |
|
|
321 |
f_lesion_txt.close() |
|
|
322 |
f_other_txt.close() |
|
|
323 |
print('Num background: ', num_background) |
|
|
324 |
print('Num lesion', num_lesion) |
|
|
325 |
print('Num other', num_other) |
|
|
326 |
print('Done!') |
|
|
327 |
|
|
|
328 |
#TEMPORARY |
|
|
329 |
#if sf.split('/') |
|
|
330 |
#during training we will sample uniformly between the two folders (uniformly select folder and uniformly select training sample) |
|
|
331 |
|
|
|
332 |
def extractPatchBatch(batch_size, patch_size, img_list, |
|
|
333 |
onlyLesions = False, center_pixel = False, |
|
|
334 |
main_folder_path = '../../Data/MS2017b/', |
|
|
335 |
postfix = '', with_priv = False): |
|
|
336 |
img_b = np.zeros([batch_size, 1, patch_size, patch_size, patch_size]) |
|
|
337 |
label_b = np.zeros([batch_size, 1, patch_size, patch_size, patch_size]) |
|
|
338 |
|
|
|
339 |
gif_b = None |
|
|
340 |
if with_priv: |
|
|
341 |
gif_b = np.zeros([batch_size, 1, patch_size, patch_size, patch_size]) |
|
|
342 |
|
|
|
343 |
for i in range(batch_size): |
|
|
344 |
if center_pixel: |
|
|
345 |
center_pixel_folder_path = main_folder_path + 'centerPixelPatches' + postfix + '_' + str(patch_size) |
|
|
346 |
locs_lesion = open(os.path.join(center_pixel_folder_path, 'lesion', 'center_locs.txt')).readlines() |
|
|
347 |
locs_other = open(os.path.join(center_pixel_folder_path, 'other', 'center_locs.txt')).readlines() |
|
|
348 |
img_patch, gt_patch, gif_patch = getCenterPixelPatch(patch_size, img_list, locs_lesion, locs_other, |
|
|
349 |
onlyLesions, main_folder_path, postfix, with_priv) |
|
|
350 |
else: |
|
|
351 |
img_patch, gt_patch, gif_patch = getRandomPatch(patch_size, img_list, onlyLesions, main_folder_path, postfix, with_priv) |
|
|
352 |
|
|
|
353 |
img_b[i, :,:,:,:] = img_patch |
|
|
354 |
label_b[i, :,:,:,:] = gt_patch |
|
|
355 |
|
|
|
356 |
if with_priv: |
|
|
357 |
gif_b[i, :,:,:,:] = gif_patch |
|
|
358 |
return img_b, label_b, gif_b |
|
|
359 |
|
|
|
360 |
################################################################################################################################## |
|
|
361 |
################################################################################################################################## |
|
|
362 |
##################################Patch functions#################################################################### |
|
|
363 |
def getRandomPatch(patch_size, img_list, onlyLesions, main_folder_path, postfix, with_priv = False): |
|
|
364 |
img_str = img_list[randint(0, len(img_list)- 1)].rstrip() |
|
|
365 |
gt_str = img_str.replace('slices', 'gt_slices').replace('FLAIR', 'wmh').replace('/pre','') |
|
|
366 |
img_np = numpyFromScan(os.path.join(main_folder_path, img_str)) |
|
|
367 |
gt_np = numpyFromScan(os.path.join(main_folder_path, gt_str), makebin = onlyLesions) |
|
|
368 |
|
|
|
369 |
img_np = img_np.transpose(3,0,1,2) |
|
|
370 |
gt_np = gt_np.transpose(3,0,1,2) |
|
|
371 |
img_dims = img_np.shape |
|
|
372 |
|
|
|
373 |
x = randint(0, img_dims[1]-patch_size-1) |
|
|
374 |
y = randint(0, img_dims[2]-patch_size-1) |
|
|
375 |
z = randint(0, img_dims[3]-patch_size-1) |
|
|
376 |
|
|
|
377 |
img_np_patch = img_np[:, x:x+patch_size, y:y+patch_size, z:z+patch_size] |
|
|
378 |
gt_np_patch = gt_np[:, x:x+patch_size, y:y+patch_size, z:z+patch_size] |
|
|
379 |
|
|
|
380 |
if with_priv: |
|
|
381 |
gif_str = img_str.replace('scans', 'gifs').replace('FLAIR','parcellation').replace('/pre','') |
|
|
382 |
gif_np = numpyFromScan(os.path.join(main_folder_path, gif_str)) |
|
|
383 |
gif_np = gif_np.transpose(3,0,1,2) |
|
|
384 |
gif_np_patch = gif_np[:, x:x+patch_size, y:y+patch_size, z:z+patch_size] |
|
|
385 |
return img_np_patch, gt_np_patch, gif_np_patch |
|
|
386 |
#draw 3 numbers between patch_size |
|
|
387 |
return img_np_patch, gt_np_patch, None |
|
|
388 |
|
|
|
389 |
#XXX not implemented for onlyLesions = True |
|
|
390 |
def getCenterPixelPatch(patch_size, img_list, locs_lesion, locs_other, |
|
|
391 |
onlyLesions, main_folder_path, postfix, with_priv = False): |
|
|
392 |
b = random.uniform(0.5, 3.5) |
|
|
393 |
#segm class = 1 |
|
|
394 |
if b < 1.5: |
|
|
395 |
loc_str = locs_lesion[randint(0, len(locs_lesion) - 1)].rstrip() |
|
|
396 |
#segm class = 2 |
|
|
397 |
elif b > 1.5 and b < 2.5 and (not onlyLesions): |
|
|
398 |
loc_str = locs_other[randint(0,len(locs_other) - 1)].rstrip() |
|
|
399 |
#segm class = 3 |
|
|
400 |
else: |
|
|
401 |
loc_str = getBackgroundLoc(patch_size, img_list, onlyLesions, main_folder_path) |
|
|
402 |
|
|
|
403 |
#extract patch given folder number, location of top left edge and patch size |
|
|
404 |
#--------------------------------------------------------------------------- |
|
|
405 |
folder_num_str, x, y, z = parseLocStr(loc_str) |
|
|
406 |
img_type_path = 'pre/FLAIR' + postfix + '.nii.gz' |
|
|
407 |
gt_type_path = 'wmh' + postfix + '.nii.gz' |
|
|
408 |
|
|
|
409 |
#read the file |
|
|
410 |
img_np = numpyFromScan(os.path.join(main_folder_path, 'scans', folder_num_str, img_type_path)) |
|
|
411 |
gt_np = numpyFromScan(os.path.join(main_folder_path, 'scans', folder_num_str, gt_type_path), makebin = onlyLesions) |
|
|
412 |
|
|
|
413 |
#extract the patch |
|
|
414 |
patch_img_np = img_np[x:x+patch_size, y:y+patch_size, z:z+patch_size, :] |
|
|
415 |
patch_gt_np = gt_np[x:x+patch_size, y:y+patch_size, z:z+patch_size, :] |
|
|
416 |
|
|
|
417 |
#reshape to 1 x dim1 x dim2 x dim3 |
|
|
418 |
patch_img_np = patch_img_np.transpose((3,0,1,2)) |
|
|
419 |
patch_gt_np = patch_gt_np.transpose((3,0,1,2)) |
|
|
420 |
|
|
|
421 |
if with_priv: |
|
|
422 |
gif_type_path = 'parcellation' + postfix + '.nii.gz' |
|
|
423 |
gif_np = numpyFromScan(os.path.join(main_folder_path, 'gifs', folder_num_str, gif_type_path)) |
|
|
424 |
patch_gif_np = gif_np[x:x+patch_size, y:y+patch_size, z:z+patch_size, :] |
|
|
425 |
patch_gif_np = patch_gif_np.transpose((3,0,1,2)) |
|
|
426 |
|
|
|
427 |
return patch_img_np, patch_gt_np, patch_gif_np |
|
|
428 |
return patch_img_np, patch_gt_np, None |
|
|
429 |
|
|
|
430 |
def getBackgroundLoc(patch_size, img_list, onlyLesions, main_folder_path): |
|
|
431 |
num_generated = 0 |
|
|
432 |
found_background = False |
|
|
433 |
|
|
|
434 |
#choose a random 3D image |
|
|
435 |
img_str = img_list[randint(0, len(img_list)- 1)].rstrip() |
|
|
436 |
curr_wmh_str = img_str.replace('slices', 'gt_slices').replace('FLAIR', 'wmh').replace('/pre','') |
|
|
437 |
wmh_np = numpyFromScan(os.path.join(main_folder_path, curr_wmh_str), makebin = onlyLesions) |
|
|
438 |
img_dims = wmh_np.shape |
|
|
439 |
folder_num = curr_wmh_str.split('/')[1] |
|
|
440 |
|
|
|
441 |
#print('THE FOLDER NUM', folder_num) |
|
|
442 |
while not found_background: |
|
|
443 |
x = randint(0, img_dims[0]-patch_size-1) |
|
|
444 |
y = randint(0, img_dims[1]-patch_size-1) |
|
|
445 |
z = randint(0, img_dims[2]-patch_size-1) |
|
|
446 |
|
|
|
447 |
#Load and check center pixel |
|
|
448 |
if wmh_np[x + ((patch_size - 1)/2), y + ((patch_size-1)/2), z + ((patch_size-1)/2)] == 0: |
|
|
449 |
found_background = True |
|
|
450 |
loc_str = str(folder_num) + '|' + str(x) + '|' + str(y) + '|' + str(z) |
|
|
451 |
return loc_str |
|
|
452 |
num_generated += 1 |
|
|
453 |
#print('Num generated until a background batch was found: ', num_generated) |
|
|
454 |
return loc_str |
|
|
455 |
|
|
|
456 |
def parseLocStr(loc_str): |
|
|
457 |
s = loc_str.split('|') |
|
|
458 |
return s[0], int(s[1]), int(s[2]), int(s[3]) |
|
|
459 |
|
|
|
460 |
################################################################################################################################## |
|
|
461 |
################################################################################################################################## |
|
|
462 |
|
|
|
463 |
#[518803341, 1496491, 217508] |
|
|
464 |
def classCount(main_folder_path = '../Data/MS2017b/', img_type_path = 'pre/FLAIR_s.nii.gz', gt_type_path = 'wmh_s.nii.gz'): |
|
|
465 |
scan_folders = glob.glob(main_folder_path + 'scans/*') |
|
|
466 |
nums = [0, 0 ,0] |
|
|
467 |
for sf in scan_folders: |
|
|
468 |
wmh_np = numpyFromScan(os.path.join(sf, gt_type_path)) |
|
|
469 |
unique, counts = np.unique(wmh_np, return_counts= True) |
|
|
470 |
d = dict(zip(unique, counts)) |
|
|
471 |
for i in range(3): |
|
|
472 |
try: |
|
|
473 |
nums[i] += d[i] |
|
|
474 |
except KeyError: |
|
|
475 |
pass |
|
|
476 |
print nums |
|
|
477 |
|
|
|
478 |
def extractImgBatch(batch_size, img_list, img_size, onlyLesions = False, main_folder_path = '../Data/MS2017b/', with_priv = False): |
|
|
479 |
img_b = np.zeros([batch_size, 1, img_size[0], img_size[1], img_size[2]]) |
|
|
480 |
label_b = np.zeros([batch_size, 1, img_size[0], img_size[1], img_size[2]]) |
|
|
481 |
if with_priv: |
|
|
482 |
gif_b = np.zeros([batch_size, 1, img_size[0], img_size[1], img_size[2]]) |
|
|
483 |
|
|
|
484 |
for i in range(batch_size): |
|
|
485 |
img_str = img_list[randint(0, len(img_list)-1)] |
|
|
486 |
img_np = numpyFromScan(os.path.join(main_folder_path, img_str)) |
|
|
487 |
img_np = img_np.transpose((3,0,1,2)) |
|
|
488 |
img_b[i, :,:,:,:] = img_np |
|
|
489 |
|
|
|
490 |
wmh_str = img_str.replace('slices', 'gt_slices').replace('FLAIR', 'wmh').replace('/pre','') |
|
|
491 |
gt_np = numpyFromScan(os.path.join(main_folder_path, wmh_str)) |
|
|
492 |
gt_np = gt_np.transpose((3,0,1,2)) |
|
|
493 |
label_b[i, :,:,:,:] = gt_np |
|
|
494 |
|
|
|
495 |
if with_priv: |
|
|
496 |
gif_str = img_str.replace('scans','gifs').replace('FLAIR', 'parcellation').replace('/pre','') |
|
|
497 |
gif_np = numpyFromScan(os.path.join(main_folder_path, gif_str)) |
|
|
498 |
gif_np = gt_np.transpose((3,0,1,2)) |
|
|
499 |
gif_b[i, :,:,:,:] = gif_np |
|
|
500 |
if with_priv: |
|
|
501 |
return img_b, label_b, gif_b |
|
|
502 |
return img_b, label_b, None |
|
|
503 |
|
|
|
504 |
#------------------------------------------------------------------------------ |
|
|
505 |
#END OF 3D PREPROCESSING |
|
|
506 |
#------------------------------------------------------------------------------ |
|
|
507 |
|
|
|
508 |
#3D |
|
|
509 |
#generateTrainValFile(0.8, img_path = '/pre/FLAIR_s.nii.gz', main_folder = '../Data/MS2017b/') |
|
|
510 |
#generateTrainValFile(0.8, img_path = '/pre/FLAIR_256x256x256orig.nii.gz', main_folder = '../Data/MS2017b/', postfix='_256x256x256orig') |
|
|
511 |
#generateTrainValFile(0.8, main_folder = '../../Data/MS2017b/', postfix='_200x200x100orig') |
|
|
512 |
#extractCenterPixelPatches(N = 81, main_folder = '../../Data/MS2017b/', postfix = '_200x200x100orig') |
|
|
513 |
#extractCenterPixelPatches(N = 71, main_folder = '../../Data/MS2017b/', postfix = '_200x200x100orig') |
|
|
514 |
#printDimensions(img_path = 'pre/FLAIR_s128.nii.gz', segm_path = 'wmh_s128.nii.gz', data_folder = '../Data/MS2017b/') |
|
|
515 |
#printDimensions(img_path = 'pre/FLAIR.nii.gz', segm_path = 'wmh.nii.gz', data_folder = '../../Data/MS2017b/') |
|
|
516 |
#printDimensions(img_path = 'pre/FLAIR256x256x256orig.nii.gz', segm_path = 'wmh.nii.gz', data_folder = '../../Data/MS2017b/') |
|
|
517 |
#extractCenterPixelPatches() |
|
|
518 |
#extractCenterPixelPatches(N = 91) |
|
|
519 |
#extractCenterPixelPatches(N = 101, main_folder = '../../Data/MS2017b/', postfix = '_256x256x256orig') |
|
|
520 |
#generateTrainValFile(0.8, img_path = '/pre/FLAIR_s128.nii.gz', main_folder = '../Data/MS2017b/', postfix='128x128x64') |
|
|
521 |
#classCount() |
|
|
522 |
|
|
|
523 |
#extractMeanDataStats() |