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
or Poetry:
poetry add csaps
The module depends only on NumPy and SciPy. Python 3.10 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.
Development
We use Poetry to manage the project:
git clone https://github.com/espdev/csaps.git
cd csaps
poetry install -E docs
Also, install pre-commit hooks:
poetry run pre-commit install
Testing and Linting
We use pytest for testing and ruff/mypy for linting.
Use poethepoet
to run tests and linters:
poetry run poe test
poetry run poe check
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
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
File details
Details for the file csaps-1.3.2.tar.gz
.
File metadata
- Download URL: csaps-1.3.2.tar.gz
- Upload date:
- Size: 16.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
234923a3a22737d342ee33f6afb8316c0c4d315855f4b3f9dcf65d24be7536c5
|
|
MD5 |
1cb94a7dd61fbd2ec1454bebc5f94745
|
|
BLAKE2b-256 |
3c5ef387a955f6392d13efd2167e7723e2b9afb606de5ff9e7450ae5dafed797
|
Provenance
The following attestation bundles were made for csaps-1.3.2.tar.gz
:
Publisher:
release.yaml
on espdev/csaps
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1
-
Predicate type:
https://docs.pypi.org/attestations/publish/v1
-
Subject name:
csaps-1.3.2.tar.gz
-
Subject digest:
234923a3a22737d342ee33f6afb8316c0c4d315855f4b3f9dcf65d24be7536c5
- Sigstore transparency entry: 197639855
- Sigstore integration time:
-
Permalink:
espdev/csaps@0522ebd6ee167d7bf99bc3b5ab91b96a8dfa4014
-
Branch / Tag:
refs/heads/master
- Owner: https://github.com/espdev
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com
-
Runner Environment:
github-hosted
-
Publication workflow:
release.yaml@0522ebd6ee167d7bf99bc3b5ab91b96a8dfa4014
-
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file csaps-1.3.2-py3-none-any.whl
.
File metadata
- Download URL: csaps-1.3.2-py3-none-any.whl
- Upload date:
- Size: 19.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
0184705ac145ece0e8229117b7b37ea69981b9238107a9b7f4ec1b16ca22d1a4
|
|
MD5 |
51631283a8d2583d0bb86b61684b29c2
|
|
BLAKE2b-256 |
54ea06fd3ca5b21a9208650cb0d59359027e50d64164da53d7bc676e4d012287
|
Provenance
The following attestation bundles were made for csaps-1.3.2-py3-none-any.whl
:
Publisher:
release.yaml
on espdev/csaps
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1
-
Predicate type:
https://docs.pypi.org/attestations/publish/v1
-
Subject name:
csaps-1.3.2-py3-none-any.whl
-
Subject digest:
0184705ac145ece0e8229117b7b37ea69981b9238107a9b7f4ec1b16ca22d1a4
- Sigstore transparency entry: 197639859
- Sigstore integration time:
-
Permalink:
espdev/csaps@0522ebd6ee167d7bf99bc3b5ab91b96a8dfa4014
-
Branch / Tag:
refs/heads/master
- Owner: https://github.com/espdev
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com
-
Runner Environment:
github-hosted
-
Publication workflow:
release.yaml@0522ebd6ee167d7bf99bc3b5ab91b96a8dfa4014
-
Trigger Event:
workflow_dispatch
-
Statement type: