henry
henry

Reputation: 965

Interpolate values at 3D coordinates

GOAL

I have values v given at specific 3D coordinates x y z. The data is stored as a pandas dataframe:

          x      y       z         v
0     -68.5  68.50  -10.00  0.297845
1     -68.5 -23.29   61.10  0.148683
2     -68.5 -23.29   63.47  0.142325
3     -68.5 -23.29   65.84  0.135908
4     -68.5 -23.29   68.21  0.129365
    ...    ...     ...       ...
91804  68.5  23.29  151.16  0.118460
91805  68.5  23.29  153.53  0.119462
91806  68.5  23.29  155.90  0.120386
91807  68.5  23.29  139.31  0.112257
91808  68.5 -68.50  227.00  0.127948

I would like to find the values at new coordinates that are not part of the dataframe, hence I am looking into how to efficiently interpolate the data.

WHAT I HAVE TRIED

I am using an interpolation function from scipy, named interpn:

from scipy.interpolate import interpn

# Put data into shape that is accepted by interpn
# I guess the error is somewhere here... 
xs = np.array(df["x"].to_list())
ys = np.array(df["y"].to_list())
zs = np.array(df["z"].to_list())
vs = np.array(df["v"].to_list())
original_points = (xs, ys, zs)
original_values = vs.reshape(len(np.unique(xs)), len(np.unique(ys)), len(np.unique(zs)))

# Point at which I would like to retrive the interpolated value
new_point = np.array([0,0,0])

# perform interpolation
new_value = interpn(original_points, original_values, new_point)

ISSUE

But I am getting the following error:

ValueError: The points in dimension 0 must be strictly ascending

I don't understand that, because if I plot xs, it looks like it is ascending:

enter image description here

Upvotes: 1

Views: 1317

Answers (1)

Saswata Chakravarty
Saswata Chakravarty

Reputation: 464

Assuming your data is on a regular grid the xs, ys, and zs need to be unique. Try something like -

xs, ind = np.unique(xs, return_index=True)
xs = xs[np.argsort(ind)]

As an alternative, you can use the interpolators on unstructured data - https://docs.scipy.org/doc/scipy/reference/interpolate.html#multivariate-interpolation.

Upvotes: 1

Related Questions