ENH: Fix buffer ownership and a redundant copy in SimpleITK interop - #6828
Merged
hjmjohnson merged 1 commit intoSep 4, 2026
Conversation
dzenanz
reviewed
Sep 2, 2026
dzenanz
left a comment
Member
There was a problem hiding this comment.
Looks good. For the other conversion direction, it seems that buffer is copied twice. Maybe replace array = itk.array_from_image(image) by array = itk.array_view_from_image(image)?
dzenanz
approved these changes
Sep 2, 2026
hjmjohnson
approved these changes
Sep 2, 2026
Member
|
@greptile review |
Contributor
|
hjmjohnson
approved these changes
Sep 3, 2026
blowekamp
marked this pull request as ready for review
September 3, 2026 20:51
image_from_simpleitk used itk.image_view_from_array, giving the itk::Image a pixel container that does not own its memory -- unsafe for Graft() and in-place filters. It now deep-copies via itk.image_from_array instead. simpleitk_from_image read the ITK image with itk.array_from_image (a deep copy) only to hand it to sitk.GetImageFromArray, which deep-copies again. It now reads a view instead, dropping the redundant copy.
blowekamp
force-pushed
the
follow-up-image-from-simpleitk-deep-copy
branch
from
September 4, 2026 12:22
7325ce3 to
a0a6c9e
Compare
dzenanz
approved these changes
Sep 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #6811. Fixes buffer-ownership in
image_from_simpleitkand removes a redundant copy insimpleitk_from_image.image_from_simpleitkwrapped a numpy buffer viaimage_view_from_array, giving the itk::Image a pixel container that does not own its memory, which is unsafe forGraft()and in-place filters. It now reads pixels withsitk.GetArrayViewFromImage(no copy) and builds the ITK image withitk.image_from_array(deep copy into an ITK-owned buffer) — same one copy overall, with correct ownership.simpleitk_from_imageread the ITK image withitk.array_from_image(a deep copy) purely to hand it tositk.GetImageFromArray, which deep-copies again into the buffer the newsitk.Imageowns. It now reads withitk.array_view_from_imageinstead, dropping the redundant first copy.Why the ownership fix matters
image_view_from_arraymarks the itk::Image pixel container as not managing its own memory, since it imports a pointer into a numpy-owned buffer. Filters that graft their output onto a pre-existing image, or that reuse/reallocate a buffer in place, assume the container they write into owns its memory.image_from_arraydeep-copies into a container ITK allocates and owns, removing that hazard.Testing
black --target-version py310and the project pre-commit hooks pass on the changed file. Not exercised against real SimpleITK locally (no SimpleITK/itk build environment available). The in-treesimpleitk_protocol.pytest does not cover either function directly; per #6811 it stubs only the geometry accessors, by design (round-trip fidelity is owned by an out-of-tree suite).