interpolated_coordinates.utils.spline
¶
scipy
Splines, with Units¶
A module for scipy splines classes with units
support.
scipy
[scipy], [Dierckx] splines do not support Quantity
because
they do not understand Unit
. A standard workaround solution when one needs
to interpolate is to strip quantities of their units, apply the interpolation,
then add units back.
As an example:
>>> import numpy as np
>>> import astropy.units as u
>>> x = np.linspace(-3, 3, 50) * u.s
>>> y = 8 * u.m / (x.value**2 + 4)
>>> xs = np.linspace(-2, 2, 10) * u.s # for evaluating spline
>>> from scipy.interpolate import InterpolatedUnivariateSpline
>>> spl = InterpolatedUnivariateSpline(x.to_value(u.s), y.to_value(u.m))
>>> spl(xs.to_value(u.s)) * u.m # evaluate, adding back units
<Quantity [1.00000009, 1.24615404, 1.52830261, 1.79999996, 1.97560874,
1.97560874, 1.79999996, 1.52830261, 1.24615404, 1.00000009] m>
This is fine, but a bit of a hassle. Instead, we can wrap the unit stripping / adding process into a unit-aware version of the spline interpolation classes.
The same example as above, but with the new class:
>>> from interpolated_coordinates.utils import InterpolatedUnivariateSplinewithUnits
>>> spl = InterpolatedUnivariateSplinewithUnits(x, y)
>>> spl(xs)
<Quantity [1.00000009, 1.24615404, 1.52830261, 1.79999996, 1.97560874,
1.97560874, 1.79999996, 1.52830261, 1.24615404, 1.00000009] m>
Using InterpolatedUnivariateSplinewithUnits
,
interpolation with ndarray
AND Quantity
inputs just work.
Plotting this example:
(Source code
, png
, hires.png
, pdf
)
References¶
Paul Dierckx, Curve and Surface Fitting with Splines, Oxford University Press, 1993
Virtanen, P., Gommers, R., Oliphant, M., Reddy, T., Cournapeau, E., Peterson, P., Weckesser, J., Walt, M., Wilson, J., Millman, N., Nelson, A., Jones, R., Larson, E., Carey, ., Feng, Y., Moore, J., Laxalde, D., Perktold, R., Henriksen, I., Quintero, C., Archibald, A., Pedregosa, P., & SciPy 1.0 Contributors (2020). SciPy 1.0: Fundamental Algorithms for Scientific Computing in Python. Nature Methods, 17, 261-272.
Reference/API¶
interpolated_coordinates.utils.spline Module¶
Classes¶
|
1-D smoothing spline fit to a given set of data points. |
1-D interpolating spline for a given set of data points, with units. |
|
|
1-D spline with explicit internal knots. |