From 3a42853812c99cc296604e846b7da9bfe0f9d71c Mon Sep 17 00:00:00 2001 From: Eugene Prilepin Date: Thu, 2 Jul 2020 00:56:24 +0300 Subject: [PATCH] fix type-hints and docstrings --- csaps/_base.py | 23 +++++++++++--------- csaps/_sspndg.py | 55 +++++++++++++++++++++++++++++++++++++++++++++--- csaps/_sspumv.py | 14 ++++++++++-- csaps/_types.py | 2 ++ 4 files changed, 79 insertions(+), 15 deletions(-) diff --git a/csaps/_base.py b/csaps/_base.py index 12e33b6..c94a1f3 100644 --- a/csaps/_base.py +++ b/csaps/_base.py @@ -6,14 +6,14 @@ """ import abc -import typing as ty +from typing import Generic, Tuple, Optional import numpy as np -from ._types import TData, TProps, TSmooth, TXi, TSpline +from ._types import TData, TProps, TSmooth, TXi, TNu, TExtrapolate, TSpline -class ISplinePPForm(abc.ABC, ty.Generic[TData, TProps]): +class ISplinePPForm(abc.ABC, Generic[TData, TProps]): """The interface class for spline representation in PP-form """ @@ -74,24 +74,24 @@ def ndim(self) -> int: @property @abc.abstractmethod - def shape(self) -> ty.Tuple[int]: - """Returns the data shape + def shape(self) -> Tuple[int]: + """Returns the source data shape Returns ------- - ndim : int - The data shape + shape : tuple of int + The source data shape """ -class ISmoothingSpline(abc.ABC, ty.Generic[TSpline, TSmooth, TXi]): +class ISmoothingSpline(abc.ABC, Generic[TSpline, TSmooth, TXi, TNu, TExtrapolate]): """The interface class for smooting splines """ @property @abc.abstractmethod def smooth(self) -> TSmooth: - """Returns smoothing parameter(s) + """Returns smoothing factor(s) """ @property @@ -101,6 +101,9 @@ def spline(self) -> TSpline: """ @abc.abstractmethod - def __call__(self, xi: TXi) -> np.ndarray: + def __call__(self, + xi: TXi, + nu: Optional[TNu] = None, + extrapolate: Optional[TExtrapolate] = None) -> np.ndarray: """Evaluates spline on the data sites """ diff --git a/csaps/_sspndg.py b/csaps/_sspndg.py index cf6d9bc..0604d4b 100644 --- a/csaps/_sspndg.py +++ b/csaps/_sspndg.py @@ -74,7 +74,33 @@ def ndim(self) -> int: def shape(self) -> Tuple[int, ...]: return tuple(len(xi) for xi in self.x) - def __call__(self, x, nu=None, extrapolate=None): + def __call__(self, + x: Sequence[UnivariateDataType], + nu: Optional[Tuple[int, ...]] = None, + extrapolate: Optional[bool] = None) -> np.ndarray: + """Evaluate the spline for given data + + Parameters + ---------- + + x : tuple of 1-d array-like + The tuple of point values for each dimension to evaluate the spline at. + + nu : [*Optional*] tuple of int + Orders of derivatives to evaluate. Each must be non-negative. + + extrapolate : [*Optional*] bool + Whether to extrapolate to out-of-bounds points based on first and last + intervals, or to return NaNs. + + Returns + ------- + + y : array-like + Interpolated values. Shape is determined by replacing the + interpolation axis in the original array with the shape of x. + + """ x = ndgrid_prepare_data_vectors(x, 'x', min_size=1) if len(x) != self.ndim: @@ -100,7 +126,9 @@ def __repr__(self): # pragma: no cover class NdGridCubicSmoothingSpline(ISmoothingSpline[ NdGridSplinePPForm, Tuple[float, ...], - NdGridDataType + NdGridDataType, + Tuple[int, ...], + bool, ]): """N-D grid cubic smoothing spline @@ -147,9 +175,30 @@ def __init__(self, def __call__(self, x: Union[NdGridDataType, Sequence[Number]], - nu: Optional[Tuple[int]] = None, + nu: Optional[Tuple[int, ...]] = None, extrapolate: Optional[bool] = None) -> np.ndarray: """Evaluate the spline for given data + + Parameters + ---------- + + x : tuple of 1-d array-like + The tuple of point values for each dimension to evaluate the spline at. + + nu : [*Optional*] tuple of int + Orders of derivatives to evaluate. Each must be non-negative. + + extrapolate : [*Optional*] bool + Whether to extrapolate to out-of-bounds points based on first and last + intervals, or to return NaNs. + + Returns + ------- + + y : array-like + Interpolated values. Shape is determined by replacing the + interpolation axis in the original array with the shape of x. + """ return self._spline(x, nu=nu, extrapolate=extrapolate) diff --git a/csaps/_sspumv.py b/csaps/_sspumv.py index 406b7fb..911f3b9 100644 --- a/csaps/_sspumv.py +++ b/csaps/_sspumv.py @@ -57,6 +57,8 @@ def ndim(self) -> int: @property def shape(self) -> Tuple[int]: + """Returns the source data shape + """ shape: List[int] = list(self.c.shape[2:]) shape.insert(self.axis, self.c.shape[1] + 1) @@ -75,7 +77,13 @@ def __repr__(self): # pragma: no cover ) -class CubicSmoothingSpline(ISmoothingSpline[SplinePPForm, float, UnivariateDataType]): +class CubicSmoothingSpline(ISmoothingSpline[ + SplinePPForm, + float, + UnivariateDataType, + int, + Union[bool, str] + ]): """Cubic smoothing spline The cubic spline implementation for univariate/multivariate data. @@ -119,7 +127,7 @@ def __init__(self, def __call__(self, x: UnivariateDataType, - nu: int = 0, + nu: Optional[int] = None, extrapolate: Optional[Union[bool, str]] = None) -> np.ndarray: """Evaluate the spline for given data @@ -147,6 +155,8 @@ def __call__(self, ``[a, b]``. """ + if nu is None: + nu = 0 return self._spline(x, nu=nu, extrapolate=extrapolate) @property diff --git a/csaps/_types.py b/csaps/_types.py index b960a32..2aec8fc 100644 --- a/csaps/_types.py +++ b/csaps/_types.py @@ -19,4 +19,6 @@ TProps = TypeVar('TProps', int, Tuple[int, ...]) TSmooth = TypeVar('TSmooth', float, Tuple[float, ...]) TXi = TypeVar('TXi', UnivariateDataType, NdGridDataType) +TNu = TypeVar('TNu', int, Tuple[int, ...]) +TExtrapolate = TypeVar('TExtrapolate', bool, Union[bool, str]) TSpline = TypeVar('TSpline')