InsightSoftwareConsortium / ITK

Insight Toolkit (ITK) -- Official Repository. ITK builds on a proven, spatially-oriented architecture for processing, segmentation, and registration of scientific images in two, three, or more dimensions.
https://itk.org
Apache License 2.0
1.42k stars 663 forks source link

Support Python dispatching on PrimaryInputName #4858

Open thewtex opened 1 month ago

thewtex commented 1 month ago

We should dispatch on PrimaryInputName when it is passed as a keyword argument instead of a positional parameter.

xref:

CC: @N-Dekker @mstaring

N-Dekker commented 1 week ago

@thewtex I'm glad you are looking at this issue. Is the following supposed to work well?

    output_image = itk.median_image_filter(input=my_float_image)

(It now says TypeError: Expecting argument of type itkImageSS2 or itkImageSourceISS2). Or should the keyword argument start with a capital, Input=my_float_image? I'm asking, because "Input" is in the list of so-called primary_input_methods at:

https://github.com/InsightSoftwareConsortium/ITK/blob/15af3aed65693811448c9af22ce9d09ff9f3000a/Wrapping/Generators/Python/itk/support/template_class.py#L672

And primary_input_methods is used for dispatching:

https://github.com/InsightSoftwareConsortium/ITK/blob/15af3aed65693811448c9af22ce9d09ff9f3000a/Wrapping/Generators/Python/itk/support/template_class.py#L711-L716

Regarding ITKElastix issue https://github.com/InsightSoftwareConsortium/ITKElastix/issues/212, obviously, "FixedImage" and "MovingImage" are not in primary_input_methods (and neither are "fixed_image" and "moving_image"). So I guess primary_input_methods should be extended, right?

blowekamp commented 1 week ago

The Process object maintains a lists of index inputs and named inputs. And a named input can be marked as primary. These named inputs are already coded input many filters e.g. PDEDeformableRegistrationFilter

The information in the Process object should likely be used.

N-Dekker commented 1 week ago

Thanks @blowekamp I see, PDEDeformableRegistrationFilter has inputs named "FixedImage" and "MovingImage":

https://github.com/InsightSoftwareConsortium/ITK/blob/15af3aed65693811448c9af22ce9d09ff9f3000a/Modules/Registration/PDEDeformable/include/itkPDEDeformableRegistrationFilter.hxx#L42-L46

So then, should something like the following work, for itk::Image<float> objects float_image1 and float_image2?

itk.pde_deformable_registration_filter(FixedImage=float_image1, MovingImage=float_image2)

(It now says TypeError: Expecting argument of type itkImageSS2 or itkImageSourceISS2)

dzenanz commented 1 week ago

In procedural interface, the parameters get converted to snake_case, so try this: itk.pde_deformable_registration_filter(fixed_image=float_image1, moving_image=float_image2).

blowekamp commented 1 week ago

I would expect ProcessObject::GetPrimaryInputName to be useful.

I am not too sure of these details in ITK Python. Specifically if the Process object's name inputs/outputs are used or if it's just mapping keyword arguments to methods.

Also I think that PDE deformable registration class may be a base class for the deacons methods, and I have a harder guess of which is the primary input, and how the template parameters get mapped from it.

thewtex commented 1 week ago

Is the following supposed to work well?

    output_image = itk.median_image_filter(input=my_float_image)

The keyword here to be supported would be the snake_case version of itk::MedianImageFilter::GetPrimaryInputName();