Coverage for tests/test_opt.py: 26%

50 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-03-03 02:22 -0800

1# 

2# LSST Data Management System 

3# 

4# Copyright 2008-2016 AURA/LSST. 

5# 

6# This product includes software developed by the 

7# LSST Project (http://www.lsst.org/). 

8# 

9# This program is free software: you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# This program is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the LSST License Statement and 

20# the GNU General Public License along with this program. If not, 

21# see <https://www.lsstcorp.org/LegalNotices/>. 

22# 

23import unittest 

24import numpy 

25 

26import lsst.utils.tests 

27import lsst.log 

28import lsst.utils.logging 

29import lsst.meas.modelfit 

30 

31# Set trace to 0-5 to view debug messages. Level 5 enables all traces. 

32lsst.utils.logging.trace_set_at("lsst.meas.modelfit.optimizer", 5) 

33log = lsst.log.Log.getLogger("lsst.meas.modelfit.optimizer") 

34 

35 

36class OptimizerTestCase(lsst.utils.tests.TestCase): 

37 

38 def setUp(self): 

39 numpy.random.seed(500) 

40 

41 def testTrustRegionSolver(self): 

42 tolerance = 1E-6 

43 # start with some positive definite matrices, constructed from random least-squares problems 

44 log.info("Testing solveTrustRegion with positive-definite matrices") 

45 m = numpy.random.randn(30, 5) 

46 y = numpy.random.randn(30) 

47 f = numpy.dot(m.transpose(), m) 

48 g = numpy.dot(m.transpose(), y) 

49 x = numpy.zeros(5) 

50 for r in numpy.linspace(1E-3, 0.8, 5): 

51 lsst.meas.modelfit.solveTrustRegion(x, f, g, r, tolerance) 

52 self.assertLessEqual(numpy.linalg.norm(x), r * (1.0 + tolerance)) 

53 # now we try some matrices with zero eigenvalues due to model degeneracies 

54 log.info("Testing solveTrustRegion with positive-semidefinite matrices") 

55 m[:, -1] = m[:, 0] 

56 f = numpy.dot(m.transpose(), m) 

57 g = numpy.dot(m.transpose(), y) 

58 for r in numpy.linspace(1E-3, 0.8, 5): 

59 lsst.meas.modelfit.solveTrustRegion(x, f, g, r, tolerance) 

60 self.assertLessEqual(numpy.linalg.norm(x), r * (1.0 + tolerance)) 

61 m[:, -2] = m[:, 1] 

62 f = numpy.dot(m.transpose(), m) 

63 g = numpy.dot(m.transpose(), y) 

64 for r in numpy.linspace(1E-3, 0.8, 5): 

65 lsst.meas.modelfit.solveTrustRegion(x, f, g, r, tolerance) 

66 self.assertLessEqual(numpy.linalg.norm(x), r * (1.0 + tolerance)) 

67 log.info("Testing solveTrustRegion with indefinite matrices") 

68 for i in range(3): 

69 m = numpy.random.randn(5, 5) 

70 f = m + m.transpose() 

71 g = numpy.random.randn(5) 

72 for r in numpy.linspace(1E-3, 0.8, 5): 

73 lsst.meas.modelfit.solveTrustRegion(x, f, g, r, tolerance) 

74 self.assertLessEqual(numpy.linalg.norm(x), r * (1.0 + tolerance)) 

75 

76 

77class TestMemory(lsst.utils.tests.MemoryTestCase): 

78 pass 

79 

80 

81def setup_module(module): 

82 lsst.utils.tests.init() 

83 

84 

85if __name__ == "__main__": 85 ↛ 86line 85 didn't jump to line 86, because the condition on line 85 was never true

86 lsst.utils.tests.init() 

87 unittest.main()