def test_sliced_input(self): # cython code chokes on non C contiguous arrays xx = np.linspace(-1, 1, 100) x = xx[::3] y = xx[::3] t = _augknt(x, 1) make_lsq_spline(x, y, t, k=1)
def test_checkfinite(self): # check_finite defaults to True; nans and such trigger a ValueError x = np.arange(12).astype(float) y = x**2 t = _augknt(x, 3) for z in [np.nan, np.inf, -np.inf]: y[-1] = z assert_raises(ValueError, make_lsq_spline, x, y, t)
def test_cubic_deriv_unstable(self): # 1st and 2nd derivative at x[0], no derivative information at x[-1] # The problem is not that it fails [who would use this anyway], # the problem is that it fails *silently*, and I've no idea # how to detect this sort of instability. # In this particular case: it's OK for len(t) < 20, goes haywire # at larger `len(t)`. k = 3 t = _augknt(self.xx, k) der_l = [(1, 3.), (2, 4.)] b = make_interp_spline(self.xx, self.yy, k, t, bc_type=(der_l, None)) assert_allclose(b(self.xx), self.yy, atol=1e-14, rtol=1e-14)
def test_int_xy(self): x = np.arange(10).astype(np.int_) y = np.arange(10).astype(np.int_) t = _augknt(x, k=1) # cython chokes on "buffer type mismatch" make_lsq_spline(x, y, t, k=1)
class TestLSQ(object): # # Test make_lsq_spline # np.random.seed(1234) n, k = 13, 3 x = np.sort(np.random.random(n)) y = np.random.random(n) t = _augknt(np.linspace(x[0], x[-1], 7), k) def test_lstsq(self): # check LSQ construction vs a full matrix version x, y, t, k = self.x, self.y, self.t, self.k c0, AY = make_lsq_full_matrix(x, y, t, k) b = make_lsq_spline(x, y, t, k) assert_allclose(b.c, c0) assert_equal(b.c.shape, (t.size - k - 1, )) # also check against numpy.lstsq aa, yy = AY c1, _, _, _ = np.linalg.lstsq(aa, y, rcond=-1) assert_allclose(b.c, c1) def test_weights(self): # weights = 1 is same as None x, y, t, k = self.x, self.y, self.t, self.k w = np.ones_like(x) b = make_lsq_spline(x, y, t, k) b_w = make_lsq_spline(x, y, t, k, w=w) assert_allclose(b.t, b_w.t, atol=1e-14) assert_allclose(b.c, b_w.c, atol=1e-14) assert_equal(b.k, b_w.k) def test_multiple_rhs(self): x, t, k, n = self.x, self.t, self.k, self.n y = np.random.random(size=(n, 5, 6, 7)) b = make_lsq_spline(x, y, t, k) assert_equal(b.c.shape, (t.size - k - 1, 5, 6, 7)) def test_complex(self): # cmplx-valued `y` x, t, k = self.x, self.t, self.k yc = self.y * (1. + 2.j) b = make_lsq_spline(x, yc, t, k) b_re = make_lsq_spline(x, yc.real, t, k) b_im = make_lsq_spline(x, yc.imag, t, k) assert_allclose(b(x), b_re(x) + 1.j * b_im(x), atol=1e-15, rtol=1e-15) def test_int_xy(self): x = np.arange(10).astype(np.int_) y = np.arange(10).astype(np.int_) t = _augknt(x, k=1) # cython chokes on "buffer type mismatch" make_lsq_spline(x, y, t, k=1) def test_sliced_input(self): # cython code chokes on non C contiguous arrays xx = np.linspace(-1, 1, 100) x = xx[::3] y = xx[::3] t = _augknt(x, 1) make_lsq_spline(x, y, t, k=1) def test_checkfinite(self): # check_finite defaults to True; nans and such trigger a ValueError x = np.arange(12).astype(float) y = x**2 t = _augknt(x, 3) for z in [np.nan, np.inf, -np.inf]: y[-1] = z assert_raises(ValueError, make_lsq_spline, x, y, t)