Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add documentation #11

Merged
merged 57 commits into from
Jan 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
1c21042
Docs initial commit
espdev Jan 1, 2020
0ab9921
Setup docs conf
espdev Jan 1, 2020
3d642c5
Update description
espdev Jan 1, 2020
edfb9b3
Update index
espdev Jan 1, 2020
b19f42a
Add extras_require to setup.py
espdev Jan 1, 2020
73a7f2b
Add read the docs configuration file
espdev Jan 1, 2020
f3408fd
Add docs badge to readme
espdev Jan 1, 2020
53421b9
Set version specifier for Sphinx
espdev Jan 1, 2020
2d5d8b5
Change docs badge position
espdev Jan 1, 2020
6d3361b
Update index
espdev Jan 1, 2020
1b61be1
Update docs config for mpl plot directive
espdev Jan 2, 2020
42ffb5f
Remove req-file
espdev Jan 2, 2020
bf0fc86
Add quickstart docs
espdev Jan 2, 2020
a99e531
Update quickstart
espdev Jan 2, 2020
1a537c0
Add formulation.rst
espdev Jan 2, 2020
c74ce86
Update formulation.rst
espdev Jan 2, 2020
90b2b0b
Update formulation.rst
espdev Jan 2, 2020
7df2834
Update quickstart.rst
espdev Jan 2, 2020
7f7fb66
Use jupyter-execute sphinx extension for code/plot examples
espdev Jan 2, 2020
261168b
Update formulation.rst
espdev Jan 2, 2020
e5ea522
Rename quickstart.rst to manual.rst
espdev Jan 3, 2020
ab0f837
Rename section name
espdev Jan 3, 2020
de29a79
Fix see also refs
espdev Jan 3, 2020
55a3ff1
Update docstring formatting
espdev Jan 3, 2020
e22b9f5
Add info about axis parameter to manual.rst
espdev Jan 3, 2020
7e970d2
Add autosummary ext
espdev Jan 3, 2020
963d514
Add autodoc api.rst
espdev Jan 3, 2020
8f7dfd9
Add a simple example in index.rst
espdev Jan 3, 2020
32a1b7f
Add a link to documentation to project_urls
espdev Jan 3, 2020
dc2a02b
Update formulation.rst
espdev Jan 5, 2020
f1dd23a
Update manual.rst
espdev Jan 5, 2020
da06a46
Update manual.rst
espdev Jan 5, 2020
5bf8bf5
Update manual.rst
espdev Jan 5, 2020
bc0618f
Update manual.rst
espdev Jan 5, 2020
c05147d
Update formulation.rst
espdev Jan 5, 2020
39d4de5
Update manual.rst
espdev Jan 5, 2020
e18e834
Update formulation.rst
espdev Jan 5, 2020
d3e8880
Remove outdated info
espdev Jan 9, 2020
51a3d1a
Update readme
espdev Jan 9, 2020
97cb648
Update readme
espdev Jan 9, 2020
d4ef2fd
Update readme
espdev Jan 9, 2020
3d92e25
Update readme
espdev Jan 9, 2020
c15f746
Capitalize headers
espdev Jan 9, 2020
6d814df
Add basic benchmark info
espdev Jan 9, 2020
087c872
Structuring benchmarks
espdev Jan 9, 2020
7a4cf51
Update description
espdev Jan 9, 2020
5d44cea
Update and cleanup docs
espdev Jan 11, 2020
e6cef5b
Add changelog to docs
espdev Jan 11, 2020
64b638f
Update changelog
espdev Jan 11, 2020
16d5359
Add changelog to long description in setup.py
espdev Jan 11, 2020
dac6a7c
Add csaps module to index
espdev Jan 11, 2020
5d6dae9
setup docs theme
espdev Jan 11, 2020
930c97e
Update readme
espdev Jan 11, 2020
d59aa48
Add attention about Python 3.5 support
espdev Jan 11, 2020
bd32d1b
Use matplotlib plot_directive instead of jupyter-sphinx for faster do…
espdev Jan 13, 2020
cbd7255
Update manual
espdev Jan 13, 2020
e74438e
Remove benchmarks info
espdev Jan 13, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions .readthedocs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# .readthedocs.yml
# Read the Docs configuration file
# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details

# Required
version: 2

# Build documentation in the docs/ directory with Sphinx
sphinx:
builder: html
configuration: docs/conf.py

# Optionally build your docs in additional formats such as PDF and ePub
formats:
- pdf

# Optionally set the version of Python and requirements required to build your docs
python:
version: 3.7
install:
- method: pip
path: .
extra_requirements:
- docs
25 changes: 19 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,37 @@
# v0.7.0
# Changelog

## v0.8.0.dev1

