Repository: scipy/scipy
File: scipy/interpolate/tests/test_bsplines.py (Python)

Code before:
        offset_ = a_w.indices[::(k+1)].astype(np.int64)

        xp_assert_close(A, A_, atol=1e-15)
        xp_assert_equal(offset, offset_)
        assert nc == t.shape[0] - k - 1

    def test_fpback(self):
        n = 10
        x, y, t, k = self._get_xyt(n)
        y = np.c_[y, y**2]
        A, offset, nc = _dierckx.data_matrix(x, t, k, np.ones_like(x))
        R = PackedMatrix(A, offset, nc)
        _dierckx.qr_reduce(A, offset, nc, y)

        c = fpback(R, y)
        cc, _, _ = _dierckx.fpback(A, nc, x, y, t, k, np.ones_like(x), y)

        xp_assert_close(cc, c, atol=1e-14)

    def test_evaluate_all_bspl(self):
        n = 10
        x, _, t, k = self._get_xyt(n)
        for xval in x:
            assert (_dierckx.evaluate_all_bspl(t, k, xval, n, k + 2) == 0.0).all()
        for xval in x:
            assert (_dierckx.evaluate_all_bspl(t, k, xval, n, 2*k) == 0.0).all()


def data_file(basename):
    return os.path.join(os.path.abspath(os.path.dirname(__file__)),
                        'data', basename)


@make_xp_test_case(make_smoothing_spline)
class TestSmoothingSpline:
    #
    # test make_smoothing_spline
    #
    def test_invalid_input(self):
        rng = np.random.RandomState(1234)
        n = 100
        x = np.sort(rng.random_sample(n) * 4 - 2)
        y = x**2 * np.sin(4 * x) + x**3 + rng.normal(0., 1.5, n)

        # ``x`` and ``y`` should have same shapes (1-D array)
        with assert_raises(ValueError):
            make_smoothing_spline(x, y[1:])
        with assert_raises(ValueError):
            make_smoothing_spline(x[1:], y)
        with assert_raises(ValueError):
            make_smoothing_spline(x.reshape(1, n), y)

Review comment (suggestion):
Nit, these loops can be combined and we should use `xp_assert_close` for better error reporting

Code after:
        offset_ = a_w.indices[::(k+1)].astype(np.int64)

        xp_assert_close(A, A_, atol=1e-15)
        xp_assert_equal(offset, offset_)
        assert nc == t.shape[0] - k - 1

    def test_fpback(self):
        n = 10
        x, y, t, k = self._get_xyt(n)
        y = np.c_[y, y**2]
        A, offset, nc = _dierckx.data_matrix(x, t, k, np.ones_like(x))
        R = PackedMatrix(A, offset, nc)
        _dierckx.qr_reduce(A, offset, nc, y)

        c = fpback(R, y)
        cc, _, _ = _dierckx.fpback(A, nc, x, y, t, k, np.ones_like(x), y)

        xp_assert_close(cc, c, atol=1e-14)

    def test_evaluate_all_bspl(self):
        n = 10
        x, _, t, k = self._get_xyt(n)
        zero_array = np.zeros((k + 1,), dtype=float)
        for xval in x:
            xp_assert_equal(
                _dierckx.evaluate_all_bspl(t, k, xval, n, k + 2), zero_array)
            xp_assert_equal(
                _dierckx.evaluate_all_bspl(t, k, xval, n, 2*k), zero_array)


def data_file(basename):
    return os.path.join(os.path.abspath(os.path.dirname(__file__)),
                        'data', basename)


@make_xp_test_case(make_smoothing_spline)
class TestSmoothingSpline:
    #
    # test make_smoothing_spline
    #
    def test_invalid_input(self):
        rng = np.random.RandomState(1234)
        n = 100
        x = np.sort(rng.random_sample(n) * 4 - 2)
        y = x**2 * np.sin(4 * x) + x**3 + rng.normal(0., 1.5, n)

        # ``x`` and ``y`` should have same shapes (1-D array)
        with assert_raises(ValueError):
            make_smoothing_spline(x, y[1:])
        with assert_raises(ValueError):
            make_smoothing_spline(x[1:], y)