ITK
ITK copied to clipboard
DOC: warning for calling GetImageViewFromArray on non-contiguous array
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 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.
I guess this is not ready for merging yet?
@HastingsGreer Would you mind summarizing what work needs to be done for thie PR to be rejected/accepted?
@HastingsGreer ping!
I rebased onmaster
and added a commit to propagate the arg and documentation into extras.py.