SplineBox
Take control of your splines
SplineBox is an open-source python package for anyone trying to fit splines. It offers a wide varaiety of spline types including Hermite splines and makes it easy to specify custom loss function to control spline properties such as smoothness.
import splinebox
# number of knots
M = 10
basis_function = splinebox.B3()
spline = splinebox.Spline(M, basis_function)
spline.fit(data)
t = np.linspace(0, M - 1, 1000)
vals = spline.eval(t)
Key Features
Intuitive API
Obejct oriented API design allows for easy interaction with and manipulation of splines.
Sensible defaults
Automatic handling of perodicity of closed splines and padding of open splines.
Integer values in parameter space correspond to knots.
Custom loss functions
Compatibility with python optimization frame works makes it easy to use custom loss functions for fitting.
Extensible
Additional basis functions can easily be added using the abstract base class of the basis functions.
High performance
Just-in-time compilation allows us to match and in some cases overcome the performance of scipy's fortran based splines.
Comparison with scipy
Ease of use
A common task in image anlysis is to fit a closed spline of a given order with a
fixed number of knots to a countour. Let's compare how we can achieve this task
in splinebox and scipy.
See full example →
In splinebox all we need to do is select a basis function and specify the number of knots M and that the spline is closed in the spline constructor. The spline can then be fit to the data using its fit method.
import splinebox
M = 10
basis_function = splinebox.B3()
spline = splinebox.Spline(
M, basis_function, closed=True
)
spline.fit(contour)
import scipy.interpolate
M = 10
N = len(data)
k = 3
t = np.arange(-k, M + k + 1) / M * N
u = np.linspace(0, N, N, endpoint=True)
tck, u = scipy.interpolate.splprep(
contour, u=u, k=k, task=-1,
s=0, t=t, per=N
)
Scipy requires you to pre-compute the parameter values for all knots and data points accounting for padding and periodicity of the data. This can be confusion and difficult to do.
For additional examples comparing splinebox to scipy check out our example gallery.
See examples →
Performance
We compare the performance to splinbox to scipy's splines on three main tasks:
- Spline creation give a set of knots
- Evaluation of a spline at a given parameter value
- Data approximation using least-squares fitting