Replacing fMRIPrep's skull-stripping with synthstrip

Dear experts,

I would like to replace fMRIPrep’s default skull-stripping with FreeSurfer’s synthstrip. I can think of two ways to do this:

  1. Run recon-all myself, replace the skull-stripping step with synthstrip, and then feed the results into fMRIPrep (e.g., via --fs-subjects-dir).
  2. Use an already skull-stripped T1w image as input to fMRIPrep and skip the skull-stripping step.

I personally prefer the first approach because it does not alter the raw data. However, I don’t have a deep understanding of fMRIPrep’s internals, so I’d like to know which approach is better from an expert perspective, and how each would affect downstream analyses. Specifically, I know that fMRIPrep uses recon-all results to refine brain extraction. If I go with Approach 1, will the brain mask still be refined by fMRIPrep?

Thank you!

Yang Hu

TBH, i’m not familiar with the internals of fMRIprep. i’ve done both 1 and 2 before and i think they give you very similar results.

if you do #1 you might as well do recon-all with freesurfer 8+. fMRIprep does, in my experience, work with freesurfer 8+ precomputed outputs, but fMRIprep on freesurfer 8+ precomputed outputs may add 1 or 2 hours of additional runtime, because fMRIprep will need to generate some freesurfer 7+ outputs that are missing in freesurfer 8

Also i believe currently ASLprep is already using synthstrip for its smriprep workflow. Not sure if its possible to do this without any asl data, but you can try to run ASLprep with --anat-only flag, and then run fMRIprep with –derivatives smriprep=/path/to/aslprep/output_dir . This seems to be the ‘cleanest’ option

1 Like

To use an already skull stripped image in fmriprep you can also synthstrip the raw image and use -—skull-strip-t1w skip.

1 Like

There’s also an option to create a mask from synthstrip and plug it into fmriprep’s system for precomputed derivatives. For example, if you have sourcedata/synthstripthat contains a BIDS-formatted copy of your masks and point to it with the –derivatives flag, fmriprep should use those masks instead of recreating another. If all goes well, you’ll see a line in the logs early on like.

	 ANAT Found brain mask - skipping Stage 6

As an aside, you may want to use the --no-csf option due to the normalization issues reported here: Spatial normalization targeting dura(?) instead of brain in presence of atrophy · Issue #954 · PennLINC/qsiprep · GitHub (fmriprep’s spatial normalization routines expect a mask that hugs the brain quite tightly)

1 Like

Apologies for the long delay. I tried your suggested approach and found that fMRIPrep performs skull-stripping on the conformed T1w image (i.e., after RAS reorientation and resampling), whereas I can only run synthstrip on the raw T1w data. This leads to a spatial mismatch between the custom mask and the image that fMRIPrep actually uses for apply_mask . One potential solution is to resample my custom mask to the conformed space, but I’m not sure how to properly do this. I noticed similar issues have been mentioned in previous posts, so I was wondering if you could point me to the right direction.

Could you share the exact commands that you used to generate the mask and run fMRIPrep? Could you also show an image demonstrating the mismatch you’re talking about?

Thank you for your reply.

The commands to create the custom brain mask:

sour_dir=/home/user/data/BIDSDATA
targ_dir=/home/user/data/DRIVDATA/SynthStrip
curr_sub=sub-S01
INPUT=${sour_dir}/${curr_sub}/anat/${curr_sub}_T1w.nii.gz
OUTDIR=${targ_dir}/${curr_sub}/anat
mri_synthstrip -i ${INPUT} -m ${OUTDIR}/${curr_sub}_desc-brain_mask.nii.gz --no-csf

The commands to run fMRIPrep (25.2.5):

sour_dir=/home/user/data/BIDSDATA
targ_dir=/home/user/data/DRIVDATA
work_dir=/home/user/data/TMPODATA
FSLICENSE=/usr/local/freesurfer/7.4.1/license.txt
TFDIR=/home/user/software/TemplateFlow
docker run -ti --rm \
        -v ${sour_dir}:/data:ro \
        -v ${targ_dir}:/out \
        -v ${work_dir}:/work \
        -u $(id -u):$(id -g) \
        -v ${TFDIR}:/opt/templateflow \
        -v ${FSLICENSE}:/opt/freesurfer/license.txt \
        -e TEMPLATEFLOW_HOME=/opt/templateflow \
        nipreps/fmriprep:25.2.5 \
        /data /out/FMRIPREP participant \
        -w /work --fs-license-file /opt/freesurfer/license.txt --nthreads 10 \
        --dummy-scans 5 \
        --notrack --stop-on-first-crash \
        --derivatives anat=/out/SynthStrip

From the fMRIPrep log, the custom mask was successfully identified:

260810-14:15:21,65 nipype.workflow INFO:
	 ANAT Found brain mask - skipping Stage 6

However, when applying the mask, the following error occurred:

Traceback (most recent call last):
  File "/app/.pixi/envs/fmriprep/lib/python3.12/site-packages/nipype/pipeline/plugins/multiproc.py", line 67, in run_node
    result["result"] = node.run(updatehash=updatehash)
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/.pixi/envs/fmriprep/lib/python3.12/site-packages/nipype/pipeline/engine/nodes.py", line 525, in run
    result = self._run_interface(execute=True)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/.pixi/envs/fmriprep/lib/python3.12/site-packages/nipype/pipeline/engine/nodes.py", line 643, in _run_interface
    return self._run_command(execute)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/.pixi/envs/fmriprep/lib/python3.12/site-packages/nipype/pipeline/engine/nodes.py", line 769, in _run_command
    raise NodeExecutionError(msg)
nipype.pipeline.engine.nodes.NodeExecutionError: Exception raised while executing Node apply_mask.

Traceback:
	Traceback (most recent call last):
	  File "/app/.pixi/envs/fmriprep/lib/python3.12/site-packages/nipype/interfaces/base/core.py", line 401, in run
	    runtime = self._run_interface(runtime)
	              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
	  File "/app/.pixi/envs/fmriprep/lib/python3.12/site-packages/niworkflows/interfaces/nibabel.py", line 78, in _run_interface
	    raise ValueError('Image and mask affines are not similar enough.')
	ValueError: Image and mask affines are not similar enough.

Regarding your request for a visual demonstration, unfortunately, I’m not familiar with fMRIPrep’s internal workflow, so I’m not sure how to generate one. As for the potential spatial mismatch, I’ve seen this issue mentioned in previous posts, which is where I learned about it.