def __get_2d_function(x, y, z, kind):
"""
Train 2-D interpolator
:param x: x-coordinates of data points
:param y: y-coordinates of data points
:param z: z-coordinates of data points
:param kind: 'linear' or 'spline' interpolation type
:return Interpolator callable object
"""
try:
from scipy.interpolate import RectBivariateSpline
except ImportError as e:
logger.error("Please install scipy on your platform to be able to use spline-based interpolation")
raise e
if len(x) == 2: # fall-back to linear interpolation
kx = 1
elif len(x) == 3: # fall-back to 2nd degree spline
kx = 2
else:
kx = 3
if len(y) == 2: # fall-back to linear interpolation
ky = 1
elif len(y) == 3: # fall-back to 2nd degree spline
ky = 2
else:
ky = 3
if kind == 'linear':
kx, ky = 1, 1
x_array, y_array, z_array = x, y, z
if not isinstance(x, np.ndarray):
x_array = np.asarray(x)
if not isinstance(y, np.ndarray):
y_array = np.asarray(y)
if not isinstance(z, np.ndarray):
z_array = np.asarray(z)
result = RectBivariateSpline(x_array, y_array, z_array, kx=kx, ky=ky, s=0)
return result
评论列表
文章目录