Skip to content

ENH: Wrap the Gaussian smoothing displacement field adaptors - #7143

Open
hjmjohnson wants to merge 2 commits into
InsightSoftwareConsortium:mainfrom
hjmjohnson:wrap-displacement-field-adaptors
Open

hjmjohnson wants to merge 2 commits into
InsightSoftwareConsortium:mainfrom
hjmjohnson:wrap-displacement-field-adaptors

Conversation

@hjmjohnson

@hjmjohnson hjmjohnson commented Nov 10, 2026 •

Copy link
Copy Markdown
Member

Wrap the adaptors for GaussianSmoothingOnUpdateDisplacementFieldTransform, so a multi-resolution ImageRegistrationMethodv4 run with that transform can take its per-level adaptors from Python.

Builds on #6998 (@sdiebolt, merged), which wraps TransformParametersAdaptor, DisplacementFieldTransformParametersAdaptor, and the adaptor vector.

Follow-up, not in this PR: BSplineSmoothingOnUpdateDisplacementFieldTransformParametersAdaptor is still unwrapped.

Changes on top of #6998
  • TransformParametersAdaptor and DisplacementFieldTransformParametersAdaptor gain GaussianSmoothingOnUpdateDisplacementFieldTransform<double, D> templates.
  • New itkGaussianSmoothingOnUpdateDisplacementFieldTransformParametersAdaptor.wrap.
  • WRAPPER_SUBMODULE_ORDER loads the adaptor base classes before the classes derived from them.
  • itkDisplacementFieldTransformParametersAdaptorPythonTest adapts a plain and a Gaussian-smoothing displacement field to a finer grid.
  • itkGaussianSmoothingDisplacementFieldRegistration{2,3}DPythonTest runs a two-level ImageRegistrationMethodv4 with one Gaussian adaptor per level, and checks the final field size, the adaptor's variance, and the recovered shift.

ImageRegistrationMethodv4 is not wrapped for the Gaussian transform itself, so Python drives it through the DisplacementFieldTransform instantiation with SetInPlace(True), as the registration test does.

This PR first duplicated #6998's wrapping; it was rebuilt on top of #6998 and the duplicate parts were dropped, including a SyN registration test that #6998's multi-level SyN tests already cover.

AI assistance
  • Tool: Claude Code
  • Role: wrote the wrapping and tests, answered review questions from the wrapping macros, and ran review passes
  • Testing: CI; the adaptor test also ran locally in a wrapped build

@github-actions github-actions Bot added type:Infrastructure Infrastructure/ecosystem related changes, such as CMake or buildbots type:Enhancement Improvement of existing methods or implementation area:Python wrapping Python bindings for a class type:Testing Ensure that the purpose of a class is met/the results on a wide set of test cases are correct area:Registration Issues affecting the Registration module labels Nov 10, 2026
@hjmjohnson
hjmjohnson marked this pull request as ready for review November 10, 2026 12:24
@greptile-apps

This comment was marked as resolved.

@hjmjohnson
hjmjohnson force-pushed the wrap-displacement-field-adaptors branch from 7af2c75 to 585b442 Compare November 10, 2026 13:24
@hjmjohnson

Copy link
Copy Markdown
Member Author

Second force-push (585b442 to 65747ca) is review fixes only, no rebase: the .wrap comment cut to one line, WRAPPER_SUBMODULE_ORDER so the adaptor bases wrap before the derived adaptors, Apache headers on both tests, and stronger tests. The field adaptation test now upsamples a constant nonzero field and checks it survives, and exercises the Gaussian adaptor through SetTransform and AdaptTransformParameters. The SyN test now checks that the moving image resampled through the result matches the fixed image better than before. Both pass locally.

@hjmjohnson
hjmjohnson force-pushed the wrap-displacement-field-adaptors branch 2 times, most recently from 65747ca to 2964d26 Compare November 10, 2026 13:35
@hjmjohnson

Copy link
Copy Markdown
Member Author

