antsBrainExtraction failure? Freesurfer does fine

I ran fmriprep with the command below.

singularity run --cleanenv -B ${BIDS_folder}:/mnt ${tools_dir}/fmriprep-1.2.6-1.simg \
/mnt/ /mnt/derivatives \
participant \
-w /tmp/utooley${subject} \
--participant-label ${subject} \
--fs-license-file $HOME/license.txt \
--use-aroma \
--output-space T1w template fsaverage5 \
--cifti-output \
--nthreads 1 \

The brain mask in my fmriprep freesurfer folder looks like it’s much too stringent, assuming because it used ANTs, screenshots from the .html report below.

Freesurfer brain mask seems to be much better. Attached are the same subject from FMRIPREP’s SUBJECTS_DIR and vanilla Freesurfer’s SUBJECTS_DIR, illustrating the issue. I hadn’t seen this before but now I’m worrying about brain extraction with ANTs–it looks like the option to use any other methods has been disabled?


If I copy the vanilla-Freesurfer-run subject into the FMRIPREP freesurfer directory and rerun FMRIPREP, will the Freesurfer brain mask be used?

It looks to me like an infant’s image. Is that correct?

Unfortunately, what you suggest is not possible at this moment. However, we have plans for integrating pediatric templates that would probably work much better with your images.

Thanks so much for the response! But no dice–it’s a 6 year old. We use Freesurfer tools with kids and if the T1 is okay, brain extraction generally works all right (but needs checking, hence the attention here).

Pediatric templates would of course be great, though!

1 Like

Do you use pediatric templates? Would you recommend one?

One more question @Ursula_Tooley : do you have the age encoded in the participants.tsv file of your BIDS dataset?

Hello,

I’m not sure if anyone has given recommendations for possible pediatric templates in other discussions, but the Fonov/ICBM templates (http://www.bic.mni.mcgill.ca/ServicesAtlases/NIHPD-obj1) would be a logical option. Like the adult template used in fmriprep, they have assymetrical versions and were also produced by McConnell Brain Imaging Center. They also have a 4.5 - 18.5 year old template, which would be able to meet the needs of many people looking to use fmriprep on pediatric datasets. I’m also very interested in this option being available.

Anthony

1 Like

fMRIPrep will eventually support any template included in TemplateFlow. Some templates that will be added in the near future are listed here - https://github.com/templateflow/templateflow/issues.

One of them is the Pediatric template from BIC.

Sorry for such a long wait, @oesteban–I was working on a different project more urgently than this one! We don’t use pediatric templates, typically, just regular ol’ freesurfer, and then lots of quality checks. My PI has said (but I’ve never personally quantitatively or qualitatively examined) that her experience has been that they don’t buy you a ton, but I am aware of the Fonov templates.

We don’t have age encoded in the participants.tsv file–it looks like that gets calculated from the DOB input at the scanner, and for deidentifiability reasons we don’t input the real participant DOB. Does that affect anything in the ppc-ing pipeline? Or were you asking because that could be one way to feed in a pediatric template and you wanted me to test?

I’ve seen a few threads here since about the skull-stripping being overly stringent/having issues…are there updates to fmriprep that might help? Should I pull a new image and try this person again?

Also, as an update–I fixed this by running freesurfer separately from FMRIPREP, giving it a ton more memory, which resulted in a good-looking segmentation. Then fed the pre-run Freesurfer directory into FMRIPREP. It looks like the FS run within FMRIPREP didn’t do as a good a job, which propagated downstream errors. No idea why that might be!

Just ran into another subject whose segmentation has a similar problem (see .html report screenshot below). Still using fmriprep 1.2.6-1, for consistency with subjects run previously. This participant has 2 T1-weighted scans, one of which is significantly better than the other.

Freesurfer run separately on only the good T1-weighted image produces a fine brainmask (see images below). I can only assume this has to do with the custom ants/freesurfer method for creating a brainmask used in fmriprep?


An even worse case!

Freesurfer on its own does fine with this person.

Hello Ursula,

Did you figure out how to fix this problem. I’m having the similar problem. Please, let’s me know.

Hi Thomas,

What I think worked, but I’m not totally positive (but looks like it from the .html output reports and digging into the output files) was running the person through Freesurfer separately, feeding only the good T1w image to Freesurfer, and then copying that person’s Freesurfer output into the fmriprep freesurfer directory, and running fmriprep with that pre-existing directory. Here’s the output from the person above when I do this instead. Maybe this will help you?

This leads me to think that it’s either a) the intensity normalization steps that are done with ANTs before freesurfer or b) feeding 2 images instead of 1 to freesurfer or c) the joint use of ANTs and FS to get a brainmask, I’ve been told that ANTs is great but that it’s built for perfect data, and our kid data is certainly not perfect.

Hi all,

Could you try the new pediatric templates of TemplateFlow? We are testing this new feature on fMRIPrep version 1.4.1rc2

