forked from tkuanlun350/3DUnet-Tensorflow-Brats18
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
456 lines (428 loc) · 17 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
#####
# some functions are borrowed from https://github.com/taigw/brats17/
#####
import nibabel
import numpy as np
import random
import os
import SimpleITK as sitk
import pickle
from scipy import ndimage
import config
import copy
def flip_lr(data):
data = copy.deepcopy(data)
img = data['images']
img = np.transpose(img, [3, 0 ,1, 2])
weight = data['weights'][:,:,:,0]
flipped_data = []
for moda in range(len(img)):
flipped_data.append(np.flip(img[moda], axis=-1))
flipped_data = np.array(flipped_data)
weight = np.flip(weight, axis=-1)
data['images'] = np.transpose(flipped_data, [1, 2, 3, 0])
data['weights'] = np.transpose(weight[np.newaxis, ...], [1, 2, 3, 0])
data['is_flipped'] = True
return data
def crop_brain_region(im, gt, with_gt=True):
mods = sorted(im.keys())
volume_list = []
for mod_idx, mod in enumerate(mods):
filename = im[mod]
volume = load_nifty_volume_as_array(filename, with_header=False)
# 155 244 244
if mod_idx == 0:
# contain whole tumor
margin = 5 # small padding value
original_shape = volume.shape
bbmin, bbmax = get_none_zero_region(volume, margin)
volume = crop_ND_volume_with_bounding_box(volume, bbmin, bbmax)
if mod_idx == 0:
weight = np.asarray(volume > 0, np.float32)
if config.INTENSITY_NORM == 'modality':
volume = itensity_normalize_one_volume(volume)
volume_list.append(volume)
## volume_list [(depth, h, w)*4]
if with_gt:
label = load_nifty_volume_as_array(gt, False)
label[label == 4] = 3
label = crop_ND_volume_with_bounding_box(label, bbmin, bbmax)
return volume_list, label, weight, original_shape, [bbmin, bbmax]
else:
return volume_list, None, weight, original_shape, [bbmin, bbmax]
def transpose_volumes(volumes, slice_direction):
"""
transpose a list of volumes
inputs:
volumes: a list of nd volumes
slice_direction: 'axial', 'sagittal', or 'coronal'
outputs:
tr_volumes: a list of transposed volumes
"""
if (slice_direction == 'axial'):
tr_volumes = volumes
elif(slice_direction == 'sagittal'):
if isinstance(volumes, list) or len(volumes.shape) > 3:
tr_volumes = [np.transpose(x, (2, 0, 1)) for x in volumes]
else:
tr_volumes = np.transpose(volumes, (2, 0, 1))
elif(slice_direction == 'coronal'):
if isinstance(volumes, list) or len(volumes.shape) > 3:
tr_volumes = [np.transpose(x, (1, 0, 2)) for x in volumes]
else:
tr_volumes = np.transpose(volumes, (1, 0, 2))
else:
print('undefined slice direction:', slice_direction)
tr_volumes = volumes
return tr_volumes
def remove_external_core(lab_main, lab_ext):
"""
remove the core region that is outside of whole tumor
"""
# for each component of lab_ext, compute the overlap with lab_main
s = ndimage.generate_binary_structure(3,2) # iterate structure
labeled_array, numpatches = ndimage.label(lab_ext,s) # labeling
sizes = ndimage.sum(lab_ext,labeled_array,range(1,numpatches+1))
sizes_list = [sizes[i] for i in range(len(sizes))]
new_lab_ext = np.zeros_like(lab_ext)
for i in range(len(sizes)):
sizei = sizes_list[i]
labeli = np.where(sizes == sizei)[0] + 1
componenti = labeled_array == labeli
overlap = componenti * lab_main
if((overlap.sum()+ 0.0)/sizei >= 0.5):
new_lab_ext = np.maximum(new_lab_ext, componenti)
return new_lab_ext
def get_largest_two_component(img, print_info = False, threshold = None):
"""
Get the largest two components of a binary volume
inputs:
img: the input 3D volume
threshold: a size threshold
outputs:
out_img: the output volume
"""
s = ndimage.generate_binary_structure(3,2) # iterate structure
labeled_array, numpatches = ndimage.label(img,s) # labeling
sizes = ndimage.sum(img,labeled_array,range(1,numpatches+1))
sizes_list = [sizes[i] for i in range(len(sizes))]
sizes_list.sort()
if(print_info):
print('component size', sizes_list)
if(len(sizes) == 1):
out_img = img
else:
if(threshold):
out_img = np.zeros_like(img)
for temp_size in sizes_list:
if(temp_size > threshold):
temp_lab = np.where(sizes == temp_size)[0] + 1
temp_cmp = labeled_array == temp_lab
out_img = (out_img + temp_cmp) > 0
return out_img
else:
max_size1 = sizes_list[-1]
max_size2 = sizes_list[-2]
max_label1 = np.where(sizes == max_size1)[0] + 1
max_label2 = np.where(sizes == max_size2)[0] + 1
component1 = labeled_array == max_label1
component2 = labeled_array == max_label2
if(max_size2*10 > max_size1):
component1 = (component1 + component2) > 0
out_img = component1
return out_img
def get_ND_bounding_box(label, margin):
"""
get the bounding box of the non-zero region of an ND volume
"""
input_shape = label.shape
if(type(margin) is int ):
margin = [margin]*len(input_shape)
assert(len(input_shape) == len(margin))
indxes = np.nonzero(label)
idx_min = []
idx_max = []
for i in range(len(input_shape)):
idx_min.append(indxes[i].min())
idx_max.append(indxes[i].max())
for i in range(len(input_shape)):
idx_min[i] = max(idx_min[i] - margin[i], 0)
idx_max[i] = min(idx_max[i] + margin[i], input_shape[i] - 1)
return idx_min, idx_max
def set_ND_volume_roi_with_bounding_box_range(volume, bb_min, bb_max, sub_volume):
"""
set a subregion to an nd image.
"""
dim = len(bb_min)
out = volume
if(dim == 2):
out[np.ix_(range(bb_min[0], bb_max[0] + 1),
range(bb_min[1], bb_max[1] + 1))] = sub_volume
elif(dim == 3):
out[np.ix_(range(bb_min[0], bb_max[0] + 1),
range(bb_min[1], bb_max[1] + 1),
range(bb_min[2], bb_max[2] + 1))] = sub_volume
elif(dim == 4):
out[np.ix_(range(bb_min[0], bb_max[0] + 1),
range(bb_min[1], bb_max[1] + 1),
range(bb_min[2], bb_max[2] + 1),
range(bb_min[3], bb_max[3] + 1))] = sub_volume
else:
raise ValueError("array dimension should be 2, 3 or 4")
return out
def convert_label(in_volume, label_convert_source, label_convert_target):
"""
convert the label value in a volume
inputs:
in_volume: input nd volume with label set label_convert_source
label_convert_source: a list of integers denoting input labels, e.g., [0, 1, 2, 4]
label_convert_target: a list of integers denoting output labels, e.g.,[0, 1, 2, 3]
outputs:
out_volume: the output nd volume with label set label_convert_target
"""
mask_volume = np.zeros_like(in_volume)
convert_volume = np.zeros_like(in_volume)
for i in range(len(label_convert_source)):
source_lab = label_convert_source[i]
target_lab = label_convert_target[i]
if(source_lab != target_lab):
temp_source = np.asarray(in_volume == source_lab)
temp_target = target_lab * temp_source
mask_volume = mask_volume + temp_source
convert_volume = convert_volume + temp_target
out_volume = in_volume * 1
out_volume[mask_volume>0] = convert_volume[mask_volume>0]
return out_volume
def set_roi_to_volume(volume, center, sub_volume):
"""
set the content of an roi of a 3d/4d volume to a sub volume
inputs:
volume: the input 3D/4D volume
center: the center of the roi
sub_volume: the content of sub volume
outputs:
output_volume: the output 3D/4D volume
"""
volume_shape = volume.shape
patch_shape = sub_volume.shape
output_volume = volume
for i in range(len(center)):
if(center[i] >= volume_shape[i]):
return output_volume
r0max = [int(x/2) for x in patch_shape]
r1max = [patch_shape[i] - r0max[i] for i in range(len(r0max))]
r0 = [min(r0max[i], center[i]) for i in range(len(r0max))]
r1 = [min(r1max[i], volume_shape[i] - center[i]) for i in range(len(r0max))]
patch_center = r0max
if(len(center) == 3):
output_volume[np.ix_(range(center[0] - r0[0], center[0] + r1[0]),
range(center[1] - r0[1], center[1] + r1[1]),
range(center[2] - r0[2], center[2] + r1[2]))] = \
sub_volume[np.ix_(range(patch_center[0] - r0[0], patch_center[0] + r1[0]),
range(patch_center[1] - r0[1], patch_center[1] + r1[1]),
range(patch_center[2] - r0[2], patch_center[2] + r1[2]))]
elif(len(center) == 4):
output_volume[np.ix_(range(center[0] - r0[0], center[0] + r1[0]),
range(center[1] - r0[1], center[1] + r1[1]),
range(center[2] - r0[2], center[2] + r1[2]),
range(center[3] - r0[3], center[3] + r1[3]))] = \
sub_volume[np.ix_(range(patch_center[0] - r0[0], patch_center[0] + r1[0]),
range(patch_center[1] - r0[1], patch_center[1] + r1[1]),
range(patch_center[2] - r0[2], patch_center[2] + r1[2]),
range(patch_center[3] - r0[3], patch_center[3] + r1[3]))]
else:
raise ValueError("array dimension should be 3 or 4")
return output_volume
def binary_dice3d(s,g):
"""
dice score of 3d binary volumes
inputs:
s: segmentation volume
g: ground truth volume
outputs:
dice: the dice score
"""
assert(len(s.shape)==3)
[Ds, Hs, Ws] = s.shape
[Dg, Hg, Wg] = g.shape
assert(Ds==Dg and Hs==Hg and Ws==Wg)
prod = np.multiply(s, g)
s0 = prod.sum()
s1 = s.sum()
s2 = g.sum()
dice = (2.0*s0 + 1e-10)/(s1 + s2 + 1e-10)
return dice
def load_nifty_volume_as_array(filename, with_header = False):
"""
load nifty image into numpy array, and transpose it based on the [z,y,x] axis order
The output array shape is like [Depth, Height, Width]
inputs:
filename: the input file name, should be *.nii or *.nii.gz
with_header: return affine and hearder infomation
outputs:
data: a numpy data array
"""
img = nibabel.load(filename)
data = img.get_data()
data = np.transpose(data, [2,1,0])
if(with_header):
return data, img.affine, img.header
else:
return data
def get_none_zero_region(im, margin):
"""
get the bounding box of the non-zero region of an ND volume
"""
input_shape = im.shape
if(type(margin) is int ):
margin = [margin]*len(input_shape)
assert(len(input_shape) == len(margin))
indxes = np.nonzero(im)
idx_min = []
idx_max = []
for i in range(len(input_shape)):
idx_min.append(indxes[i].min())
idx_max.append(indxes[i].max())
for i in range(len(input_shape)):
idx_min[i] = max(idx_min[i] - margin[i], 0)
idx_max[i] = min(idx_max[i] + margin[i], input_shape[i] - 1)
return idx_min, idx_max
def itensity_normalize_one_volume(volume):
"""
normalize the itensity of an nd volume based on the mean and std of nonzeor region
inputs:
volume: the input nd volume
outputs:
out: the normalized nd volume
"""
pixels = volume[volume > 0]
mean = pixels.mean()
std = pixels.std()
out = (volume - mean)/std
# random normal too slow
#out_random = np.random.normal(0, 1, size = volume.shape)
out_random = np.zeros(volume.shape)
out[volume == 0] = out_random[volume == 0]
return out
def crop_ND_volume_with_bounding_box(volume, min_idx, max_idx):
"""
crop/extract a subregion form an nd image.
"""
dim = len(volume.shape)
assert(dim >= 2 and dim <= 5)
if(dim == 2):
output = volume[np.ix_(range(min_idx[0], max_idx[0] + 1),
range(min_idx[1], max_idx[1] + 1))]
elif(dim == 3):
output = volume[np.ix_(range(min_idx[0], max_idx[0] + 1),
range(min_idx[1], max_idx[1] + 1),
range(min_idx[2], max_idx[2] + 1))]
elif(dim == 4):
output = volume[np.ix_(range(min_idx[0], max_idx[0] + 1),
range(min_idx[1], max_idx[1] + 1),
range(min_idx[2], max_idx[2] + 1),
range(min_idx[3], max_idx[3] + 1))]
elif(dim == 5):
output = volume[np.ix_(range(min_idx[0], max_idx[0] + 1),
range(min_idx[1], max_idx[1] + 1),
range(min_idx[2], max_idx[2] + 1),
range(min_idx[3], max_idx[3] + 1),
range(min_idx[4], max_idx[4] + 1))]
else:
raise ValueError("the dimension number shoud be 2 to 5")
return output
def get_random_roi_sampling_center(input_shape, output_shape, sample_mode='full', bounding_box = None):
"""
get a random coordinate representing the center of a roi for sampling
inputs:
input_shape: the shape of sampled volume
output_shape: the desired roi shape
sample_mode: 'valid': the entire roi should be inside the input volume
'full': only the roi centre should be inside the input volume
bounding_box: the bounding box which the roi center should be limited to
outputs:
center: the output center coordinate of a roi
"""
center = []
for i in range(len(input_shape)):
if(sample_mode[i] == 'full'):
if(bounding_box):
x0 = bounding_box[i*2]; x1 = bounding_box[i*2 + 1]
else:
x0 = 0; x1 = input_shape[i]
else:
if(bounding_box):
x0 = bounding_box[i*2] + int(output_shape[i]/2)
x1 = bounding_box[i*2+1] - int(output_shape[i]/2)
else:
x0 = int(output_shape[i]/2)
x1 = input_shape[i] - x0
if(x1 <= x0):
centeri = int((x0 + x1)/2)
else:
centeri = random.randint(x0, x1)
center.append(centeri)
return center
def extract_roi_from_volume(volume, in_center, output_shape, fill = 'random'):
"""
extract a roi from a 3d volume
inputs:
volume: the input 3D volume
in_center: the center of the roi
output_shape: the size of the roi
fill: 'random' or 'zero', the mode to fill roi region where is outside of the input volume
outputs:
output: the roi volume
"""
input_shape = volume.shape
if(fill == 'random'):
output = np.random.normal(0, 1, size = output_shape)
else:
output = np.zeros(output_shape)
r0max = [int(x/2) for x in output_shape]
r1max = [output_shape[i] - r0max[i] for i in range(len(r0max))]
r0 = [min(r0max[i], in_center[i]) for i in range(len(r0max))]
r1 = [min(r1max[i], input_shape[i] - in_center[i]) for i in range(len(r0max))]
out_center = r0max
output[np.ix_(range(out_center[0] - r0[0], out_center[0] + r1[0]),
range(out_center[1] - r0[1], out_center[1] + r1[1]),
range(out_center[2] - r0[2], out_center[2] + r1[2]))] = \
volume[np.ix_(range(in_center[0] - r0[0], in_center[0] + r1[0]),
range(in_center[1] - r0[1], in_center[1] + r1[1]),
range(in_center[2] - r0[2], in_center[2] + r1[2]))]
return output
def save_to_pkl(probs, filename, outdir=""):
"""
probs => [155, 240, 240]
"""
if not os.path.exists("./{}".format(outdir)):
os.mkdir("./{}".format(outdir))
with open("./{}/{}.pkl".format(outdir, filename), 'wb') as f:
pickle.dump(probs, f, protocol=pickle.HIGHEST_PROTOCOL)
def save_to_nii(im, filename, outdir="", mode="image", system="sitk"):
"""
Save numpy array to nii.gz format to submit
im: 3d numpy array ex: [155, 240, 240]
"""
if system == "sitk":
if mode == 'label':
img = sitk.GetImageFromArray(im.astype(np.uint8))
else:
img = sitk.GetImageFromArray(im.astype(np.float32))
if not os.path.exists("./{}".format(outdir)):
os.mkdir("./{}".format(outdir))
sitk.WriteImage(img, "./{}/{}.nii.gz".format(outdir, filename))
else:
img = np.rot90(im, k=2, axes= (1,2))
OUTPUT_AFFINE = np.array(
[[0, 0, 1, 0],
[0, 1, 0, 0],
[1, 0, 0, 0],
[0, 0, 0, 1]])
if mode == 'label':
img = nibabel.Nifti1Image(img.astype(np.uint8), OUTPUT_AFFINE)
else:
img = nibabel.Nifti1Image(img.astype(np.float32), OUTPUT_AFFINE)
if not os.path.exists("./{}".format(outdir)):
os.mkdir("./{}".format(outdir))
nibabel.save(img, "./{}/{}.nii.gz".format(outdir, filename))