- Python 100%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
Felix H. noticed that for a target file with resolution in name, the code produces a file with resolution appearing twice. That is, given these files (sink copies entities from brain mask): ``` sub-0011_task-rest_space-MNI152NLin2009cAsym_res-2_desc-brain_mask.nii.gz sub-0011_task-rest_space-MNI152NLin2009cAsym_res-2_desc-preproc_bold.nii.gz ``` our data sink would produce: ``` sub-0011_task-rest_space-MNI152NLin2009cAsym_res-2_desc-preproc_res-2_bold.nii.gz ``` I do not understand why that happens. This can be fixed by telling the sink to dismiss the "res" entity (as done here) or by specifying the entity explicitly, e.g. `sinker.inputs.resolution = "2"`. We should probably be reading the resolution from the to-be-produced file and doing the latter, but the current solution does not break known workflows, so it should be good for the time being. |
||
| LICENSE | ||
| README.md | ||
| resample.py | ||
Reproduce preprocessed BOLD from ancillary fMRIPrep derivatives
This is an example implementation of a Nipype pipeline using fMRIPrep workflows to reproduce fMRIPrep's preprocessed BOLD image (projected onto a target space) from the raw image, ancillary fMRIPrep derivatives, and related metadata. It combines fMRIPrep workflows for slice timing correction (STC) and single-shot volumetric resampling (which includes motion and field map corrections, and applies transforms already computed during the full fMRIPrep run).
It is not as flexible as fMRIPrep itself, but has a degree of flexibility built in, and should serve as a good base for small per-dataset modifications.
Depends on fMRIPrep (the Python package, and its dependencies) and AFNI (3dTshift, only if STC is applied). Verified to work with fMRIPrep 24.1.1 and AFNI 24.2.06 / 24.3.01.
It was created as an example application for DataLad remake.
Background
The end result of (volumetric) preprocessing with fMRIPrep is a BOLD image projected onto a target space.
In addition, fMRIPrep saves all the step-wise transformation (xfm) files, brain masks, single-volume references, and field map coefficients required to reconstruct the image from its raw counterpart in a bit-identical fashion.
Thanks to fMRIPrep modular implementation in Nipype, the relevant workflows can be applied selectively, creating a "shortcut" compared to full fMRIPrep re-computation.
The main motivation is space saving. For a 700 MB raw image, its preprocessed (projected) version can typically be between 700 MB - 1.2 GB, depending on the selected target resolution, and more than one target space can be requested by the users. Meanwhile, the ancillary files listed above would take only 100 MB per target space, the majority of which is taken by the transformation between anatomical image and the reference space (the remaining spatial transforms are rigid-body transforms). Field maps are only a few MB large.
Usage
usage: resample.py [-h] [--afni AFNI] [--dry-run] [--relative] target_file raw_dataset out_dir
positional arguments:
target_file Resampled file (fMRIPrep derivative) to reproduce
raw_dataset Raw BIDS dataset location
out_dir Base output path
options:
-h, --help show this help message and exit
--afni AFNI Executable AFNI container or exec call for 3dTshift.
E.g. 'path/to/afni.simg' if using official AFNI image
or 'singularity exec path/to/fmriprep.simg 3dTshift'
to use 3dTshift bundled with fMRIPrep. Will default
to system's native 3dTshift if not provided; does not
matter if no STC is performed.
--dry-run Report file dependencies and exit.
--relative Report file dependencies relative to PWD
Quirks and features
AFNI
FMRIPrep slice timing workflow uses Nipype interface to AFNI 3dTshift, expecting to find the executable in the environment.
The --afni argument can be used to patch the command.
If the argument starts with "apptainer", "singularity", or "docker", it will be interpreted as an exec call, and used as-is.
Otherwise, it will be treated as an executable container, and the 3dTshift command will be appended.
This way, it is possible to use either the AFNI (smaller) or the fMRIPrep (larger) image to provide the 3dTshift command.
All other fMRIPrep workflow steps seem to be Python-only.
Dry run
The --dry-run will discover the data dependencies by looking at the target file sidecar json (and, possibly, fieldmap metadata files in the derived dataset), and print them out.
For this reason, it needs to be ran with the sidecar file(s) present.
Having a list of dependencies can be useful e.g. when provisioning data for datalad-remake.
By DataLad convention, all paths need to be relative to the root of the DataLad (super)dataset.
This can be done using an additional --relative flag, which causes paths to be reported relative to the current working directory.
It will error out when raw or derived datasets are not subpaths of the working directory (no "walk up").
NIfTI header
The header of the output image produced by FMRIPrep depends on the target template and requested resolution (native vs resliced). The shortcut workflow uses projected BOLD reference as a template, which guarantees the correct spatial transformation (data matrix) but produces a header which is different still. To ensure bit-exact reproducibility, the workflow also edits the header, using hard-coded values which cover only some output space / resolution combinations. Code tweaks or additional inputs may be needed on a per-dataset basis.
Acknowledgments
The source code of fmripost-aroma, which implements a similar pipeline, was very helpful as a usage example.