In principle, it should be as easy as adding the following arguments:

--skull-strip-template MNIInfant:cohort-3 --output-spaces MNIInfant:cohort-3:res-2 <other-spaces>

Please replace MNIInfant with MNIPediatricAsym if those fit better your age range (MNIInfant is 0-4.5yr and MNIPediatricAsym goes from 4.5yo through 18yo).

Please replace cohort-3 with the age range that better approximates your application (MNIInfant, MNIPediatricAsym)

1 Like

Hmmm. I’m getting a problem with templateflow, error below. I feel like I’ve seen stuff like this on Neurostars, but searching didn’t yield anything useful.

Making sure the input data is BIDS compliant (warnings can be ignored in most cases).
190607-15:01:09,47 nipype.workflow IMPORTANT:
	 
    Running fMRIPREP version 1.4.1rc2:
      * BIDS dataset path: /mnt.
      * Participant list: ['CBPD0142'].
      * Run identifier: 20190607-150108_7a1900d7-3648-40a8-af17-3351dc673405.
    
Downloading https://templateflow.s3.amazonaws.com/tpl-MNIPediatricAsym/cohort-2/tpl-MNIPediatricAsym_cohort-2_res-1_T1w.nii.gz
Process Process-2:
Traceback (most recent call last):
  File "/usr/local/miniconda/lib/python3.7/multiprocessing/process.py", line 297, in _bootstrap
    self.run()
  File "/usr/local/miniconda/lib/python3.7/multiprocessing/process.py", line 99, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/local/miniconda/lib/python3.7/site-packages/fmriprep/cli/run.py", line 608, in build_workflow
    work_dir=str(work_dir),
  File "/usr/local/miniconda/lib/python3.7/site-packages/fmriprep/workflows/base.py", line 259, in init_fmriprep_wf
    use_syn=use_syn,
  File "/usr/local/miniconda/lib/python3.7/site-packages/fmriprep/workflows/base.py", line 551, in init_single_subject_wf
    skull_strip_template=skull_strip_template,
  File "/usr/local/miniconda/lib/python3.7/site-packages/smriprep/workflows/anatomical.py", line 230, in init_anat_preproc_wf
    normalization_quality='precise' if not debug else 'testing')
  File "/usr/local/miniconda/lib/python3.7/site-packages/niworkflows/anat/ants.py", line 188, in init_brain_extraction_wf
    tpl_target_path = get_template(in_template, **template_spec)
  File "/usr/local/miniconda/lib/python3.7/site-packages/templateflow/api.py", line 39, in get
    _s3_get(filepath)
  File "/usr/local/miniconda/lib/python3.7/site-packages/templateflow/api.py", line 139, in _s3_get
    with filepath.open('wb') as f:
  File "/usr/local/miniconda/lib/python3.7/pathlib.py", line 1176, in open
    opener=self._opener)
  File "/usr/local/miniconda/lib/python3.7/pathlib.py", line 1030, in _opener
    return self._accessor.open(self, flags, mode)
OSError: [Errno 30] Read-only file system: '/opt/templateflow/tpl-MNIPediatricAsym/cohort-2/tpl-MNIPediatricAsym_cohort-2_res-1_T1w.nii.gz'

Command is:


unset PYTHONPATH;
echo 'job is running'
singularity run --cleanenv -B ${BIDS_folder}:/mnt ${tools_dir}/fmriprep-1-4-1rc2.simg \
/mnt/ /mnt/derivatives/fmriprep_test \
participant \
-w /tmp/utooley${subject} \
--participant-label ${subject} \
--fs-license-file $HOME/license.txt \
--skull-strip-template MNIPediatricAsym:cohort-2 \
--output-spaces MNIPediatricAsym:cohort-2:res-2 T1w template fsaverage5 \
--nthreads 1 \

Can you try adding (before the singularity run call):

export SINGULARITYENV_TEMPLATEFLOW_HOME=/mnt/derivatives/templateflow

Assuming that your singularity installation automatically binds $HOME, you probably want to use a different path other than/mnt/derivatives.

@Ursula_Tooley did that work? I was writing on my cellphone, but now I can give some more context.

We have created a repository of templates called TemplateFlow to aid fMRIPrep (or any other tool, it’s totally open) with retrieving brain templates just giving an identifier. Templates can be navigated via the Python client (after pip install templateflow):

>>> from templateflow.api import templates, get
>>> templates()
... list of available template ids ...
>>> get('MNIInfant', suffix='T1w', resolution=1)
... list of T1w images of resolution 1mm^3 under the MNIInfant template ...

Or using datalad:

$ datalad install -r ///templateflow
$ cd templateflow/
$ datalad get tpl-MNIInfant/cohort-*/tpl-MNIInfant_cohort-*_res-1_T1w.nii.gz

Templates with several averages corresponding to age ranges or any other criteria are split by “cohorts”. The cohorts are annotated in the top template_description.json file (e.g., MNIInfant’s template_description.json).

