| 1 | <p align="center"> |
| 2 | <a href="https://github.com/espdev/csaps"><img src="https://user-images.githubusercontent.com/1299189/76571441-8d97e400-64c8-11ea-8c05-58850f8311a1.png" alt="csaps" width="400" /></a><br> |
| 3 | </p> |
| 4 | |
| 5 | <p align="center"> |
| 6 | <a href="https://pypi.python.org/pypi/csaps"><img src="https://img.shields.io/pypi/v/csaps.svg" alt="PyPI version" /></a> |
| 7 | <a href="https://pypi.python.org/pypi/csaps"><img src="https://img.shields.io/pypi/pyversions/csaps.svg" alt="Supported Python versions" /></a> |
| 8 | <a href="https://github.com/espdev/csaps"><img src="https://github.com/espdev/csaps/workflows/main/badge.svg" alt="GitHub Actions (Tests)" /></a> |
| 9 | <a href="https://csaps.readthedocs.io/en/latest/?badge=latest"><img src="https://readthedocs.org/projects/csaps/badge/?version=latest" alt="Documentation Status" /></a> |
| 10 | <a href="https://coveralls.io/github/espdev/csaps?branch=master"><img src="https://coveralls.io/repos/github/espdev/csaps/badge.svg?branch=master" alt="Coverage Status" /></a> |
| 11 | <a href="https://choosealicense.com/licenses/mit/"><img src="https://img.shields.io/pypi/l/csaps.svg" alt="License" /></a> |
| 12 | </p> |
| 13 | |
| 14 | **csaps** is a Python package for univariate, multivariate and n-dimensional grid data approximation using cubic smoothing splines. |
| 15 | The package can be useful in practical engineering tasks for data approximation and smoothing. |
| 16 | |
| 17 | ## Installing |
| 18 | |
| 19 | Use pip for installing: |
| 20 | |
| 21 | ``` |
| 22 | pip install -U csaps |
| 23 | ``` |
| 24 | |
| 25 | or Poetry: |
| 26 | |
| 27 | ``` |
| 28 | poetry add csaps |
| 29 | ``` |
| 30 | |
| 31 | The module depends only on NumPy and SciPy. Python 3.10 or above is supported. |
| 32 | |
| 33 | ## Simple Examples |
| 34 | |
| 35 | Here is a couple of examples of smoothing data. |
| 36 | |
| 37 | An univariate data smoothing: |
| 38 | |
| 39 | ```python |
| 40 | import numpy as np |
| 41 | import matplotlib.pyplot as plt |
| 42 | |
| 43 | from csaps import csaps |
| 44 | |
| 45 | np.random.seed(1234) |
| 46 | |
| 47 | x = np.linspace(-5., 5., 25) |
| 48 | y = np.exp(-(x/2.5)**2) + (np.random.rand(25) - 0.2) * 0.3 |
| 49 | xs = np.linspace(x[0], x[-1], 150) |
| 50 | |
| 51 | ys = csaps(x, y, xs, smooth=0.85) |
| 52 | |
| 53 | plt.plot(x, y, 'o', xs, ys, '-') |
| 54 | plt.show() |
| 55 | ``` |
| 56 | |
| 57 | <p align="center"> |
| 58 | <img src="https://user-images.githubusercontent.com/1299189/72231304-cd774380-35cb-11ea-821d-d5662cc1eedf.png" alt="univariate" /> |
| 59 | <p/> |
| 60 | |
| 61 | A surface data smoothing: |
| 62 | |
| 63 | ```python |
| 64 | import numpy as np |
| 65 | import matplotlib.pyplot as plt |
| 66 | from mpl_toolkits.mplot3d import Axes3D |
| 67 | |
| 68 | from csaps import csaps |
| 69 | |
| 70 | np.random.seed(1234) |
| 71 | xdata = [np.linspace(-3, 3, 41), np.linspace(-3.5, 3.5, 31)] |
| 72 | i, j = np.meshgrid(*xdata, indexing='ij') |
| 73 | ydata = (3 * (1 - j)**2. * np.exp(-(j**2) - (i + 1)**2) |
| 74 | - 10 * (j / 5 - j**3 - i**5) * np.exp(-j**2 - i**2) |
| 75 | - 1 / 3 * np.exp(-(j + 1)**2 - i**2)) |
| 76 | ydata = ydata + (np.random.randn(*ydata.shape) * 0.75) |
| 77 | |
| 78 | ydata_s = csaps(xdata, ydata, xdata, smooth=0.988) |
| 79 | |
| 80 | fig = plt.figure(figsize=(7, 4.5)) |
| 81 | ax = fig.add_subplot(111, projection='3d') |
| 82 | ax.set_facecolor('none') |
| 83 | c = [s['color'] for s in plt.rcParams['axes.prop_cycle']] |
| 84 | ax.plot_wireframe(j, i, ydata, linewidths=0.5, color=c[0], alpha=0.5) |
| 85 | ax.scatter(j, i, ydata, s=10, c=c[0], alpha=0.5) |
| 86 | ax.plot_surface(j, i, ydata_s, color=c[1], linewidth=0, alpha=1.0) |
| 87 | ax.view_init(elev=9., azim=290) |
| 88 | |
| 89 | plt.show() |
| 90 | ``` |
| 91 | |
| 92 | <p align="center"> |
| 93 | <img src="https://user-images.githubusercontent.com/1299189/72231252-7a9d8c00-35cb-11ea-8890-487b8a7dbd1d.png" alt="surface" /> |
| 94 | <p/> |
| 95 | |
| 96 | ## Documentation |
| 97 | |
| 98 | More examples of usage and the full documentation can be found at https://csaps.readthedocs.io. |
| 99 | |
| 100 | ## Development |
| 101 | |
| 102 | We use Poetry to manage the project: |
| 103 | |
| 104 | ``` |
| 105 | git clone https://github.com/espdev/csaps.git |
| 106 | cd csaps |
| 107 | poetry install -E docs |
| 108 | ``` |
| 109 | |
| 110 | Also, install pre-commit hooks: |
| 111 | |
| 112 | ``` |
| 113 | poetry run pre-commit install |
| 114 | ``` |
| 115 | |
| 116 | ## Testing and Linting |
| 117 | |
| 118 | We use pytest for testing and ruff/mypy for linting. |
| 119 | Use `poethepoet` to run tests and linters: |
| 120 | |
| 121 | ``` |
| 122 | poetry run poe test |
| 123 | poetry run poe check |
| 124 | ``` |
| 125 | |
| 126 | ## Algorithm and Implementation |
| 127 | |
| 128 | **csaps** Python package is inspired by MATLAB [CSAPS](https://www.mathworks.com/help/curvefit/csaps.html) function that is an implementation of |
| 129 | Fortran routine SMOOTH from [PGS](http://pages.cs.wisc.edu/~deboor/pgs/) (originally written by Carl de Boor). |
| 130 | |
| 131 | Also, the algothithm implementation in other languages: |
| 132 | |
| 133 | * [csaps-rs](https://github.com/espdev/csaps-rs) Rust ndarray/sprs based implementation |
| 134 | * [csaps-cpp](https://github.com/espdev/csaps-cpp) C++11 Eigen based implementation (incomplete) |
| 135 | |
| 136 | ## References |
| 137 | |
| 138 | C. de Boor, A Practical Guide to Splines, Springer-Verlag, 1978. |
| 139 | |
| 140 | ## License |
| 141 | |
| 142 | [MIT](https://choosealicense.com/licenses/mit/) |