Third force-push (65747ca to 2964d26), review fixes only: shorter .wrap comment, the adaptation test derives its target grid from a fine reference field and sets a non-default update variance so the assertion is not vacuous, and the commit message names the BSpline adaptor as a follow-up.

@hjmjohnson
hjmjohnson requested a review from gdevenyi November 10, 2026 19:57
@hjmjohnson
hjmjohnson force-pushed the wrap-displacement-field-adaptors branch from 2964d26 to 073840d Compare November 10, 2026 21:37
@hjmjohnson

Copy link
Copy Markdown
Member Author

Rebuilt this PR on top of #6998 (2964d260d73..073840dd108): only the Gaussian smoothing adaptor wrapping, the submodule load order, and the adaptor test remain. Merge #6998 first.

@github-actions github-actions Bot added the area:Filtering Issues affecting the Filtering module label Nov 10, 2026
@hjmjohnson
hjmjohnson force-pushed the wrap-displacement-field-adaptors branch from 073840d to 3252cc1 Compare November 11, 2026 01:45
@hjmjohnson

Copy link
Copy Markdown
Member Author

Force-push 073840dd108→3252cc1e4bd is a rebase only, onto main now that #6998 has merged: #6998's commit drops out and this PR's commit is unchanged (range-diff =). The five related Python tests pass locally.

@github-actions github-actions Bot removed the area:Filtering Issues affecting the Filtering module label Nov 11, 2026
@hjmjohnson
hjmjohnson force-pushed the wrap-displacement-field-adaptors branch from 3252cc1 to 0862dfb Compare November 11, 2026 01:53
@hjmjohnson hjmjohnson changed the title ENH: Wrap the displacement field transform parameter adaptors ENH: Wrap the Gaussian smoothing displacement field adaptors Nov 11, 2026
@hjmjohnson

Copy link
Copy Markdown
Member Author

Force-push 3252cc1e4bd→0862dfbc857 is content-only (same base): the commit message and PR description no longer call GaussianSmoothingOnUpdateDisplacementFieldTransform the default SyN transform (it is used through ImageRegistrationMethodv4), the adaptor test is now guarded by ITKBridgeNumPy_ENABLED like the other NumPy tests, and it also checks the Gaussian adaptor is a DisplacementFieldTransformParametersAdaptor. The title now matches the commit.

@hjmjohnson

Copy link
Copy Markdown
Member Author

@greptileai review

@hjmjohnson hjmjohnson added the status:Confirmed Confirmed/reproduced issue on a different machine with same or similar settings to those reported label Nov 11, 2026
@hjmjohnson
hjmjohnson requested a review from dzenanz November 11, 2026 13:09

@dzenanz dzenanz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Mostly looks good after a quick review.

@gdevenyi gdevenyi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The wrapping looks correct to me. I read it against the wrapping macros, and CI is green.

  • WRAPPER_SUBMODULE_ORDER is needed. itk_auto_load_submodules.cmake loads the .wrap files in sorted order, which would put itkTransformParametersAdaptorBase after the classes derived from it. The new Gaussian .wrap is appended after the three listed ones.
  • ITKRegistrationCommon already depends on ITKDisplacementField, which wraps the transform.
  • The test's variances (2.0, 0.0) differ from the transform defaults (3.0, 0.5), so those checks are not vacuous. The [2:-2] interior lies inside the coarse field (0 to 14 mm on a 0 to 15 mm grid).

Gap: the motivating use is not exercised. itkImageRegistrationMethodv4.wrap has no template with GaussianSmoothingOnUpdateDisplacementFieldTransform as the output transform; it has one for BSplineSmoothingOnUpdateDisplacementFieldTransform (BSOUDFT). From Python, the only route is an ImageRegistrationMethodv4 templated on DisplacementFieldTransform, given a Gaussian transform through SetInitialTransform. The adaptor's dynamic_cast in itkTransformParametersAdaptor.h:88 should then succeed. No test covers that path, or a Gaussian adaptor passing through itk.vector[AdaptorBase] and SetTransformParametersAdaptorsPerLevel. Could you either:

  • add a few lines to the test that build that vector with gaussian_adaptor and pass it to a DisplacementFieldTransform ImageRegistrationMethodv4, or
  • add a REGv4…GSOUDFT template next to the BSOUDFT one?

