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

Python GetArrayViewFromImage doesn't raise error if used the wrong way #1566

Closed
MohamedKiouaz opened this issue Jan 14, 2020 · 3 comments
Closed
Labels

Comments

@MohamedKiouaz
Copy link

@MohamedKiouaz MohamedKiouaz commented Jan 14, 2020

Description

The following code :

image = itk.imread(file_path)
image = itk.GetArrayViewFromImage(image)
image[0]

creates an invalid numpy array.
This cannot be seen right away and causes a delayed crash of python the moment when the array is actually used.
This is unexpected since the following code works in Numpy :

A = np.zeros((10, 10))
A = A[5:, :]
print(A)

Expected behavior

Raise any kind of error, or produce a valid numpy array.

Actual behavior

Nothing happens immediately. Cannot be debugged. The software crashes later without any error (not even segfault).

Reproducibility

100%

Versions & Environment

ITK 5.0.1 with Python 3.7.4 64-bit on Windows 10 1909

@thewtex

This comment has been minimized.

Copy link
Member

@thewtex thewtex commented Jan 15, 2020

The View in the function name is referring to the NumPy view concept. This saves memory but comes with a restriction / limitation: the original image instance must be kept around, or undefined behaviour will occur. In this example,

image = itk.imread(file_path)
image = itk.GetArrayViewFromImage(image)

Assigning the result in the second line is removing the original image -- it needs to be around for image to be valid.

Since GetArrayViewFromImage does not know what its result is being assigned to, it is difficult to throw an error here.

There are two options:

  1. Use GetArrayFromImage or array_from_image. This is safe.
  2. Assign to a different variable name, i.e.
image = itk.imread(file_path)
image_view = itk.GetArrayViewFromImage(image)
@thewtex thewtex closed this Jan 15, 2020
@MohamedKiouaz

This comment has been minimized.

Copy link
Author

@MohamedKiouaz MohamedKiouaz commented Jan 16, 2020

Thanks for the reply @thewtex.

Do you have an idea about why such bug doesn't happen in pure Numpy ? Assigning a view to the original numpy array does not destroy the array. The code below doesn't make the python crash.

A = np.zeros((10, 10))
A = A[5:, :] # Create a view of A and assign it to A.
print(A) # No crash

Even if we can't fix it, I think it is important to find the reason behind this weird behavior.

@thewtex

This comment has been minimized.

Copy link
Member

@thewtex thewtex commented Jan 17, 2020

A NumPy array view holds a reference to the ndarray that owns the memory in the base attribute. Before A is a view, A.base is None. After, A.base holds the original A. So, a reference to the original array is still around.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.