Skip to content

Commit

Permalink
fix type-hints and docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
espdev committed Jul 1, 2020
1 parent f375f8d commit 3a42853
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 15 deletions.
23 changes: 13 additions & 10 deletions csaps/_base.py
Expand Up @@ -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
"""

Expand Down Expand Up @@ -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
Expand All @@ -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
"""
55 changes: 52 additions & 3 deletions csaps/_sspndg.py
Expand Up @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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)

Expand Down
14 changes: 12 additions & 2 deletions csaps/_sspumv.py
Expand Up @@ -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)

Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -147,6 +155,8 @@ def __call__(self,
``[a, b]``.
"""
if nu is None:
nu = 0
return self._spline(x, nu=nu, extrapolate=extrapolate)

@property
Expand Down
2 changes: 2 additions & 0 deletions csaps/_types.py
Expand Up @@ -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')

0 comments on commit 3a42853

Please sign in to comment.