-
Notifications
You must be signed in to change notification settings - Fork 10
/
data_declaration.py
162 lines (123 loc) · 4.15 KB
/
data_declaration.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
'''The following module declares the Dataset objects required by torch to iterate over the data.'''
from enum import Enum
import glob
import pathlib
import numpy as np
import nibabel as nib
import pandas as pd
import torch
from torch.utils.data import Dataset
class Task(Enum):
'''
Enum class for the two classification tasks
'''
NC_v_AD = 1
sMCI_v_pMCI = 2
def __str__(self) -> str:
if super().__str__() == "Task.NC_v_AD":
return "NC_v_AD"
else:
return "sMCI_v_pMCI"
def get_im_id(path):
'''Gets the image id from the file path string'''
fname = path.stem
im_id_str = ""
#the I that comes before the id needs to be removed hence [1:]
im_id_str = fname.split("_")[-1][1:]
return int(im_id_str)
def get_ptid(path):
'''Gets the ptid from the file path string'''
fname = path.stem
ptid_str = ""
count = 0
for char in fname:
if count == 4:
break
if 0 < count < 4:
ptid_str += char
if char == '_':
count += 1
return ptid_str[:-1]
def get_acq_year(im_data_id, im_df):
'''Gets the acquisition year from a pandas dataframe by searching the image id'''
acq_date = im_df[im_df['Image Data ID'] == im_data_id]["Acq Date"].iloc[0]
acq_year_str = ""
slash_count = 0
for char in acq_date:
if char == "/":
slash_count += 1
if slash_count == 2:
acq_year_str += char
return acq_year_str[1:]
def get_label(path, labels):
'''Gets label from the path'''
label_str = path.parent.stem
label = None
if label_str == labels[0]:
label = np.array([0], dtype=np.double)
elif label_str == labels[1]:
label = np.array([1], dtype=np.double)
return label
def get_mri(path):
'''Gets a numpy array representing the mri object from a file path'''
mri = nib.load(str(path)).get_fdata()
mri.resize(1, 110, 110, 110)
mri = np.asarray(mri)
return mri
def get_clinical(im_id, clin_df):
'''Gets clinical features vector by searching dataframe for image id'''
clinical = np.zeros(21)
row = clin_df.loc[clin_df["Image Data ID"] == im_id]
for k in range(1, 22):
clinical[k-1] = row.iloc[0][k]
return clinical
class MRIDataset(Dataset):
'''Provides an object for the MRI data that can be iterated.'''
def __init__(self, root_dir, labels, transform=None):
self.root_dir = root_dir
self.transform = transform
self.directories = []
self.len = 0
self.labels = labels
self.clin_data = pd.read_csv("../data/clinical.csv")
train_dirs = []
for label in labels:
train_dirs.append(root_dir + label)
for train_dir in train_dirs:
for path in glob.glob(train_dir + "/*"):
self.directories.append(pathlib.Path(path))
self.len = len(self.directories)
def __len__(self):
return self.len
def __getitem__(self, idx):
if torch.is_tensor(idx):
idx = idx.tolist()
repeat = True
while repeat:
try:
path = self.directories[idx]
im_id = get_im_id(path)
mri = get_mri(path)
clinical = get_clinical(im_id, self.clin_data)
label = get_label(path, self.labels)
sample = {'mri': mri, 'clinical':clinical, 'label':label}
if self.transform:
sample = self.transform(sample)
return sample
except IndexError as index_e:
print(index_e)
if idx < self.len:
idx += 1
else:
idx = 0
return sample
class ToTensor():
'''Convert ndarrays in sample to Tensors.'''
def __call__(self, sample):
image, clinical, label = sample['mri'], sample['clinical'], sample['label']
mri_t = torch.from_numpy(image) / 255.0
clin_t = torch.from_numpy(clinical)
label = torch.from_numpy(label).double()
return {'mri': mri_t,
'clin_t': clin_t,
'label': label}