Skip to content

Commit

Permalink
Increase test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
espdev committed Jan 21, 2020
1 parent a6d296a commit ce0cc85
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 12 deletions.
2 changes: 1 addition & 1 deletion csaps/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def evaluate(self, xi: TData) -> np.ndarray:
Interpolated/smoothed data
"""

def __repr__(self):
def __repr__(self): # pragma: no cover
return (
f'{type(self).__name__}\n'
f' breaks: {self.breaks}\n'
Expand Down
4 changes: 2 additions & 2 deletions csaps/_reshape.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def to_2d(arr: np.ndarray, axis: int) -> np.ndarray:
arr = np.asarray(arr)
axis = arr.ndim + axis if axis < 0 else axis

if axis >= arr.ndim:
if axis >= arr.ndim: # pragma: no cover
raise ValueError(f'axis {axis} is out of array axes {arr.ndim}')

tr_axes = list(range(arr.ndim))
Expand Down Expand Up @@ -135,7 +135,7 @@ def from_2d(arr: np.ndarray, shape: ty.Sequence[int], axis: int) -> np.ndarray:
ndim = len(shape)
axis = ndim + axis if axis < 0 else axis

if axis >= ndim:
if axis >= ndim: # pragma: no cover
raise ValueError(f'axis {axis} is out of N-D array axes {ndim}')

new_shape = list(shape)
Expand Down
2 changes: 1 addition & 1 deletion csaps/_sspndg.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def __call__(self, xi: NdGridDataType) -> np.ndarray:
"""
xi = ndgrid_prepare_data_sites(xi, 'xi')

if len(xi) != self._ndim:
if len(xi) != self._ndim: # pragma: no cover
raise ValueError(f'xi ({len(xi)}) and xdata ({self._ndim}) dimensions mismatch')

return self._spline.evaluate(xi)
Expand Down
6 changes: 3 additions & 3 deletions csaps/_sspumv.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,14 +331,14 @@ def __init__(self,
xdata, ydata, weights=weights, smooth=smooth, axis=axis)

@property
def smooth(self) -> TSmooth:
def smooth(self) -> float:
return self._cssp.smooth

@property
def spline(self) -> TSpline:
def spline(self) -> SplinePPForm:
return self._cssp.spline

def __call__(self, xi: TXi) -> np.ndarray:
def __call__(self, xi: UnivariateDataType) -> np.ndarray:
return self._cssp(xi)


Expand Down
4 changes: 4 additions & 0 deletions tests/test_multivariate.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ def test_auto_tdata():
t = [0., 3.74165739, 8.10055633, 12.68313203]

sp = csaps.MultivariateCubicSmoothingSpline(data)

assert isinstance(sp.spline, csaps.SplinePPForm)
assert 0 < sp.smooth < 1
assert sp(t).shape == (3, 4)
np.testing.assert_allclose(sp.t, t)
11 changes: 9 additions & 2 deletions tests/test_gridded.py → tests/test_ndgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
([[1, 2, 3], [1, 2, 3]], np.ones((3, 3)), [1, 2, 3], None),
([[1, 2, 3], [1, 2, 3]], np.ones((3, 3)), [[1, 2, 3]], None),
([[1, 2, 3], [1, 2, 3]], np.ones((3, 3)), [[1, 2], [1, 2]], None),
([[1, 2, 3], [1, 2, 3]], np.ones((3, 3)), None, [0.5, 0.4, 0.2])
([[1, 2, 3], [1, 2, 3]], np.ones((3, 3)), None, [0.5, 0.4, 0.2]),
(np.array([[1, 2, 3], [4, 5, 6]]), np.ones((3, 3)), None, None),
([np.arange(6).reshape(2, 3), np.arange(6).reshape(2, 3)], np.ones((6, 6)), None, None),
])
def test_invalid_data(x, y, w, p):
with pytest.raises((ValueError, TypeError)):
Expand All @@ -33,4 +35,9 @@ def test_surface():
noisy = ydata + (np.random.randn(*ydata.shape) * 0.75)

sp = csaps.NdGridCubicSmoothingSpline(xdata, noisy)
_ = sp(xdata)
noisy_s = sp(xdata)

assert isinstance(sp.smooth, tuple)
assert len(sp.smooth) == 2
assert isinstance(sp.spline, csaps.NdGridSplinePPForm)
assert noisy_s.shape == noisy.shape
12 changes: 9 additions & 3 deletions tests/test_shortcut.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest
import numpy as np

from csaps import csaps, AutoSmoothingResult, UnivariateCubicSmoothingSpline, NdGridCubicSmoothingSpline
from csaps import csaps, AutoSmoothingResult, CubicSmoothingSpline, NdGridCubicSmoothingSpline


@pytest.fixture(scope='module')
Expand Down Expand Up @@ -34,7 +34,7 @@ def data(curve, surface, request):
if request.param == 'univariate':
x, y = curve
xi = np.linspace(x[0], x[-1], 150)
return x, y, xi, 0.85, UnivariateCubicSmoothingSpline
return x, y, xi, 0.85, CubicSmoothingSpline

elif request.param == 'ndgrid':
x, y = surface
Expand All @@ -46,9 +46,15 @@ def data(curve, surface, request):
'univariate',
'ndgrid',
], indirect=True)
def test_shortcut_output(data):
@pytest.mark.parametrize('tolist', [True, False])
def test_shortcut_output(data, tolist):
x, y, xi, smooth, sp_cls = data

if tolist and isinstance(x, np.ndarray):
x = x.tolist()
y = y.tolist()
xi = xi.tolist()

yi = csaps(x, y, xi, smooth=smooth)
assert isinstance(yi, np.ndarray)

Expand Down
1 change: 1 addition & 0 deletions tests/test_univariate.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ def test_auto_smooth():
xi = np.linspace(x[0], x[-1], 120)
yi = sp(xi)

assert isinstance(sp.spline, csaps.SplinePPForm)
np.testing.assert_almost_equal(sp.smooth, 0.996566686)

desired_yi = [
Expand Down

0 comments on commit ce0cc85

Please sign in to comment.