noobermin
noobermin

Reputation: 203

Is there a scipy/numpy method to get the indices for nearest interpolation?

I essentially have a set of large (GBs) set of data with points that I need to do nearest interpolation on. Essentially, it is a scalar/vector field that is a function of time. I am aware of scipy.interpolate and all it's goodness, and I've been using it. However, I realized that the spatial structure of the data is constant in time (Eulerian), so once I figure out which indices correspond to this or that point for interpolation for, say, the first time step, I could just obtain those indices, and the same indices should be able to create the same shaped, interpolated data for other times. Essentially, it saves the step of re-interpolating the data again and again.

I am willing to do this myself, but I wanted to check if there already exists a method to do this that may be not in scipy but elsewhere. scipy.interpolate doesn't have anything ready-made that looks like it can do this. In the event there are no answers, I'll post my solution.

One final note, the grid is Eulerian but not regular and has possible repeats, so nearest interpolation is needed. I can (and have) done a lexical sort on the spatial points, and this will aid in a quick look up.

Upvotes: 4

Views: 1228

Answers (1)

ali_m
ali_m

Reputation: 74252

k-D trees are commonly used to partition multidimensional data in order to perform fast nearest-neighbour searches.

For example, scipy.interpolate.NearestNDInterpolator is essentially just a wrapper around scipy.spatial.cKDTree (see the source code here). cKDTree.query will return the nearest-neighbour indices as well as the corresponding distances for a given set of input coordinates.

Upvotes: 3

Related Questions