Is there a library module or other straightforward way to implement multivariate spline interpolation in python?
Specifically, I have a set of scalar data on a regularly-spaced three-dimensional grid which I need to interpolate at a small number of points scattered throughout the domain. For two dimensions, I have been using scipy.interpolate.RectBivariateSpline, and I’m essentially looking for an extension of that to three-dimensional data.
The N-dimensional interpolation routines I have found are not quite good enough: I would prefer splines over LinearNDInterpolator for smoothness, and I have far too many data points (often over one million) for, e.g., a radial basis function to work.
If anyone knows of a python library that can do this, or perhaps one in another language that I could call or port, I’d really appreciate it.
If I’m understanding your question correctly, your input “observation” data is regularly gridded?
If so,
scipy.ndimage.map_coordinatesdoes exactly what you want.It’s a bit hard to understand at first pass, but essentially, you just feed it a sequence of coordinates that you want to interpolate the values of the grid at in pixel/voxel/n-dimensional-index coordinates.
As a 2D example:
To do this in n-dimensions, we just need to pass in the appropriate sized arrays:
As far as scaling and memory usage goes,
map_coordinateswill create a filtered copy of the array if you’re using an order > 1 (i.e. not linear interpolation). If you just want to interpolate at a very small number of points, this is a rather large overhead. It doesn’t increase with the number points you want to interpolate at, however. As long as have enough RAM for a single temporary copy of your input data array, you’ll be fine.If you can’t store a copy of your data in memory, you can either a) specify
prefilter=Falseandorder=1and use linear interpolation, or b) replace your original data with a filtered version usingndimage.spline_filter, and then call map_coordinates withprefilter=False.Even if you have enough ram, keeping the filtered dataset around can be a big speedup if you need to call map_coordinates multiple times (e.g. interactive use, etc).