-
Notifications
You must be signed in to change notification settings - Fork 26
/
_shortcut.py
264 lines (207 loc) · 7.64 KB
/
_shortcut.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
"""
The module provided `csaps` shortcut function for smoothing data
"""
from typing import NamedTuple, Optional, Sequence, Union, overload
from collections import abc as c_abc
from ._base import ISmoothingSpline
from ._sspndg import NdGridCubicSmoothingSpline, ndgrid_prepare_data_vectors
from ._sspumv import CubicSmoothingSpline
from ._types import MultivariateDataType, NdGridDataType, UnivariateDataType
class AutoSmoothingResult(NamedTuple):
"""The result for auto smoothing for `csaps` function"""
values: MultivariateDataType
"""Smoothed data values"""
smooth: Union[float, Sequence[Optional[float]]]
"""The calculated smoothing parameter"""
# **************************************
# csaps signatures
#
@overload
def csaps(
xdata: UnivariateDataType,
ydata: MultivariateDataType,
*,
weights: Optional[UnivariateDataType] = None,
smooth: Optional[float] = None,
axis: Optional[int] = None,
normalizedsmooth: bool = False,
) -> ISmoothingSpline: # pragma: no cover
...
@overload
def csaps(
xdata: UnivariateDataType,
ydata: MultivariateDataType,
xidata: UnivariateDataType,
*,
weights: Optional[UnivariateDataType] = None,
axis: Optional[int] = None,
normalizedsmooth: bool = False,
) -> AutoSmoothingResult: # pragma: no cover
...
@overload
def csaps(
xdata: UnivariateDataType,
ydata: MultivariateDataType,
xidata: UnivariateDataType,
*,
smooth: float,
weights: Optional[UnivariateDataType] = None,
axis: Optional[int] = None,
normalizedsmooth: bool = False,
) -> MultivariateDataType: # pragma: no cover
...
@overload
def csaps(
xdata: NdGridDataType,
ydata: MultivariateDataType,
*,
weights: Optional[NdGridDataType] = None,
smooth: Optional[Sequence[float]] = None,
axis: Optional[int] = None,
normalizedsmooth: bool = False,
) -> ISmoothingSpline: # pragma: no cover
...
@overload
def csaps(
xdata: NdGridDataType,
ydata: MultivariateDataType,
xidata: NdGridDataType,
*,
weights: Optional[NdGridDataType] = None,
axis: Optional[int] = None,
normalizedsmooth: bool = False,
) -> AutoSmoothingResult: # pragma: no cover
...
@overload
def csaps(
xdata: NdGridDataType,
ydata: MultivariateDataType,
xidata: NdGridDataType,
*,
smooth: Sequence[float],
weights: Optional[NdGridDataType] = None,
axis: Optional[int] = None,
normalizedsmooth: bool = False,
) -> MultivariateDataType: # pragma: no cover
...
#
# csaps signatures
# **************************************
def csaps(
xdata: Union[UnivariateDataType, NdGridDataType],
ydata: MultivariateDataType,
xidata: Optional[Union[UnivariateDataType, NdGridDataType]] = None,
*,
weights: Optional[Union[UnivariateDataType, NdGridDataType]] = None,
smooth: Optional[Union[float, Sequence[float]]] = None,
axis: Optional[int] = None,
normalizedsmooth: bool = False,
) -> Union[MultivariateDataType, ISmoothingSpline, AutoSmoothingResult]:
"""Smooths the univariate/multivariate/gridded data or computes the corresponding splines
This function might be used as the main API for smoothing any data.
Parameters
----------
xdata : np.ndarray, array-like
The data sites ``x1 < x2 < ... < xN``:
- 1-D data vector/sequence (array-like) for univariate/multivariate ``ydata`` case
- The sequence of 1-D data vectors for nd-gridded ``ydata`` case
ydata : np.ndarray, array-like
The data values:
- 1-D data vector/sequence (array-like) for univariate data case
- N-D array/array-like for multivariate data case
- N-D array for nd-gridded data case
xidata : [*Optional*] np.ndarray, array-like, Sequence[array-like]
The data sites for output smoothed data:
- 1-D data vector/sequence (array-like) for univariate/multivariate ``ydata`` case
- The sequence of 1-D data vectors for nd-gridded ``ydata`` case
If this argument was not set, the function will return computed spline
for given data in :class:`ISmoothingSpline` object.
weights : [*Optional*] np.ndarray, array-like, Sequence[array-like]
The weights data vectors:
- 1-D data vector/sequence (array-like) for univariate/multivariate ``ydata`` case
- The sequence of 1-D data vectors for nd-gridded ``ydata`` case
smooth : [*Optional*] float, Sequence[float]
The smoothing factor value(s):
- float value in the range ``[0, 1]`` for univariate/multivariate ``ydata`` case
- the sequence of float in the range ``[0, 1]`` or None for nd-gridded ``ydata`` case
If this argument was not set or None or sequence with None-items, the function will return
named tuple :class:`AutoSmoothingResult` with computed smoothed data values and smoothing factor value(s).
axis : [*Optional*] int
The ``ydata`` axis. Axis along which ``ydata`` is assumed to be varying.
If this argument was not set the last axis will be used (``axis == -1``).
.. note::
Currently, `axis` will be ignored for nd-gridded ``ydata`` case.
normalizedsmooth : [*Optional*] bool
If True, the smooth parameter is normalized such that results are invariant to xdata range
and less sensitive to nonuniformity of weights and xdata clumping
.. versionadded:: 1.1.0
Returns
-------
yidata : np.ndarray
Smoothed data values if ``xidata`` and ``smooth`` were set.
autosmoothing_result : AutoSmoothingResult
The named tuple object with two fileds:
- 'values' -- smoothed data values
- 'smooth' -- computed smoothing factor
This result will be returned if ``xidata`` was set and ``smooth`` was not set.
ssp_obj : ISmoothingSpline
Smoothing spline object if ``xidata`` was not set:
- :class:`CubicSmoothingSpline` instance for univariate/multivariate data
- :class:`NdGridCubicSmoothingSpline` instance for nd-gridded data
Examples
--------
Univariate data smoothing
.. code-block:: python
import numpy as np
from csaps import csaps
x = np.linspace(-5., 5., 25)
y = np.exp(-(x/2.5)**2) + (np.random.rand(25) - 0.2) * 0.3
xi = np.linspace(-5., 5., 150)
# Smooth data with smoothing factor 0.85
yi = csaps(x, y, xi, smooth=0.85)
# Smooth data and compute smoothing factor automatically
yi, smooth = csaps(x, y, xi)
# Do not evaluate the spline, only compute it
sp = csaps(x, y, smooth=0.98)
See Also
--------
CubicSmoothingSpline
NdGridCubicSmoothingSpline
"""
if isinstance(xdata, c_abc.Sequence):
try:
ndgrid_prepare_data_vectors(xdata, 'xdata')
except ValueError:
umv = True
else:
umv = False
else:
umv = True
if umv:
axis = -1 if axis is None else axis
sp = CubicSmoothingSpline(
xdata,
ydata,
weights=weights,
smooth=smooth,
axis=axis,
normalizedsmooth=normalizedsmooth,
)
else:
sp = NdGridCubicSmoothingSpline(
xdata,
ydata,
weights,
smooth,
normalizedsmooth=normalizedsmooth,
)
if xidata is None:
return sp
yidata = sp(xidata)
auto_smooth = smooth is None
if isinstance(smooth, Sequence):
auto_smooth = any(sm is None for sm in smooth)
if auto_smooth:
return AutoSmoothingResult(yidata, sp.smooth)
else:
return yidata