Splinebox

0.5.1 · active · verified Tue Apr 14

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

Install

Imports

Quickstart

This quickstart demonstrates how to create and visualize a closed cubic B-spline using Splinebox. It defines a set of control points (knots), instantiates a Spline object with a B3 basis function, evaluates the spline along a range of parameter values, and then plots both the knots and the resulting spline curve using Matplotlib.

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()

view raw JSON →