-
Notifications
You must be signed in to change notification settings - Fork 2
/
data.py
289 lines (223 loc) · 10.1 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
import subprocess
from pathlib import Path
import pandas as pd
class Data:
datasets = {
'brain': {
'version_list': ['r20210726'],
'filename': 'neuromod-anat-brain-qmri.zip',
'data_files': "results-neuromod-anat-brain-qmri.csv"
},
'spine': {
'version_list': ['r20210610'],
'filename': 'spinalcord_results.zip',
'data_files': {
'T1w': 'csa-SC_T1w.csv',
'T2w': 'csa-SC_T2w.csv',
'GMT2w': 'csa-GM_T2s.csv'
},
},
'qmri':{
'version_list': ['r20210610'],
'filename': 'spinalcord_results.zip',
'data_files': {
'DWI_FA': 'DWI_FA.csv',
'DWI_MD': 'DWI_MD.csv',
'DWI_RD': 'DWI_RD.csv',
'MTR': 'MTR.csv',
'MTSat': 'MTsat.csv',
'T1': 'T1.csv'
},
},
'url': 'https://github.com/courtois-neuromod/anat-processing/releases/download/'
}
def __init__(self, data_type):
self.data_type = data_type
self.available_versions = self.get_available_versions()
self.version = None
self.release_file = None
self.data_dir = None
self.data = None
def get_available_versions(self):
version_list = Data.datasets[self.data_type]['version_list']
version_list.sort()
return version_list
def download(self, release_version):
# Set release version
if release_version == 'latest':
self.version = self.available_versions[-1]
elif release_version in self.available_versions:
self.version = release_version
else:
Exception('Release version not listed in available versions. Please update requested version and restart.')
# Get release file name
release_file = Data.datasets[self.data_type]['filename']
# Get release global url
global_url = Data.datasets['url']
# Set release-specific url
url = global_url + "/" + self.version + "/" + release_file
# Set output directory
self.data_dir = Path('data') / Path(self.data_type)
# Download
if self.data_dir.exists() is False:
# Create directory
subprocess.run(["mkdir", "-p", self.data_dir])
# Get data from GitHub release and extract
subprocess.run(["wget", "-O", release_file, url])
subprocess.run(["unzip", "-j", release_file, "-d", self.data_dir])
def load(self):
data_type = self.data_type
if data_type == 'brain':
data_file = Data.datasets[data_type]['data_files']
file_path = self.data_dir / data_file
# Read data
self.data = pd.read_csv(file_path, converters={'project_id': lambda x: str(x)})
# Sort data acording to subject and session columns
self.data.sort_values(['subject', 'session'], ascending=[True, True])
elif data_type == 'spine':
# Prep data property
self.data = {
'T1w': None,
'T2w': None,
'GMT2w': None
}
for acq in self.data:
data_file = Data.datasets[data_type]['data_files'][acq]
file_path = self.data_dir / data_file
# Load the data
self.data[acq] = pd.read_csv(file_path, converters={'project_id': lambda x: str(x)})
# Insert new columns for Subject and Session and start inserting values
self.data[acq].insert(0, "Subject", "Any")
self.data[acq].insert(1, "Session", "Any")
# Get Subject and Session from csv
for index, row in self.data[acq].iterrows():
subject = int(row['Filename'].split("/")[6].split('-')[1])
session = int(row['Filename'].split("/")[7].split("-")[1])
self.data[acq].at[index, 'Subject'] = subject
self.data[acq].at[index, 'Session'] = session
# Sort values based on Subject -- Session
self.data[acq]=self.data[acq].sort_values(['Subject', 'Session'], ascending=[True, True])
elif data_type == 'qmri':
self.data = {
'DWI_FA': None,
'DWI_MD': None,
'DWI_RD': None,
'MTR': None,
'MTSat': None,
'T1': None
}
for acq in self.data:
data_file = Data.datasets[data_type]['data_files'][acq]
file_path = self.data_dir / data_file
# Load the data
self.data[acq] = pd.read_csv(file_path, converters={'project_id': lambda x: str(x)})
# Insert new columns for Subject and Session and start inserting values
self.data[acq].insert(0, "Subject", "Any")
self.data[acq].insert(1, "Session", "Any")
# Get Subject and Session from csv
for index, row in self.data[acq].iterrows():
subject = int(row['Filename'].split("/")[6].split('-')[1])
session = int(row['Filename'].split("/")[7].split("-")[1])
self.data[acq].at[index, 'Subject'] = subject
self.data[acq].at[index, 'Session'] = session
# Sort values based on Subject -- Session
self.data[acq]=self.data[acq].sort_values(['Subject', 'Session'], ascending=[True, True])
def extract_data(self, tissue):
if self.data_type == 'brain':
num_sub = 6
num_session = 4
matrix = {
'MP2RAGE': [],
'MTS': [],
'MTR': [],
'MTsat': []
}
elif self.data_type == 'spine':
num_sub = 5
num_session = 3
matrix = {
'T1w': {
'Area': [],
'AP': [],
'RL': [],
'Angle': []
},
'T2w': {
'Area': [],
'AP': [],
'RL': [],
'Angle': []
},
'GMT2w': {
'Area': []
}
}
elif self.data_type == 'qmri':
num_sub = 5
num_session = 3
matrix = {
'DWI_FA': [],
'DWI_MD': [],
'DWI_RD': [],
'MTR': [],
'MTSat': [],
'T1': []
}
default_val = -100
if self.data_type == 'brain':
for metric in matrix:
for i in range(1, num_sub+1, 1):
sub_values = self.data.loc[self.data['subject'] == i]
metric_ses = []
for j in range(1, num_session+1, 1):
ses_values = sub_values.loc[sub_values['session'] == j]
mean_val = default_val
for index, row in ses_values.iterrows():
if metric == 'MP2RAGE' or metric == 'MTS':
if row['acquisition'] == metric and row['metric'] == 'T1map' and row['label'] == tissue:
mean_val = row['mean']
elif metric == 'MTR':
if row['metric'] == 'MTRmap' and row['label'] == tissue:
mean_val = row['mean']
elif metric == 'MTsat':
if row['metric'] == 'MTsat' and row['label'] == tissue:
mean_val = row['mean']
# Append values to lists for sessions
metric_ses.append(mean_val)
matrix[metric].append(metric_ses)
elif self.data_type == 'spine':
for type in matrix:
db = self.data[type]
for metric in matrix[type]:
for i in range(0, num_sub+1, 1):
sub_values = db.loc[db['Subject'] == i+1]
metric_ses = []
for j in range(0, num_session+1, 1):
ses_values = sub_values.loc[sub_values['Session'] == j+1]
mean_val = default_val
for index, row in ses_values.iterrows():
if metric == 'Area':
mean_val = row['MEAN(area)']
if metric == 'AP':
mean_val = row['MEAN(diameter_AP)']
if metric == 'RL':
mean_val = row['MEAN(diameter_RL)']
if metric == 'Angle':
mean_val = row['MEAN(angle_AP)']
# Append values to lists for sessions
metric_ses.append(mean_val)
matrix[type][metric].append(metric_ses)
elif self.data_type == 'qmri':
for metric in matrix:
for i in range(0, num_sub+1, 1):
sub_values = self.data[metric].loc[self.data[metric]['Subject'] == i+1]
metric_ses = []
for j in range(0, num_session+1, 1):
ses_values = sub_values.loc[sub_values['Session'] == j+1]
mean_val = default_val
for index, row in ses_values.iterrows():
val = row['WA()']
# Append values to lists for sessions
metric_ses.append(val)
matrix[metric].append(metric_ses)
return matrix