Skip to content

Commit 914d153

Browse files
authored
Merge pull request #101 from espdev/modern-typehints
Update type-hints
2 parents 4016446 + 4a80ed7 commit 914d153

File tree

5 files changed

+51
-54
lines changed

5 files changed

+51
-54
lines changed

csaps/_base.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
The base classes and interfaces
33
"""
44

5-
from typing import Generic, Optional, Tuple
5+
from typing import Generic
66
import abc
77

88
import numpy as np
@@ -72,7 +72,7 @@ def ndim(self) -> int:
7272

7373
@property
7474
@abc.abstractmethod
75-
def shape(self) -> Tuple[int, ...]:
75+
def shape(self) -> tuple[int, ...]:
7676
"""Returns the source data shape
7777
7878
Returns
@@ -101,7 +101,7 @@ def spline(self) -> TSpline:
101101
def __call__(
102102
self,
103103
xi: TXi,
104-
nu: Optional[TNu] = None,
105-
extrapolate: Optional[TExtrapolate] = None,
104+
nu: TNu | None = None,
105+
extrapolate: TExtrapolate | None = None,
106106
) -> FloatNDArrayType:
107107
"""Evaluates spline on the data sites"""

csaps/_reshape.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import typing as ty
21
import functools
32
from itertools import chain
43
import operator
@@ -158,7 +157,7 @@ def umv_coeffs_to_flatten(arr: np.ndarray):
158157
return arr_view
159158

160159

161-
def ndg_coeffs_to_canonical(arr: np.ndarray, pieces: ty.Tuple[int, ...]) -> np.ndarray:
160+
def ndg_coeffs_to_canonical(arr: np.ndarray, pieces: tuple[int, ...]) -> np.ndarray:
162161
"""Returns array canonical view for given n-d grid coeffs flatten array
163162
164163
Creates n-d array canonical view with shape (k0, ..., kn, p0, ..., pn) for given

csaps/_shortcut.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
The module provided `csaps` shortcut function for smoothing data
33
"""
44

5-
from typing import NamedTuple, Optional, Sequence, Union, overload
6-
from collections import abc as c_abc
5+
from typing import NamedTuple, Sequence, overload
76

87
import numpy as np
98

@@ -19,7 +18,7 @@ class AutoSmoothingResult(NamedTuple):
1918
values: MultivariateDataType
2019
"""Smoothed data values"""
2120

22-
smooth: Union[float, Sequence[Optional[float]]]
21+
smooth: float | Sequence[float | None]
2322
"""The calculated smoothing parameter"""
2423

2524

@@ -33,9 +32,9 @@ def csaps(
3332
xdata: UnivariateDataType,
3433
ydata: MultivariateDataType,
3534
*,
36-
weights: Optional[UnivariateDataType] = None,
37-
smooth: Optional[float] = None,
38-
axis: Optional[int] = None,
35+
weights: UnivariateDataType | None = None,
36+
smooth: float | None = None,
37+
axis: int | None = None,
3938
normalizedsmooth: bool = False,
4039
) -> ISmoothingSpline: # pragma: no cover
4140
...
@@ -47,8 +46,8 @@ def csaps(
4746
ydata: MultivariateDataType,
4847
xidata: UnivariateDataType,
4948
*,
50-
weights: Optional[UnivariateDataType] = None,
51-
axis: Optional[int] = None,
49+
weights: UnivariateDataType | None = None,
50+
axis: int | None = None,
5251
normalizedsmooth: bool = False,
5352
) -> AutoSmoothingResult: # pragma: no cover
5453
...
@@ -61,8 +60,8 @@ def csaps(
6160
xidata: UnivariateDataType,
6261
*,
6362
smooth: float,
64-
weights: Optional[UnivariateDataType] = None,
65-
axis: Optional[int] = None,
63+
weights: UnivariateDataType | None = None,
64+
axis: int | None = None,
6665
normalizedsmooth: bool = False,
6766
) -> MultivariateDataType: # pragma: no cover
6867
...
@@ -73,9 +72,9 @@ def csaps(
7372
xdata: SequenceUnivariateDataType,
7473
ydata: MultivariateDataType,
7574
*,
76-
weights: Optional[SequenceUnivariateDataType] = None,
77-
smooth: Optional[Sequence[float]] = None,
78-
axis: Optional[int] = None,
75+
weights: SequenceUnivariateDataType | None = None,
76+
smooth: Sequence[float | None] | None = None,
77+
axis: int | None = None,
7978
normalizedsmooth: bool = False,
8079
) -> ISmoothingSpline: # pragma: no cover
8180
...
@@ -87,8 +86,8 @@ def csaps(
8786
ydata: MultivariateDataType,
8887
xidata: SequenceUnivariateDataType,
8988
*,
90-
weights: Optional[SequenceUnivariateDataType] = None,
91-
axis: Optional[int] = None,
89+
weights: SequenceUnivariateDataType | None = None,
90+
axis: int | None = None,
9291
normalizedsmooth: bool = False,
9392
) -> AutoSmoothingResult: # pragma: no cover
9493
...
@@ -100,9 +99,9 @@ def csaps(
10099
ydata: MultivariateDataType,
101100
xidata: SequenceUnivariateDataType,
102101
*,
103-
smooth: Sequence[float],
104-
weights: Optional[SequenceUnivariateDataType] = None,
105-
axis: Optional[int] = None,
102+
smooth: Sequence[float | None],
103+
weights: SequenceUnivariateDataType | None = None,
104+
axis: int | None = None,
106105
normalizedsmooth: bool = False,
107106
) -> MultivariateDataType: # pragma: no cover
108107
...
@@ -224,8 +223,8 @@ def csaps(
224223
"""
225224

226225
umv = True
227-
if isinstance(xdata, c_abc.Sequence):
228-
if len(xdata) and isinstance(xdata[0], (np.ndarray, c_abc.Sequence)):
226+
if isinstance(xdata, Sequence):
227+
if len(xdata) and isinstance(xdata[0], (np.ndarray, Sequence)):
229228
umv = False
230229

231230
if umv:

csaps/_sspndg.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
ND-Gridded cubic smoothing spline implementation
33
"""
44

5-
from typing import Optional, Sequence, Tuple, Union, cast
6-
import collections.abc as c_abc
5+
from typing import Optional, Sequence, cast
76

87
import numpy as np
98
from scipy.interpolate import NdPPoly, PPoly
@@ -24,8 +23,8 @@ def ndgrid_prepare_data_vectors(
2423
data: SequenceUnivariateDataType,
2524
name: str,
2625
min_size: int = 2,
27-
) -> Tuple[Float1DArrayTupe, ...]:
28-
if not isinstance(data, c_abc.Sequence):
26+
) -> tuple[Float1DArrayTupe, ...]:
27+
if not isinstance(data, Sequence):
2928
raise TypeError(f"'{name}' must be a sequence of 1-d array-like (vectors) or scalars.")
3029

