def test_cubic(self): signal = np.array([-1.0, -0.125, 0.0, 0.125, 1.0]) expected_coeffs = np.array([0.0, 0.0, 0.0, 1.0]) expected_diff = 0.0 expected_approx = np.power(np.linspace(-1, 1, 5), 3) actual_coeffs, actual_approx, actual_diff = utils._fit_poly(3, signal) self.assertTrue(array_equal(expected_coeffs, actual_coeffs)) self.assertTrue(array_equal(expected_approx, actual_approx)) self.assertTrue(array_equal(expected_diff, actual_diff))
def test_line(self): signal = np.array([-1.0, 1.0]) expected_coeffs = np.array([0.0, 1.0]) expected_diff = 0.0 expected_approx = signal actual_coeffs, actual_approx, actual_diff = utils._fit_poly(1, signal) self.assertTrue(array_equal(expected_coeffs, actual_coeffs)) self.assertTrue(array_equal(expected_approx, actual_approx)) self.assertTrue(array_equal(expected_diff, actual_diff))
def test_grid_not_centered(self): grid = np.array([0, 1, 2, 3, 4]) signal = np.array([4, 1, 0, 1, 4]) expected_coeffs = np.array([0.0, 0.0, 1.0]) expected_diff = 0.0 expected_approx = np.power(grid - 2.0, 2) actual_coeffs, actual_approx, actual_diff = utils._fit_poly( 2, signal, grid=grid ) self.assertTrue(array_equal(expected_coeffs, actual_coeffs)) self.assertTrue(array_equal(expected_approx, actual_approx)) self.assertTrue(array_equal(expected_diff, actual_diff))
def test_too_short(self): with self.assertRaises(ValueError): utils._fit_poly(5, np.array([0.0, 1.0]))