Griddata scipy interpolation not working (gives nan)

I tested the 2d example provided in scipy.interpolation.griddata help file. It works to interpolate with "closest". But it gives a matrix filled with nan using any other interpolation such as "linear" or "cubic". If I give the argument fill_value = 5 it gives a matrix filled with 5.

Is this due to some installation issue?

I tried to point out exactly the same as they indicated in the reference document. But somehow it gives the result as if the points I asked to interpolate were outside the input points. (it is not! I followed the example)

I'll post an example to reproduce the bug (the accepted form doc)

def func(x, y):
    return x*(1-x)*np.cos(4*np.pi*x) * np.sin(4*np.pi*y**2)**2

grid_x, grid_y = np.mgrid[0:1:10j, 0:1:10j]
points = np.random.rand(100, 2)
values = func(points[:,0], points[:,1])

from scipy.interpolate import griddata

grid_z0 = griddata(points, values, (grid_x, grid_y), method='nearest')
grid_z1 = griddata(points, values, (grid_x, grid_y), method='linear')
grid_z2 = griddata(points, values, (grid_x, grid_y), method='cubic')

      

I am getting grid_z1 and grid_z2 as a matrix filled with nan.

UPDATE . I have installed all packages on another Ubuntu 11.10 machine. And the same script gave a perfectly correct answer. Earlier I tried to use the Porteus distribution (family of live slackware). So I think I can safely conclude that this was some kind of problem in my setup. Anyone can figure out what could have gone wrong? Does library conflict affect this behavior? Since my main machine is Portues, I have no choice but to rebuild scipy in it.

+3


source to share


3 answers


You say you are "filled with nano", but it is not filled. Using your code, but adding

np.random.seed(7)

      

at the beginning, so that we are working with the same dataset, I find

>>> np.isnan(grid_z1).sum()
744
>>> np.isnan(grid_z2).sum()
744

      

And these NaNs meet all on the strip outside:

>>> np.isnan(grid_z1[5:-5,5:-5]).sum()
0

      



which makes it likely that the problem is. The points that give the NaN are outside the specified points, so it doesn't know what to do with them. For the special case of "closest" interpolation, you can still find something close, so you won't get any NaNs.

So when you say that the points to be interpolated do not lie outside the input points, I ask to distinguish:

# brute force, because I'm too lazy
from collections import Counter
d = Counter()
for x, y, val in zip(grid_x.flat, grid_y.flat, grid_z1.flat):
    pg = (points >= [x, y])
    boxed = len(set(tuple(p) for p in pg)) == 4
    d[np.isnan(val), boxed] += 1

      

produces

>>> d
Counter({(False, True): 19189, (True, False): 744, (False, False): 67})

      

And there are no (True, True) cases. IOW, every NaN has no bounding box at points. There are some (False, False) cases where the value does not have a bounding rectangle but does not call NaN, which is slightly surprising, but if they assumed everything is contained, it will probably be down to boredom of implementation detail what happens if they are not. Short version: I think everything here is probably working correctly, in the sense as expected.

+7


source


It is not clear how you installed scipy (or which version you are using - try $ python -c "import scipy; print scipy.__version__"

to find out), but since griddata uses compiled code, it is possible that what you are seeing is the result of a build issue or (less likely) griddata errors characteristic of for your platform.

I suggest reporting this on the mailing list scipy-user

http://mail.scipy.org/mailman/listinfo/scipy-user , which is more suitable for solving build and installation problems than Stack Overflow.

Before submitting to the mailing list, it's worth setting up nose

the testing framework http://packages.python.org/nose so you can run



$ python -c "import scipy; scipy.test()"

      

and report any test failures at the same time.

+1


source


I have the same problem, but I think it was fixed in scipy 0.11rc2 (not that I was able to install this at the top of my Enthought Python Distribution to find out ..)?

0


source







All Articles