nipreps / nibabies

fMRIPrep-Infants - A robust, transparent workflow tailored for neonate and infant MRI
https://nibabies.readthedocs.io/en/latest
Apache License 2.0
23 stars 11 forks source link

Failure using nibabies: Image and mask affines are not similar enough #364

Closed kellyhi08 closed 5 months ago

kellyhi08 commented 5 months ago

What happened?

I am running nibabies after using BIBSnet for segmentation. I have used the same code for multiple subjects successfully, but I got an error I am having trouble parsing.

What command did you use?

conda activate local 
singularity run --cleanenv -B /path/BIBSnet_inputs:/data:ro -B /path/BIBSnet_outputs/nibabies:/out -B /usr/local/freesurfer/7.2.0/license.txt:/opt/freesurfer/license.txt:ro -B /path/BIBSnet_outputs/bibsnet:/derivatives:ro /path/nibabies-23.1.0.sif /data /out participant --derivatives /derivatives --anat-only -vvv --participant-label $sub  --session-id $ses

What version of NiBabies are you using?

nibabies-23.1.0.sif: latest as of Jan 16th, 2024

Relevant log output

240510-13:36:04,790 nipype.workflow ERROR:
     could not run node: nibabies_23_1_wf.single_subject_024_01_wf.infant_anat_wf.anat_derivatives_wf.mask_anat

INFO:    underlay of /etc/localtime required more than 50 (109) bind mounts
(node:5964) Warning: Closing directory handle on garbage collection
(Use `node --trace-warnings ...` to show where the warning was created)
[WARNING] Citeproc: citation templateflow not found
RuntimeError: Traceback (most recent call last):
  File "/opt/conda/envs/nibabies/lib/python3.10/site-packages/nipype/pipeline/plugins/multiproc.py", line 67, in run_node
    result["result"] = node.run(updatehash=updatehash)
  File "/opt/conda/envs/nibabies/lib/python3.10/site-packages/nipype/pipeline/engine/nodes.py", line 527, in run
    result = self._run_interface(execute=True)
  File "/opt/conda/envs/nibabies/lib/python3.10/site-packages/nipype/pipeline/engine/nodes.py", line 645, in _run_interface
    return self._run_command(execute)
  File "/opt/conda/envs/nibabies/lib/python3.10/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 apply_mask.

Traceback:
    Traceback (most recent call last):
      File "/opt/conda/envs/nibabies/lib/python3.10/site-packages/nipype/interfaces/base/core.py", line 397, in run
        runtime = self._run_interface(runtime)
      File "/opt/conda/envs/nibabies/lib/python3.10/site-packages/niworkflows/interfaces/nibabel.py", line 75, in _run_interface
        raise ValueError("Image and mask affines are not similar enough.")
    ValueError: Image and mask affines are not similar enough.

Add any additional information or context about the problem here.

No response

mgxd commented 5 months ago

It looks like the anatomical template image and precomputed mask do not share the same affine - are the images aligned?

kellyhi08 commented 5 months ago

I am not sure exactly which files you are referring too, but it is correct that the MNIInfant_cohort-2 images do not line up with the subject specific segmentations (dseg) output from BIBSnet, but I don't think they should. There is a file that might be for registration (sub-024_ses-01_from-MNIInfant+2_to-T1w_mode-image_xfm.h5) between those two spaces.

mgxd commented 5 months ago

The mask provided by the --derivatives is expected to be aligned with your input anatomical image, so in this case either the raw T1w or T2w for your subject.

kellyhi08 commented 5 months ago

I see. Yes the mask provided by derivatives does align with my subject's raw T1w.

mgxd commented 5 months ago

Ok, in that case it probably is because your affines differ enough (perhaps having different levels of precision) to exceed the threshold - first, you should verify this difference by viewing and comparing the affines, and then you can overwrite the derived image's header with the raw T1's. I see you are using BIBSnet outputs - I'm surprised this isn't already applied to the output.

kellyhi08 commented 5 months ago

how would I view the affines to compare them?

mgxd commented 5 months ago

You can load each image with nibabel, which will let you check out the affines.

import nibabel as nb
import numpy as np

t1 = nb.load("/path/to/raw/T1w")
mask = nb.load("/path/to/mask")

print(t1.affine)
print(mask.affine)
print(np.allclose(t1.affine, mask.affine))

Then, if you wish to use T1w's header:

new_mask = mask.__class__(mask.dataobj, t1.affine, t1.header)
new_mask.set_data_dtype(mask.get_data_dtype())
new_mask.to_filename("/path/to/save/new/mask")
mgxd commented 5 months ago

Please reopen if you are still running into issues.