Interpolation¶
Module for safe interpolations.
Safe splines¶
Scipy does not provide a way to create “safe” splines that raise errors when evaluated outside their definition range. This module provides light wrappers to achieve this.
Simply use make_safe_interp_spline() instead of
scipy.interpolate.make_interp_spline() to create such splines.
from l2orbits.interp import make_safe_interp_spline
# Create a safe spline
spline = make_safe_interp_spline(x, y, k=3)
# Evaluate the spline (raises if x_eval is outside the definition range)
y_eval = spline(x_eval)
The returned SafeBSpline object behaves like a normal
scipy.interpolate.BSpline, so that all its methods and attributes are
available.
# Derivative of the spline
spline_deriv = spline.derivative()
# Antiderivative of the spline
spline_anti = spline.antiderivative()
- make_safe_interp_spline(*args: Any, **kwargs: Any) SafeBSpline¶
Create a safe B-spline interpolation.
This is a wrapper around
scipy.interpolate.make_interp_spline()that returns aSafeBSplineinstance.- Parameters:
*args – Positional arguments passed to
make_interp_spline().**kwargs – Keyword arguments passed to
make_interp_spline().
- Return type:
SafeBSplineinstance.
- class SafeBSpline(bspline: BSpline)¶
B-spline object that prevents extrapolation outside of base domain.
This is a light wrapper around
scipy.interpolate.BSplinethat raises an error when evaluated outside of its definition range (defined by the spline knots).This is a passthrough class, so that all attributes and methods of the underlying
BSplineobject are available, except for__call__(), which is overridden to add the extrapolation check. Derivative and antiderivative methods are also overridden to returnSafeBSplineinstances.- Parameters:
bspline – Underlying
scipy.interpolate.BSplineobject.
- antiderivative(nu: int = 1) SafeBSpline¶
Return a SafeBSpline for the nu-th antiderivative.
- Parameters:
nu – Order of antiderivative.
- derivative(nu: int = 1) SafeBSpline¶
Return a SafeBSpline for the nu-th derivative.
- Parameters:
nu – Order of derivative.