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 a SafeBSpline instance.

Parameters:
Return type:

SafeBSpline instance.

class SafeBSpline(bspline: BSpline)

B-spline object that prevents extrapolation outside of base domain.

This is a light wrapper around scipy.interpolate.BSpline that 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 BSpline object are available, except for __call__(), which is overridden to add the extrapolation check. Derivative and antiderivative methods are also overridden to return SafeBSpline instances.

Parameters:

bspline – Underlying scipy.interpolate.BSpline object.

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.

property xmax: Any

Maximum x value of the spline definition range.

property xmin: Any

Minimum x value of the spline definition range.