3130
data_: list[Float1DArrayTupe] = []
@@ -41,7 +40,7 @@ def ndgrid_prepare_data_vectors(
4140
return tuple(data_)
4241

4342

44-
class NdGridSplinePPForm(ISplinePPForm[Tuple[np.ndarray, ...], Tuple[int, ...]], NdPPoly):
43+
class NdGridSplinePPForm(ISplinePPForm[tuple[np.ndarray, ...], tuple[int, ...]], NdPPoly):
4544
"""N-D grid spline representation in PP-form
4645
4746
N-D grid spline is represented in piecewise tensor product polynomial form.
@@ -56,33 +55,33 @@ class NdGridSplinePPForm(ISplinePPForm[Tuple[np.ndarray, ...], Tuple[int, ...]],
5655
__module__ = 'csaps'
5756

5857
@property
59-
def breaks(self) -> Tuple[np.ndarray, ...]:
58+
def breaks(self) -> tuple[np.ndarray, ...]:
6059
return self.x
6160

6261
@property
6362
def coeffs(self) -> np.ndarray:
6463
return self.c
6564

6665
@property
67-
def order(self) -> Tuple[int, ...]:
66+
def order(self) -> tuple[int, ...]:
6867
return self.c.shape[: self.c.ndim // 2]
6968

7069
@property
71-
def pieces(self) -> Tuple[int, ...]:
70+
def pieces(self) -> tuple[int, ...]:
7271
return self.c.shape[self.c.ndim // 2 :]
7372

7473
@property
7574
def ndim(self) -> int:
7675
return len(self.x)
7776

7877
@property
79-
def shape(self) -> Tuple[int, ...]:
78+
def shape(self) -> tuple[int, ...]:
8079
return tuple(len(xi) for xi in self.x)
8180

8281
def __call__( # type: ignore[override]
8382
self,
8483
x: SequenceUnivariateDataType,
85-
nu: Optional[Tuple[int, ...]] = None,
84+
nu: Optional[tuple[int, ...]] = None,
8685
extrapolate: Optional[bool] = None,
8786
) -> np.ndarray:
8887
"""Evaluate the spline for given data
@@ -160,9 +159,9 @@ def __repr__(self): # pragma: no cover
160159
class NdGridCubicSmoothingSpline(
161160
ISmoothingSpline[
162161
NdGridSplinePPForm,
163-
Tuple[float, ...],
162+
tuple[float, ...],
164163
SequenceUnivariateDataType,
165-
Tuple[int, ...],
164+
tuple[int, ...],
166165
bool,
167166
]
168167
):
@@ -209,8 +208,8 @@ def __init__(
209208
self,
210209
xdata: SequenceUnivariateDataType,
211210
ydata: np.ndarray,
212-
weights: Optional[SequenceUnivariateDataType] = None,
213-
smooth: Optional[Union[float, Sequence[Optional[float]]]] = None,
211+
weights: SequenceUnivariateDataType | None = None,
212+
smooth: float | Sequence[float | None] | None = None,
214213
normalizedsmooth: bool = False,
215214
) -> None:
216215
x, y, w, s = self._prepare_data(xdata, ydata, weights, smooth)
@@ -220,8 +219,8 @@ def __init__(
220219
def __call__(
221220
self,
222221
x: SequenceUnivariateDataType,
223-
nu: Optional[Tuple[int, ...]] = None,
224-
extrapolate: Optional[bool] = None,
222+
nu: tuple[int, ...] | None = None,
223+
extrapolate: bool | None = None,
225224
) -> FloatNDArrayType:
226225
"""Evaluate the spline for given data
227226
@@ -249,12 +248,12 @@ def __call__(
249248
return self._spline(x, nu=nu, extrapolate=extrapolate)
250249

251250
@property
252-
def smooth(self) -> Tuple[float, ...]:
251+
def smooth(self) -> tuple[float, ...]:
253252
"""Returns a tuple of smoothing parameters for each axis
254253
255254
Returns
256255
-------
257-
smooth : Tuple[float, ...]
256+
smooth : tuple[float, ...]
258257
The smoothing parameter in the range ``[0, 1]`` for each axis
259258
"""
260259
return self._smooth
@@ -299,7 +298,7 @@ def _prepare_data(cls, xdata, ydata, weights, smooth):
299298
if smooth is None:
300299
smooth = [None] * data_ndim
301300

302-
if not isinstance(smooth, c_abc.Sequence):
301+
if not isinstance(smooth, Sequence):
303302
smooth = [float(smooth)] * data_ndim
304303
else:
305304
smooth = list(smooth)

csaps/_sspumv.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Univariate/multivariate cubic smoothing spline implementation
33
"""
44

5-
from typing import List, Optional, Tuple, Union, Literal, cast
5+
from typing import Literal, cast
66
import functools
77

88
import numpy as np
@@ -57,9 +57,9 @@ def ndim(self) -> int:
5757
return prod(shape)
5858

5959
@property
60-
def shape(self) -> Tuple[int, ...]:
60+
def shape(self) -> tuple[int, ...]:
6161
"""Returns the source data shape"""
62-
shape: List[int] = list(self.c.shape[2:])
62+
shape: list[int] = list(self.c.shape[2:])
6363
shape.insert(self.axis, self.c.shape[1] + 1)
6464

6565
return tuple(shape)
@@ -83,7 +83,7 @@ class CubicSmoothingSpline(
8383
float,
8484
UnivariateDataType,
8585
int,
86-
Union[bool, Literal['periodic']],
86+
bool | Literal['periodic'],
8787
]
8888
):
8989
"""Cubic smoothing spline
@@ -126,8 +126,8 @@ def __init__(
126126
self,
127127
xdata: UnivariateDataType,
128128
ydata: MultivariateDataType,
129-
weights: Optional[UnivariateDataType] = None,
130-
smooth: Optional[float] = None,
129+
weights: UnivariateDataType | None = None,
130+
smooth: float | None = None,
131131
axis: int = -1,
132132
normalizedsmooth: bool = False,
133133
) -> None:
@@ -138,8 +138,8 @@ def __init__(
138138
def __call__(
139139
self,
140140
x: UnivariateDataType,
141-
nu: Optional[int] = None,
142-
extrapolate: Optional[Union[bool, Literal['periodic']]] = None,
141+
nu: int | None = None,
142+
extrapolate: bool | Literal['periodic'] | None = None,
143143
) -> FloatNDArrayType:
144144
"""Evaluate the spline for given data
145145
@@ -242,7 +242,7 @@ def trace(m: sp.dia_matrix):
242242
return 1.0 / (1.0 + trace(a) / (6.0 * trace(b)))
243243

244244
@staticmethod
245-
def _normalize_smooth(x: np.ndarray, w: np.ndarray, smooth: Optional[float]):
245+
def _normalize_smooth(x: np.ndarray, w: np.ndarray, smooth: float | None) -> float:
246246
"""
247247
See the explanation here: https://github.com/espdev/csaps/pull/47
248248
"""

0 commit comments

Comments
 (0)