The caveat is that TemplateFlow needs some location where all these templates are pulled (corresponding to the environment variable TEMPLATEFLOW_HOME), however, singularity images are -in general- read-only. In other words, we need users to prescribe that space. There are two ways of doing that:

  1. Overloading the default TEMPLATEFLOW_HOME, which points to /opt/templateflow, via binding:
singularity run -B $HOME/.cache/templateflow:/opt/templateflow
  1. Rewriting the TEMPLATEFLOW_HOME environment variable to some directory with write permissions:
export SINGULARITYENV_TEMPLATEFLOW_HOME=/scratch/templateflow
singularity run -B /host/workdir:/scratch fmriprep.simg /scratch/bids /scratch/bids/derivatives participant <...>

My previous post proposed solution 2, but solution 1 is equally valid. Please note how we can define environment variables that traverse the --cleanenv firewall and are thus defined in the container (i.e., using the SINGULARITYENV_ prefix).

1 Like

Looking at releases on Github, looks like I should now be trying 1.4.1rc4, right? Sorry for the delay responding to this, was traveling, but I appreciate the detailed explanation!

Setting SINGULARITYENV_TEMPLATEFLOW_HOME beforehand didn’t seem to make a difference, I got the same read-only error, but it looks like you’ve debugged templateflow since the rc2 release. Do I need to do anything special using 1.4.1rc4?

Does your singularity installation bind $HOME automatically?

If so, it should work out off the box, without any SINGULARITYENV_* set before hand.

If $HOME is not bound by default, then the SINGULARITYENV_TEMPLATEFLOW_HOME export is a must have.

You can see whether singularity is correctly setting it if you run:

export SINGULARITYENV_TEMPLATEFLOW_HOME=/scratch/templateflow
singularity exec -B /host/workdir:/scratch fmriprep.simg env | grep TEMPLATEFLOW

All of this definitely requires 1.4.1rc4.

Hmmm. Sorry again for the delay, I just got back to lab after two months of travel. I am still getting the same type of error with templateflow (below), running with 1.4.1rc4. I think this might be because our singularity installation doesn’t have a /scratch dir in the container. Not sure why, I assumed that was bundled with the container? Am I missing something? Some googling yielded that probably file system overlay is not enable on my Linux cluster, or the particular singularity configuration is different…?

Running the command above yields:

WARNING: Skipping user bind, non existent bind point (directory) in container: '/scratch'
TEMPLATEFLOW_HOME=/scratch/templateflow

The error:

 Running fMRIPREP version 1.4.1rc2:
      * BIDS dataset path: /mnt.
      * Participant list: ['CBPD0142'].
      * Run identifier: 20190716-191532_434bd2ab-c16e-46da-96d2-5ba907b1ce6d.
    
Downloading https://templateflow.s3.amazonaws.com/tpl-MNIPediatricAsym/cohort-2/tpl-MNIPediatricAsym_cohort-2_res-1_T1w.nii.gz
Process Process-2:
Traceback (most recent call last):
  File "/usr/local/miniconda/lib/python3.7/multiprocessing/process.py", line 297, in _bootstrap
    self.run()
  File "/usr/local/miniconda/lib/python3.7/multiprocessing/process.py", line 99, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/local/miniconda/lib/python3.7/site-packages/fmriprep/cli/run.py", line 608, in build_workflow
    work_dir=str(work_dir),
  File "/usr/local/miniconda/lib/python3.7/site-packages/fmriprep/workflows/base.py", line 259, in init_fmriprep_wf
    use_syn=use_syn,
  File "/usr/local/miniconda/lib/python3.7/site-packages/fmriprep/workflows/base.py", line 551, in init_single_subject_wf
    skull_strip_template=skull_strip_template,
  File "/usr/local/miniconda/lib/python3.7/site-packages/smriprep/workflows/anatomical.py", line 230, in init_anat_preproc_wf
    normalization_quality='precise' if not debug else 'testing')
  File "/usr/local/miniconda/lib/python3.7/site-packages/niworkflows/anat/ants.py", line 188, in init_brain_extraction_wf
    tpl_target_path = get_template(in_template, **template_spec)
  File "/usr/local/miniconda/lib/python3.7/site-packages/templateflow/api.py", line 39, in get
    _s3_get(filepath)
  File "/usr/local/miniconda/lib/python3.7/site-packages/templateflow/api.py", line 139, in _s3_get
    with filepath.open('wb') as f:
  File "/usr/local/miniconda/lib/python3.7/pathlib.py", line 1176, in open
    opener=self._opener)
  File "/usr/local/miniconda/lib/python3.7/pathlib.py", line 1030, in _opener
    return self._accessor.open(self, flags, mode)
OSError: [Errno 30] Read-only file system: '/opt/templateflow/tpl-MNIPediatricAsym/cohort-2/tpl-MNIPediatricAsym_cohort-2_res-1_T1w.nii.gz'