Skip to content

Commit

Permalink
Merge pull request #74 from espdev/linters-and-type-hints
Browse files Browse the repository at this point in the history
Add linters, code auto formatting and fix type hints
  • Loading branch information
espdev authored Dec 18, 2023
2 parents bd5f041 + 76bb4e1 commit 4e91fe8
Show file tree
Hide file tree
Showing 17 changed files with 714 additions and 572 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ jobs:
- name: Install the project dependencies
run: poetry install

- name: Run static analysis with ruff
run: poetry run ruff check .
- name: Run static analysis and linters
run: |
poetry run ruff check .
poetry run isort -c .
- name: Run tests
run: poetry run pytest -v --color=yes --cov=csaps --cov-report=term --cov-report=lcov:coverage.info
Expand Down
30 changes: 7 additions & 23 deletions csaps/__init__.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,13 @@
# -*- coding: utf-8 -*-

"""
Cubic spline approximation (smoothing)
"""

from csaps._version import __version__ # noqa

from csaps._base import (
ISplinePPForm,
ISmoothingSpline,
)
from csaps._sspumv import (
SplinePPForm,
CubicSmoothingSpline,
)
from csaps._sspndg import (
NdGridSplinePPForm,
NdGridCubicSmoothingSpline,
)
from csaps._types import (
UnivariateDataType,
MultivariateDataType,
NdGridDataType,
)
from csaps._shortcut import csaps, AutoSmoothingResult
from csaps._base import ISmoothingSpline, ISplinePPForm
from csaps._shortcut import AutoSmoothingResult, csaps
from csaps._sspndg import NdGridCubicSmoothingSpline, NdGridSplinePPForm
from csaps._sspumv import CubicSmoothingSpline, SplinePPForm
from csaps._types import MultivariateDataType, NdGridDataType, UnivariateDataType
from csaps._version import __version__

__all__ = [
# Shortcut
Expand All @@ -43,4 +26,5 @@
'UnivariateDataType',
'MultivariateDataType',
'NdGridDataType',
'__version__',
]
14 changes: 4 additions & 10 deletions csaps/_base.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
# -*- coding: utf-8 -*-

"""
The base classes and interfaces
"""

from typing import Generic, Optional, Tuple
import abc
from typing import Generic, Tuple, Optional

import numpy as np

from ._types import TData, TProps, TSmooth, TXi, TNu, TExtrapolate, TSpline
from ._types import TData, TExtrapolate, TNu, TProps, TSmooth, TSpline, TXi


class ISplinePPForm(abc.ABC, Generic[TData, TProps]):
Expand Down Expand Up @@ -76,7 +73,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 @@ -105,9 +102,6 @@ def spline(self) -> TSpline:
"""

@abc.abstractmethod
def __call__(self,
xi: TXi,
nu: Optional[TNu] = None,
extrapolate: Optional[TExtrapolate] = None) -> np.ndarray:
def __call__(self, xi: TXi, nu: Optional[TNu] = None, extrapolate: Optional[TExtrapolate] = None) -> np.ndarray:
"""Evaluates spline on the data sites
"""
11 changes: 4 additions & 7 deletions csaps/_reshape.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
# -*- coding: utf-8 -*-

import typing as ty
import functools
import operator
from itertools import chain
import typing as ty
import operator

import numpy as np
from numpy.lib.stride_tricks import as_strided
Expand Down Expand Up @@ -152,13 +150,12 @@ def umv_coeffs_to_flatten(arr: np.ndarray):
strides = arr.strides[:-3:-1]
arr_view = as_strided(arr, shape=shape, strides=strides)
else: # pragma: no cover
raise ValueError(
f"The array ndim must be 2 or 3, but given array has ndim={arr.ndim}.")
raise ValueError(f"The array ndim must be 2 or 3, but given array has ndim={arr.ndim}.")

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: ty.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
155 changes: 93 additions & 62 deletions csaps/_shortcut.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
# -*- coding: utf-8 -*-

"""
The module provised `csaps` shortcut function for smoothing data
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 Optional, Union, Sequence, NamedTuple, overload

from ._base import ISmoothingSpline
from ._sspndg import NdGridCubicSmoothingSpline, ndgrid_prepare_data_vectors
from ._sspumv import CubicSmoothingSpline
from ._sspndg import ndgrid_prepare_data_vectors, NdGridCubicSmoothingSpline
from ._types import UnivariateDataType, MultivariateDataType, NdGridDataType
from ._types import MultivariateDataType, NdGridDataType, UnivariateDataType


class AutoSmoothingResult(NamedTuple):
Expand All @@ -28,78 +25,100 @@ class AutoSmoothingResult(NamedTuple):
# 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: ...
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: ...
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: ...
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: ...
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: ...
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: ...
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]:
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.
Expand Down Expand Up @@ -213,10 +232,22 @@ def csaps(xdata: Union[UnivariateDataType, NdGridDataType],

if umv:
axis = -1 if axis is None else axis
sp = CubicSmoothingSpline(xdata, ydata, weights=weights, smooth=smooth, axis=axis,
normalizedsmooth=normalizedsmooth)
sp = CubicSmoothingSpline(
xdata,
ydata,
weights=weights,
smooth=smooth,
axis=axis,
normalizedsmooth=normalizedsmooth,
)
else:
sp = NdGridCubicSmoothingSpline(xdata, ydata, weights, smooth, normalizedsmooth=normalizedsmooth)
sp = NdGridCubicSmoothingSpline(
xdata,
ydata,
weights,
smooth,
normalizedsmooth=normalizedsmooth,
)

if xidata is None:
return sp
Expand Down
Loading

0 comments on commit 4e91fe8

Please sign in to comment.