-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmotion_slice_openfmri.py
More file actions
49 lines (40 loc) · 1.45 KB
/
motion_slice_openfmri.py
File metadata and controls
49 lines (40 loc) · 1.45 KB
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
#!/usr/bin/env python
""" Script to run motion correction / slice timing on Haxby Open FMRI dataset
"""
import os
import sys
import numpy as np
from nipy.algorithms.registration import SpaceTimeRealign
from nipy import load_image, save_image
# Library to fetch filenames from Open FMRI data layout
from openfmri import get_subjects
def time_space_realign(run_fnames, TR, slice_times, slice_axis):
run_imgs = [load_image(run) for run in run_fnames]
# Spatio-temporal realigner
R = SpaceTimeRealign(run_imgs,
tr=TR,
slice_times=slice_times,
slice_info=(slice_axis, 1))
# Estimate motion within- and between-sessions
R.estimate(refscan=None)
# Save back out
for i, fname in enumerate(run_fnames):
corr_run = R.resample(i)
pth, name = os.path.split(fname)
processed_fname = os.path.join(pth, 'ra' + name)
save_image(corr_run, processed_fname)
def main():
try:
DATA_PATH = sys.argv[1]
except IndexError:
raise RuntimeError("Pass data path on command line")
TR = 2.5
subjects = get_subjects(DATA_PATH)
for name, subject in subjects.items():
run_fnames = []
for run in subject['functionals']:
run_fnames.append(run['filename'])
print("Realigning subject " + name)
time_space_realign(run_fnames, TR, 'ascending', slice_axis=0)
if __name__ == '__main__':
main()