Hello, I was just experimenting with SimpleITK in a Jupyter Notebook and was surprised by the following behavior.
> junk = np.zeros((3,3), dtype=np.uint8)
> junk
array([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]], dtype=uint8)
> sitk.GetArrayFromImage(sitk.GetImageFromArray(junk))
array([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]], dtype=uint8)
> sitk.GetArrayViewFromImage(sitk.GetImageFromArray(junk))
array([[ 96, 117, 250],
[ 79, 5, 86],
[ 0, 0, 48]], dtype=uint8)
> tmp = sitk.GetImageFromArray(junk)
> sitk.GetArrayViewFromImage(tmp)
array([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]], dtype=uint8)
The odd part is the third result where I am using GetArrayViewFromImage
on the same line as GetImageFromArray
. The values it returns change each time, leading me to believe it is just getting garbage from memory.
Interestingly, it seems to stop when I print the result:
> print(sitk.GetArrayViewFromImage(sitk.GetImageFromArray(junk)))
[[0 0 0]
[0 0 0]
[0 0 0]]
But sometimes it will report bogus units for the first run of the cell before printing the correct values for each run after.
Based on a search of the forum, I think it might be related to this post.
The issue only seems to arise when I have multiple function calls on one line which I can easily avoid doing in practice. This behavior seems like a bug but I wanted to mention it here before submitting an issue in case I was missing something. If this is (somehow) expected behavior, please let me know. Thanks!