test_nnls.py 914 B

12345678910111213141516171819202122232425262728293031323334
  1. """ Unit tests for nonnegative least squares
  2. Author: Uwe Schmitt
  3. Sep 2008
  4. """
  5. import numpy as np
  6. from numpy.testing import assert_
  7. from pytest import raises as assert_raises
  8. from scipy.optimize import nnls
  9. from numpy import arange, dot
  10. from numpy.linalg import norm
  11. class TestNNLS:
  12. def test_nnls(self):
  13. a = arange(25.0).reshape(-1,5)
  14. x = arange(5.0)
  15. y = dot(a,x)
  16. x, res = nnls(a,y)
  17. assert_(res < 1e-7)
  18. assert_(norm(dot(a,x)-y) < 1e-7)
  19. def test_maxiter(self):
  20. # test that maxiter argument does stop iterations
  21. # NB: did not manage to find a test case where the default value
  22. # of maxiter is not sufficient, so use a too-small value
  23. rndm = np.random.RandomState(1234)
  24. a = rndm.uniform(size=(100, 100))
  25. b = rndm.uniform(size=100)
  26. with assert_raises(RuntimeError):
  27. nnls(a, b, maxiter=1)