-
Notifications
You must be signed in to change notification settings - Fork 1
/
mri_dataset.py
184 lines (144 loc) · 6.04 KB
/
mri_dataset.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
import torch
from torch.utils.data import Dataset
from torch.utils.data.dataloader import default_collate
import numpy as np
from scipy.ndimage import zoom
class MRIDataset(Dataset):
def __init__(self, input_data, target, resize, norms=None, log=False, nan=False):
self.X_data = input_data
self.Y_data = target
self.resize = resize
self.normalize = norms
self.log = log
self.nan = nan
if self.normalize:
self.mean = norms[0]
self.std = norms[1]
print('Normalization applied to dataset')
if self.log:
print('Log applied to dataset')
if self.nan:
print('NaN replaced with 0s')
def __len__(self):
return len(self.Y_data)
def get_x_data(self):
return [np.array(x.dataobj) for x in self.X_data]
def get_y_data(self):
return [y for y in self.Y_data]
def __getitem__(self, idx):
x = np.array(self.X_data[idx].dataobj).astype(np.float32, copy=False)
y = self.Y_data[idx]
if self.nan:
x = np.nan_to_num(x)
assert(not np.isnan(x).any())
if self.resize > 0:
np.resize(x, (self.resize, self.resize, self.resize))
if self.normalize:
x = np.divide(np.subtract(x, self.mean), self.std)
if self.log:
y = np.log(y+100)
return (x, y)
class MultiMRIDataset(Dataset):
def __init__(self, input_data, target, resize, norms=None, log=False, nan=False):
self.X_data = input_data
self.Y_data = target
self.resize = resize
self.normalize = norms
self.log = log
self.nan = nan
if self.normalize:
self.mean = norms[0]
self.std = norms[1]
print('Normalization applied to dataset')
if self.log:
print('Log applied to dataset')
if self.nan:
print('NaN replaced with 0s')
def __len__(self):
return len(self.Y_data)
def get_x_data(self):
return [np.array(x.dataobj) for x in self.X_data]
def get_y_data(self):
return [y for y in self.Y_data]
def __getitem__(self, idx):
x = np.array(self.X_data[idx].dataobj).astype(np.float32, copy=False)
y = []
# Perform y data modification, if available
if len(self.Y_data) > 0:
y = np.copy(self.Y_data[idx])
# Converting age from months to year
y[0] = y[0] / 12.0
# Subtracting 1 to re-adjust range to start from 0
y[2] = y[2] - 1
y[3] = y[3] - 1
y[4] = y[4] - 1
y[5] = y[5] - 1
y[6] = y[6] - 1
if self.log:
y[11] = np.log(y[11]+10)
# Replacing NaN values with 0
if self.nan:
x = np.nan_to_num(x)
assert(not np.isnan(x).any())
if self.resize > 0:
np.resize(x, (self.resize, self.resize, self.resize))
if self.normalize:
x = np.divide(np.subtract(x, self.mean), self.std)
return (x, y)
class ThreeInputMRIDataset(Dataset):
def __init__(self, input_data1, input_data2, input_data3, target, resize, norms=None, log=False):
self.dataset1 = MRIDataset(input_data1, target, resize, norms[0], log)
self.dataset2 = MRIDataset(input_data2, target, resize, norms[1], log=False, nan=True)
self.dataset3 = MRIDataset(input_data3, target, resize, norms[2], log=False, nan=True)
self.resize = resize
self.normalize = norms
self.log = log
def __len__(self):
return len(self.dataset1.get_y_data())
def get_x_data(self):
return [self.dataset1.get_x_data(), self.dataset2.get_x_data(), self.dataset3.get_x_data()]
def get_y_data(self):
return self.dataset1.get_y_data()
def __getitem__(self, idx):
return ([self.dataset1[idx][0], self.dataset2[idx][0], self.dataset3[idx][0]], self.dataset1[idx][1])
class SixInputMultiOutputMRIDataset(Dataset):
def __init__(self, input_data1, input_data2, input_data3, input_data4, input_data5, input_data6, target, resize, norms, log, nan):
self.dataset1 = MultiMRIDataset(input_data1, target, resize, norms[0], log, nan)
self.dataset2 = MultiMRIDataset(input_data2, [], resize, norms[1], log, nan)
self.dataset3 = MultiMRIDataset(input_data3, [], resize, norms[2], log, nan)
self.dataset4 = MultiMRIDataset(input_data4, [], resize, norms[3], log, nan)
self.dataset5 = MultiMRIDataset(input_data5, [], resize, norms[4], log, nan)
self.dataset6 = MultiMRIDataset(input_data6, [], resize, norms[5], log, nan)
self.resize = resize
self.normalize = norms
self.log = log
def __len__(self):
return len(self.dataset1.get_y_data())
def get_x_data(self):
return [self.dataset1.get_x_data(), self.dataset2.get_x_data(), self.dataset3.get_x_data(),
self.dataset4.get_x_data(), self.dataset5.get_x_data(), self.dataset6.get_x_data()]
def get_y_data(self):
return self.dataset1.get_y_data()
def __getitem__(self, idx):
return ([self.dataset1[idx][0], self.dataset2[idx][0], self.dataset3[idx][0], self.dataset4[idx][0], self.dataset5[idx][0], self.dataset6[idx][0]]
, self.dataset1[idx][1])
"""
class SliceMRIDataset(Dataset):
def __init__(self, dataset, collate_fn=default_collate):
self.dataset = dataset
self.collate_fn = collate_fn
self._indices = list(range(len(self.dataset)))
def __len__(self):
return len(self.dataset)
@property
def shape(self):
return len(self),
def __getitem__(self, i):
if isinstance(i, (int, np.integer)):
Xb = self.dataset[i][0]
return Xb
if isinstance(i, slice):
i = self._indices[i]
Xb = self.collate_fn([self.dataset[j][0] for j in i])
return Xb
"""