* Add `csaps` function that can be used as the main API
* Refactor the internal structure of the package
* Add the [documentation](https://csaps.readthedocs.io)

**Attention**

This is the last version that supports Python 3.5.
The next versions will support Python 3.6 or above.

## v0.7.0

* Add Generic-based type-hints and mypy-compatibility

# v0.6.1
## v0.6.1

* A slight refactoring and extra data copies removing

# v0.6.0
## v0.6.0

* Add "axis" parameter for univariate/multivariate cases

# v0.5.0
## v0.5.0

* Reorganize the project to package-based structure
* Add the interface class for all smoothing spline classes

# v0.4.2
## v0.4.2

* FIX: "smooth" value is 0.0 was not used

# v0.4.1
## v0.4.1

* First PyPI release
190 changes: 23 additions & 167 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,206 +1,59 @@
# CSAPS: Cubic spline approximation (smoothing)

[![PyPI version](https://img.shields.io/pypi/v/csaps.svg)](https://pypi.python.org/pypi/csaps)
[![Documentation Status](https://readthedocs.org/projects/csaps/badge/?version=latest)](https://csaps.readthedocs.io/en/latest/?badge=latest)
[![Build status](https://travis-ci.org/espdev/csaps.svg?branch=master)](https://travis-ci.org/espdev/csaps)
[![Coverage Status](https://coveralls.io/repos/github/espdev/csaps/badge.svg?branch=master)](https://coveralls.io/github/espdev/csaps?branch=master)
![Supported Python versions](https://img.shields.io/pypi/pyversions/csaps.svg)
[![License](https://img.shields.io/pypi/l/csaps.svg)](LICENSE)

This module provides cubic smoothing spline for univariate/multivariate/gridded data approximation.
The smoothing parameter can be calculated automatically or it can be set manually.

The smoothing parameter should be in range `[0, 1]` where bounds are:
* 0: The smoothing spline is the least-squares straight line fit to the data
* 1: The natural cubic spline interpolant

The calculation of the smoothing spline requires the solution of a linear system
whose coefficient matrix has the form `p*A + (1 - p)*B`, with the matrices `A` and `B`
depending on the data sites `X`. The automatically computed smoothing parameter makes
`p*trace(A) equal (1 - p)*trace(B)`.
**csaps** is a package for univariate, multivariate and nd-gridded data approximation using cubic smoothing splines.

## Installation

Python 3.5 or above is supported.

```
pip install csaps
pip install -U csaps
```

The module depends only on NumPy and SciPy.

On Windows we highly recommend to use unofficial builds [NumPy+MKL](https://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy)
and [SciPy](https://www.lfd.uci.edu/~gohlke/pythonlibs/#scipy) from Christoph Gohlke.
## Simple Examples

```
> mkdir depends
Here are a couple of examples of smoothing data.

download numpy (https://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy) to "depends" directory
download scipy (https://www.lfd.uci.edu/~gohlke/pythonlibs/#scipy) to "depends" directory

> pip install --find-links depends/ csaps
```

## Smoothing univariate data

Univariate data are two vectors: X and Y with the same size. X is data sites, Y is data values.
For univariate case X-values must satisfy the condition: `x1 < x2 < ... < xN`.

You can use `UnivariateCubicSmoothingSpline` class for uivariate data smoothing.
An univariate data smoothing:

```python
import numpy as np
import matplotlib.pyplot as plt
import csaps

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

sp = csaps.UnivariateCubicSmoothingSpline(x, y, smooth=0.85)

xs = np.linspace(x[0], x[-1], 150)
ys = sp(xs)

ys = csaps(x, y, xs, smooth=0.85)

plt.plot(x, y, 'o', xs, ys, '-')
plt.show()
```

![csaps1d](https://user-images.githubusercontent.com/1299189/27611703-f3093c14-5b9b-11e7-9f18-6d0c3cc7633a.png)

### Smoothing weighted univariate data

The algorithm supports weighting. You can set weights vector that will determine
weights for all data values:

```python
import csaps

x = [1., 2., 4., 6.]
y = [2., 4., 5., 7.]
w = [0.5, 1, 0.7, 1.2]

sp = csaps.UnivariateCubicSmoothingSpline(x, y, w)
...
```

### Smoothing univariate data with vectorization

The algorithm supports vectorization. You can compute smoothing splines for
`X`, `Y` data where `X` is data site vector and `Y` is ND-array of data value vectors.
The shape of `Y` array must be: `(d0, d1, ..., dN)` where `dN` must equal of `X` vector size.

In this case the smoothing splines will be computed for all `Y` data vectors at a time.

For example:

```python
import numpy as np
import csaps

# data sites
x = [1, 2, 3, 4]

# two data vectors
y = np.array([(2, 4, 6, 8),
(1, 3, 5, 7)])

sp = csaps.UnivariateCubicSmoothingSpline(x, y)

xi = np.linspace(1, 4, 10)
yi = sp(xi)

print(yi.shape) # (2, 10)
assert yi.shape[:-1] == y.shape[:-1]
assert yi.shape[-1] == xi.size
```

**Important**:
The same weights vector and the same smoothing parameter will be used for all Y data.

## Smoothing multivariate data

We can easily smooth multivariate data using univariate smoothing spline, vectorization and parametrization.
We should define parametric data sites vector `t` with condition `t1 < t2 < ... < tN` and make spline for each dimension `X(t), Y(t), ..., M(t)`.

The module provides `MultivariateCubicSmoothingSpline` class for this case.

This class is a simple wrapper and it just automatically computes `t` vector by default and calls `UnivariateCubicSmoothingSpline`:

```python
# Construct multivariate spline from t and X, Y, Z, ..., M
sx = UnivariateCubicSmoothingSpline(t, data[x])
sy = UnivariateCubicSmoothingSpline(t, data[y])
sz = UnivariateCubicSmoothingSpline(t, data[z])
...
sm = UnivariateCubicSmoothingSpline(t, data[m])

# Or the same with using vectorization
sxyz_m = UnivariateCubicSmoothingSpline(t, data)
```

### Smoothing 3D noisy parametric curve example

In this example we make parametric 3D curve from `theta` parameter and compute spline from the same `theta` parameter.
In fact we could use `UnivariateCubicSmoothingSpline` with vectorization instead of `MultivariateCubicSmoothingSpline` in this case.

```python
import numpy as np

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

import csaps

n = 100
theta = np.linspace(-4 * np.pi, 4 * np.pi, n)
z = np.linspace(-2, 2, n)
r = z ** 2 + 1
np.random.seed(1234)
x = r * np.sin(theta) + np.random.randn(n) * 0.3
np.random.seed(5678)
y = r * np.cos(theta) + np.random.randn(n) * 0.3

data = np.vstack((x, y, z))

sp_theta = csaps.MultivariateCubicSmoothingSpline(data, theta, smooth=0.95)

# or the same
# sp_theta = csaps.UnivariateCubicSmoothingSpline(theta, data, smooth=0.95)

theta_i = np.linspace(-4 * np.pi, 4 * np.pi, 250)
data_i = sp_theta(theta_i)

xi = data_i[0, :]
yi = data_i[1, :]
zi = data_i[2, :]

fig = plt.figure()
ax = fig.gca(projection='3d')
plt.show()
```

<img width="663" alt="2019-04-04_02-28-52" src="https://user-images.githubusercontent.com/1299189/55519724-a75a6580-5681-11e9-8beb-2111664f8ee3.png">

## Smoothing ND-gridded data

The algorithm can make smoothing splines for ND-gridded data approximation.
In this case we use coordinatewise smoothing (tensor-product of univariate splines coefficients).

You can use `NdGridCubicSmoothingSpline` class for ND-gridded data smoothing.
You also can set weights and smoothing parameters for each dimension.

Currently the implementation does not support vectorization for ND-gridded data.

### Smoothing 2D-gridded data (surface) example
A surface data smoothing:

```python
import numpy as np

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

import csaps
from csaps import csaps

xdata = [np.linspace(-3, 3, 61), np.linspace(-3.5, 3.5, 51)]
i, j = np.meshgrid(*xdata, indexing='ij')
Expand All @@ -212,38 +65,41 @@ ydata = (3 * (1 - j)**2. * np.exp(-(j**2) - (i + 1)**2)
np.random.seed(12345)
noisy = ydata + (np.random.randn(*ydata.shape) * 0.75)

sp = csaps.NdGridCubicSmoothingSpline(xdata, noisy, smooth=0.988)
ysmth = sp(xdata)
ysmth = csaps(xdata, noisy, xdata, smooth=0.988)

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

ax.plot_wireframe(j, i, noisy, linewidths=0.5, color='r')
ax.scatter(j, i, noisy, s=5, c='r')

ax.plot_surface(j, i, ysmth, linewidth=0, alpha=1.0)

plt.show()
```

<img width="653" alt="2019-03-22_10-22-59" src="https://user-images.githubusercontent.com/1299189/54817564-2ff30200-4c8f-11e9-8afd-9055efcd6ea0.png">

## More examples
## Documentation

More examples of usage and the full documentation can be found at ReadTheDocs.

Please look through [csaps.ipynb](examples/csaps.ipynb) file for more examples.
https://csaps.readthedocs.io

## Testing

We use pytest and tox (on Travis CI) for testing.
Please see [test_csaps.py](tests/test_csaps.py) file.
pytest, tox and Travis CI are used for testing. Please see [test_csaps.py](tests).

## Algorithms and implementations

`csaps` is a Python modified port of MATLAB [CSAPS](https://www.mathworks.com/help/curvefit/csaps.html) function that is an implementation of
**csaps** package is a Python modified port of MATLAB [CSAPS](https://www.mathworks.com/help/curvefit/csaps.html) function that is an implementation of
Fortran routine SMOOTH from [PGS](http://pages.cs.wisc.edu/~deboor/pgs/) (originally written by Carl de Boor).

[csaps-cpp](https://github.com/espdev/csaps-cpp) C++11 Eigen based implementation of the algorithm.

## References

C. de Boor, A Practical Guide to Splines, Springer-Verlag, 1978.

## License

[MIT](https://choosealicense.com/licenses/mit/)
4 changes: 2 additions & 2 deletions csaps/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
MultivariateDataType,
NdGridDataType,
)
from csaps._shortcut import csaps, SmoothingResult
from csaps._shortcut import csaps, AutoSmoothingResult

__all__ = [
# Shortcut
'csaps',
'SmoothingResult',
'AutoSmoothingResult',

# Classes
'SplinePPFormBase',
Expand Down