Skip to content

Improve spline efficiency and correct unexpected behavior #609

@ajacoby9

Description

@ajacoby9

Splines are very slow and occasionally have unexpected behavior. To reproduce the error:

import torch
from pina.model import Spline
from scipy.interpolate._bsplines import BSpline
from matplotlib import pyplot as plt

seed = 1999
torch.manual_seed(seed)

x_min, x_max = 0,1
x = torch.linspace(x_min, x_max, 50) 
order = 3
knots = torch.linspace(x_min, x_max, 10)
n_coeffs = len(knots) - order 
control_points = torch.randn(n_coeffs)
print(control_points)
model = Spline(knots=knots, control_points=control_points, order=order)
y = model(x)

spline = BSpline(
    t=knots.detach().numpy(),
    c=control_points.detach().numpy(),
    k=model.order - 1,
)
y_scipy = spline(x).flatten()
y = y.detach().numpy()
plt.plot(x,y, label="PINA", marker="o" )
plt.plot(x,y_scipy, label="SciPy", marker="x")
plt.legend()
plt.show()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions