fMRIPrep Workbench - Pipeline orchestration toolbox for fMRI preprocessing and analysis
342
███████╗███╗ ███╗██████╗ ██╗██████╗ ██████╗ ███████╗██████╗
██╔════╝████╗ ████║██╔══██╗██║██╔══██╗██╔══██╗██╔════╝██╔══██╗
█████╗ ██╔████╔██║██████╔╝██║██████╔╝██████╔╝█████╗ ██████╔╝
██╔══╝ ██║╚██╔╝██║██╔══██╗██║██╔═══╝ ██╔══██╗██╔══╝ ██╔═══╝
██║ ██║ ╚═╝ ██║██║ ██║██║██║ ██║ ██║███████╗██║
╚═╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝╚═╝ ╚═╝ ╚═╝╚══════╝╚═╝
██╗ ██╗ ██████╗ ██████╗ ██╗ ██╗██████╗ ███████╗███╗ ██╗ ██████╗██╗ ██╗
██║ ██║██╔═══██╗██╔══██╗██║ ██╔╝██╔══██╗██╔════╝████╗ ██║██╔════╝██║ ██║
██║ █╗ ██║██║ ██║██████╔╝█████╔╝ ██████╔╝█████╗ ██╔██╗ ██║██║ ███████║
██║███╗██║██║ ██║██╔══██╗██╔═██╗ ██╔══██╗██╔══╝ ██║╚██╗██║██║ ██╔══██║
╚███╔███╔╝╚██████╔╝██║ ██║██║ ██╗██████╔╝███████╗██║ ╚████║╚██████╗██║ ██║
╚══╝╚══╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚═════╝ ╚══════╝╚═╝ ╚═══╝ ╚═════╝╚═╝ ╚═╝
This WIP repo transforms the Stanford Memory Lab's (SML) internal fMRI preprocessing scripts into a generalizable toolbox for consistency within and across lab projects.
As such, this repo is intended to be used as a GitHub template for setting up fMRI preprocessing pipelines that handle:
dcm2niix converter (converts raw DICOM to BIDS format via heudiconv)Note
- [x] indicates workflows that have been finished and validated - [ ] indicates workflows that are still under active development
Full documentation is available on ReadTheDocs.
For quick reference, see:
The easiest way to use fMRIPrep Workbench is via Docker:
# Pull the image
docker pull shawnschwartz/fmriprep-workbench:latest
# Clone the repository for the wrapper script
git clone https://github.com/shawntz/fmriprep-workbench.git
cd fmriprep-workbench
# Start the container
./fmriprep-workbench start
# Launch the interactive TUI
./fmriprep-workbench launch
For HPC clusters with Singularity/Apptainer:
# Download pre-built image (replace vX.Y.Z with the desired release tag)
wget https://github.com/shawntz/fmriprep-workbench/releases/download/vX.Y.Z/fmriprep-workbench_vX.Y.Z.sif
# Or convert from Docker Hub
singularity build fmriprep-workbench.sif docker://shawnschwartz/fmriprep-workbench:latest
# Run the TUI
singularity exec --bind $(pwd):/workspace fmriprep-workbench.sif /opt/fmriprep-workbench/launch
See the Docker Usage Guide for detailed instructions.
This will create a new repository with all the files from this template, allowing you to customize it for your specific preprocessing needs while maintaining the core functionality for handling:
The template provides a standardized structure and validated scripts that you can build upon, while keeping your specific study parameters and paths separate in configuration files.
After creating your repository from this template:
config.template.yaml to config.yaml and customize parametersall-subjects.template.txt to all-subjects.txt and add your subject IDsThe preprocessing pipeline requires proper configuration of several parameters to handle your study's specific requirements. This guide explains how to set up the config.yaml file that controls the pipeline's behavior.
Important
## Submitting Jobs to Slurm Workload ManagerThere are two approaches you can take to trigger each preprocessing step following proper configuration in the
config.yamlfile:
Use the provided TUI
launcherexecutable, which provides an interactive popup window with more context and explanations + interactive parameter setting (as needed) for any given step.Manually running each step's sidecar executable, which for each core step directory (e.g.,
01-prepare), there exists an associated sidecar executable (e.g.,01-run.sbatch).Note: The provided
launchermentioned in point 1 above simply calls upon these sidecar executables; the added context and interactivity of this method may be more comfortable for users less familiar with running commands in the terminal.Thus, from the root of your project scripts directory, you can either call:
launcher executable approach./launch
launcher welcome screen:
launcher workflow selector:
launcher example parameter selector for the fmriprep step:
# Step 1: FlyWheel download
./01-run.sbatch <fw_subject_id> <fw_session_id> <new_bids_subject_id>
# Step 2: dcm2niix BIDS conversion
./02-run.sbatch <fw_session_id> <new_bids_subject_id> [--skip-tar]
# Step 3: Prep for fMRIPrep
./03-run.sbatch
# Step 4: QC - verify metadata
./04-run.sbatch
# Step 5: QC - verify volume counts
./05-run.sbatch
# Step 6: fMRIPrep anatomical workflows only
./06-run.sbatch
# Step 7: Download FreeSurfer outputs for manual editing
./toolbox/download_freesurfer.sh --server <server> --user <user> --remote-dir <dir> --subjects <list>
# Step 8: Upload edited FreeSurfer outputs back to server
./toolbox/upload_freesurfer.sh --server <server> --user <user> --remote-dir <dir> --subjects <list>
# Step 9: fMRIPrep full workflows (anatomical + functional)
./07-run.sbatch
# Step 10: FSL GLM - Setup new statistical model
./10-fsl-glm/setup_glm.sh
# Step 11: FSL GLM - Run Level 1 analysis (individual runs)
./08-run.sbatch <model-name> [--no-feat]
# Step 12: FSL GLM - Run Level 2 analysis (subject-level)
./09-run.sbatch <model-name> [--no-feat]
# Step 13: FSL GLM - Run Level 3 analysis (group-level)
./10-run.sbatch <model-name> [--no-feat]
# Step 14: Tarball/Untar utility for sourcedata directories
./toolbox/tarball_sourcedata.sh [--tar-all|--tar-subjects|--untar-all|--untar-subjects] --sourcedata-dir <dir>
cp config.template.yaml config.yaml
BASE_DIR to your study's root directoryRAW_DIR points to your BIDS-formatted dataTRIM_DIR location for trimmed BIDS-compliant outputs that will later be used for fmriprepWORKFLOW_LOG_DIR for fMRIPrep workflow logsTEMPLATEFLOW_HOST_HOME for templateflow local cacheFMRIPREP_HOST_CACHE for fmriprep local cacheFREESURFER_LICENSE to the location of your freesurfer licensetask_id to match your BIDS task namenew_task_id if task renaming is neededrun_numbers to match your scan sequence / number of task runsn_dummy based on your scanning protocolEXPECTED_FMAP_VOLS to match your fieldmap acquisitionEXPECTED_BOLD_VOLS to match your BOLD acquisitionfmap_mapping to reflect your fieldmap/BOLD correspondenceall-subjects.template.txt to all-subjects.txt and list all subject ids (just the numbers, not the "sub-" part)DIR_PERMISSIONS and FILE_PERMISSIONS based on your system requirementsfMRIPrep Pipeline PathsfMRIPrep Command PromptDEBUG mode (for testing)# ============================================================================
# (1) SETUP DIRECTORIES
# ============================================================================
directories:
base_dir: '/my/project/dir'
scripts_dir: '${BASE_DIR}/scripts'
raw_dir: '${BASE_DIR}/bids'
trim_dir: '${BASE_DIR}/bids_trimmed'
workflow_log_dir: '${BASE_DIR}/logs/workflows'
templateflow_host_home: '${HOME}/.cache/templateflow'
fmriprep_host_cache: '${HOME}/.cache/fmriprep'
freesurfer_license: '${HOME}/freesurfer.txt'
# ============================================================================
# (2) USER CONFIGURATION
# ============================================================================
user:
email: '[email protected]'
username: 'johndoe'
fw_group_id: 'pi'
fw_project_id: 'amass'
# ============================================================================
# (3) TASK/SCAN PARAMETERS
# ============================================================================
scan:
task_id: 'SomeTaskName'
new_task_id: 'cleanname'
n_dummy: 5
run_numbers:
- '01'
- '02'
- '03'
- '04'
- '05'
- '06'
- '07'
- '08'
# ============================================================================
# (4) DATA VALIDATION VALUES FOR UNIT TESTS
# ============================================================================
validation:
expected_fmap_vols: 12
expected_bold_vols: 220
expected_bold_vols_after_trimming: 210
# ============================================================================
# (5) FIELDMAP <-> TASK BOLD MAPPING
# ============================================================================
# Each key represents a BOLD run number, and its value is the fieldmap number
# Example: here, each fmap covers two runs
fmap_mapping:
'01': '01' # TASK BOLD RUN 01 USES FMAP 01
'02': '01' # TASK BOLD RUN 02 USES FMAP 01
'03': '02' # TASK BOLD RUN 03 USES FMAP 02
'04': '02' # TASK BOLD RUN 04 USES FMAP 02
'05': '03'
'06': '03'
'07': '04'
'08': '04'
# ============================================================================
# (6) SUBJECT IDS <-> PER PREPROC STEP MAPPING (OPTIONAL)
# ============================================================================
# By default, subjects will be pulled from the master 'all-subjects.txt' file
# However, if you want to specify different subject lists per pipeline step,
# you may do so here by uncommenting and configuring the mapping below:
#
# subjects_mapping:
# '01-fw2server': '01-subjects.txt'
# '02-raw2bids': '02-subjects.txt'
#
# Note: keep in mind that we've built in checks at the beginning of each pipeline
# step that skip a subject if there's already a record of them being preprocessed;
# thus, you shouldn't necessarily need separate 0x-subjects.txt files per step
# unless this extra layer of control is useful for your needs.
Subject list files now support suffix modifiers for granular per-subject control. This allows you to maintain a single subject list while specifying different behavior for each subject.
Syntax: subject_id:modifier1:modifier2:...
Supported Modifiers:
step1, step2, step3, step4, step5, step6 - Only run specified step(s) for this subjectforce - Force rerun even if subject was already processedskip - Skip this subject entirelyExamples:
101 # Standard subject ID, runs all steps normally
102:step4 # Only run step 4 (prep-fmriprep) for this subject
103:step4:step5 # Only run steps 4 and 5 for this subject
104:force # Force rerun all steps for this subject
105:step5:force # Only run step 5, force rerun
106:skip # Skip this subject entirely
Example Subject List File (e.g., 04-subjects.txt):
101
102:step4
103:step4:force
104
105:skip
This feature allows the template to maintain a single subject list file while providing extensible, fine-grained control over how the pipeline handles different subjects.
# ============================================================================
# (7) DEFAULT PERMISSIONS
# ============================================================================
permissions:
dir_permissions: '775'
file_permissions: '775'
# ============================================================================
# (8) SLURM JOB HEADER CONFIGURATOR (FOR GENERAL TASKS)
# ============================================================================
slurm:
email: '${USER_EMAIL}'
time: '2:00:00'
dcmniix_time: '6:00:00'
mem: '8G'
cpus: '8'
array_throttle: '10'
log_dir: '${BASE_DIR}/logs/slurm'
partition: 'hns,normal'
# ============================================================================
# (9) PIPELINE SETTINGS
# ============================================================================
pipeline:
fmriprep_version: '24.0.1'
derivs_dir: '${TRIM_DIR}/derivatives/fmriprep-${FMRIPREP_VERSION}'
singularity_image_dir: '${BASE_DIR}/singularity_images'
singularity_image: 'fmriprep-${FMRIPREP_VERSION}.simg'
heudiconv_image: 'heudiconv_latest.sif'
# ============================================================================
# (10) FMRIPREP SPECIFIC SLURM SETTINGS
# ============================================================================
fmriprep_slurm:
job_name: 'fmriprep${FMRIPREP_VERSION//.}_${new_task_id}'
array_size: '1'
time: '48:00:00'
cpus_per_task: '16'
mem_per_cpu: '4G'
# ============================================================================
# (11) FMRIPREP SETTINGS
# ============================================================================
fmriprep:
omp_threads: 8
nthreads: 12
mem_mb: 30000
fd_spike_threshold: 0.9
dvars_spike_threshold: 3.0
output_spaces: 'MNI152NLin2009cAsym:res-2 anat fsnative fsaverage5'
# ============================================================================
# (12) MISC SETTINGS
# ============================================================================
misc:
debug: 0
Tip
## Before running the pipeline: 1. Verify all paths exist and are accessible 2. Confirm volume counts match your acquisition protocol 3. Test the configuration on a single subject 4. Review logs for any configuration warnings
Caution
## Common Issues - Incorrect path specifications - Mismatched volume counts - Incorrect fieldmap mappings - Permission issues
The toolbox/ directory contains helpful utilities for managing your fMRI data:
The tarball_sourcedata.sh script helps optimize inode usage on supercompute environments by archiving subject sourcedata directories into tar files.
Features:
Usage Examples:
# Tarball all subjects in sourcedata directory
./toolbox/tarball_sourcedata.sh --tar-all --sourcedata-dir /path/to/sourcedata
# Tarball specific subjects (removes original directories by default)
./toolbox/tarball_sourcedata.sh --tar-subjects "001,002,003" --sourcedata-dir /path/to/sourcedata
# Tarball subjects from a file
./toolbox/tarball_sourcedata.sh --tar-subjects all-subjects.txt --sourcedata-dir /path/to/sourcedata
# Tarball but keep original directories
./toolbox/tarball_sourcedata.sh --tar-all --sourcedata-dir /path/to/sourcedata --keep-original
# Store tar files in a separate directory
./toolbox/tarball_sourcedata.sh --tar-all --sourcedata-dir /path/to/sourcedata --output-dir /path/to/tarballs
# Extract all tar files
./toolbox/tarball_sourcedata.sh --untar-all --sourcedata-dir /path/to/sourcedata
# Extract specific subjects
./toolbox/tarball_sourcedata.sh --untar-subjects "001,002" --sourcedata-dir /path/to/sourcedata
# Get help
./toolbox/tarball_sourcedata.sh --help
Why use this utility?
The download_freesurfer.sh and upload_freesurfer.sh scripts enable a complete workflow for manually editing FreeSurfer surface reconstructions.
Features:
Usage Examples:
# Download FreeSurfer outputs interactively
./toolbox/download_freesurfer.sh
# Download specific subjects non-interactively
./toolbox/download_freesurfer.sh \
--server login.sherlock.stanford.edu \
--user mysunetid \
--remote-dir /oak/stanford/groups/mylab/projects/mystudy \
--subjects sub-001,sub-002
# Upload edited outputs with automatic backup
./toolbox/upload_freesurfer.sh
# Upload specific subjects non-interactively
./toolbox/upload_freesurfer.sh \
--server login.sherlock.stanford.edu \
--user mysunetid \
--remote-dir /oak/stanford/groups/mylab/projects/mystudy \
--subjects sub-001,sub-002
Complete Workflow:
./06-run.sbatch./toolbox/download_freesurfer.sh./toolbox/upload_freesurfer.sh./07-run.sbatchSee toolbox/FREESURFER_EDITING.md for complete documentation including:
verify_nii_metadata.py - Quality control for converted NIfTI metadatadir_checksum_compare.py - Compare directories using checksumspull_fmriprep_reports.sh - Download fMRIPrep HTML reports from serversummarize_bold_scan_volume_counts.sh - Validate scan volumes match expected countsNote
### Comments, suggestions, questions, issues?Please use the issues tab (https://github.com/shawntz/fmriprep-workbench/issues) to make note of any bugs, comments, suggestions, feedback, etc… all are welcomed and appreciated, thanks!
-Shawn
See our Contributing Guidelines for how to get involved.
Content type
Image
Digest
sha256:dc6662ec4…
Size
247.8 MB
Last updated
9 months ago
docker pull shawnschwartz/fmriprep-workbench