test_nnls.py 988 B

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