# [SciPy-Dev] SciPy-Dev Digest, Vol 92, Issue 9

Charles R Harris charlesr.harris@gmail....
Mon Jun 13 12:40:25 CDT 2011

On Mon, Jun 13, 2011 at 10:57 AM, Pauli Virtanen <pav@iki.fi> wrote:

> On Mon, 13 Jun 2011 09:32:31 +0700, Ole Nielsen wrote:
> [clip]
> > Meanwhile, if you could tell me where the LinearNDInterpolator package
> can be found?
>
> See the link I posted earlier:
>
>    http://docs.scipy.org/doc/scipy/reference/interpolate.html
>
> It's not a separate package, but a part of Scipy 0.9. Usage for your case:
>
> --------------------
> # some data on a grid, containing nans
> x = np.linspace(0, 1, 20)
> y = np.linspace(0, 1, 30)
> z = np.cos(x[:,None]) + np.sin(y[None,:])
> z[np.random.rand(*z.shape) > 0.9] = np.nan
>
> # flatten grid data to (x, y, z) point list
> xx = (x[:,None] + 0*y[None,:]).ravel()
> yy = (0*x[:,None] + y[None,:]).ravel()
> zz = z.ravel()
>
> # filter out nans
> m = np.isnan(zz)
> xx = xx[~m]
> yy = yy[~m]
> zz = zz[~m]
>
> # new grid
> xi = np.linspace(0, 1, 100)
> yi = np.linspace(0, 1, 200)
>
> # interpolate onto new grid
> zi = scipy.interpolate.griddata((xx, yy), zz, (xi[:,None], yi[None,:]))
> --------------------
>
> It's linear interpolation, so it will not overshoot. However, the result
> depends on how the Delaunay triangulation of the point set looks like,
> so to most natural results, you need to ensure that the `x` and `y` axes
> are scaled so that the euclidean distance `sqrt(x^2 + y^2)` means something
> natural.
>
> Also, the performance if perhaps not completely optimal for very large data
> sets, as it needs to construct the triangulation first.
>
>
>
What Pauli said. BTW, check your scipy version to make sure it is .9

If you want bilinear, I think splines of order 1 will do that. Since there
is no prefiltering, I expect they consist of simple convolution and nans
shouldn't propagate far.
ndimage.interpolation.map_coordinates<http://tinyurl.com/3jbnw24>should
do the trick. The interpolated values on a grid are in the convex
hull of the values at the surrounding corners. More specifically, it is
between the linear interpolations you get from the two different
triangulations of a rectangle obtained by using either of the two diagonals.

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-dev/attachments/20110613/d99f666c/attachment.html