Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MutualInformationAffine Python example file #251

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

tbirdso
Copy link
Contributor

@tbirdso tbirdso commented Mar 17, 2021

No description provided.

Copy link
Member

@dzenanz dzenanz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good on a glance. Please squash before marking ready for review.

@tbirdso tbirdso force-pushed the mutual-information-affine-python branch 2 times, most recently from 2df7c7e to 8ef0032 Compare March 17, 2021 20:06
Copy link
Member

@thewtex thewtex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

A few requests for the functional interface.

I have not tried the image_registration_method, but we should use it, if possible.

# small steps along scale parameters
optimizer.SetScales([100, 0.5, 0.5, 100, 0.001, 0.001])

registrar = itk.ImageRegistrationMethod[type(fixed_image), type(moving_image)].New()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we used the functional Python interface here? e.g.

result_transform = itk.image_registration_method(fixed_image=fixed_image, moving_image=moving_image, transform=transform, [...]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The functional Python interface appears to be expecting images of type signed short and fails when unsigned char images are provided. I've checked that ITK is wrapped for both types. Is there a way to explicitly set the image type? Is this a wrapping that needs to be added to ITK?

Error:

Traceback (most recent call last):
File "Code.py", line 73, in
result_object = itk.image_registration_method(
File "C:\repos\ITK-builds-2\Wrapping\Generators\Python\itk\support\itkHelpers.py", line 211, in image_filter_wrapper
return image_filter(*args, **kwargs)
File "C:\repos\ITK-builds-2\Wrapping\Generators\Python\itk\itkImageRegistrationMethodPython.py", line 581, in image_registration_method
instance = itk.ImageRegistrationMethod.New(*args, **kwargs)
File "C:\repos\ITK-builds-2\Wrapping\Generators\Python\itk\support\itkTemplate.py", line 726, in New
return self[list(keys)[0]].New(*args, **kwargs)
File "C:\repos\ITK-builds-2\Wrapping\Generators\Python\itk\itkImageRegistrationMethodPython.py", line 339, in New
itkTemplate.New(obj, *args, **kargs)
File "C:\repos\ITK-builds-2\Wrapping\Generators\Python\itk\support\itkTemplate.py", line 792, in New
itk.set_inputs(self, args, kargs)
File "C:\repos\ITK-builds-2\Wrapping\Generators\Python\itk\support\itkExtras.py", line 928, in set_inputs
attrib(itk.output(value))
TypeError: in method 'itkImageRegistrationMethodISS2ISS2_SetFixedImage', argument 2 of type 'itkImageSS2 const *'

Verified input types:

> print(type(fixed_image))
<class 'itk.itkImagePython.itkImageUC2'>
> print(type(moving_image))
<class 'itk.itkImagePython.itkImageUC2'>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also note that itk.image_registration_method returns an object of type 'itk.itkTransformBasePython.itkDataObjectDecoratorTD22' which cannot be passed directly to resample_image_filter as an itk.transform. Not a big deal given that the registration transform is updated in-place and can be passed on, but I'm not sure what this object contains. Where can I find documentation for the functional interface?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can be explicit with the types we want with the ttype keyword argument.

However, we could have the filter select the proper default template type by looking at the first positional argument / template argument. This corresponds to the first indexed input to the filter, the primary input. While itk::ImageRegistrationMethod does not identify the FixedImage as its primary input, itk::ImageRegistrationMethodv4 does:

https://github.com/InsightSoftwareConsortium/ITK/blob/cde72b3e7d716542b11d2dbb402bbe5816cf731f/Modules/Registration/RegistrationMethodsv4/include/itkImageRegistrationMethodv4.hxx#L45

So, we should update this examples to use the v4 registration method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example currently relies on the Viola-Wells mutual information metric but it seems that only the Mattes mutual information metric has a v4 counterpart. Given that a v4 metric is required for the v4 registration method, is it suitable to explicitly set ttype in this example for the v3 metric and open an issue to add a new example using the Mattes mutual information v4 metric? It doesn't look like we currently have an example making use of MattesMutualInformationImageToImageMetricv4.

DefaultPixelValue=100,
)

resample.Update()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use the simpler, functional interface:

resampled = itk.resample_image_filter(moving_image, output_origin=fixed_image.GetOrigin(), [...]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same type issue as noted above. Functional interface seems to expect images of type itk.SS.

Traceback (most recent call last):
File "Code.py", line 93, in
resampled = itk.resample_image_filter(
File "C:\repos\ITK-builds-2\Wrapping\Generators\Python\itk\support\itkHelpers.py", line 211, in image_filter_wrapper
return image_filter(*args, **kwargs)
File "C:\repos\ITK-builds-2\Wrapping\Generators\Python\itk\itkResampleImageFilterPython.py", line 1927, in resample_image_filter
instance = itk.ResampleImageFilter.New(*args, **kwargs)
File "C:\repos\ITK-builds-2\Wrapping\Generators\Python\itk\support\itkTemplate.py", line 726, in New
return self[list(keys)[0]].New(*args, **kwargs)
File "C:\repos\ITK-builds-2\Wrapping\Generators\Python\itk\itkResampleImageFilterPython.py", line 680, in New
itkTemplate.New(obj, *args, **kargs)
File "C:\repos\ITK-builds-2\Wrapping\Generators\Python\itk\support\itkTemplate.py", line 792, in New
itk.set_inputs(self, args, kargs)
File "C:\repos\ITK-builds-2\Wrapping\Generators\Python\itk\support\itkExtras.py", line 928, in set_inputs
attrib(itk.output(value))
TypeError: Expecting argument of type itkImageSS2 or itkImageSourceISS2.
Additional information:
Wrong number or type of arguments for overloaded function 'itkImageToImageFilterISS2ISS2_SetInput'.
Possible C/C++ prototypes are:
itkImageToImageFilterISS2ISS2::SetInput(itkImageSS2 const *)
itkImageToImageFilterISS2ISS2::SetInput(unsigned int,itkImageSS2 const *)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is type(moving_image)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

> print(type(moving_image))
<class 'itk.itkImagePython.itkImageUC2'>

Note that no error is thrown when reading in images of type itk.SS but itk.imwrite does not support writing itk.SS PNG output images by default.

Per above, this may be resolved by updating to the v4 metric.

@tbirdso tbirdso force-pushed the mutual-information-affine-python branch from 8ef0032 to 33af31e Compare March 18, 2021 23:16
@tbirdso tbirdso force-pushed the mutual-information-affine-python branch from 649845d to 2e055b9 Compare March 24, 2021 00:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants