Cubic spline approximation (smoothing)
Project description
csaps is a Python package for univariate, multivariate and n-dimensional grid data approximation using cubic smoothing splines. The package can be useful in practical engineering tasks for data approximation and smoothing.
Installing
Use pip for installing:
pip install -U csaps
The module depends only on NumPy and SciPy. Python 3.6 or above is supported.
Simple Examples
Here is a couple of examples of smoothing data.
An univariate data smoothing:
import numpy as np
import matplotlib.pyplot as plt
from csaps import csaps
np.random.seed(1234)
x = np.linspace(-5., 5., 25)
y = np.exp(-(x/2.5)**2) + (np.random.rand(25) - 0.2) * 0.3
xs = np.linspace(x[0], x[-1], 150)
ys = csaps(x, y, xs, smooth=0.85)
plt.plot(x, y, 'o', xs, ys, '-')
plt.show()
A surface data smoothing:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from csaps import csaps
np.random.seed(1234)
xdata = [np.linspace(-3, 3, 41), np.linspace(-3.5, 3.5, 31)]
i, j = np.meshgrid(*xdata, indexing='ij')
ydata = (3 * (1 - j)**2. * np.exp(-(j**2) - (i + 1)**2)
- 10 * (j / 5 - j**3 - i**5) * np.exp(-j**2 - i**2)
- 1 / 3 * np.exp(-(j + 1)**2 - i**2))
ydata = ydata + (np.random.randn(*ydata.shape) * 0.75)
ydata_s = csaps(xdata, ydata, xdata, smooth=0.988)
fig = plt.figure(figsize=(7, 4.5))
ax = fig.add_subplot(111, projection='3d')
ax.set_facecolor('none')
c = [s['color'] for s in plt.rcParams['axes.prop_cycle']]
ax.plot_wireframe(j, i, ydata, linewidths=0.5, color=c[0], alpha=0.5)
ax.scatter(j, i, ydata, s=10, c=c[0], alpha=0.5)
ax.plot_surface(j, i, ydata_s, color=c[1], linewidth=0, alpha=1.0)
ax.view_init(elev=9., azim=290)
plt.show()
Documentation
More examples of usage and the full documentation can be found at https://csaps.readthedocs.io.
Testing
pytest, tox and Travis CI are used for testing. Please see tests.
Algorithm and Implementation
csaps Python package is inspired by MATLAB CSAPS function that is an implementation of Fortran routine SMOOTH from PGS (originally written by Carl de Boor).
Also the algothithm implementation in other languages:
- csaps-rs Rust ndarray/sprs based implementation
- csaps-cpp C++11 Eigen based implementation (incomplete)
References
C. de Boor, A Practical Guide to Splines, Springer-Verlag, 1978.
License
Changelog
v1.0.2 (19.07.2020)
- Fix using 'nu' argument when n-d grid spline evaluating #32
v1.0.1 (19.07.2020)
- Fix n-d grid spline evaluating performance regression #31
v1.0.0 (11.07.2020)
- Use
PPolyandNdPPolybase classes from SciPy interpolate module forSplinePPFormandNdGridSplinePPFormrespectively. - Remove deprecated classes
UnivariateCubicSmoothingSplineandMultivariateCubicSmoothingSpline - Update the documentation
Notes
In this release the spline representation (the array of spline coefficients) has been changed
according to PPoly/NdPPoly.
See SciPy PPoly
and NdPPoly documentation for details.
v0.11.0 (28.03.2020)
- Internal re-design
SplinePPFormandNdGridSplinePPFormclasses #17:- Remove
shapeandaxisproperties and reshaping data in these classes NdGridSplinePPFormcoefficients array for 1D grid now is 1-d instead of 2-d
- Remove
- Refactoring the code and decrease memory consumption
- Add
overloadtype-hints forcsapsfunction signatures
v0.10.1 (19.03.2020)
- Fix call of
numpy.padfunction for numpy <1.17 #15
v0.10.0 (18.02.2020)
- Significant performance improvements for make/evaluate splines and memory consumption optimization
- Change format for storing spline coefficients (reshape coeffs array) to improve performance
- Add shape property to
SplinePPForm/NdGridSplinePPFormand axis property toSplinePPForm - Fix issues with the smoothing factor in nd-grid case: inverted ordering and unnable to use 0.0 value
- Update documentation
v0.9.0 (21.01.2020)
- Drop support of Python 3.5
weights,smoothandaxisarguments incsapsfunction are keyword-only nowUnivariateCubicSmoothingSplineandMultivariateCubicSmoothingSplineclasses are deprecated and will be removed in 1.0.0 version. UseCubicSmoothingSplineinstead.
v0.8.0 (13.01.2020)
- Add
csapsfunction that can be used as the main API - Refactor the internal structure of the package
- Add the documentation
Attention
This is the last version that supports Python 3.5. The next versions will support Python 3.6 or above.
v0.7.0 (19.09.2019)
- Add Generic-based type-hints and mypy-compatibility
v0.6.1 (13.09.2019)
- A slight refactoring and extra data copies removing
v0.6.0 (12.09.2019)
- Add "axis" parameter for univariate/multivariate cases
v0.5.0 (10.06.2019)
- Reorganize the project to package-based structure
- Add the interface class for all smoothing spline classes
v0.4.2 (07.09.2019)
- FIX: "smooth" value is 0.0 was not used
v0.4.1 (30.05.2019)
- First PyPI release
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file csaps-1.0.2.tar.gz.
File metadata
- Download URL: csaps-1.0.2.tar.gz
- Upload date:
- Size: 17.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/43.0.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
520c111d4062e5cd60c7c61ca1e0a54ceee671a5e5f837ee1d8aedc5ff8c33e9
|
|
| MD5 |
885460a100a96cfefc4356b1a01ffcc8
|
|
| BLAKE2b-256 |
073c91ac71d6b2d3c04110e1e8f81c3f4542a68d24e89d22c871847a025cc7b1
|
File details
Details for the file csaps-1.0.2-py3-none-any.whl.
File metadata
- Download URL: csaps-1.0.2-py3-none-any.whl
- Upload date:
- Size: 17.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/43.0.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4473f4aa4b0e8ca5677ae62841e92530a2071c32a13aa3ef45394823a149f883
|
|
| MD5 |
2af77fbd8a0c0a266dca6c60cec2e8fc
|
|
| BLAKE2b-256 |
d7bf074ac15b5e509268f6a202ff7ca314184bf394d5af76574b444917a320d3
|