def test_nd_array(shape: tuple): xdata = [np.arange(s) for s in shape] ydata = np.arange(0, np.prod(shape)).reshape(shape) sp = csaps.NdGridCubicSmoothingSpline(xdata, ydata, smooth=1.0) ydata_s = sp(xdata) assert ydata_s == pytest.approx(ydata)
def test_invalid_evaluate_data(x, xi): np.random.seed(1234) y = np.random.randn(*tuple(len(xx) for xx in x)) s = csaps.NdGridCubicSmoothingSpline(x, y) with pytest.raises((ValueError, TypeError)): s(xi)
def test_ndgrid_2d_integrate(ndgrid_2d_data): xy = ndgrid_2d_data.xy z = ndgrid_2d_data.z integral_expected = ndgrid_2d_data.integral spline = csaps.NdGridCubicSmoothingSpline(xy, z, smooth=None).spline integral = spline.integrate(((xy[0][0], xy[0][-1]), (xy[1][0], xy[1][-1]))) assert integral == pytest.approx(integral_expected)
def test_surface(surface_data): xdata, ydata = surface_data sp = csaps.NdGridCubicSmoothingSpline(xdata, ydata) noisy_s = sp(xdata) assert isinstance(sp.smooth, tuple) assert len(sp.smooth) == 2 assert isinstance(sp.spline, csaps.NdGridSplinePPForm) assert noisy_s.shape == ydata.shape
def test_auto_smooth_2d(ndgrid_2d_data): xy = ndgrid_2d_data.xy z = ndgrid_2d_data.z zi_expected = ndgrid_2d_data.zi smooth_expected = ndgrid_2d_data.smooth s = csaps.NdGridCubicSmoothingSpline(xy, z, smooth=None) zi = s(xy) assert s.smooth == pytest.approx(smooth_expected) assert zi == pytest.approx(zi_expected)
def test_smooth_factor(shape, smooth): x = [np.arange(s) for s in shape] y = np.arange(0, np.prod(shape)).reshape(shape) sp = csaps.NdGridCubicSmoothingSpline(x, y, smooth=smooth) if isinstance(smooth, tuple): expected_smooth = smooth else: expected_smooth = tuple([smooth] * len(shape)) assert sp.smooth == pytest.approx(expected_smooth)
def test_ndsplineppform(shape, coeffs_shape, order, pieces, ndim): x = tuple(np.arange(s) for s in shape) y = np.arange(float(np.prod(shape))).reshape(shape) ss = csaps.NdGridCubicSmoothingSpline(x, y).spline assert all(np.allclose(bi, xi) for bi, xi in zip(ss.breaks, x)) assert ss.coeffs.shape == coeffs_shape assert ss.order == order assert ss.pieces == pieces assert ss.ndim == ndim assert ss.shape == shape
def test_ndgrid_2d_antiderivative(ndgrid_2d_data): xy = ndgrid_2d_data.xy z = ndgrid_2d_data.z zi_ad1_expected = ndgrid_2d_data.zi_ad1 spline = csaps.NdGridCubicSmoothingSpline(xy, z, smooth=None).spline spline_ad1: csaps.NdGridSplinePPForm = spline.antiderivative(nu=(1, 1)) zi_ad1 = spline_ad1(xy) assert spline_ad1.order == (5, 5) assert zi_ad1 == pytest.approx(zi_ad1_expected)
def test_surface_smoothing(): xdata = [np.linspace(-3, 3, 61), np.linspace(-3.5, 3.5, 51)] i, j = np.meshgrid(*xdata, indexing='ij') ydata = (3 * (1 - j)**2. * np.exp(-(j**2) - (i + 1)**2) - 10 * (j / 5 - j**3 - i**5) * np.exp(-j**2 - i**2) - 1 / 3 * np.exp(-(j + 1)**2 - i**2)) np.random.seed(12345) noisy = ydata + (np.random.randn(*ydata.shape) * 0.75) sp = csaps.NdGridCubicSmoothingSpline(xdata, noisy) _ = sp(xdata)
def test_evaluate_nu_extrapolate(nu: tuple, extrapolate: bool): x = ([1, 2, 3, 4], [1, 2, 3, 4]) xi = ([0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5]) y = np.arange(4 * 4).reshape((4, 4)) ss = csaps.NdGridCubicSmoothingSpline(x, y, smooth=1.0) y_ss = ss(xi, nu=nu, extrapolate=extrapolate) pp = NdPPoly(ss.spline.c, x) xx = tuple(np.meshgrid(*xi, indexing='ij')) y_pp = pp(xx, nu=nu, extrapolate=extrapolate) np.testing.assert_allclose(y_ss, y_pp, rtol=1e-05, atol=1e-08, equal_nan=True)
def test_surface(): xdata = [np.linspace(-3, 3, 61), np.linspace(-3.5, 3.5, 51)] i, j = np.meshgrid(*xdata, indexing='ij') ydata = (3 * (1 - j)**2. * np.exp(-(j**2) - (i + 1)**2) - 10 * (j / 5 - j**3 - i**5) * np.exp(-j**2 - i**2) - 1 / 3 * np.exp(-(j + 1)**2 - i**2)) np.random.seed(12345) noisy = ydata + (np.random.randn(*ydata.shape) * 0.75) sp = csaps.NdGridCubicSmoothingSpline(xdata, noisy) 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
def test_ndgrid_2d_derivative(ndgrid_2d_data): xy = ndgrid_2d_data.xy z = ndgrid_2d_data.z zi_d1_expected = ndgrid_2d_data.zi_d1 zi_d2_expected = ndgrid_2d_data.zi_d2 spline = csaps.NdGridCubicSmoothingSpline(xy, z, smooth=None).spline spline_d1: csaps.NdGridSplinePPForm = spline.derivative(nu=(1, 1)) spline_d2: csaps.NdGridSplinePPForm = spline.derivative(nu=(2, 2)) zi_d1 = spline_d1(xy) zi_d2 = spline_d2(xy) assert spline_d1.order == (3, 3) assert spline_d2.order == (2, 2) assert zi_d1 == pytest.approx(zi_d1_expected) assert zi_d2 == pytest.approx(zi_d2_expected)
def test_grid_invalid_data(x, y, w, p): with pytest.raises((ValueError, TypeError)): csaps.NdGridCubicSmoothingSpline(x, y, w, p)