Last active
August 18, 2021 23:19
-
-
Save tsalo/83828e0c1e9009f3cbd82caed888afba to your computer and use it in GitHub Desktop.
A short script to collect echo-wise processed files from an fMRIPrep working directory.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Collect native-space preprocessed data from fMRIPrep working directory and | |
copy into fMRIPrep derivatives directory, in BIDS format. | |
This script has only been tested on fMRIPrep v20.2.1. | |
It may not work with other versions. | |
Written by Julio Peraza (@JulioAPeraza) | |
""" | |
import argparse | |
import os.path as op | |
from glob import glob | |
from shutil import copyfile | |
def collect_fmriprep(deriv_dir, work_dir, subs): | |
""" | |
Collect native-space preprocessed data from fMRIPrep working directory and | |
copy into fMRIPrep derivatives directory, in BIDS format. | |
Warning | |
-------- | |
This script has only been tested on fMRIPrep v20.2.1. It may not work with other versions. | |
Parameters | |
---------- | |
deriv_dir : str | |
Path to fMRIPrep output directory. | |
work_dir : str | |
Path to fMRIPrep working directory. | |
subs : list | |
A list of subject IDs. Cannot include the `sub-` prefix. | |
""" | |
for sub in subs: | |
print(f"Wrangling subject {sub}", flush=True) | |
sub_in_dir = op.join(work_dir, f"single_subject_{sub}_wf") | |
task_dirs = glob(op.join(sub_in_dir, "func_preproc*_task_*_wf")) | |
for task_dir in task_dirs: | |
bb_wf_dir = op.join(task_dir, "bold_bold_trans_wf") | |
bf_dirs = sorted(glob(op.join(bb_wf_dir, "_bold_file_*"))) | |
for bf_dir in bf_dirs: | |
# Collect partially preprocessed data | |
bf_dir_list = bf_dir.split("..") | |
idx = bf_dir_list.index("sub-{0}".format(sub)) | |
sub_deriv_dir = op.join( | |
deriv_dir, | |
op.dirname("/".join(bf_dir_list[idx:])), | |
) | |
bf_filename = bf_dir_list[-1] | |
in_file = op.join( | |
bf_dir, | |
"merge", | |
"vol0000_xform-00000_merged.nii.gz", | |
) | |
# Conform output name | |
orig_fn_list = bf_filename.split("_") | |
fn_list = orig_fn_list[:] | |
fn_list.insert(-1, "space-scanner") | |
fn_list.insert(-1, "desc-partialPreproc") | |
out_file = op.join(sub_deriv_dir, "_".join(fn_list)) | |
print(f"Writing {out_file}") | |
copyfile(in_file, out_file) | |
def _get_parser(): | |
parser = argparse.ArgumentParser( | |
description="Collecting partially preprocesed data from fmriprep", | |
) | |
parser.add_argument( | |
"-i", | |
"--deriv_dir", | |
dest="deriv_dir", | |
required=True, | |
help="Path to the fMRIPrep output directory, to which the files will be written.", | |
) | |
parser.add_argument( | |
"-o", | |
"--work_dir", | |
dest="work_dir", | |
required=True, | |
help="Path to the fMRIPrep working directory, from which the files will be extracted.", | |
) | |
parser.add_argument( | |
"-s", | |
"--subs", | |
dest="subs", | |
required=True, | |
nargs="+", | |
help="List with subject IDs. These cannot include the `sub-` prefix.", | |
) | |
return parser | |
def _main(argv=None): | |
option = _get_parser().parse_args(argv) | |
kwargs = vars(option) | |
collect_fmriprep(**kwargs) | |
if __name__ == "__main__": | |
_main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment