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