Splinebox
Splinebox is an open-source Python package for fitting splines. It offers a wide variety of spline types, including Hermite splines, and makes it easy to specify custom loss functions to control spline properties such as smoothness. Currently at version 0.5.1, the library is actively developed with a steady release cadence.
Warnings
- deprecated The `eval` method on `Spline` and `HermiteSpline` classes is deprecated. Direct calling of the spline object (`spline(t)`) should be used instead.
- breaking A future warning indicates an upcoming change in the 'squeeze' policy for outputs. This means the default behavior for reshaping single-dimensional outputs might change, potentially affecting code that implicitly relies on the current squeezing behavior.
- gotcha When using `moving_frame` for unsorted arrays of parameters, versions prior to 0.5.1 could produce incorrect results. This was fixed in v0.5.1.
- gotcha Splinebox automatically handles periodicity and padding for closed splines. This differs from `scipy.interpolate.splprep`, which requires manual pre-computation of parameter values for knots and data points, accounting for padding and periodicity.
Install
-
pip install splinebox
Imports
- splinebox
import splinebox
- Spline
from splinebox.spline_curves import Spline
- B3
from splinebox.basis_functions import B3
Quickstart
import splinebox
import numpy as np
import matplotlib.pyplot as plt
# Define the number of knots and the basis function
n_knots = 4
basis_function = splinebox.basis_functions.B3()
# Create a closed cubic B-spline with initial knots
spline = splinebox.spline_curves.Spline(M=n_knots, basis_function=basis_function, closed=True)
spline.knots = np.array([[1, 2], [3, 2], [4, 3], [1, 1]])
# Evaluate the spline at parameter values
t = np.linspace(0, n_knots, 100) # Parameter values along the spline
vals = spline(t, derivative=0) # Get the spline points
# Plot the spline and its knots
plt.figure(figsize=(6, 6))
plt.scatter(spline.knots[:, 0], spline.knots[:, 1], color='red', marker='o', label='Knots')
plt.plot(vals[:, 0], vals[:, 1], color='blue', label='Spline Curve')
plt.title('Splinebox Quickstart Example')
plt.xlabel('X-coordinate')
plt.ylabel('Y-coordinate')
plt.grid(True)
plt.legend()
plt.axis('equal')
plt.show()