forked from 2jieun2/UHF-guided_segmentation
-
Notifications
You must be signed in to change notification settings - Fork 2
/
data.py
351 lines (321 loc) · 13.7 KB
/
data.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
import numpy as np
import nibabel as nib
import torch
from torch.utils.data import Dataset
from scipy.ndimage.interpolation import affine_transform
from scipy.ndimage import rotate
from skimage import exposure
def save_nii(arr, path, affine=np.eye(4)):
nii_img = nib.Nifti1Image(arr, affine=affine)
nib.save(nii_img, path)
def load_nii(path_file):
proxy = nib.load(path_file)
array = proxy.get_fdata()
return array
class Paired3T7T_3D(Dataset):
def __init__(self, path_dataset, train=False):
self.patient_ids, self.x_list, self.y_list = self.get_dataset(path_dataset)
self.train = train
def __len__(self):
return len(self.patient_ids)
def __getitem__(self, index):
patient_id = self.patient_ids[index]
x = self.x_list[index]
y = self.y_list[index]
x_h, x_w, x_d = x.shape
y_h, y_w, y_d = y.shape
if self.train:
aug_index = np.random.rand(3)
data = [x, y]
if aug_index[0] > 0.5:
x, y = rand_rotate_specific(data)
if aug_index[1] > 0.5:
x, y = rand_scale(data, 0.8, 1.2)
if aug_index[2] > 0.5:
x, y = flip_by_axis(data, axis=0)
x = torch.from_numpy(x.copy()).float().view(1, x_h, x_w, x_d)
if self.seg:
y = segmap_to_onehot(y) # output shape: (num_labels, h, w, d)
y = torch.from_numpy(y.copy()).float()
else:
y = torch.from_numpy(y.copy()).float().view(1, y_h, y_w, y_d)
return {'patient_id': patient_id, 'x': x, 'y': y}
def get_dataset(self, path_dataset):
patient_ids = []
x_list = []
y_list = []
for path_data in path_dataset:
patient_id = path_data.split('/')[-1]
path_3t = f'{path_data}/3t_norm.nii'
path_7t = f'{path_data}/7t_norm.nii'
x = nib.load(path_3t).get_data()
y = nib.load(path_7t).get_data()
x_list.append(x)
y_list.append(y)
patient_ids.append(patient_id)
return patient_ids, x_list, y_list
class Paired3T7T_2D(Dataset):
def __init__(self, path_dataset, train=False, plane='axial'):
self.patient_ids, self.idx_list, self.x_list, self.y_list = self.get_dataset(path_dataset, plane)
self.path_dataset = path_dataset
self.train = train
self.plane = plane
def __len__(self):
return len(self.x_list)
def __getitem__(self, index):
data_index = self.idx_list[index]
x = self.x_list[index]
y = self.y_list[index]
h, w = x.shape
if self.train:
aug_index = np.random.rand(3)
data = [x, y]
if aug_index[0] > 0.5:
x, y = rand_rotate_specific(data)
if aug_index[1] > 0.5:
x, y = rand_scale(data, 0.8, 1.2, type='2D')
if aug_index[2] > 0.5:
x, y = flip_by_axis(data, axis=0)
x = torch.from_numpy(x.copy()).float().view(1, h, w)
if self.seg:
y = segmap_to_onehot(y)
y = torch.from_numpy(y.copy()).float()
else:
y = torch.from_numpy(y.copy()).float().view(1, h, w)
return {'index': data_index, 'x': x, 'y': y}
def blank_vox_list(self, vox_dim=1):
vox_list = []
for path_data in self.path_dataset:
patient_id = path_data.split('/')[-1]
path_x = f'{path_data}/3t_norm.nii'
x = nib.load(path_x).get_data()
vox_list.append(np.zeros((vox_dim, *x.shape)))
return vox_list
def patient_list(self):
return self.patient_ids
def get_dataset(self, path_dataset, plane):
patient_ids = []
idx_list = []
x_list = []
y_list = []
for patient_idx, path_data in enumerate(path_dataset):
patient_id = path_data.split('/')[-1]
patient_ids.append(patient_id)
path_3t = f'{path_data}/3t_norm.nii'
path_7t = f'{path_data}/7t_norm.nii'
x = nib.load(path_3t).get_data()
y = nib.load(path_7t).get_data()
if plane == 'axial':
x = np.transpose(x, (2, 0, 1))
y = np.transpose(y, (2, 0, 1))
elif plane == 'coronal':
x = np.transpose(x, (1, 0, 2))
y = np.transpose(y, (1, 0, 2))
for slice_idx in range(x.shape[0]):
idx_list.append(np.array([patient_idx, slice_idx]))
x_list.append(x[slice_idx])
y_list.append(y[slice_idx])
return patient_ids, idx_list, x_list, y_list
def segmap_to_onehot(y, num_labels=4):
out = np.zeros((num_labels, *y.shape))
for idx, label in enumerate(range(num_labels)):
out[idx] = np.where(y == label, 1, 0)
return out
def flip_by_axis(data, axis):
out = []
for d in data:
out.append(np.flip(d, axis=axis))
return out
def rand_scale(data, range_min, range_max, type='3D'):
scale_uniform = np.random.rand(1)[0]
scale = (range_max - range_min) * scale_uniform + range_min
if type == '3D':
aff_matrix = np.array([[scale, 0, 0],
[0, scale, 0],
[0, 0, scale]])
elif type == '2D':
aff_matrix = np.array([[scale, 0],
[0, scale]])
center = 0.5 * np.array(data[0].shape)
offset = center - center.dot(aff_matrix)
out = []
for d in data:
out.append(affine_transform(d, aff_matrix, offset=offset))
return out
def rand_rotate_specific(data, specific_angle=[90, 180, 270]):
angle = np.random.choice(specific_angle)
out = []
for d in data:
out.append(rotate(d, angle, reshape=False))
return out
def clahe(arr, clip_limit=0.2, type='3D'):
if type == '3D':
kernel_size = (arr.shape[0] // 5,
arr.shape[1] // 5,
arr.shape[2] // 5)
elif type == '2D':
kernel_size = (arr.shape[0] // 5,
arr.shape[1] // 5)
kernel_size = np.array(kernel_size)
out = [exposure.equalize_adapthist(im,
kernel_size=kernel_size,
clip_limit=clip_limit)
for im in [arr]]
return out[0]
# https://www.nitrc.org/projects/ibsr
class IBSR(Dataset):
def __init__(self, path_dataset, train=False, seg_num=4):
self.patient_ids, self.x_list, self.y_list = self.get_dataset_IBSR(path_dataset)
self.seg_num = seg_num
self.train = train
def __len__(self):
return len(self.patient_ids)
def __getitem__(self, index):
patient_id = self.patient_ids[index]
x = self.x_list[index]
y = self.y_list[index]
x = clahe(x)
if self.train:
aug_index = np.random.rand(3)
data = [x, y]
if aug_index[0] > 0.5:
x, y = rand_rotate_specific(data)
if aug_index[1] > 0.5:
x, y = rand_scale(data, 0.8, 1.2)
if aug_index[2] > 0.5:
x, y = flip_by_axis(data, axis=0)
x_h, x_w, x_d = x.shape
x = torch.from_numpy(x.copy()).float().view(1, x_h, x_w, x_d)
y = segmap_to_onehot(y, self.seg_num) # output shape: (num_labels, h, w, d)
y = torch.from_numpy(y.copy()).float()
if self.train == False:
ulb = self.ulb_list[index]
ulb = torch.from_numpy(ulb.copy()).float().view(1, x_h, x_w, x_d)
return {'patient_id': patient_id, 'x': x, 'y': y, 'ulb': ulb}
else:
return {'patient_id': patient_id, 'x': x, 'y': y}
def get_dataset_IBSR(self, path_dataset):
patient_ids = []
x_list = []
y_list = []
for path_data in path_dataset:
patient_id = path_data.split('/')[-1].split('_')[-1]
path_x = f'{path_data}/IBSR_{patient_id}_ana_strip_norm.nii'
# The 'fill' files have any regions of zeros that are inside the brain mask set to 1 (the CSF value). https://www.nitrc.org/forum/message.php?msg_id=25702
path_y = f'{path_data}/IBSR_{patient_id}_segTRI_fill_ana.nii'
x = nib.load(path_x).get_fdata().squeeze()
y = nib.load(path_y).get_fdata().squeeze()
x_list.append(x)
y_list.append(y)
patient_ids.append(patient_id)
return patient_ids, x_list, y_list
class MALC(Dataset):
def __init__(self, path_dataset, train=False, seg_num=28, plane='axial', spatial_info=False):
self.patient_ids, self.idx_list, self.x_list, self.y_list, self.w_list = self.get_dataset_MALC(path_dataset, plane, spatial_info)
self.path_dataset = path_dataset
self.seg_num = seg_num
self.train = train
self.plane = plane
self.spatial_info = spatial_info
def __len__(self):
return len(self.x_list)
def __getitem__(self, index):
data_idx = self.idx_list[index]
x = self.x_list[index]
y = self.y_list[index]
w = self.w_list[index]
if self.spatial_info:
x = torch.from_numpy(x.copy()).float()
else:
x_h, x_w = x.shape
x = torch.from_numpy(x.copy()).float().view(1, x_h, x_w)
y = segmap_to_onehot(y, self.seg_num) # output shape: (num_labels, h, w)
y = torch.from_numpy(y.copy()).float()
w = torch.from_numpy(w.copy()).float()
return {'index': data_idx, 'x': x, 'y': y, 'w': w}
def blank_vox_list(self, vox_dim=1):
vox_list = []
for path_data in self.path_dataset:
patient_id = path_data.split('/')[-1]
path_x = f'{path_data}/{patient_id}_norm.nii.gz'
x = nib.load(path_x).get_data()
vox_list.append(np.zeros((vox_dim, *x.shape)))
return vox_list
def patient_list(self):
return self.patient_ids
def get_dataset_MALC(self, path_dataset, plane, spatial_info):
patient_ids = []
idx_list = []
x_list = []
y_list = []
w_list = []
for patient_idx, path_data in enumerate(path_dataset):
patient_id = path_data.split('/')[-1]
patient_ids.append(patient_id)
path_x = f'{path_data}/{patient_id}_norm.nii.gz'
path_y = f'{path_data}/{patient_id}_glm_27.nii.gz'
x = nib.load(path_x).get_fdata().squeeze()
y = nib.load(path_y).get_fdata().squeeze()
if plane == 'sagittal':
lut_aseg = np.zeros(int(y.max() + 1), dtype='int')
labels_sag = np.array([0, 3, 4, 12, 13, 14, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27])
for idx, value in enumerate(labels_sag):
lut_aseg[value] = idx # lut_aseg => [0, 0, 0, 3, 4, 0, ..., 27]
left_right = {1: 3, 2: 4, 5: 18, 6: 19, 7: 20, 8: 21, 9: 22, 10: 23, 11: 24, 15: 25, 16: 26, 17: 27}
for key, value in left_right.items():
y[y == key] = value
mapped_aseg_sag = lut_aseg.ravel()[y.astype(int).ravel()]
y = mapped_aseg_sag.reshape((y.shape[0], y.shape[1], y.shape[2]))
w = self.create_weight_mask(y.astype(int)) # weight mask
if plane == 'axial':
x = np.transpose(x, (2, 0, 1))
y = np.transpose(y, (2, 0, 1))
w = np.transpose(w, (2, 0, 1))
elif plane == 'coronal':
x = np.transpose(x, (1, 0, 2))
y = np.transpose(y, (1, 0, 2))
w = np.transpose(w, (1, 0, 2))
for slice_idx in range(x.shape[0]):
idx_list.append(np.array([patient_idx, slice_idx]))
if spatial_info:
if slice_idx < 3:
pad_num = 3 - slice_idx
x_ = x[0:slice_idx+4]
x_ = np.pad(x_, ((pad_num,0),(0,0),(0,0)), mode='constant', constant_values=0)
elif slice_idx > (x.shape[0]-4):
pad_num = 3 - (x.shape[0]-1-slice_idx)
x_ = x[slice_idx-3:]
x_ = np.pad(x_, ((0,pad_num),(0,0),(0,0)), mode='constant', constant_values=0)
else:
x_ = x[slice_idx-3:slice_idx+4]
x_list.append(x_)
else:
x_list.append(x[slice_idx])
y_list.append(y[slice_idx])
w_list.append(w[slice_idx])
return patient_ids, idx_list, x_list, y_list, w_list
# weight map generator https://github.com/Deep-MI/FastSurfer/blob/stable/FastSurferCNN/data_loader/load_neuroimaging_data.py
def create_weight_mask(self, mapped_aseg, max_weight=5, max_edge_weight=5):
"""
Function to create weighted mask - with median frequency balancing and edge-weighting
:param np.ndarray mapped_aseg: label space segmentation
:param int max_weight: an upper bound on weight values
:param int max_edge_weight: edge-weighting factor
:return: np.ndarray weights_mask: generated weights mask
"""
unique, counts = np.unique(mapped_aseg, return_counts=True)
for i in range(np.max(unique)):
if i not in unique:
counts = np.insert(counts, i, 0)
# Median Frequency Balancing
class_wise_weights = np.median(counts) / counts
class_wise_weights[class_wise_weights > max_weight] = max_weight
(h, w, d) = mapped_aseg.shape
weights_mask = np.reshape(class_wise_weights[mapped_aseg.ravel()], (h, w, d))
# Gradient Weighting
(gx, gy, gz) = np.gradient(mapped_aseg)
grad_weight = max_edge_weight * np.asarray(
np.power(np.power(gx, 2) + np.power(gy, 2) + np.power(gz, 2), 0.5) > 0,
dtype='float')
weights_mask += grad_weight
return weights_mask