Actually this is my mistake in the original question.
If we look at the position that he is trying to interpolate twoD_interpolate(arr, 0, 1, 1400, 3200, 0.5, 1684) , we get arr[ 170400, 0.1] as the value for the search, which will be truncated using mode='nearest' to arr[ -1 , 0.1] . Note. I switched x and y to get the positions to be displayed in the array.
This corresponds to interpolation from the values arr[-1,0] = 3.3 and arr[-1,1] = 4.7 , so the interpolation looks like 3.3 * .9 + 4.7 * .1 = 3.44 .
Problems go a step. If we take an array from 50 to 250 in size:
>>> a=np.arange(50,300,50) >>> a array([ 50, 100, 150, 200, 250]) >>> stride=float(a.max()-a.min())/(a.shape[0]-1) >>> stride 50.0 >>> (75-a.min()) * stride 1250.0
We can view this with map_coordinates :
#Input array from the above. print map_coordinates(arr, np.array([[.5,2.5,1250]]), order=1, mode='nearest') [ 75 175 250]
So we really need (x-xmin) / stride , for the previous examples, the step was 1, so it did not matter.
Here is what the code should do:
def twoD_interpolate(arr, xmin, xmax, ymin, ymax, x1, y1): """ interpolate in two dimensions with "hard edges" """ arr = np.atleast_2d(arr) ny, nx = arr.shape
Note that a clip is not required with mode='nearest' .
print twoD_interpolate(arr, 0, 1, 1400, 3200, 0.5, 1684) [ 21.024] print twoD_interpolate(arr, 0, 1, 1400, 3200, 0, 50000) [ 3.3] print twoD_interpolate(arr, 0, 1, 1400, 3200, .5, 50000) [ 5.3]
Checking arrays that are 1D or pseudo 1D. Interpolates the measurement x only if the input array has the correct shape:
arr = np.arange(50,300,50) print twoD_interpolate(arr, 50, 250, 0, 5, 75, 0) [75] arr = np.arange(50,300,50)[None,:] print twoD_interpolate(arr, 50, 250, 0, 5, 75, 0) [75] arr = np.arange(50,300,50) print twoD_interpolate(arr, 0, 5, 50, 250, 0, 75) [50]