ITK
ITK copied to clipboard
Unexpected behavior of `itk.image_from_array` when passing `array.T`
Hi, and thanks again for making ITK!
Description
The shape of an image created with itk.image_from_array is unexpected when the input is not a contiguous array. I discovered this when passing array.T and was surprised when the shape was the same as for array. The issue doesn't happen with itk. image_view_from_array.
Steps to Reproduce
import itk
import numpy as np
array = np.empty((1, 2, 3))
print(f"{array.shape = }")
print()
print(f"{itk.image_from_array(array).shape = }")
print(f"{itk.image_from_array(array.T).shape = }")
print(f"{itk.image_from_array(array.T.copy()).shape = }")
print()
print(f"{itk.image_view_from_array(array).shape = }")
print(f"{itk.image_view_from_array(array.T).shape = }")
print(f"{itk.image_view_from_array(array.T.copy()).shape = }")
print()
print(f"{array.flags['C_CONTIGUOUS'] = }")
print(f"{array.T.flags['C_CONTIGUOUS'] = }")
print(f"{array.T.copy().flags['C_CONTIGUOUS'] = }")
print()
print(f"{array.flags['F_CONTIGUOUS'] = }")
print(f"{array.T.flags['F_CONTIGUOUS'] = }")
print(f"{array.T.copy().flags['F_CONTIGUOUS'] = }")
Expected behavior
I expected reversed shapes for itk.image_from_array(array) and itk.image_from_array(array.T).
Actual behavior (output of script above)
array.shape = (1, 2, 3)
itk.image_from_array(array).shape = (1, 2, 3)
itk.image_from_array(array.T).shape = (1, 2, 3) # I expected (3, 2, 1) here!
itk.image_from_array(array.T.copy()).shape = (3, 2, 1)
itk.image_view_from_array(array).shape = (1, 2, 3)
itk.image_view_from_array(array.T).shape = (3, 2, 1)
itk.image_view_from_array(array.T.copy()).shape = (3, 2, 1)
array.flags['C_CONTIGUOUS'] = True
array.T.flags['C_CONTIGUOUS'] = False
array.T.copy().flags['C_CONTIGUOUS'] = True
array.flags['F_CONTIGUOUS'] = False
array.T.flags['F_CONTIGUOUS'] = True
array.T.copy().flags['F_CONTIGUOUS'] = False
Reproducibility
Always.
Versions
$ python -c "import numpy; print(numpy.__version__)"
1.26.2
$ python -c "import itk; print(itk.Version.GetITKVersion())"
5.3.0
Environment
$ python <(curl -s https://raw.githubusercontent.com/fepegar/torchio/main/print_system.py)
Platform: macOS-14.0-arm64-arm-64bit
[...]
Python: 3.11.5 (main, Sep 11 2023, 08:31:25) [Clang 14.0.6 ]