Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected behavior of itk.image_from_array when passing array.T #4329

Open
fepegar opened this issue Nov 14, 2023 · 0 comments
Open

Unexpected behavior of itk.image_from_array when passing array.T #4329

fepegar opened this issue Nov 14, 2023 · 0 comments
Assignees
Labels
type:Bug Inconsistencies or issues which will cause an incorrect result under some or all circumstances

Comments

@fepegar
Copy link

fepegar commented Nov 14, 2023

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 ]
@fepegar fepegar added the type:Bug Inconsistencies or issues which will cause an incorrect result under some or all circumstances label Nov 14, 2023
@thewtex thewtex added this to the ITK 6.0 Beta 1 milestone Nov 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:Bug Inconsistencies or issues which will cause an incorrect result under some or all circumstances
Projects
None yet
Development

No branches or pull requests

2 participants