Skip to content

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

Draft
SID-6921 wants to merge 1 commit into
InsightSoftwareConsortium:mainfrom
SID-6921:bug-vtk-image-2d-scalar-roundtrip
Draft

SID-6921 wants to merge 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

The 2D-vs-3D trim for a trailing size-1 VTK z dimension only ran
for vector pixels. A 2D scalar image round-tripped through
vtk_image_from_image/image_from_vtk_image came back 3D with a
spurious z extent of 1. Apply the same trim regardless of
pixel type.
@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.

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.

2 participants