Hi everyone.
I need to warp functional mri image into MNI space. The original space has NaN values. I would like to preserve them in the MNI space. Do you which interpolation algorithm would allow to have that ? I tried: linear and nearest neighbor but they substitute nan values with zero values. Other information: I am using AntsApplyTransform.
@Manuela1 I would first think carefully about whether this is the correct order of processing. NaNs are like black holes - any value that has any interaction with a NaN necessarily becomes a NaN. Therefore, they contaminate all of their neighbors - with any higher order interpolation (linear, sinc, etc). While nearest neighbor interpolation is the most resilient to NaNs, it does this by discarding high frequency information. In reality, since imaging data is smoothed and partial volume effects, significant clusters are always surrounded by near-significant voxels. Artificially thresholding an image with either NaNs or zeros will therefore be detrimental to any subsequent spatial processing steps. For this reason, one should always attempt to mask with NaNs or zeros as the last possible step and certainly after spatial reslicing (or never, letting the visualization software leverage neighboring voxels when applying a threshold). My sense is you should check the provenance of your images, and defer the insertion of the NaN values until after you warp the images into a different space.
If there is no alternative, you could use the script (like below) to make a binary image where all finite values are zero and all infinite values are one. Shadow register this to your desired space using nearest neighbor interpolation and then use the resliced mask to re-insert the NaNs.
#!/usr/bin/env python3
import nibabel as nib
import numpy as np
import sys
def binarize_nan_inf(input_nii, output_nii):
img = nib.load(input_nii)
data = img.get_fdata()
binary_data = np.where(np.isnan(data) | np.isinf(data), 1, 0).astype(np.uint8)
new_img = nib.Nifti1Image(binary_data, img.affine, img.header)
nib.save(new_img, output_nii)
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: python binarize_nan_inf.py input.nii.gz output.nii.gz")
sys.exit(1)
binarize_nan_inf(sys.argv[1], sys.argv[2])