Skip to content

BUG: Keep 2D scalar images 2D in image_from_vtk_image - #6996

Merged
hjmjohnson merged 1 commit into
InsightSoftwareConsortium:mainfrom
SID-6921:bug-vtk-image-2d-scalar-roundtrip
Oct 31, 2026
Merged

hjmjohnson merged 1 commit into
InsightSoftwareConsortium:mainfrom
SID-6921:bug-vtk-image-2d-scalar-roundtrip

Conversation

@SID-6921

Copy link
Copy Markdown
Contributor

image_from_vtk_image only trimmed a trailing size-1 VTK z dimension for vector-pixel images, so round-tripping a 2D scalar image through the VTK bridge produced a 3D image. Apply the same trim for scalar pixels.

AI assistance
  • Tool: Claude Code (claude-sonnet-5)
  • Role: located the bug by comparing the scalar and vector branches of image_from_vtk_image in Wrapping/Generators/Python/itk/support/extras.py; drafted the fix and the added round-trip test
  • Contribution: the vector branch already trimmed a trailing size-1 z dimension (VTK images are always stored with 3 dims) before calling image_view_from_array, but the scalar branch had no equivalent check, so 2D scalar images silently grew a spurious 3rd dimension on the VTK round trip
  • All code was reviewed locally before committing; no build environment was available to execute the test, so CI must confirm it

@github-actions github-actions Bot added type:Bug Inconsistencies or issues which will cause an incorrect result under some or all circumstances 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 labels Oct 23, 2026

@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.

Test looks good. Does looking at git history reveal why was is_vector in the if condition? Oversight, or some special handling of color or some other non-spatial dimension?

@SID-6921

Copy link
Copy Markdown
Contributor Author

Checked history. Both functions were added together in f13e760 (2020-04-29, "Add itk.vtk_image_from_image and itk.image_from_vtk_image"), and the is_vector check goes back to that same commit, unchanged since.

The original code was actually two separate branches, not one shared trim:

if is_vector and dims[-1] == 1:
    dims = dims[:2]
    dims.reverse()
    dims.append(point_data.GetScalars().GetNumberOfComponents())
else:
    dims.reverse()

The vector branch needs is_vector specifically because it has to append the component count as its own trailing axis, which only makes sense for a vector image. The else branch just reverses all 3 dims unconditionally, including the padded 1, so a 2D scalar image always kept its spurious third axis.

The PR's own test suite only ever covered a 3D scalar round trip and a 3D vector round trip, never 2D of either kind, so this never got exercised. Nothing in the commit message, code, or tests points to intentional handling of color or some other non-spatial dimension for the scalar case. Looks like a plain oversight to me, not a deliberate choice.

@dzenanz
dzenanz requested a review from thewtex October 23, 2026 18:38
@dzenanz

dzenanz commented Oct 23, 2026

Copy link
Copy Markdown
Member

LGTM. It would be good if Matt reviewed, too.

@thewtex thewtex 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.

This looks ok.

Since VTK does not have a way to represent non-3D rank images, we loose the original rank. So, if it was a 3D image with a size of 1 in the third dimension, it is always converted to a 2D image. That may be a better default.

@SID-6921
SID-6921 marked this pull request as ready for review October 28, 2026 16:55
Copilot AI balanced review requested due to automatic review settings October 28, 2026 16:55

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@SID-6921

Copy link
Copy Markdown
Contributor Author

Thanks both for reviewing. Marked this ready for review now that it's approved by both of you — happy to make any more changes if something still needs a look, otherwise it should be good to merge.

@greptile-apps

greptile-apps Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 4/5

[Medium impact] The change reaches callers converting VTK images with a trailing size-1 dimension.

Preserve single-slice 3D scalar images before merging.

Findings

  1. P1 Single-slice images lose 3D geometry ▶

Summary

Extends the singleton-z trim in image_from_vtk_image to scalar images.

  • 2D scalar images stay 2D after a VTK round trip.

Reviews (1) · Last reviewed commit: "BUG: Keep 2D scalar images 2D in image_f..." · Reviewed by Greptile

@SID-6921
SID-6921 force-pushed the bug-vtk-image-2d-scalar-roundtrip branch from e46a41d to ac517c5 Compare October 28, 2026 17:33
@hjmjohnson
hjmjohnson force-pushed the bug-vtk-image-2d-scalar-roundtrip branch from ac517c5 to 67f4007 Compare October 29, 2026 00:48
@hjmjohnson

Copy link
Copy Markdown
Member

Rebased on main (plain rebase, patch unchanged) to pick up #7028, which fixes the itkIndent.h include-cycle error behind the ARMBUILD failure. Pull with git pull --rebase before pushing again.

@SID-6921

Copy link
Copy Markdown
Contributor Author

Got it, thanks for sorting that out. Noted, will pull before pushing anything further to this branch.

@hjmjohnson
hjmjohnson merged commit 2e0c0b6 into InsightSoftwareConsortium:main Oct 31, 2026
19 checks passed
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 type:Bug Inconsistencies or issues which will cause an incorrect result under some or all circumstances 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.

5 participants