InsightSoftwareConsortium / InsightSoftwareConsortium/ITK

itkPython: itkImage use in np.where returns incorrect results

Open
#2,835 3 comments 1 reaction 1 assignee Claimed by @thewtex View on GitHub
type:Bug
Dominant language
C++
Stars
1.7k
Forks
748
Avg merge
1d 1h
Merged PRs (30d)
64

Description

### Description

It is possible to pass an itkImage to a numpy.where call, but the results are incorrect. A test of
`np.where(==, , )` will return an image full of ``, regardless of the value of `` and regardless of the content of the image.

### Steps to Reproduce

See attached ipython notebook. In summary

```
import itk
import numpy as np

print("A numpy array (arr) and itkImage (img) set to identity.")
arr = np.eye(3,3)
img = itk.GetImageFromArray(arr)

print("Using an itkImage in np.where statement produces unexpected results...")
print(" Array where arr[i,j]==0 change to a 2 =", np.where(arr==0,2,arr))
print(" itkImage where img[i,j]==0 change to a 2 =", np.where(img==0,2,img))
```

Produces...

```
A numpy array (arr) and itkImage (img) set to identity.
Using an itkImage in np.where statement produces unexpected results...
Array where arr[i,j]==0 change to a 2 = [[1. 2. 2.]
[2. 1. 2.]
[2. 2. 1.]]
itkImage where img[i,j]==0 change to a 2 = [[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
```

Additionally,

```
print("The test itkImage== appears to always return false?")
arr = np.zeros([3,3])
img = itk.GetImageFromArray(arr)
print(" Given arr=0, using np.where to change any instance of arr[i,j]==0 to a 2 =", np.where(arr==0,2,arr))
print(" Given img=0, using np.where to change any instance of img[i,j]==0 to a 2 =", np.where(img==0,2,img))
```

produces...

```
The test itkImage== appears to always return false?
Given arr=0, using np.where to change any instance of arr[i,j]==0 to a 2 = [[2. 2. 2.]
[2. 2. 2.]
[2. 2. 2.]]
Given img=0, using np.where to change any instance of img[i,j]==0 to a 2 = [[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]
```

### Expected behavior

Using an itk:image in a numpy conditional should work as if a numpy array was used.

### Actual behavior

Using an itk::image in a numpy.where conditional always returns false.

### Reproducibility

See attached ipython notebook.

### Versions

ITKv5.3rc01

### Environment

Linux, Ubuntu 20.XX

Zipped ipython notebook attached.
[itkImageAndNPWhereAreNotCompatible.zip](https://github.com/InsightSoftwareConsortium/ITK/files/7406050/itkImageAndNPWhereAreNotCompatible.zip)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.