-
Notifications
You must be signed in to change notification settings - Fork 21
/
openneuro.py
329 lines (267 loc) · 13.4 KB
/
openneuro.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
import os
import re
import shutil
import json
from glob import glob
from bdpy import makedir_ifnot
def makedata(src, source_type='bids_daily', output_dir='./output', root_dir='./', bids_dir='bids', fmap=False, dry_run=False):
'''Create BIDS dataset for OpenNeuro.'''
if not source_type == 'bids_daily':
raise NotImplementedError('Source type %s not supported.' % source_type)
__create_dir(output_dir)
__create_dir(os.path.join(output_dir, 'sourcedata'))
__create_dir(os.path.join(output_dir, 'derivatives'))
readme = os.path.join(output_dir, 'README')
description = os.path.join(output_dir, 'dataset_description.json')
if not os.path.exists(readme):
print('Creating %s' % readme)
with open(readme, 'w'):
pass
if not os.path.exists(description):
print('Creating %s' % description)
desc_init = {
'Name': '',
'License': '',
'Authors': [],
'Acknowledgements': '',
'HowToAcknowledge': '',
'Funding': [],
'ReferencesAndLinks': [],
'DatasetDOI': '',
'BIDSVersion': '1.0.2',
}
with open(description, 'w') as f:
json.dump(desc_init, f, indent=4)
for subject, sub_data in src.items():
print('----------------------------------------')
print(subject)
subject_dir = os.path.join(output_dir, subject)
__create_dir(subject_dir)
# Reference anatomy
src_anat = os.path.join(root_dir, sub_data['anat'])
trg_anat_dir = os.path.join(subject_dir, 'ses-anatomy', 'anat')
trg_anat_raw = os.path.join(trg_anat_dir, '%s_ses-anatomy_T1w_raw.nii.gz' % subject)
trg_anat_defaced = os.path.join(trg_anat_dir, '%s_ses-anatomy_T1w.nii.gz' % subject)
__create_dir(trg_anat_dir)
if not dry_run and not os.path.exists(trg_anat_defaced):
ext = os.path.splitext(src_anat)[1]
if ext == '.gz':
shutil.copy2(src_anat, trg_anat_raw) # FIXME: convert to nii.gz
else:
convert_command = 'mri_convert %s %s' % (src_anat, trg_anat_raw)
os.system(convert_command)
deface_command = 'pydeface %s --outfile %s' % (trg_anat_raw, trg_anat_defaced)
os.system(deface_command)
os.remove(trg_anat_raw)
# Functionals
tasks = sub_data['func']
if tasks is None:
continue
if isinstance(tasks, list):
tasks = {'': tasks}
print('%d task(s)' % len(tasks))
print('')
for task, srcdata in tasks.items():
print('--------------------')
print(task)
print('%d data' % len(srcdata))
print('')
task_sessions = []
for src in srcdata:
if isinstance(src, dict):
src_name = list(src.keys())[0]
src_info = src[src_name]
else:
src_name = src
src_info = None
src_bids_path = os.path.join(root_dir, src_name, bids_dir)
print(src_name)
if not os.path.isdir(src_bids_path):
raise RuntimeError('Invalid BIDS directory: %s' % src_bids_path)
sessions = __parse_bids_dir(src_bids_path, data_info=src_info)
task_sessions.extend(sessions)
print('Total sessions: %d' % len(task_sessions))
print('Total runs: %d' % sum([len(ses['functionals']) for ses in task_sessions]))
print('')
# Copy files
for i, ses in enumerate(task_sessions):
session_label = 'ses-%s%02d' % (task, i +1)
print('Session: %s' % session_label)
session_dir = os.path.join(subject_dir, session_label)
if not dry_run:
__create_dir(session_dir)
__create_dir(os.path.join(session_dir, 'anat'))
__create_dir(os.path.join(session_dir, 'func'))
# T2 inplane image
src_inplane = ses['inplane']
if not src_inplane is None:
rename_table = {
os.path.basename(src_inplane).split('_')[0]: subject, # SUbject ID
os.path.basename(src_inplane).split('_')[1]: session_label, # Session label
}
trg_inplane = os.path.join(session_dir, 'anat',
__rename_file(os.path.basename(src_inplane).split('.')[0] + '.nii.gz',
rename=rename_table))
print('Copying\n from: %s\n to: %s' % (src_inplane, trg_inplane))
if not dry_run and not os.path.exists(trg_inplane):
ext = os.path.splitext(src_inplane)[1]
if ext == '.gz':
shutil.copy2(src_inplane, trg_inplane)
else:
convert_command = 'mri_convert %s %s' % (src_inplane, trg_inplane)
os.system(convert_command)
# Field map
if fmap:
src_fmap_dir = os.path.join(os.path.dirname(os.path.dirname(ses['inplane'])), 'fmap')
trg_fmap_dir = os.path.join(session_dir, 'fmap')
if not dry_run:
__create_dir(os.path.join(session_dir, 'fmap'))
for f in glob(os.path.join(src_fmap_dir, '*.nii.gz')):
src_f = f
trg_f = os.path.join(
trg_fmap_dir,
__rename_file(os.path.basename(src_f).split('.')[0] + '.nii.gz', rename=rename_table)
)
print('Copying\n from: %s\n to: %s' % (src_f, trg_f))
if not dry_run and not os.path.exists(trg_f):
ext = os.path.splitext(src_f)[1]
if ext == '.gz':
shutil.copy2(src_f, trg_f)
else:
convert_command = 'mri_convert %s %s' % (src_f, trg_f)
os.system(convert_command)
for f in glob(os.path.join(src_fmap_dir, '*.json')):
src_f = f
trg_f = os.path.join(
trg_fmap_dir,
__rename_file(os.path.basename(src_f).split('.')[0] + '.json', rename=rename_table)
)
print('Copying\n from: %s\n to: %s' % (src_f, trg_f))
if not dry_run and not os.path.exists(trg_f):
with open(src_f, 'r') as f:
js = json.load(f)
if 'IntendedFor' in js:
fs = [
__rename_file(f, rename=rename_table)
for f in js['IntendedFor']
]
js['IntendedFor'] = fs
with open(trg_f, 'w') as f:
json.dump(js, f, indent=4)
# Functionals
for j, run in enumerate(ses['functionals']):
src_bold = run['bold']
src_bold_json = run['bold_json']
src_event = run['event']
# File renaming
rename_table = {
os.path.basename(src_bold).split('_')[0]: subject, # SUbject ID
os.path.basename(src_bold).split('_')[1]: session_label, # Session label
}
# Fix run label
run_label = re.match('.*_run-(\d+)_.*', os.path.basename(src_bold)).group(1)
if (j + 1) != int(run_label):
print('Fix run label: run-%s --> run-%02d' % (run_label, j + 1))
rename_table.update({'run-%s' % run_label: 'run-%02d' % (j + 1)})
trg_bold = os.path.join(session_dir, 'func',
__rename_file(os.path.basename(src_bold).split('.')[0] + '.nii.gz',
rename=rename_table))
trg_bold_json = os.path.join(session_dir, 'func',
__rename_file(os.path.basename(src_bold_json), rename=rename_table))
trg_event = os.path.join(session_dir, 'func',
__rename_file(os.path.basename(src_event), rename=rename_table))
print('Copying\n from: %s\n to: %s' % (src_bold, trg_bold))
print('Copying\n from: %s\n to: %s' % (src_bold_json, trg_bold_json))
print('Copying\n from: %s\n to: %s' % (src_event, trg_event))
if not dry_run and not os.path.exists(trg_bold):
ext = os.path.splitext(src_bold)[1]
if ext == '.gz':
shutil.copy2(src_bold, trg_bold)
else:
convert_command = 'mri_convert %s %s' % (src_bold, trg_bold)
os.system(convert_command)
shutil.copy2(src_bold_json, trg_bold_json)
shutil.copy2(src_event, trg_event)
return None
def __parse_bids_dir(dpath, data_info=None):
print('BIDS directory: %s' % dpath)
sub_dirs = glob(os.path.join(dpath, 'sub-*'))
if len(sub_dirs) != 1:
raise RuntimeError('Unsupported BIDS data (invalid number of subjects: %d)' % len(sub_dirs))
sub_dir = os.path.join(dpath, sub_dirs[0])
print('Subject directory: %s' % sub_dir)
ses_dirs = sorted(glob(os.path.join(sub_dirs[0], 'ses-*', 'func')))
print('%d func session(s) found' % len(ses_dirs))
sessions = []
for i, ses_dir in enumerate(ses_dirs):
# Session selection
skip = False
if (not data_info is None) and 'ses' in data_info:
if isinstance(data_info['ses'], int) and (i + 1) != data_info['ses']:
skip = True
elif isinstance(data_info['ses'], list) and (i + 1) not in data_info['ses']:
skip = True
elif isinstance(data_info['ses'], str) and (i + 1) not in [int(x) for x in data_info['ses'].split(',')]:
skip = True
if skip:
print('Skipping session %02d' % (i + 1))
continue
# T2 inplane image
inplane_file = __aggregate_mri_files(os.path.join(ses_dir, '../anat'), mri_filetype='nii') + __aggregate_mri_files(os.path.join(ses_dir, '../anat'), mri_filetype='nii.gz')
if len(inplane_file) != 1:
raise RuntimeError('Invalid inplane anatomy')
inplane_file = inplane_file[0]
# Functionals
run_files = __aggregate_runs(ses_dir, mri_filetype='nii') + __aggregate_runs(ses_dir, mri_filetype='nii.gz')
print('Ses %02d: %d run(s) found' % (i + 1, len(run_files)))
#print(run_files)
# Run selection
run_files_keep = []
if (not data_info is None) and 'discard_run' in data_info:
for j, rf in enumerate(run_files):
skip_run = False
if isinstance(data_info['discard_run'], int) and (j + 1) == data_info['discard_run']:
skip_run = True
elif isinstance(data_info['discard_run'], list) and (j + 1) in data_info['discard_run']:
skip_run = True
elif isinstance(data_info['discard_run'], str) and (j + 1) in [int(x) for x in data_info['discard_run'].split(',')]:
skip_run = True
if skip_run:
print('Skipping run %02d' % (j + 1))
else:
run_files_keep.append(rf)
else:
run_files_keep = run_files
sessions.append({'inplane': inplane_file,
'functionals': run_files_keep})
print('')
return sessions
def __aggregate_runs(dpath, mri_filetype='nii'):
mri_files = __aggregate_mri_files(dpath, mri_filetype=mri_filetype)
run_files = []
for mri_file in mri_files:
basename = os.path.basename(mri_file)
# Json file
json_file = os.path.join(dpath, basename.replace('_bold.' + mri_filetype, '_bold.json'))
if not os.path.isfile(json_file):
json_file = None
# Task event file
task_event_file = os.path.join(dpath, basename.replace('_bold.' + mri_filetype, '_events.tsv'))
if not os.path.isfile(task_event_file):
task_event_file = None
scan_files = sorted(glob(os.path.join(dpath, basename + '*')))
run_files.append({'bold': mri_file,
'bold_json': json_file,
'event': task_event_file})
return run_files
def __aggregate_mri_files(dpath, mri_filetype='nii'):
mri_files = glob(os.path.join(dpath, '*.' + mri_filetype))
return mri_files
def __rename_file(fname, rename={}):
for before, after in rename.items():
fname = fname.replace(before, after)
return fname
def __create_dir(dirpath):
print('Creating %s' % dirpath)
makedir_ifnot(dirpath)
return None