andrea
andrea

Reputation: 161

Which scipy function should I use to interpolate from a rectangular grid to regularly spaced rectangular grid in 2D?

I pretty new to python, and I'm looking for the most efficient pythonic way to interpolate from a grid to another one. The original grid is a structured grid (the terms regular or rectangular grid are also used), and the spacing is not uniform. The new grid is a regularly spaced grid. Both grids are 2D. For now it's ok using a simple interpolation method like linear interpolation, pheraps in the future I could try something different like bicubic.

I'm aware that there are methods to interpolate from an irregular grid to a regular one, however given the regular structure of my original grid, more efficient methods should be available.

After searching in the scipy docs, I have found 2 methods that seem to fit my case: scipy.interpolate.RegularGridInterpolator and scipy.interpolate.RectBivariateSpline. I don't understand the difference between the two functions, which one should I use? Is the difference purely in the interpolation methods? Also, while the non-uniform spacing of the original grid is explicitly mentioned in RegularGridInterpolator, RectBivariateSpline doc is silent about it.

Thanks,

Andrea

Upvotes: 0

Views: 1226

Answers (1)

Jacob Panikulam
Jacob Panikulam

Reputation: 1218

RectBivariateSpline

Imagine your grid as a canyon, where the high values are peaks and the low values are valleys. The bivariate spline is going to try to fit a thin sheet over that canyon to interpolate. This will still work on irregularly spaced input, as long as the x and y array you supply are also irregularly spaced, and everything still lies on a rectangular grid.

RegularGridInterpolator

Same canyon, but now we'll linearly interpolate the surrounding gridpoints to interpolate. We'll assume the input data is regularly spaced to save some computation. It sounds like this won't work for you.

Now What?

Both of these map 2D-1D. It sounds like you have an irregular input space with, but rectangularly spaced sample points, and an output space with regularly spaced sample points. You might just try LinearNDInterpolator, since you're in 2D it won't be that much more expensive.

If you're trying to interpolate a mapping between two 2D things, you'll want to do two interpolations, one that interpolates (x1, y1) -> x2 and one that interpolates (x1, y1) -> y2.

Vstacking the output of those will give you an array of points in your output space.

I don't know of a more efficient method in scipy for taking advantage of the expected structure of the interpolation output, given an irregular grid input.

Upvotes: 2

Related Questions