Skip to content

Instantly share code, notes, and snippets.

@ilkayisik
Created April 16, 2019 09:36
Show Gist options
  • Save ilkayisik/8e0b41ff0a009bef3d938af5eebddd31 to your computer and use it in GitHub Desktop.
Save ilkayisik/8e0b41ff0a009bef3d938af5eebddd31 to your computer and use it in GitHub Desktop.
extract the first volume of nifti file for every participant
from glob import glob
import nibabel as nib
import numpy as np
root='path'
subject_ids = [s[-6:] for s in sorted(glob(root + 'sub-*'))] # to get subject ids in format sub-01
# load the functional files for every participant
funcs = sorted(glob(root+'fmriprep/sub-*/func/sub-*_task-x_run-01_bold_space-MNI152NLin2009cAsym_preproc.nii.gz'))
for c, func in enumerate(funcs):
out_name = root + subject_ids[c] + '/example_func.nii.gz'
img = nib.load(func)
img_data = img.get_data()
example_img = img_data[:, :, :, 0]
out_data = nib.Nifti1Image(example_img, header=img.header, affine=img.affine)
nib.save(out_data, out_name)
print('data for ', subject_ids[c], ' is saved')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment