Reputation: 129
I am converting a matlab script to a python script where I use the function scatteredInterpolant(). In matlab it has the nice property that it creates an interpolant that I can evaluate at few selected points a lot faster than creating the interpolated griddata over the whole domain. From the matlab manual it says:
% Fast to create interpolant F and evaluate multiple times
F = scatteredInterpolant(X,Y,V)
v1 = F(Xq1,Yq1)
v2 = F(Xq2,Yq2)
% Slower to compute interpolations separately using griddata
v1 = griddata(X,Y,V,Xq1,Yq1)
v2 = griddata(X,Y,V,Xq2,Yq2)
So far I have only found a python alternative to the latter approach and it takes approximatly the same time no matter how many points i evaluate the interpolant at:
import numpy as np
from scipy.interpolate import griddata
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
# example surface
[theta, phi] = np.meshgrid(
np.linspace(0, 2.0 * np.pi, 100),
np.linspace(np.spacing(1), np.pi/2-np.pi/20, 100))
x = 1.0*np.multiply(np.cos(theta), np.sin(phi))
y = 1.0*np.multiply(np.sin(theta), np.sin(phi))
z = 1.0 * np.cos(phi)
[xi, yi] = np.meshgrid(np.linspace(-1, 1, 10),
np.linspace(-1, 1, 10))
%timeit zi = griddata((np.reshape(x, -1), np.reshape(y, -1)), np.reshape(z, -1), (xi, yi), method='linear')
%timeit zi_singlepoint = griddata((np.reshape(x, -1), np.reshape(y, -1)), np.reshape(z, -1), (0.0, 0.0), method='linear')
Is it possible to get an interpolant that can be evaluated as in matlab using the griddata() function in scipy or other module in python?
EDIT: I just found that functions based on scipy.interpolate.BivariateSpline works in a similar fasion to matlabs scatteredinterpolant(). From the documentation: "The BivariateSpline class is the 2-dimensional analog of the UnivariateSpline class. It and its subclasses implement the FITPACK functions described above in an object oriented fashion, allowing objects to be instantiated that can be called to compute the spline value by passing in the two coordinates as the two arguments." I will try this out and get back.
Upvotes: 1
Views: 178
Reputation: 129
Some of the interpolations methods that can be called and evaluated with given coordinates are:
scipy.interpolate.SmoothBivariateSpline
scipy.interpolate.Rbf
scipy.interpolate.NearestNDInterpolator
scipy.interpolate.LinearNDInterpolator
scipy.interpolate.CloughTocher2DInterpolator
Upvotes: 0