-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_loader_abide.py
379 lines (326 loc) · 16 KB
/
data_loader_abide.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
import os
import copy
import numpy as np
import pandas as pd
import nibabel as nib
from sklearn.preprocessing import LabelEncoder
from tqdm import tqdm
import torch
import torch.nn as nn
import torchvision.transforms as transforms
import torch.utils.data as data
from utils import *
from transforms import *
# ABIDE-{I, II}
class ABIDEMRI(data.Dataset):
"""
Arguments:
path: path to data folder
labels_path: path to file with targets and additional information
target: column of targets df with target to predict. If None, loads images only
encode_target: if True, encode target with LabelEncoder
load_online (bool): if True, load mri images online. Else, preload everything during initialization
"""
def __init__(self, paths, labels_path, target=None, encode_target=False, load_online=False,
sub_path="ses-2", mri_type="sMRI", mri_file_suffix="", transform=None,
use_sources=[],
sources_boundaries={
"all" : [(0, 0, 0,), (152, 152, 152,)]
},
sources_scales={
"all" : 1.
},
clip=False,
start_pos=None, seq_len=None,
domain_target=None# "SOURCE"
):
self.mri_paths = {
"participant_id" : [],
"path" : [],
}
self.paths = paths if type(paths) is list else [paths]
self.labels = pd.read_csv(labels_path)
self.target, self.domain_target = self.set_target(target, encode_target, domain_target)
self.load_online = load_online
self.mri_type = mri_type
if self.mri_type == "sMRI":
self.type = "anat"
elif self.mri_type == "fMRI":
self.type = "func"
else:
self.type = None
# raise ValueError("Select sMRI or fMRI mri type.")
self.mri_file_suffix = mri_file_suffix
self.use_sources = use_sources
self.sources_boundaries = sources_boundaries
self.sources_scales = sources_scales
self.clip = clip
self.start_pos = start_pos
self.seq_len = seq_len
self.transform = transform
for path_to_folder in self.paths:
for patient_folder_name in tqdm(os.listdir(path_to_folder)):
if 'sub-' in patient_folder_name and os.path.isdir(path_to_folder + patient_folder_name):
if self.type is not None and self.type in os.listdir(os.path.join(path_to_folder, patient_folder_name, sub_path)):
temp_path = os.path.join(path_to_folder, patient_folder_name, sub_path, self.type)
elif self.type is None:
temp_path = os.path.join(path_to_folder, patient_folder_name, sub_path)
else:
continue
for filename in os.listdir(temp_path):
if self.mri_file_suffix in filename:
self.mri_paths["participant_id"].append(patient_folder_name)
full_path = os.path.join(temp_path, filename)
self.mri_paths["path"].append(full_path)
self.mri_paths = pd.DataFrame(self.mri_paths)
self.labels = self.labels.merge(self.mri_paths, on="participant_id")
self.mri_files = self.labels["path"].tolist()
if not self.load_online:
self.mri_files = [self.get_image(index, self.start_pos, self.seq_len) for index in tqdm(range(len(self.mri_files)))]
# update self.img_shape (and other params ?)
# self.output_img_shape = self[0].shape[1:4]
def set_target(self, target=None, encode_target=False, domain_target=None):
self.target, self.domain_target = None, None
if target is not None:
self.target = self.labels[target].copy()
if self.use_sources:
# зануляем таргеты для объектов из неинтересных нам источников
null_idx = ~self.labels["SOURCE"].isin(self.use_sources)
self.target[null_idx] = np.nan
if encode_target:
enc = LabelEncoder()
idx = self.target.notnull()
self.target[idx] = enc.fit_transform(self.target[idx])
if domain_target is not None:
self.domain_target = self.labels[domain_target].copy()
if self.use_sources:
# зануляем таргеты для объектов из неинтересных нам источников
null_idx = ~self.labels["SOURCE"].isin(self.use_sources)
self.domain_target[null_idx] = np.nan
self.domain_enc = LabelEncoder()
idx = self.domain_target.notnull()
self.domain_target[idx] = self.domain_enc.fit_transform(self.domain_target[idx])
return self.target, self.domain_target
def reshape_image(self, mri_img, coord_min, img_shape):
if self.mri_type == "sMRI":
return mri_img[coord_min[0]:coord_min[0] + img_shape[0],
coord_min[1]:coord_min[1] + img_shape[1],
coord_min[2]:coord_min[2] + img_shape[2]].reshape((1,) + img_shape)
if self.mri_type == "fMRI":
seq_len = mri_img.shape[-1]
return mri_img[coord_min[0]:coord_min[0] + img_shape[0],
coord_min[1]:coord_min[1] + img_shape[1],
coord_min[2]:coord_min[2] + img_shape[2], :].reshape((1,) + img_shape + (seq_len,))
def get_image(self, index, start_pos=None, seq_len=None):
def load_mri(mri_file):
if "nii" in mri_file:
img = load_nii_to_array(mri_file)
else:
img = np.load(mri_file)
return img
mri_file = self.mri_files[index]
s = self.labels["SOURCE"][index]
if self.use_sources and s not in self.use_sources:
return None
if s in self.sources_boundaries:
coord_min, img_shape = self.sources_boundaries[s]
else:
coord_min, img_shape = self.sources_boundaries["all"]
if s in self.sources_scales:
scale = self.sources_scales[s]
else:
scale = self.sources_scales["all"]
img = load_mri(mri_file)
# check for padding
cur_shape = np.array(img[..., 0].shape) if self.mri_type == "fMRI" else np.array(img.shape)
req_shape = np.array(coord_min) + np.array(img_shape)
padding = np.maximum(req_shape - cur_shape, 0)
img = Pad(tuple(padding), img_type=self.mri_type)(img[np.newaxis, :])[0]
# reshape
img = self.reshape_image(img, coord_min, img_shape)
if self.clip:
img = np.clip(img, 0., scale)
img /= scale
if self.mri_type == "sMRI":
return img
if self.mri_type == "fMRI":
if seq_len is None:
seq_len = img.shape[-1]
# what if seq_len == 0 ?
if start_pos is None:
start_pos = np.random.choice(img.shape[-1] - seq_len)
if seq_len == 1:
img = img[:, :, :, :, start_pos]
else:
img = img[:, :, :, :, start_pos:start_pos + seq_len]
img = img.transpose((4, 0, 1, 2, 3))
return img
def __getitem__(self, index):
img = self.get_image(index, self.start_pos, self.seq_len) if self.load_online else self.mri_files[index]
if self.transform is not None:
img = self.transform(img)
if self.target is None:
item = img
else:
item = [img, self.target[index]]
if self.domain_target is not None:
item += [self.domain_target[index]]
return item
def __len__(self):
return len(self.mri_files)
# CPAC
class CPACMRI(data.Dataset):
"""
Arguments:
path: path to data folder
labels_path: path to file with targets and additional information
target: column of targets df with target to predict. If None, loads images only
encode_target: if True, encode target with LabelEncoder
load_online (bool): if True, load mri images online. Else, preload everything during initialization
# CPAC - weare only supposed to find only fMRI files.
"""
def __init__(self, paths, labels_path, target=None, encode_target=False, load_online=False,
mri_type="fMRI", mri_file_suffix="",
get_patient_id=lambda p: "sub-" + p.split("_")[-3],
transform=None,
use_sources=[],
sources_boundaries={
"all" : [(0, 0, 0,), (152, 152, 152,)]
},
sources_scales={
"all" : 1.
},
clip=False,
start_pos=None, seq_len=None,
domain_target=None# "SOURCE"
):
self.mri_paths = {
"participant_id" : [],
"path" : [],
}
self.paths = paths if type(paths) is list else [paths]
self.labels = pd.read_csv(labels_path)
self.target, self.domain_target = self.set_target(target, encode_target, domain_target)
self.load_online = load_online
self.mri_type = mri_type
if self.mri_type == "sMRI":
self.type = "anat"
elif self.mri_type == "fMRI":
self.type = "func"
self.mri_file_suffix = mri_file_suffix
self.get_patient_id = get_patient_id
self.use_sources = use_sources
self.sources_boundaries = sources_boundaries
self.sources_scales = sources_scales
self.clip = clip
self.start_pos = start_pos
self.seq_len = seq_len
self.transform = transform
for path_to_folder in self.paths:
for filename in tqdm(os.listdir(path_to_folder)):
if self.mri_file_suffix in filename:
patient_id = self.get_patient_id(filename)
self.mri_paths["participant_id"].append(patient_id)
full_path = os.path.join(path_to_folder, filename)
self.mri_paths["path"].append(full_path)
self.mri_paths = pd.DataFrame(self.mri_paths)
self.labels = self.labels.merge(self.mri_paths, on="participant_id")
self.mri_files = self.labels["path"].tolist()
if not self.load_online:
self.mri_files = [self.get_image(index, self.start_pos, self.seq_len) for index in tqdm(range(len(self.mri_files)))]
# update self.img_shape (and other params ?)
# self.output_img_shape = self[0].shape[1:4]
def set_target(self, target=None, encode_target=False, domain_target=None):
self.target, self.domain_target = None, None
if target is not None:
self.target = self.labels[target].copy()
if self.use_sources:
# зануляем таргеты для объектов из неинтересных нам источников
null_idx = ~self.labels["SOURCE"].isin(self.use_sources)
self.target[null_idx] = np.nan
if encode_target:
enc = LabelEncoder()
idx = self.target.notnull()
self.target[idx] = enc.fit_transform(self.target[idx])
if domain_target is not None:
self.domain_target = self.labels[domain_target].copy()
if self.use_sources:
# зануляем таргеты для объектов из неинтересных нам источников
null_idx = ~self.labels["SOURCE"].isin(self.use_sources)
self.domain_target[null_idx] = np.nan
self.domain_enc = LabelEncoder()
idx = self.domain_target.notnull()
self.domain_target[idx] = self.domain_enc.fit_transform(self.domain_target[idx])
return self.target, self.domain_target
def reshape_image(self, mri_img, coord_min, img_shape):
if self.mri_type == "sMRI":
return mri_img[coord_min[0]:coord_min[0] + img_shape[0],
coord_min[1]:coord_min[1] + img_shape[1],
coord_min[2]:coord_min[2] + img_shape[2]].reshape((1,) + img_shape)
if self.mri_type == "fMRI":
seq_len = mri_img.shape[-1]
return mri_img[coord_min[0]:coord_min[0] + img_shape[0],
coord_min[1]:coord_min[1] + img_shape[1],
coord_min[2]:coord_min[2] + img_shape[2], :].reshape((1,) + img_shape + (seq_len,))
def get_image(self, index, start_pos=None, seq_len=None):
def load_mri(mri_file):
if "nii" in mri_file:
img = load_nii_to_array(mri_file)
else:
img = np.load(mri_file)
return img
mri_file = self.mri_files[index]
s = self.labels["SOURCE"][index]
if self.use_sources and s not in self.use_sources:
return None
if s in self.sources_boundaries:
coord_min, img_shape = self.sources_boundaries[s]
else:
coord_min, img_shape = self.sources_boundaries["all"]
if s in self.sources_scales:
scale = self.sources_scales[s]
else:
scale = self.sources_scales["all"]
img = load_mri(mri_file)
# check for padding
cur_shape = np.array(img[..., 0].shape) if self.mri_type == "fMRI" else np.array(img.shape)
req_shape = np.array(coord_min) + np.array(img_shape)
padding = np.maximum(req_shape - cur_shape, 0)
img = Pad(tuple(padding), img_type=self.mri_type)(img[np.newaxis, :])[0]
# reshape
img = self.reshape_image(img, coord_min, img_shape)
if self.clip:
img = np.clip(img, -scale, scale)
img /= scale
if self.mri_type == "sMRI":
return img
if self.mri_type == "fMRI":
if seq_len is None:
seq_len = img.shape[-1]
# what if seq_len == 0 ?
if start_pos is None:
start_pos = np.random.choice(img.shape[-1] - seq_len)
if seq_len == 1:
img = img[:, :, :, :, start_pos]
else:
img = img[:, :, :, :, start_pos:start_pos + seq_len]
img = img.transpose((4, 0, 1, 2, 3))
return img
def __getitem__(self, index):
img = self.get_image(index, self.start_pos, self.seq_len) if self.load_online else self.mri_files[index]
if self.transform is not None:
img = self.transform(img)
if self.target is None:
item = img
else:
item = [img, self.target[index]]
if self.domain_target is not None:
item += [self.domain_target[index]]
return item
def __len__(self):
return len(self.mri_files)
# таргеты возвращаются в том же порядке, с _теми же_ индексами
# но, видимо, в процессе обучения мы будем брать только те индексы, которые соответствуют нотналл позициям здесь
# те для списка с данными (упорядоченного в том же порядке, что и общий список индексов)
# ничего не меняется в зависимости от задачи,
# варьируется только то, какое _подмножество индексов_ мы используем для получения данных для обучения