(self)
| 396 | class TestFitting: |
| 397 | |
| 398 | def test_hermfit(self): |
| 399 | def f(x): |
| 400 | return x*(x - 1)*(x - 2) |
| 401 | |
| 402 | def f2(x): |
| 403 | return x**4 + x**2 + 1 |
| 404 | |
| 405 | # Test exceptions |
| 406 | assert_raises(ValueError, herm.hermfit, [1], [1], -1) |
| 407 | assert_raises(TypeError, herm.hermfit, [[1]], [1], 0) |
| 408 | assert_raises(TypeError, herm.hermfit, [], [1], 0) |
| 409 | assert_raises(TypeError, herm.hermfit, [1], [[[1]]], 0) |
| 410 | assert_raises(TypeError, herm.hermfit, [1, 2], [1], 0) |
| 411 | assert_raises(TypeError, herm.hermfit, [1], [1, 2], 0) |
| 412 | assert_raises(TypeError, herm.hermfit, [1], [1], 0, w=[[1]]) |
| 413 | assert_raises(TypeError, herm.hermfit, [1], [1], 0, w=[1, 1]) |
| 414 | assert_raises(ValueError, herm.hermfit, [1], [1], [-1,]) |
| 415 | assert_raises(ValueError, herm.hermfit, [1], [1], [2, -1, 6]) |
| 416 | assert_raises(TypeError, herm.hermfit, [1], [1], []) |
| 417 | |
| 418 | # Test fit |
| 419 | x = np.linspace(0, 2) |
| 420 | y = f(x) |
| 421 | # |
| 422 | coef3 = herm.hermfit(x, y, 3) |
| 423 | assert_equal(len(coef3), 4) |
| 424 | assert_almost_equal(herm.hermval(x, coef3), y) |
| 425 | coef3 = herm.hermfit(x, y, [0, 1, 2, 3]) |
| 426 | assert_equal(len(coef3), 4) |
| 427 | assert_almost_equal(herm.hermval(x, coef3), y) |
| 428 | # |
| 429 | coef4 = herm.hermfit(x, y, 4) |
| 430 | assert_equal(len(coef4), 5) |
| 431 | assert_almost_equal(herm.hermval(x, coef4), y) |
| 432 | coef4 = herm.hermfit(x, y, [0, 1, 2, 3, 4]) |
| 433 | assert_equal(len(coef4), 5) |
| 434 | assert_almost_equal(herm.hermval(x, coef4), y) |
| 435 | # check things still work if deg is not in strict increasing |
| 436 | coef4 = herm.hermfit(x, y, [2, 3, 4, 1, 0]) |
| 437 | assert_equal(len(coef4), 5) |
| 438 | assert_almost_equal(herm.hermval(x, coef4), y) |
| 439 | # |
| 440 | coef2d = herm.hermfit(x, np.array([y, y]).T, 3) |
| 441 | assert_almost_equal(coef2d, np.array([coef3, coef3]).T) |
| 442 | coef2d = herm.hermfit(x, np.array([y, y]).T, [0, 1, 2, 3]) |
| 443 | assert_almost_equal(coef2d, np.array([coef3, coef3]).T) |
| 444 | # test weighting |
| 445 | w = np.zeros_like(x) |
| 446 | yw = y.copy() |
| 447 | w[1::2] = 1 |
| 448 | y[0::2] = 0 |
| 449 | wcoef3 = herm.hermfit(x, yw, 3, w=w) |
| 450 | assert_almost_equal(wcoef3, coef3) |
| 451 | wcoef3 = herm.hermfit(x, yw, [0, 1, 2, 3], w=w) |
| 452 | assert_almost_equal(wcoef3, coef3) |
| 453 | # |
| 454 | wcoef2d = herm.hermfit(x, np.array([yw, yw]).T, 3, w=w) |
| 455 | assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T) |
nothing calls this directly
no test coverage detected