-
Notifications
You must be signed in to change notification settings - Fork 0
/
bids.py
116 lines (92 loc) · 4.3 KB
/
bids.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
import subprocess
import sys
import json
def BidsData(root, subs):
# create required directories and files
subprocess.run(['mkdir', f'{root}/code'])
subprocess.run(['echo', f'{root}/README', '>>', 'not empty!'])
for sub in subs:
print(f'[Check] processing.. sub{sub}')
# uncompress
orig_sub_file = f'{root}/subject_{sub}.tar.gz'
subprocess.run(['tar', 'xvzf', f'{orig_sub_file}', '--directory', f'{root}/'])
# rename subject dir name
orig_sub_path = f'{root}/scratch/01812/mm63378/evolverep/subject_{sub}'
new_sub_path = f'{root}/sub-{sub}'
subprocess.run(['mv', f'{orig_sub_path}', f'{new_sub_path}'])
# remove the untar `scratch` directory because next sub once untar will
# also produce a directory called `scratch`
subprocess.run(['rm', '-r', f'{root}/scratch'])
# rename functional to func, anatomy to anat, fieldmap to fmap
subprocess.run(['mv', f'{root}/sub-{sub}/functional', f'{root}/sub-{sub}/func'])
subprocess.run(['mv', f'{root}/sub-{sub}/anatomy', f'{root}/sub-{sub}/anat'])
subprocess.run(['mv', f'{root}/sub-{sub}/fieldmap', f'{root}/sub-{sub}/fmap'])
# rename a bunch of directories and files
typesOfData = ['func', 'fmap', 'anat']
for typeOfData in typesOfData:
# move into each sub dir and make changes
sub_dir = f'{root}/sub-{sub}/{typeOfData}'
if typeOfData == 'func':
run = 0
for unique_run in range(1, total_runs+1):
if unique_run <=4:
study = 1
elif unique_run > 4 and unique_run <= 8:
study = 2
else:
study = 3
if run == 4:
run = 1
else:
run += 1
# rename sub-task-run level file
print('--------------------------------------------------------------')
print(f'[Check]')
print(f'{sub_dir}/functional_{unique_run}/bold.nii.gz')
print(f'{sub_dir}/sub-{sub}_task-{study}_run-0{run}_bold.nii.gz')
print('--------------------------------------------------------------')
subprocess.run(
['mv',
f'{sub_dir}/functional_{unique_run}/bold.nii.gz',
f'{sub_dir}/sub-{sub}_task-{study}_run-0{run}_bold.nii.gz'
]
)
# remove the old functional_* (now empty) directories
subprocess.run(
['rm', '-r', f'{sub_dir}/functional_{unique_run}']
)
# add minimum meta data
fpath = f'{sub_dir}/sub-{sub}_task-{study}_run-0{run}_bold'
data_dictionary = {}
data_dictionary['RepetitionTime'] = 2.
data_dictionary['TaskName'] = 'SHJ'
with open(f'{fpath}.json', 'w') as f:
json.dump(data_dictionary, f)
print(f'{fpath}.json dumped.')
# Simply remove fmap data as not used in Mack et al.
# Removing this will not cause error when BIDS
elif typeOfData == 'fmap':
subprocess.run(
['rm', '-r', f'{sub_dir}']
)
elif typeOfData == 'anat':
subprocess.run(
['mv',
f'{sub_dir}/highres.nii.gz',
f'{sub_dir}/sub-{sub}_T1w.nii.gz'
]
)
subprocess.run(['rm', f'{sub_dir}/coronal1.nii.gz'])
subprocess.run(['rm', f'{sub_dir}/coronal2.nii.gz'])
if __name__ == '__main__':
subs = []
for i in range(2, 25):
if len(f'{i}') == 1:
subs.append(f'0{i}')
else:
subs.append(f'{i}')
total_runs = 12 # each problem has 4 runs
num_studies = 3 # problem_type 1, 2, 6
runs_per_study = 12 / 3
root = 'Mack-Data/dropbox'
BidsData(root, subs)