Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1D scipy.interpolate.griddata using method=nearest produces nans outside boundaries #4233

Closed
revoltek opened this issue Dec 4, 2014 · 1 comment · Fixed by #5201
Closed
Labels
defect A clear bug or issue that prevents SciPy from being installed or used as expected scipy.interpolate
Milestone

Comments

@revoltek
Copy link

revoltek commented Dec 4, 2014

When the coordinates are 1D the nearest method produces nans instead of the closest values when outside boundaries. An example:

import numpy as np
from scipy.interpolate import griddata

target_points = [1.,2.,3.,4.,5.,6.,7.]
points = np.random.rand(50)*2*np.pi
values = np.sin(points)

interp = griddata(points, values, target_points, method='nearest')
print interp

The last value printed is a NaN. Note that linear/cubic modes are expected to give NaNs outside the convex hull, but this should not be the case for the 'nearest' mode.

@rgommers rgommers added defect A clear bug or issue that prevents SciPy from being installed or used as expected scipy.interpolate labels Dec 5, 2014
@maniteja123
Copy link
Contributor

The documentation says that fill_value has no effect for nearest method here. Does that mean there won't be a fill_value for nearest method or that NaN isn't the default fill_value ?

In the above example, the function interp1d is called here, rather than NearestNDInterpolator since ndim is 1.

The value of interpolated y_new at _call_nearest stage is

[[ 0.85659341]
 [ 0.96631064]
 [ 0.17542803]
 [-0.78798655]
 [-0.98080843]
 [-0.25246329]
 [-0.05942813]] 

But the call in griddatahere performs _evaluate call, which does the _check_bounds that fills the last value as NaN.

[[ 0.85659341]
 [ 0.96631064]
 [ 0.17542803]
 [-0.78798655]
 [-0.98080843]
 [-0.25246329]
 [              nan]] 

In some instances, there were even more than one NaNs in the output.

Just pointing out that _evaluate doesn't take into account whether the method was linear, cubic or nearest. Hope it helps and correct me if I am wrong since I am not that familiar with this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
defect A clear bug or issue that prevents SciPy from being installed or used as expected scipy.interpolate
Projects
None yet
4 participants