NBCLab / power-replication

A replication and extension of Power et al. (2018)
https://www.overleaf.com/read/swgjxcjqytxg
Apache License 2.0
2 stars 0 forks source link

Investigate error warping *some* files generated with nilearn #16

Closed tsalo closed 2 years ago

tsalo commented 2 years ago

Only some subjects have files with problems being warped. So far it's 2 out of the first 20 subjects I tested my warping script on. However, it looks like it affects all files created with nilearn, including:

Most of these are created with my _generic_regression function, which uses nilearn's NiftiMasker.

The only ones that I don't think were created with that are the GODEC ones, which are created with nilearn's unmask here.

All of the files created by tedana, rapidtide, and fMRIPrep seem fine.

I ran the denoising step with nilearn version 0.8.0.

tsalo commented 2 years ago

Here's an example of the antsApplyTransforms error:

Exception caught during reference file reading

itk::ExceptionObject (0x39fafa0)
Location: "unknown"
File: /home/software/ANTs/ANTs-2.3.5/ANTs-2.3.5/build/ITKv5/Modules/IO/NIFTI/src/itkNiftiImageIO.cxx
Line: 1972
Description: itk::ERROR: ITK only supports orthonormal direction cosines.  No orthonormal definition found!

 file /home/data/nbc/misc-projects/Salo_PowerReplication/dset-cambridge/derivatives/tedana/sub-20503/func/sub-20503_task-rest_desc-optcomMIRDenoised_errorts.nii.gz
terminate called after throwing an instance of 'itk::ExceptionObject'
  what():  /home/software/ANTs/ANTs-2.3.5/ANTs-2.3.5/build/ITKv5/Modules/Core/Common/src/itkProcessObject.cxx:1339:
itk::ERROR: itk::ERROR: ResampleImageFilter(0x39ffd80): Input Primary is required but not set.
tsalo commented 2 years ago

Since this comes up with the rapidtide errorts output, which only uses math_img, I think the problem must come down to one of the following functions:

tsalo commented 2 years ago

I ran a quick test on these three functions. Basically, I took the tedana output from one of the subjects that failed and created new files that had only been touched by one of the three functions. I also included one that was only touched with nibabel as a control.

Here's the Python code I used to create new files that should only be touched by one of the functions:

import nibabel as nib
from nilearn._utils.niimg import _safe_get_data
from nilearn._utils.niimg_conversions import check_niimg
from nilearn.image.image import new_img_like

in_file = "sub-20503_task-rest_desc-optcomDenoised_bold.nii.gz"
img = nib.load(in_file)

# Test nibabel-only operation
data = img.get_fdata()
data = data ** 2
nib_only_img = nib.Nifti1Image(data, img.affine, img.header)
nib_only_img.to_filename("tedana_nibabel.nii.gz")

# Test check_niimg
check_niimg_img = check_niimg(in_file)
check_niimg_img.to_filename("tedana_check_niimg.nii.gz")

# Test _safe_get_data
data = _safe_get_data(img)
_safe_get_data_img = nib.Nifti1Image(data, img.affine, img.header)
_safe_get_data_img.to_filename("tedana__safe_get_data.nii.gz")

# Test new_img_like
img = nib.load(in_file)
data = img.get_fdata()
new_img_like_img = new_img_like(img, data, img.affine)
new_img_like_img.to_filename("tedana_new_img_like.nii.gz")

And here's the ANTS script to apply the xforms to each of these files:

#!/bin/bash
module load ants-2.3.5

# Run xforms on tedana-generated file. Should be good.
antsApplyTransforms -d 3 \
    -i sub-20503_task-rest_desc-optcomDenoised_bold.nii.gz \
    -r sub-20503_task-rest_space-MNI152NLin6Asym_boldref.nii.gz \
    -o tedana_out.nii.gz \
    -n NearestNeighbor \
    -t sub-20503_task-rest_from-scanner_to-T1w_mode-image_xfm.txt sub-20503_from-T1w_to-MNI152NLin6Asym_mode-image_xfm.h5

# Run xforms on tedana + nibabel. Should be good.
antsApplyTransforms -d 3 \
    -i tedana_nibabel.nii.gz \
    -r sub-20503_task-rest_space-MNI152NLin6Asym_boldref.nii.gz \
    -o tedana_nibabel_out.nii.gz \
    -n NearestNeighbor \
    -t sub-20503_task-rest_from-scanner_to-T1w_mode-image_xfm.txt sub-20503_from-T1w_to-MNI152NLin6Asym_mode-image_xfm.h5

# Run xforms on tedana + check_niimg
antsApplyTransforms -d 3 \
    -i tedana_check_niimg.nii.gz \
    -r sub-20503_task-rest_space-MNI152NLin6Asym_boldref.nii.gz \
    -o tedana_check_niimg_out.nii.gz \
    -n NearestNeighbor \
    -t sub-20503_task-rest_from-scanner_to-T1w_mode-image_xfm.txt sub-20503_from-T1w_to-MNI152NLin6Asym_mode-image_xfm.h5

# Run xforms on tedana + _safe_get_data
antsApplyTransforms -d 3 \
    -i tedana__safe_get_data.nii.gz \
    -r sub-20503_task-rest_space-MNI152NLin6Asym_boldref.nii.gz \
    -o tedana__safe_get_data_out.nii.gz \
    -n NearestNeighbor \
    -t sub-20503_task-rest_from-scanner_to-T1w_mode-image_xfm.txt sub-20503_from-T1w_to-MNI152NLin6Asym_mode-image_xfm.h5

# Run xforms on tedana + new_img_like
antsApplyTransforms -d 3 \
    -i tedana_new_img_like.nii.gz \
    -r sub-20503_task-rest_space-MNI152NLin6Asym_boldref.nii.gz \
    -o tedana_new_img_like_out.nii.gz \
    -n NearestNeighbor \
    -t sub-20503_task-rest_from-scanner_to-T1w_mode-image_xfm.txt sub-20503_from-T1w_to-MNI152NLin6Asym_mode-image_xfm.h5

I haven't viewed the output files, but new_img_like is the only one to raise the error.

tsalo commented 2 years ago

new_img_like only fails when copy_header is False, which happens to be both the default and outside of user control when calling unmask and math_img.

tsalo commented 2 years ago

I will probably open an issue in the nilearn repo about changing the default for copy_header from False to True, but the next release won't be until January and there's no guarantee the other devs will want to make the change, so I will need to (1) create a new branch of my nilearn fork with the parameter changed, (2) install the my fork's version in the Power environment, (3) remove all denoised files, (4) re-run the denoising job, and (5) run the warping job.

tsalo commented 2 years ago

I'm now up to re-running the denoising job. It shouldn't take more than a day or so.

tsalo commented 2 years ago

sub-20503 successfully warped this time(!), so I'm going to try warping everything else. I will close this issue if there are no other problems with the warps.

tsalo commented 2 years ago

Only the problematic runs from #15 are missing the warped files, so it's all good now.