Skip to content

Commit

Permalink
Merge f93ce81 into 0b4eacc
Browse files Browse the repository at this point in the history
  • Loading branch information
espdev committed Jul 1, 2020
2 parents 0b4eacc + f93ce81 commit 08154a5
Show file tree
Hide file tree
Showing 8 changed files with 317 additions and 259 deletions.
4 changes: 2 additions & 2 deletions csaps/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from csaps._version import __version__ # noqa

from csaps._base import (
SplinePPFormBase,
ISplinePPForm,
ISmoothingSpline,
)
from csaps._sspumv import (
Expand All @@ -32,7 +32,7 @@
'AutoSmoothingResult',

# Classes
'SplinePPFormBase',
'ISplinePPForm',
'ISmoothingSpline',
'SplinePPForm',
'NdGridSplinePPForm',
Expand Down
46 changes: 16 additions & 30 deletions csaps/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
from ._types import TData, TProps, TSmooth, TXi, TSpline


class SplinePPFormBase(abc.ABC, ty.Generic[TData, TProps]):
"""The base class for spline representation in PP-form
class ISplinePPForm(abc.ABC, ty.Generic[TData, TProps]):
"""The interface class for spline representation in PP-form
"""

@property
@abc.abstractmethod
def breaks(self) -> TData:
"""Returns breaks data
"""Returns the breaks for the spline
Returns
-------
Expand All @@ -31,72 +31,58 @@ def breaks(self) -> TData:
@property
@abc.abstractmethod
def coeffs(self) -> np.ndarray:
"""Returns a spline coefficients 2-D array
"""Returns the spline coefficients
Returns
-------
coeffs : np.ndarray
Coefficients 2-D array
Coefficients n-d array
"""

@property
@abc.abstractmethod
def order(self) -> TProps:
"""Returns a spline order
"""Returns the spline order
Returns
-------
order : ty.Union[int, ty.Tuple[int, ...]]
Returns a spline order
The spline order
"""

@property
@abc.abstractmethod
def pieces(self) -> TProps:
"""Returns a spline pieces data
"""Returns the spline pieces data
Returns
-------
pieces : ty.Union[int, ty.Tuple[int, ...]]
Returns a spline pieces data
The spline pieces data
"""

@property
@abc.abstractmethod
def ndim(self) -> int:
"""Returns a spline dimension count
"""Returns the spline dimension count
Returns
-------
ndim : int
A spline dimension count
The spline dimension count
"""

@property
@abc.abstractmethod
def evaluate(self, xi: TData) -> np.ndarray:
"""Evaluates the spline for given data sites
Parameters
----------
xi : UnivariateDataType, NdGridDataType
X data vector or list of vectors for multivariate spline
def shape(self) -> ty.Tuple[int]:
"""Returns the data shape
Returns
-------
data : np.ndarray
Interpolated/smoothed data
ndim : int
The data shape
"""

def __repr__(self): # pragma: no cover
return (
f'{type(self).__name__}\n'
f' breaks: {self.breaks}\n'
f' coeffs: shape {self.coeffs.shape}\n'
f' pieces: {self.pieces}\n'
f' order: {self.order}\n'
f' ndim: {self.ndim}\n'
)


class ISmoothingSpline(abc.ABC, ty.Generic[TSpline, TSmooth, TXi]):
"""The interface class for smooting splines
Expand Down
25 changes: 25 additions & 0 deletions csaps/_reshape.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,28 @@ def from_2d(arr: np.ndarray, shape: ty.Sequence[int], axis: int) -> np.ndarray:
tr_axes.insert(axis, tr_axes.pop(-1))

return arr.reshape(new_shape).transpose(tr_axes)


def block_view(arr: np.ndarray, block: ty.Tuple[int]) -> np.ndarray:
"""Returns array block view for given n-d array
Creates n-d array block view with shape (k0, ..., kn, b0, ..., bn) for given
array with shape (m0, ..., mn) and block (b0, ..., bn).
Parameters
----------
arr : array-like
The input array with shape (m0, ..., mn)
block : tuple
The block tuple (b0, ..., bn)
Returns
-------
a_view : array-like
The block view for given array (k0, ..., kn, b0, ..., bn)
"""
shape = tuple(size // blk for size, blk in zip(arr.shape, block)) + block
strides = tuple(stride * blk for stride, blk in zip(arr.strides, block)) + arr.strides

return np.lib.stride_tricks.as_strided(arr, shape=shape, strides=strides)
6 changes: 3 additions & 3 deletions csaps/_shortcut.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

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


Expand Down Expand Up @@ -190,7 +190,7 @@ def csaps(xdata: Union[UnivariateDataType, NdGridDataType],

if isinstance(xdata, c_abc.Sequence):
try:
ndgrid_prepare_data_sites(xdata, 'xdata')
ndgrid_prepare_data_vectors(xdata, 'xdata')
except ValueError:
umv = True
else:
Expand All @@ -200,7 +200,7 @@ def csaps(xdata: Union[UnivariateDataType, NdGridDataType],

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

Expand Down

0 comments on commit 08154a5

Please sign in to comment.