Moving images from MNI to BOLD/EPI/native space

Hi @oesteban,

I work with Zeynab and we were exploring a quick workaround to look at the data in native space by inverting the transformations.

However, I am having trouble getting antsApplyTransforms to take the affine matrix created by bbregister. I can get the *preproc.nii.gz bold image in MNI space registered to T1 space using the .h5 file. But antsApplyTransforms doesn’t like any of the affine .txt files. I’ve tried using the affine.txt file located in the /fsl2itk/ folder in workflows and the *T1_affine.txt file (inverting it) located in the /anat/ output folder.

Obviously it’s much simpler to just be able to have the native space be the output, but I’m curious as to why the transformations aren’t working.

Best,

  • Harris

The command I’m using is

antsApplyTransforms -d 3 -e 3 --input $fmriprepdir/$sub/func/"$sub"_task-rest_bold_space-MNI152NLin2009cAsym_preproc.nii.gz --reference-image $bidsdir/$sub/func/"$sub"_task-rest_bold.nii --output $fmriprepdir/$sub/func/"$sub"_space-nativetest2_preproc.nii.gz /autofs/space/rainier_002/users/harris/CTS/data/BIDS_output/tmp_workdir/fmriprep_wf/single_subject_$subname_wf/func_preproc_task_rest_wf/bold_reg_wf/bbreg_wf/fsl2itk_inv/affine.txt --transform $fmriprepdir/$sub/anat/"$sub"_T1w_space-MNI152NLin2009cAsym_target-T1w_warp.h5 -v 1

and the error message is

terminate called after throwing an instance of 'itk::ImageFileWriterException'
  what():  /autofs/space/rainier_001/users/harris/Sim4Life/antsbin/ITKv4-install/include/ITK-4.12/itkImageFileWriter.hxx:151:
 Could not create IO object for writing file /autofs/space/rainier_002/users/harris/CTS/data/BIDS_output/tmp_workdir/fmriprep_wf/single_subject_HV03baseline_wf/func_preproc_task_rest_wf/bold_reg_wf/bbreg_wf/fsl2itk_inv/affine.txt

Hi @hpfisher3 and @Zeynab .

Before I try to help you with antsApplyTransforms lets make sure that what you are trying to do is the right thing to do.

For people looking for native/EPI/BOLD space in FMRIPREP outputs we recommend using the T1w output space. It is aligned with the participants T1w template which allows for easy derivation of anatomical ROI, uses the resolution of the raw input data (which saves space), and was created using only one interpolation (minimizing smoothing, by combining transforms). There are very few legitimate cases where native/EPI/BOLD space would be needed instead of T1w space.

What you are attempting to do is to go from MNI to native/EPI/BOLD space. This has a few problems:

  • nonlinear transformations can be lossy, multiple voxels are squished into one and reversing this cannot be done without losing some information
  • applying additional interpolation to the data will introduce some smoothing

So why not just use T1w space?

Hi @ChrisGorgolewski,

Thanks for the quick response! Yes, I agree with all the points you raised. Selecting the T1w space as the output would definitely be the way to go if we are hoping to look at native space with fmriprep in its current form.

Mostly I was just trying to check my understanding of how the transformations work and was confused why it wasn’t working, but I’m also thinking about certain cases where one might want to look at a how a group level result from MNI space maps to individual subject spaces. Though based on your points that can probably be done by just transforming back to T1w space rather than all the way back to the original bold space.

Yup - that’s correct.

As for your command line - one thing I see being suspicious is lack of --transform in front of the path to affine.txt.

Yes that was it, left it out somehow. It runs but something isn’t right because the resulting image appears all but completely shifted out of view. Only a small sliver of the brain is visible on the edge of the image.

But it seems to be applying the transforms correctly:
The composite transform comprises the following transforms (in order):

  1. /autofs/space/rainier_002/users/harris/CTS/data/BIDS_output/fmriprep/sub-HV03baseline/anat/sub-HV03baseline_T1w_space-MNI152NLin2009cAsym_target-T1w_warp.h5[0] (type = DisplacementFieldTransform)
  2. /autofs/space/rainier_002/users/harris/CTS/data/BIDS_output/fmriprep/sub-HV03baseline/anat/sub-HV03baseline_T1w_space-MNI152NLin2009cAsym_target-T1w_warp.h5[1] (type = AffineTransform)
  3. /autofs/space/rainier_002/users/harris/CTS/data/BIDS_output/tmp_workdir/fmriprep_wf/single_subject_HV03baseline_wf/func_preproc_task_rest_wf/bold_reg_wf/bbreg_wf/fsl2itk_inv/affine.txt (type = MatrixOffsetTransformBase)

Perhaps the reference is at fault - you might need to dig up the BOLD reference image in the working directory. This is the one that is used for bold<->T1w registration - it might have different sform/qform which could lead to these issues.

Ok got it now. Thanks! I used func_preproc_task_rest_wf/bold_split/vol0001.nii.gz as the reference and it now aligns to the original raw bold perfectly.

3 years later I went to the same topic (sry for digging this out).

In case someone want to do it and don’t have the temporary fmriprep work files anymore, I may have a workaround.

The bold_ref and the original data might have different sform/qform (probably due to the fslsplit step). You can generate a nifti to serve as reference for the ApplyTransform using fslroi (faster than fslsplit in case of a long bold run) :

fslroi original_bold.nii first_volume_bold.nii 0 1

And then apply the transform (here the rigid from T1w to scanner)

antsApplyTransforms -d 3 -i image_in_space-T1w.nii -r first_volume_bold.nii -o image_in_space-scanner.nii -n Linear -t sub-XXX_ses-XXX_task-XXX_from-T1w_to-scanner_mode-image_xfm.txt
1 Like

Hi, Chris, I’m running into the similar question, in my case, I have 2 sessions, leading to 2 T1, however, in fmriprep output files, there is only 1 sub-008_desc-preproc_T1w.nii.gz, and corresponding xfm.h5 file, but in each ses_folder, there is additional sub-008_ses-01_from-orig_to-T1w_mode-image_xfm.txt for each session, my code works well for those with single T1:
antsApplyTransforms --verbose --default-value 0 --dimensionality 3 --float 0
–input “{source_path}/sub-001_task-BART_Inflate_fx_contrast.nii" \ --interpolation Linear \ --output "{source_path}/sub-001_task-BART_Inflate_fx_contrast_in_T1_space.nii”
–reference-image “{transform_path}/sub-001_ses-01_desc-preproc_T1w.nii.gz" \ --transform "{transform_path}/sub-001_ses-01_from-MNI152NLin2009cAsym_to-T1w_mode-image_xfm.h5”

my question is: how could i convert the bold file into individual native space in the scenario with 2 T1? Any comment would be really appreciated!

Hi @Isabella , when you have two T1w images in two sessions for one subject, fmriprep will create a reference T1w image that will serve as a target for all your bold-2-anat registration.

So your code should still work to project back your image from MNI space to this reference subject T1w space.

On the way fmriprep creates this T1w reference, there are two possibilities:

  • either the image of the 1st session (in alphabetical order) image is chosen as a reference
  • or the average image between the two T1w is created (by freesurfer mri_robust_template) and used as a reference.

More details here: Processing pipeline details — fmriprep version documentation