Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fast 2-D interpolation in Python with SciPy regular grid to scattered / irregular evaluation

I have a regular grid of training values (vectors x and y with respective grids xmesh and ymesh and known values of zmesh) but an scattered / ragged / irregular group of values to be interpolated (vectors xI and yI, where we are interested in zI[0] = f(xI[0],yI[0]) ... zI[N-1] = f(xI[N-1],yI[N-1]). This interpolation will be called millions of times as part of an optimization problem, so performance is too important to simply to use a method that makes the grid and takes the trace.

So far, I've been able to find one scipy.interpolate function that comes close to what I want, the Bpf function. However, because it tales a scattered input, I assume that it doesn't have good performance and I'd like to test it against spline, linear, and nearest neighbor interpolation methods I understand better and I expect will be faster. All of the methods that implement these that I could find that take regular grids as training data (like RectBivariateSpline ) also seem to require regular grids for values to interpolate.

This code will hopefully make clear what I'm asking.

import numpy as np
import scipy as sp
import scipy.interpolate as interp

x = np.arange(0,2*np.pi,.1)
y = x
xmesh,ymesh = np.meshgrid(x,y)
zmesh = np.sin(xmesh)+np.cos(ymesh)
rbf = interp.Rbf(xmesh, ymesh, zmesh, epsilon=2)
xI = np.arange(0,np.pi,.05)
yI = xI
XI, YI = np.meshgrid(xI,yI)
# Notice how this is happy to take a vector or grid as input   
zI = rbf(xI, yI)
ZI = rbf(XI,YI) # equiv. to zImesh
myspline = interp.RectBivariateSpline(x, y, zmesh)
# myspline takes vectors as input but makes them into meshes for evaluation 
splineoutput = myspline(xI, yI) 
# myspline returns ZI but I want zI
print(splineoutput)
print(ZI)
print(zI)

Is there something I can do to use a function like RectBivariateSpline but to get zI (vector) instead of ZI (mesh)? Or alternatively, is there another family of functions that works the way that I want on alternative optimization methods, and if so, what should I look for?

Just a quick reminder that what I'm looking for is a fast optimization technique on with relatively large arrays of data (20,000+ entries), with small distances between grid points, and where the data is pretty smooth. I'm suspect that there is a nice, simple, way to do what I need with existing libraries but I can't find it. Thank you for the help.

like image 817
BKay Avatar asked Jul 30 '12 17:07

BKay


1 Answers

This: http://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.RectBivariateSpline.ev.html

I.e. myspline.ev(xI, yI)

like image 82
pv. Avatar answered Sep 22 '22 22:09

pv.