Nit: GSOUDFT is mangled by hand in CMakeLists.txt. #6998 registered DFT with wrap_type in Wrapping/WrapITKTypes.cmake, though itkImageRegistrationMethodv4.wrap also hand-mangles BSOUDFT. Either is fine; wrap_type would match #6998.

@hjmjohnson

Copy link
Copy Markdown
Member Author

@gdevenyi Thanks for the careful read. d388b268ac3 adds itkGaussianSmoothingDisplacementFieldRegistrationTest.py (2D and 3D), which runs the Gaussian adaptors through a two-level ImageRegistrationMethodv4. On GSOUDFT, I kept the hand mangling to match BSOUDFT; moving both to wrap_type can be a follow-up.

What the test checks

It runs a two-level ImageRegistrationMethodv4 templated on DisplacementFieldTransform, with a Gaussian transform as the initial transform and one Gaussian adaptor per level passed through itk.vector[TransformParametersAdaptorBase]. It checks that the adaptors set the update-field variance, that the final field is on the full-resolution grid, and that the blob center maps to the moving center. Without the adaptors the run fails on a field and virtual-domain size mismatch, and without Update() the assertions fail.

The test sets its own optimizer, because the default optimizer's scales estimator keeps the default Mattes metric after SetMetric.

@hjmjohnson
hjmjohnson force-pushed the wrap-displacement-field-adaptors branch from d388b26 to c6133b7 Compare November 14, 2026 17:21
@hjmjohnson

Copy link
Copy Markdown
Member Author

Force-push d388b268ac3→c6133b7522 is a rebase only, onto current main (141 commits); both commits are unchanged (range-diff =).

GaussianSmoothingOnUpdateDisplacementFieldTransform is optimized with
ImageRegistrationMethodv4, which takes one parameter adaptor per level
for a multi-resolution run, but its adaptors could not be built from
Python. Add its templates to the displacement field and transform
parameter adaptors, wrap its own adaptor, and load the adaptor base
classes before the classes derived from them.
Run a two-level ImageRegistrationMethodv4 on a shifted blob with a
GaussianSmoothingOnUpdateDisplacementFieldTransform as the initial
transform and one Gaussian adaptor per level, passed through
itk.vector[TransformParametersAdaptorBase]. The test checks that the
adaptors set the update-field variance, that the final field is on the
full-resolution grid, and that the blob center maps to the moving one.
@hjmjohnson
hjmjohnson force-pushed the wrap-displacement-field-adaptors branch from c6133b7 to 8e40470 Compare November 14, 2026 17:25
@hjmjohnson

Copy link
Copy Markdown
Member Author

Force-push c6133b7522→8e4047055d is content-only (same base), from a review pass: shorter test comments (one now says why the explicit scales estimator is needed), a redundant alias dropped in the registration test, and https in both license headers. No wrapping change.

@hjmjohnson

Copy link
Copy Markdown
Member Author

@greptileai review

@hjmjohnson

Copy link
Copy Markdown
Member Author

ARMBUILD-Python failed because the macOS linker crashed (clang++: error: unable to execute command: Segmentation fault: 11) while linking _ITKPyBasePython, so every Python test then failed to import itk. That is a runner crash, not this PR's code; re-running the failed job.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:Python wrapping Python bindings for a class area:Registration Issues affecting the Registration module status:Confirmed Confirmed/reproduced issue on a different machine with same or similar settings to those reported type:Enhancement Improvement of existing methods or implementation type:Infrastructure Infrastructure/ecosystem related changes, such as CMake or buildbots type:Testing Ensure that the purpose of a class is met/the results on a wide set of test cases are correct

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants