FreeSurfer outside fmriprep: longitudinal?

I need to run freesurfer 7.1.1 on my mri data, therefore I will need to run FreeSurfer outside of fmriprep.

I have longitudinal data that is approximately 1 year apart for each timepoint. In order to make the FreeSurfer results compatible with fmriprep, how should I run the recon-all command? Comments from previous threads seem somewhat inconclusive.

I was thinking of merging the two T1w images together and running freesurfer on the combined T1, though this may not be ideal, since I would want separate structural measures for each timepoint.

Any help would be greatly appreciated!

Hi @ajschadler,

Did you figure this out? I am also having the same case…

Hi @WenjunSu and @ajschadler,

The proper procedure is largely dependent on your analytical goals.

Do you want a single template made across all time points? If so, you can use the longitudinal stream in FreeSurfer FsTutorial/LongitudinalTutorial - Free Surfer Wiki. Put the combined template recon-all output into a folder like bids/derivatives/freesurfer_long/sub-xxx, and then pass bids/derivatives/freesurfer_long in the --fs-subjects-dir fmriprep argument.

If you want to keep the time points separate (which hinders the ability to longitudinal analyses but might be warranted if the T1s have drastically different shapes or are very far apart in time) then you should run recon-all separately for each T1, putting outputs into different folders, e.g., bids/derivatives/freesurfer_T1/sub-xxx, bids/derivatives/freesurfer_T2/sub-xxx etc. Then set the --fs-subjects-dir in fMRIPrep to bids/derivatives/freesurfer_TX and use a --bids-filter-file to only analyze the data from the session you want to analyze. Loop across sessions.

Best,
Steven

Hi Steven,

Thank you very much for replying. Now I ran fmriprep with the --fs-no-reconall flag. I wanna ran freesurfer outside fmriprep and then link the output to fmriprep output. The fmriprep output has an merged T1 file across two sessions fmriprep/anat/*_desc-preproc_T1w.nii.gz. Is it the correct input for freesurfer?

Hi @WenjunSu,

No, raw T1s should be used for FreeSurfer. The fmriprep anatomical outputs are preprocessed. And fs-no-reconall is not recommended. It disables surface based algorithms for spatial normalization which tend to work better than volume-based methods.

Best,
Steven

Hi @Steven ,

I am using your first suggested option. Specifically, I have data from timepoint 1, timepoint 2, …, timepoint 18 for a single subject. I wanted to use an averaged template for all time points, so I used the FreeSurfer longitudinal pipeline to create this averaged template. I then used this template as input for the --fs-subjects-dir argument in fMRIPrep.

Here’s my Docker command:

docker run -ti --rm \

-v project_path/bids:/data:ro \

-v project_path/derivatives/fmriprep:/out \

-v project_path/derivatives/work/fmriprep:/work \

-v /Applications/freesurfer/7.1.1/license.txt:/opt/freesurfer/license.txt \

-v project_path/derivatives/freesurfer:/opt/subjects \

nipreps/fmriprep:24.1.1 /data /out participant --participant-label 10001 \

--n_cpus 8 --skip_bids_validation --cifti-output 91k --stop-on-first-crash \

--fs-license-file /opt/freesurfer/license.txt --fs-subjects-dir /opt/subjects --work-dir /work

However, the fmriprep threw the following error:

241212-00:20:52,675 nipype.interface INFO:
	 resume recon-all : recon-all -autorecon1 -T2 /data/sub-10001/ses-scan01/anat/sub-10001_ses-scan01_T2w.nii.gz -noskullstrip -noT2pial -noFLAIRpial -cw256 -openmp 7 -subjid sub-10001 -sd /opt/subjects -notalairach -nonuintensitycor -nonormalization
2024-12-12 00:20:52,675 [    INFO] resume recon-all : recon-all -autorecon1 -T2 /data/sub-10001/ses-scan01/anat/sub-10001_ses-scan01_T2w.nii.gz -noskullstrip -noT2pial -noFLAIRpial -cw256 -openmp 7 -subjid sub-10001 -sd /opt/subjects -notalairach -nonuintensitycor -nonormalization
241212-00:20:52,676 nipype.interface INFO:
	 resume recon-all : recon-all -autorecon1 -T2 /data/sub-10001/ses-scan01/anat/sub-10001_ses-scan01_T2w.nii.gz -noskullstrip -noT2pial -noFLAIRpial -cw256 -openmp 7 -subjid sub-10001 -sd /opt/subjects -notalairach -nonuintensitycor -nonormalization
2024-12-12 00:20:52,676 [    INFO] resume recon-all : recon-all -autorecon1 -T2 /data/sub-10001/ses-scan01/anat/sub-10001_ses-scan01_T2w.nii.gz -noskullstrip -noT2pial -noFLAIRpial -cw256 -openmp 7 -subjid sub-10001 -sd /opt/subjects -notalairach -nonuintensitycor -nonormalization
2024-12-12 00:20:52,961 [ WARNING] WARNING: 'locale.getdefaultlocale' is deprecated and slated for removal in Python 3.15. Use setlocale(), getencoding() and getlocale() instead.
2024-12-12 00:20:52,961 [ WARNING] WARNING: 'locale.getdefaultlocale' is deprecated and slated for removal in Python 3.15. Use setlocale(), getencoding() and getlocale() instead.
241212-00:20:53,118 nipype.workflow INFO:
	 [Node] Finished "autorecon1", elapsed time 0.447008s.
2024-12-12 00:20:53,118 [    INFO] [Node] Finished "autorecon1", elapsed time 0.447008s.
241212-00:20:53,122 nipype.workflow WARNING:
	 [Node] Error on "fmriprep_24_1_wf.sub_10001_wf.anat_fit_wf.surface_recon_wf.autorecon1" (/work/fmriprep_24_1_wf/sub_10001_wf/anat_fit_wf/surface_recon_wf/autorecon1)
2024-12-12 00:20:53,122 [ WARNING] [Node] Error on "fmriprep_24_1_wf.sub_10001_wf.anat_fit_wf.surface_recon_wf.autorecon1" (/work/fmriprep_24_1_wf/sub_10001_wf/anat_fit_wf/surface_recon_wf/autorecon1)
241212-00:20:54,338 nipype.workflow ERROR:
	 Node autorecon1 failed to run on host aff3d29e813b.
2024-12-12 00:20:54,338 [   ERROR] Node autorecon1 failed to run on host aff3d29e813b.
241212-00:20:54,342 nipype.workflow ERROR:
	 Saving crash info to /out/sub-10001/log/20241211-230558_798c357b-6629-4379-95e9-69168390283b/crash-20241212-002054-root-autorecon1-77fbb309-5aa0-4ddf-a158-30b4c09789dc.txt
Traceback (most recent call last):
  File "/opt/conda/envs/fmriprep/lib/python3.11/site-packages/nipype/pipeline/plugins/multiproc.py", line 67, in run_node
    result["result"] = node.run(updatehash=updatehash)
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/conda/envs/fmriprep/lib/python3.11/site-packages/nipype/pipeline/engine/nodes.py", line 527, in run
    result = self._run_interface(execute=True)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/conda/envs/fmriprep/lib/python3.11/site-packages/nipype/pipeline/engine/nodes.py", line 645, in _run_interface
    return self._run_command(execute)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/conda/envs/fmriprep/lib/python3.11/site-packages/nipype/pipeline/engine/nodes.py", line 771, in _run_command
    raise NodeExecutionError(msg)
nipype.pipeline.engine.nodes.NodeExecutionError: Exception raised while executing Node autorecon1.

Cmdline:
	recon-all -autorecon1 -T2 /data/sub-10001/ses-scan01/anat/sub-10001_ses-scan01_T2w.nii.gz -noskullstrip -noT2pial -noFLAIRpial -cw256 -openmp 7 -subjid sub-10001 -sd /opt/subjects -notalairach -nonuintensitycor -nonormalization
Stdout:

	ERROR: It appears that this subject ID is an existing
	       base/template from longitudinal processing (-base):
	       sub-10001
	       If you are trying to re-run a -base template you
	       need to pass the -base and all -tp flags: 

	       \' -base <templateid> -tp <tpNid> ... -all \'

	       (Instead of -all you can pass other flags, such
	       as -autorecon2 -autorecon3 to run only parts.)
Stderr:

Traceback:
	RuntimeError: subprocess exited with code 1.

It appears that fMRIPrep is still trying to re-run recon-all and isn’t accepting the base/template generated from longitudinal processing.

Am I using the correct workflow here? Is there something I’m missing or doing wrong in integrating the FreeSurfer longitudinal template with fMRIPrep?

Thanks for your help!

Hi @Yanbin_Niu,

You need to use the --fs-no-resume flag for longitudinal or fastsurfer outputs.

Best,
Steven

Hi @Steven, Thank you so much — it worked!! I have a couple of questions out of curiosity:

  1. What is the magic behind --fs-no-resume? I remember using --fs-subjects-dir (but without --fs-no-resume) previously on cross-sectional data with fMRIPrep version 21.0.1, and I didn’t encounter any errors. What makes --fs-no-resume necessary for longitudinal data compared to cross-sectional data?
  2. I wanted to confirm my understanding of using average template (from FreeSurfer) with fMRIPrep: fMRIPrep will use the averaged template as the anatomical data (as opposed to the raw T1/T2) for co-registration between MNI space and the functional native space. Is that correct?

Thank you again for your help!

Best regards,
Yanbin

Hi @Yanbin_Niu

It simply tells fmriprep to not run recon-all. When not specified, it will try to run recon-all again (and skip steps that have already been done), and this leads to errors with unusual directories like fastsurfer/longitudinal.

No, the bbregister utility afforded by surface outputs is used to align BOLD to subject-space anatomicals. In that respect, I don’t know how well a longitudinal freesurfer output will work for that, since it will be trying to align session-wise BOLD to a freesurfer template derived from multiple sessions. MNI normalization still uses a volume-based ANTsRegistration workflow.

Best,
Steven

1 Like

Thank you, @Steven! This is really helpful! We are thinking about placing the averaged template T1 in the BIDS anat folder as well to make sure that fMRIPrep uses the averaged template across all its workflows.

fMRIPrep already makes an average T1w across sessions as part of it’s anatomical processing.

1 Like

Thank you, @Steven !

It seems that the average T1w across sessions is aligned to the first session using mri_robust_template, which might be slightly different from the averaged template generated by the longitudinal stream in FreeSurfer. My understanding is that it’s better to make sure that the FreeSurfer’s surface output (specified with the --fs-subjects-dir flag) and the T1w file in the BIDS anat folder are in the same space.

As such, we are considering placing the averaged template (generated from FreeSurfer’s recon-all -base command: recon-all -base averaged_template -tp tp1 -tp tp2 ... -all) into the BIDS anat folder for all sessions, and using --fs-subjects-dir flag as well. we would allow processing each session as separate data.

However, I could be wrong—it might not be necessary as long as all sessions use the same anatomical reference (e.g., the average T1w from mri_robust_template).