Build ITKPython module with additional supported data types

Dear ITK community,

after generating radially distorted images of lung tissue for my PhD project with ITKPython, I have realized that multiple filters I use do not support certain ITK data types. In order to solve this issue, I built the ITK python module myself and selected the previously missing ITK data types to be supported. However, after running a test script, the situation did not change. This is my test script that is supposed to show the supported data types of the NearestneighbourInterpolateImageFunction. After the build, itk.F should be supported as well:

`import itk

x = itk.NearestNeighborInterpolateImageFunction.GetTypes()
print(x)`

However, this is the output I receive:

`
Options:
[<class ‘itk.itkImagePython.itkImageD2’>, ]
[<class ‘itk.itkImagePython.itkImageD3’>, ]
[<class ‘itk.itkImagePython.itkImageF2’>, ]
[<class ‘itk.itkImagePython.itkImageF3’>, ]
[<class ‘itk.itkImagePython.itkImageSC2’>, ]
[<class ‘itk.itkImagePython.itkImageSC3’>, ]
[<class ‘itk.itkImagePython.itkImageSS2’>, ]
[<class ‘itk.itkImagePython.itkImageSS3’>, ]
[<class ‘itk.itkImagePython.itkImageUC2’>, ]
[<class ‘itk.itkImagePython.itkImageUC3’>, ]
[<class ‘itk.itkImagePython.itkImageULL2’>, ]
[<class ‘itk.itkImagePython.itkImageULL3’>, ]
[<class ‘itk.itkImagePython.itkImageUS2’>, ]
[<class ‘itk.itkImagePython.itkImageUS3’>, ]
None

Process finished with exit code 0`

For the build process, this was my knowledge souce:
https://itk.org/ITKSoftwareGuide/html/Book1/ITKSoftwareGuide-Book1ch3.html#x39-440003.7

The error message of my original script for the radial image warping is the following:
Traceback (most recent call last): File "./Test_Displacement.py", line 49, in <module> interpolator = itk.NearestNeighborInterpolateImageFunction[ImageType, itk.F].New() File "/home/reimelta/.local/lib/python2.7/site-packages/itkTemplate.py", line 340, in __getitem__ raise TemplateTypeError(self, tuple(cleanParameters)) itkTemplate.TemplateTypeError: itk.NearestNeighborInterpolateImageFunction is not wrapped for input type itk.Image[itk.US,3], itk.F`.

To limit the size of the package, only a limited number of
types are available in ITK Python. To print the supported
types, run the following command in your python environment:

itk.NearestNeighborInterpolateImageFunction.GetTypes()

Possible solutions:

  • If you are an application user:
    ** Convert your input image into a supported format (see below).
    ** Contact developer to report the issue.

  • If you are an application developer, force input images to be
    loaded in a supported pixel type.

    e.g.: instance = itk.NearestNeighborInterpolateImageFunction[itk.Image[itk.SS,2], itk.D].New(my_input)

  • (Advanced) If you are an application developer, build ITK Python yourself and
    turned to ON the corresponding CMake option to wrap the pixel type or image
    dimension you need. When configuring ITK with CMake, you can set
    ITK_WRAP_${type} (replace ${type} with appropriate pixel type such as
    double). If you need to support images with 4 or 5 dimensions, you can add
    these dimensions to the list of dimensions in the CMake variable
    ITK_WRAP_IMAGE_DIMS.

Supported input types:

itk.Image[itk.SS,2]
itk.Image[itk.UC,2]
itk.Image[itk.US,2]
itk.Image[itk.F,2]
itk.Image[itk.SS,3]
itk.Image[itk.UC,3]
itk.Image[itk.US,3]
itk.Image[itk.F,3]`

My question is: what could be the most common errors during a special build of the ITKPython module?

Thank you in advance!

Alex Reimelt

Hi Alex,

itk.F corresponds to a PixelType (float).
It’s the default, and that filter is already wrapped for 2D and 3D float images:

It seems ok to me, what were you expecting?

1 Like

Hi Pablo,

thank you for your quick response! Indeed, now more ImageTypes are supported, but the TCoordRep is still itkCType double in every combination. This is the value we wanted to change to float (see updated question).

itkNearestNeighborInterpolateImageFunction wrap file is here:

The second parameter TCoordRep is fixed to double, which is the default in the C++ side.

If you want to support float, you first need to check that the base class InterpolateImageFunction is wrapped for float, which is the case!

And then, add to itkNearestNeighborInterpolateImageFunction.wrap:5

  itk_wrap_template("${ITKM_I${t}${d}}${ITKM_F}" "${ITKT_I${t}${d}},${ITKT_F}")

After you added that line, and test that it works, you might want to close the circle, and contribute to ITK, opening a pull-request with the change. :partying_face:

1 Like

Thank you! I will try it out.

Hi Pablo,

it partly worked out. But now, there still seems to be a remaining problem with the WarpImageFilter which expects itk.D. This is the current error message:

File "Test_Displacement.py", line 51, in <module>
warpFilter.SetInterpolator(interpolator)
TypeError: in method 'itkWarpImageFilterIUS3IUS3IVF33_SetInterpolator', argument 2 of type 'itkInterpolateImageFunctionIUS3D *'

Yes, that happens. It seems way easier that you cast your TCoordRep from float32 to float (double) and don’t deal with extra wrapping types.

Or, you keep adding the float wrap to all the filters used in your pipeline.

First option seems easier.

1 Like