I need to perform an interpolation of some Nan
values in a 2d numpy array, see for example the following picture:
In my current approach I use scipy.interpolate.griddata
for the interpolation procedure. However I noticed that when
mirroring the array on both axis i.e. d2 = d[::-1, ::-1]
the interpolation gives different results.
Here is a complete example :
import numpy as np
import matplotlib.pyplot as plt
import scipy.interpolate as interp
def replace_outliers(f):
mask = np.isnan(f)
lx, ly = f.shape
x, y = np.mgrid[0:lx, 0:ly]
z = interp.griddata(np.array([x[~mask].ravel(),y[~mask].ravel()]).T,
f[~mask].ravel(),
(x,y), method='linear', fill_value=0)
return z
def main():
d = np.load('test.npy')
d2 = d[::-1, ::-1]
dn = replace_outliers(d)
dn2 = replace_outliers(d2)
print np.sum(dn - dn2[::-1, ::-1])
plt.imshow(dn-dn2[::-1, ::-1], interpolation='nearest')
plt.colorbar()
plt.show()
if __name__=='__main__':
main()
This gives the difference between the two interpolations:
or as evaluated by np.sum
its about -62.7
So how can it be that a simple mirroring of the array gives different results in the interpolation procedure? Is there maybe something wrong with the coordinates I use ?