ANTsPy icon indicating copy to clipboard operation
ANTsPy copied to clipboard

Inconsistent interpolation calls

Open mbramerloh opened this issue 3 years ago • 6 comments

In AntsPy, applying a transformation to an image with an interpolation other than linear results in an error message.

Minimal working example:

This works:

img1 = ants.from_numpy(np.zeros([2,2,2]))
tx = ants.ants_transform.ANTsTransform()
tx.apply_to_image(img1,reference=img1,interpolation='linear')

This does not work:

img1 = ants.from_numpy(np.zeros([2,2,2]))
tx = ants.ants_transform.ANTsTransform()
tx.apply_to_image(img1,reference=img1,interpolation='nearestNeighbor')

but results in the following error message:

RuntimeError                              Traceback (most recent call last)
Input In [29], in <cell line: 3>()
      1 img1 = ants.from_numpy(np.zeros([2,2,2]))
      2 tx = ants.ants_transform.ANTsTransform()
----> 3 tx.apply_to_image(img1,reference=img1,interpolation='nearestNeighbor')

File /data/u_mbrammerloh_diss/tools/anaconda/anaconda3/envs/upgrade22/lib/python3.9/site-packages/ants/core/ants_transform.py:178, in ANTsTransform.apply_to_image(self, image, reference, interpolation)
    175 tform_fn = utils.get_lib_fn('transformImage%s%s' % (self._libsuffix, image._libsuffix))
    176 reference = reference.clone(image.pixeltype)
--> 178 img_ptr = tform_fn(self.pointer, image.pointer, reference.pointer, interpolation)
    179 return iio.ANTsImage(pixeltype=image.pixeltype,
    180                     dimension=image.dimension,
    181                     components=image.components,
    182                     pointer=img_ptr)

RuntimeError: /build104/ANTsPy/itksource/Modules/Filtering/ImageGrid/include/itkResampleImageFilter.hxx:551:
itk::ERROR: itk::ERROR: ResampleImageFilter(0x5619817a0e10): Interpolator not set

PS: I am aware that this can be circumvented by using the apply_transforms function, but I think it is not a good solution to save each transformation to a file before being able to apply it.

mbramerloh avatar Mar 03 '22 17:03 mbramerloh

Try tx.apply_to_image(img1,reference=img1,interpolation='nearestneighbor')

ntustison avatar Mar 03 '22 17:03 ntustison

Thanks, that solved it! It is a little confusing that the case of the interpolator changes within antspy. What are your thoughts on adding a doc string to the tx.apply_to_image function? That may be the quickest fix of people not running into this.

mbramerloh avatar Mar 04 '22 09:03 mbramerloh

the interpolation calls choices are very inconsistent. I believe we have

  • interpolator
  • interp_type
  • interpolation

maybe others in addition to mixes of case and numeric encoding of the choices. very confusing .... same for ANTsR. any help appreciated.

stnava avatar Mar 04 '22 10:03 stnava

This title should be more informative. I'll see if I have some time to look into it. Thank you for the quick responses!

mbramerloh avatar Mar 04 '22 10:03 mbramerloh

This should be a simple matter of remapping the interpolation string into what's expected by the underlying library.... I'll try to get around to it sometime soon. The only question is what the interpolation methods should be called. My suggestion would be something similar to scipy.interpolate.interp1d.

So, nearestNeighbor would become nearest, lanczosWindowedSinc would become lanczos, etc. Can multiLabel be deprecated?

rueberger avatar Mar 18 '22 07:03 rueberger

multiLabel can be deprecated. the interpolation names should be the same as what C++ ants expects. and backward compatible as much as possible. a lot of the issue is in the function signatures --- very inconsistent naming. I don't recall how this came about - probably just lots of different individuals implementing stuff in R which then got mapped to antspyx.

stnava avatar Mar 18 '22 14:03 stnava