scijit.interpolate.splrep

scijit.interpolate.splrep(x, y, w=None, xb=None, xe=None, k=3, task=0, s=None, t=None, full_output=0, per=0, quiet=1)

Compute the B-spline representation of a 1-D curve.

Parameters:
x1-D array_like of float, length m

Abscissae, strictly increasing.

y1-D array_like of float, length m

Ordinates.

w1-D array_like of float, optional

Positive weights, length m. None (the default) means unit weights AND selects s = 0; supplying w selects s = m - sqrt(2*m) instead.

xb, xefloat, optional

Interval the fit is made over. None (the default) means x[0] and x[-1]. They may only WIDEN the data interval.

kint, optional

Spline degree, 1 <= k <= 5. Default 3.

taskint, optional

0 (default) finds the smoothing spline. -1 finds the weighted least-squares spline for the knots given in t. 1 is not supported; see Deviations.

sfloat, optional

Smoothing factor: the fit satisfies sum(w[i]*(y[i]-spl(x[i])))**2 <= s. None (the default) resolves as described under w.

t1-D array_like of float, optional

INTERIOR knots for task=-1, without the boundary repetitions. Supplying it forces task=-1. t and per are read independently, so the two together give the periodic least-squares fit on those knots.

full_outputbool, optional

Non-zero returns the 4-tuple described below. Must be a compile-time constant inside @njit, since it selects the return type.

perbool, optional

Non-zero fits a periodic spline through percur. y[m-1] and w[m-1] are not used. Any non-zero value behaves as 1.

quietbool, optional

Zero also warns on a SUCCESSFUL fit, reporting k, the allocated knot count, m, fp and s. Default 1, which suppresses it. The ier in {1, 2, 3} warning is issued either way.

Returns:
tcktuple of (t, c, k)

Knot vector, coefficients in FITPACK’s padded form (only the first len(t) - k - 1 are meaningful) and the degree.

fpfloat

Weighted sum of squared residuals. Returned only when full_output is non-zero.

ierint

FITPACK status: -1 interpolating spline, -2 least-squares polynomial, 0 smoothing achieved, 1/2/3 failure, 10 invalid input. Returned only when full_output is non-zero. It is the value FITPACK set, including on the failure paths that raise when full_output is zero.

msgstr

The exit message for the FITPACK status code ier. Returned only when full_output is non-zero, and reads “An error occurred” for any ier outside the table.

Raises:
TypeError

len(w) != m, len(y) != m, k outside 1..5, m <= k, task outside -1..1, task == 1, task == -1 without t, or a FITPACK failure with ier outside {1, 2, 3, 10}.

ValueError

ier == 10, FITPACK’s invalid-input code, when full_output is zero. Without the raise, the empty t/c evaluate to 0.0 through splev: a wrong number and no error.

Warns:
RuntimeWarning

ier in {1, 2, 3} with full_output=0: the fit is returned and the matching _iermess text is warned. With quiet=0, also on ier <= 0, reporting the successful fit. Both are issued through a numba.objmode block, which runs its body in the interpreter, so warnings.catch_warnings and -W see them from compiled and uncompiled callers alike.

See also

scipy.interpolate.splrep

The scipy routine this mirrors.

Notes

  • full_output must be a compile-time constant inside @njit: it selects between a 3-tuple and a 4-tuple return, and numba compiles one return type per specialization. A bool, an int, a string and the default are read while the call compiles; a float, a container and a runtime variable are not, and raise TypingError naming the argument. From Python every object is read by its truthiness.

  • task=1 raises. scipy keeps the previous fit in a function-local _curfit_cache, so its own task=1 raises UnboundLocalError on every call, including immediately after a task=0 call. There is no behaviour to reproduce.

  • A parametric y (2-D, or a list of arrays) is not accepted.

For task=-1 the interior knots are validated before FITPACK sees them, and two codes FITPACK never produces are reported: 30 for knots that are not strictly increasing or not strictly inside (xb, xe), and 50 for knots outside (x[0], x[-1]). Neither is in scipy’s _iermess table, so both surface as TypeError("An error occurred"). Those two checks run only while the data lies inside [xb, xe]; when it does not, fpchec fails first and reports ier = 10. A NaN knot passes both checks. They belong to the non-periodic route: with per non-zero the knots go to percur, which reports a bad knot vector as ier = 10 and ValueError("Error on input data").

A SMOOTHING FIT MAY WARN THAT IT IS RANK DEFICIENT. With a small s on noisy data, FITPACK’s knot search can place knots so that one B-spline coefficient is not determined by the data at all. The curve still passes through the data as asked, and fp reports success, but between the data points it carries an arbitrary component. A UserWarning naming the number of undetermined coefficients is issued when this happens, and a larger s is the fix. scipy issues no warning for it.

prange-safe: yes.

Examples

>>> import numpy as np
>>> from numba import njit
>>> from scijit.interpolate import splrep, splev
>>> x = np.linspace(0, 4, 40)
>>> y = np.sin(x)
>>> @njit
... def fit_and_eval(x, y, q):
...     tck = splrep(x, y)
...     return splev(q, tck)
>>> float(fit_and_eval(x, y, np.array([1.5]))[0])
0.9974947337577743