Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions csaps/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"""
3 changes: 1 addition & 2 deletions csaps/_reshape.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import typing as ty
import functools
from itertools import chain
import operator
Expand Down Expand Up @@ -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
Expand Down
39 changes: 19 additions & 20 deletions csaps/_shortcut.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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"""


Expand All @@ -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
...
Expand All @@ -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
...
Expand All @@ -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
...
Expand All @@ -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
...
Expand All @@ -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
...
Expand All @@ -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
...
Expand Down Expand Up @@ -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:
Expand Down
37 changes: 18 additions & 19 deletions csaps/_sspndg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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] = []
Expand All @@ -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.
Expand All @@ -56,33 +55,33 @@ 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
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
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
Expand Down Expand Up @@ -160,9 +159,9 @@ def __repr__(self): # pragma: no cover
class NdGridCubicSmoothingSpline(
ISmoothingSpline[
NdGridSplinePPForm,
Tuple[float, ...],
tuple[float, ...],
SequenceUnivariateDataType,
Tuple[int, ...],
tuple[int, ...],
bool,
]
):
Expand Down Expand Up @@ -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)
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
18 changes: 9 additions & 9 deletions csaps/_sspumv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -83,7 +83,7 @@ class CubicSmoothingSpline(
float,
UnivariateDataType,
int,
Union[bool, Literal['periodic']],
bool | Literal['periodic'],
]
):
"""Cubic smoothing spline
Expand Down Expand Up @@ -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:
Expand All @@ -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

Expand Down Expand Up @@ -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
"""
Expand Down