I have difficulties understanding the behavior of scipy.ndimage.zoom()
when order=0
.
Consider the following code:
import numpy as np
import scipy as sp
import scipy.ndimage
arr = np.arange(3) + 1
print(arr)
for order in range(5):
zoomed = sp.ndimage.zoom(arr.astype(float), 4, order=order)
print(order, np.round(zoomed, 3))
whose output is:
0 [1. 1. 1. 2. 2. 2. 2. 2. 2. 3. 3. 3.]
1 [1. 1.182 1.364 1.545 1.727 1.909 2.091 2.273 2.455 2.636 2.818 3. ]
2 [1. 1.044 1.176 1.394 1.636 1.879 2.121 2.364 2.606 2.824 2.956 3. ]
3 [1. 1.047 1.174 1.365 1.601 1.864 2.136 2.399 2.635 2.826 2.953 3. ]
4 [1. 1.041 1.162 1.351 1.59 1.86 2.14 2.41 2.649 2.838 2.959 3. ]
So, when order=0
the values are (expectedly) not interpolated.
However, I was expecting to have:
[1. 1. 1. 1. 2. 2. 2. 2. 3. 3. 3. 3.]
i.e. exactly the same number of elements for each value, since the zoom is a whole number.
Hence, I was expecting to get the same result as np.repeat()
:
print(np.repeat(arr.astype(float), 4))
[1. 1. 1. 1. 2. 2. 2. 2. 3. 3. 3. 3.]
Why is there a variation in the number of times each element gets repeated?
Note that np.repeat()
does not directly work with multi-dimensional arrays and that is the reason why I would like to get the "correct" behavior from scipy.ndimage.zoom()
.
My NumPy and SciPy versions are:
print(np.__version__)
# 1.17.4
print(sp.__version__)
# 1.3.3
I found this:
`scipy.ndimage.zoom` vs `skimage.transform.rescale` with `order=0`
which points toward some unexpected behavior for scipy.ndimage.zoom()
but I am not quite sure it is the same effect being observed.