From 4a80ed73570d3528dd7bea47998fd5be707acde8 Mon Sep 17 00:00:00 2001 From: Evgeny Prilepin Date: Mon, 21 Apr 2025 17:35:36 +0100 Subject: [PATCH] feat: update type-hints --- csaps/_base.py | 8 ++++---- csaps/_reshape.py | 3 +-- csaps/_shortcut.py | 39 +++++++++++++++++++-------------------- csaps/_sspndg.py | 37 ++++++++++++++++++------------------- csaps/_sspumv.py | 18 +++++++++--------- 5 files changed, 51 insertions(+), 54 deletions(-) diff --git a/csaps/_base.py b/csaps/_base.py index 86dfacb..3b1aa77 100644 --- a/csaps/_base.py +++ b/csaps/_base.py @@ -2,7 +2,7 @@ The base classes and interfaces """ -from typing import Generic, Optional, Tuple +from typing import Generic import abc import numpy as np @@ -72,7 +72,7 @@ def ndim(self) -> int: @property @abc.abstractmethod - def shape(self) -> Tuple[int, ...]: + def shape(self) -> tuple[int, ...]: """Returns the source data shape Returns @@ -101,7 +101,7 @@ def spline(self) -> TSpline: def __call__( self, xi: TXi, - nu: Optional[TNu] = None, - extrapolate: Optional[TExtrapolate] = None, + nu: TNu | None = None, + extrapolate: TExtrapolate | None = None, ) -> FloatNDArrayType: """Evaluates spline on the data sites""" diff --git a/csaps/_reshape.py b/csaps/_reshape.py index ab85989..d97c2bf 100644 --- a/csaps/_reshape.py +++ b/csaps/_reshape.py @@ -1,4 +1,3 @@ -import typing as ty import functools from itertools import chain import operator @@ -158,7 +157,7 @@ def umv_coeffs_to_flatten(arr: np.ndarray): return arr_view -def ndg_coeffs_to_canonical(arr: np.ndarray, pieces: ty.Tuple[int, ...]) -> np.ndarray: +def ndg_coeffs_to_canonical(arr: np.ndarray, pieces: tuple[int, ...]) -> np.ndarray: """Returns array canonical view for given n-d grid coeffs flatten array Creates n-d array canonical view with shape (k0, ..., kn, p0, ..., pn) for given diff --git a/csaps/_shortcut.py b/csaps/_shortcut.py index d32de84..f30b52d 100644 --- a/csaps/_shortcut.py +++ b/csaps/_shortcut.py @@ -2,8 +2,7 @@ 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 typing import NamedTuple, Sequence, overload import numpy as np @@ -19,7 +18,7 @@ class AutoSmoothingResult(NamedTuple): values: MultivariateDataType """Smoothed data values""" - smooth: Union[float, Sequence[Optional[float]]] + smooth: float | Sequence[float | None] """The calculated smoothing parameter""" @@ -33,9 +32,9 @@ def csaps( xdata: UnivariateDataType, ydata: MultivariateDataType, *, - weights: Optional[UnivariateDataType] = None, - smooth: Optional[float] = None, - axis: Optional[int] = None, + weights: UnivariateDataType | None = None, + smooth: float | None = None, + axis: int | None = None, normalizedsmooth: bool = False, ) -> ISmoothingSpline: # pragma: no cover ... @@ -47,8 +46,8 @@ def csaps( ydata: MultivariateDataType, xidata: UnivariateDataType, *, - weights: Optional[UnivariateDataType] = None, - axis: Optional[int] = None, + weights: UnivariateDataType | None = None, + axis: int | None = None, normalizedsmooth: bool = False, ) -> AutoSmoothingResult: # pragma: no cover ... @@ -61,8 +60,8 @@ def csaps( xidata: UnivariateDataType, *, smooth: float, - weights: Optional[UnivariateDataType] = None, - axis: Optional[int] = None, + weights: UnivariateDataType | None = None, + axis: int | None = None, normalizedsmooth: bool = False, ) -> MultivariateDataType: # pragma: no cover ... @@ -73,9 +72,9 @@ def csaps( xdata: SequenceUnivariateDataType, ydata: MultivariateDataType, *, - weights: Optional[SequenceUnivariateDataType] = None, - smooth: Optional[Sequence[float]] = None, - axis: Optional[int] = None, + weights: SequenceUnivariateDataType | None = None, + smooth: Sequence[float | None] | None = None, + axis: int | None = None, normalizedsmooth: bool = False, ) -> ISmoothingSpline: # pragma: no cover ... @@ -87,8 +86,8 @@ def csaps( ydata: MultivariateDataType, xidata: SequenceUnivariateDataType, *, - weights: Optional[SequenceUnivariateDataType] = None, - axis: Optional[int] = None, + weights: SequenceUnivariateDataType | None = None, + axis: int | None = None, normalizedsmooth: bool = False, ) -> AutoSmoothingResult: # pragma: no cover ... @@ -100,9 +99,9 @@ def csaps( ydata: MultivariateDataType, xidata: SequenceUnivariateDataType, *, - smooth: Sequence[float], - weights: Optional[SequenceUnivariateDataType] = None, - axis: Optional[int] = None, + smooth: Sequence[float | None], + weights: SequenceUnivariateDataType | None = None, + axis: int | None = None, normalizedsmooth: bool = False, ) -> MultivariateDataType: # pragma: no cover ... @@ -224,8 +223,8 @@ def csaps( """ umv = True - if isinstance(xdata, c_abc.Sequence): - if len(xdata) and isinstance(xdata[0], (np.ndarray, c_abc.Sequence)): + if isinstance(xdata, Sequence): + if len(xdata) and isinstance(xdata[0], (np.ndarray, Sequence)): umv = False if umv: diff --git a/csaps/_sspndg.py b/csaps/_sspndg.py index 0ec1a4c..4ec0c34 100644 --- a/csaps/_sspndg.py +++ b/csaps/_sspndg.py @@ -2,8 +2,7 @@ ND-Gridded cubic smoothing spline implementation """ -from typing import Optional, Sequence, Tuple, Union, cast -import collections.abc as c_abc +from typing import Optional, Sequence, cast import numpy as np from scipy.interpolate import NdPPoly, PPoly @@ -24,8 +23,8 @@ def ndgrid_prepare_data_vectors( data: SequenceUnivariateDataType, name: str, min_size: int = 2, -) -> Tuple[Float1DArrayTupe, ...]: - if not isinstance(data, c_abc.Sequence): +) -> tuple[Float1DArrayTupe, ...]: + if not isinstance(data, Sequence): raise TypeError(f"'{name}' must be a sequence of 1-d array-like (vectors) or scalars.") data_: list[Float1DArrayTupe] = [] @@ -41,7 +40,7 @@ def ndgrid_prepare_data_vectors( return tuple(data_) -class NdGridSplinePPForm(ISplinePPForm[Tuple[np.ndarray, ...], Tuple[int, ...]], NdPPoly): +class NdGridSplinePPForm(ISplinePPForm[tuple[np.ndarray, ...], tuple[int, ...]], NdPPoly): """N-D grid spline representation in PP-form N-D grid spline is represented in piecewise tensor product polynomial form. @@ -56,7 +55,7 @@ class NdGridSplinePPForm(ISplinePPForm[Tuple[np.ndarray, ...], Tuple[int, ...]], __module__ = 'csaps' @property - def breaks(self) -> Tuple[np.ndarray, ...]: + def breaks(self) -> tuple[np.ndarray, ...]: return self.x @property @@ -64,11 +63,11 @@ def coeffs(self) -> np.ndarray: return self.c @property - def order(self) -> Tuple[int, ...]: + def order(self) -> tuple[int, ...]: return self.c.shape[: self.c.ndim // 2] @property - def pieces(self) -> Tuple[int, ...]: + def pieces(self) -> tuple[int, ...]: return self.c.shape[self.c.ndim // 2 :] @property @@ -76,13 +75,13 @@ def ndim(self) -> int: return len(self.x) @property - def shape(self) -> Tuple[int, ...]: + def shape(self) -> tuple[int, ...]: return tuple(len(xi) for xi in self.x) def __call__( # type: ignore[override] self, x: SequenceUnivariateDataType, - nu: Optional[Tuple[int, ...]] = None, + nu: Optional[tuple[int, ...]] = None, extrapolate: Optional[bool] = None, ) -> np.ndarray: """Evaluate the spline for given data @@ -160,9 +159,9 @@ def __repr__(self): # pragma: no cover class NdGridCubicSmoothingSpline( ISmoothingSpline[ NdGridSplinePPForm, - Tuple[float, ...], + tuple[float, ...], SequenceUnivariateDataType, - Tuple[int, ...], + tuple[int, ...], bool, ] ): @@ -209,8 +208,8 @@ def __init__( self, xdata: SequenceUnivariateDataType, ydata: np.ndarray, - weights: Optional[SequenceUnivariateDataType] = None, - smooth: Optional[Union[float, Sequence[Optional[float]]]] = None, + weights: SequenceUnivariateDataType | None = None, + smooth: float | Sequence[float | None] | None = None, normalizedsmooth: bool = False, ) -> None: x, y, w, s = self._prepare_data(xdata, ydata, weights, smooth) @@ -220,8 +219,8 @@ def __init__( def __call__( self, x: SequenceUnivariateDataType, - nu: Optional[Tuple[int, ...]] = None, - extrapolate: Optional[bool] = None, + nu: tuple[int, ...] | None = None, + extrapolate: bool | None = None, ) -> FloatNDArrayType: """Evaluate the spline for given data @@ -249,12 +248,12 @@ def __call__( return self._spline(x, nu=nu, extrapolate=extrapolate) @property - def smooth(self) -> Tuple[float, ...]: + def smooth(self) -> tuple[float, ...]: """Returns a tuple of smoothing parameters for each axis Returns ------- - smooth : Tuple[float, ...] + smooth : tuple[float, ...] The smoothing parameter in the range ``[0, 1]`` for each axis """ return self._smooth @@ -299,7 +298,7 @@ def _prepare_data(cls, xdata, ydata, weights, smooth): if smooth is None: smooth = [None] * data_ndim - if not isinstance(smooth, c_abc.Sequence): + if not isinstance(smooth, Sequence): smooth = [float(smooth)] * data_ndim else: smooth = list(smooth) diff --git a/csaps/_sspumv.py b/csaps/_sspumv.py index 83abc12..4d00803 100644 --- a/csaps/_sspumv.py +++ b/csaps/_sspumv.py @@ -2,7 +2,7 @@ Univariate/multivariate cubic smoothing spline implementation """ -from typing import List, Optional, Tuple, Union, Literal, cast +from typing import Literal, cast import functools import numpy as np @@ -57,9 +57,9 @@ def ndim(self) -> int: return prod(shape) @property - def shape(self) -> Tuple[int, ...]: + def shape(self) -> tuple[int, ...]: """Returns the source data shape""" - shape: List[int] = list(self.c.shape[2:]) + shape: list[int] = list(self.c.shape[2:]) shape.insert(self.axis, self.c.shape[1] + 1) return tuple(shape) @@ -83,7 +83,7 @@ class CubicSmoothingSpline( float, UnivariateDataType, int, - Union[bool, Literal['periodic']], + bool | Literal['periodic'], ] ): """Cubic smoothing spline @@ -126,8 +126,8 @@ def __init__( self, xdata: UnivariateDataType, ydata: MultivariateDataType, - weights: Optional[UnivariateDataType] = None, - smooth: Optional[float] = None, + weights: UnivariateDataType | None = None, + smooth: float | None = None, axis: int = -1, normalizedsmooth: bool = False, ) -> None: @@ -138,8 +138,8 @@ def __init__( def __call__( self, x: UnivariateDataType, - nu: Optional[int] = None, - extrapolate: Optional[Union[bool, Literal['periodic']]] = None, + nu: int | None = None, + extrapolate: bool | Literal['periodic'] | None = None, ) -> FloatNDArrayType: """Evaluate the spline for given data @@ -242,7 +242,7 @@ def trace(m: sp.dia_matrix): return 1.0 / (1.0 + trace(a) / (6.0 * trace(b))) @staticmethod - def _normalize_smooth(x: np.ndarray, w: np.ndarray, smooth: Optional[float]): + def _normalize_smooth(x: np.ndarray, w: np.ndarray, smooth: float | None) -> float: """ See the explanation here: https://github.com/espdev/csaps/pull/47 """