ITK icon indicating copy to clipboard operation
ITK copied to clipboard

DOC: warning for calling GetImageViewFromArray on non-contiguous array

Open HastingsGreer opened this issue 3 years ago • 4 comments

There's a bit of a foot-gun in GetImageViewFromArray, where if you pass it a non-contiguous array, it makes a contiguous copy of that array and returns an image view of that copy. The copy is then stored in the .base member of the returned image, to keep it from being garbage collected. This behavior is used by GetImageFromArray, which should be able to get images from non-contiguous arrays.

However, the .base member can be lost if the returned image is passed to, for example, a SetTransform method in itk, and then no longer referenced from python. This can cause surprise segfaults.

This pull request adds a warning to GetImageViewFromArray that is displayed when it is not actually returning a view, which can be suppressed with a keyword argument.

Another possibility would be to move the handling of non-contiguous arrays to GetImageFromArray, and throw an error if a non-contiguous array is passed to GetImageViewFromArray. That would be a cleaner solution, but a breaking change.

HastingsGreer avatar Jan 25 '22 00:01 HastingsGreer

@HastingsGreer thanks!! I like your approach.

Could a note in the docstring please be added?, perhaps need_contiguous=False can be passed to suppress the warning that indicates a reference must be kept to the result when a non-contiguous array is passed.

thewtex avatar Jan 25 '22 15:01 thewtex

I guess this is not ready for merging yet?

dzenanz avatar Feb 04 '22 23:02 dzenanz

@HastingsGreer Would you mind summarizing what work needs to be done for thie PR to be rejected/accepted?

hjmjohnson avatar May 06 '22 11:05 hjmjohnson

@HastingsGreer ping!

dzenanz avatar Jul 21 '22 15:07 dzenanz

I rebased onmaster and added a commit to propagate the arg and documentation into extras.py.

thewtex avatar Nov 08 '22 18:11 thewtex