Coverage for tests/test_semiEmpiricalPrior.py: 26%
59 statements
« prev ^ index » next coverage.py v7.2.3, created at 2023-04-20 04:09 -0700
« prev ^ index » next coverage.py v7.2.3, created at 2023-04-20 04:09 -0700
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 os
25import numpy
27import lsst.utils.tests
28import lsst.meas.modelfit
31class SemiEmpiricalPriorTestCase(lsst.utils.tests.TestCase):
33 NUM_DIFF_STEP = 1E-4
35 def setUp(self):
36 # a prior with broad ramps and non-zero slope; broad ramps makes evaluating numerical
37 # derivatives easier, and we want to do that to check the analytic ones
38 numpy.random.seed(500)
39 self.ctrl = lsst.meas.modelfit.SemiEmpiricalPrior.Control()
40 self.ctrl.ellipticityCore = 4.0
41 self.ctrl.ellipticitySigma = 10.0
42 self.ctrl.logRadiusMinOuter = self.ctrl.logRadiusMinInner - 8.0
43 self.ctrl.logRadiusMu = 2.0
44 self.ctrl.logRadiusSigma = 5.0
45 self.ctrl.logRadiusNu = 2.0
46 self.prior = lsst.meas.modelfit.SemiEmpiricalPrior(self.ctrl)
47 self.amplitudes = numpy.array([1.0], dtype=lsst.meas.modelfit.Scalar)
48 dtype = numpy.dtype([("eta1", float), ("eta2", float), ("lnR", float), ("p", float),
49 ("d_eta1", float), ("d_eta2", float), ("d_lnR", float),
50 ("d2_eta1_eta1", float), ("d2_eta1_eta2", float),
51 ("d2_eta1_lnR", float), ("d2_eta2_eta2", float),
52 ("d2_eta2_lnR", float), ("d2_lnR_lnR", float)])
53 self.data = numpy.loadtxt(os.path.join(os.path.dirname(
54 os.path.realpath(__file__)), "data", "SEP.txt"), dtype=dtype)
56 def tearDown(self):
57 del self.prior
58 del self.amplitudes
60 def testEvaluate(self):
61 for row in self.data:
62 p = self.prior.evaluate(numpy.array([row["eta1"], row["eta2"], row["lnR"]]), self.amplitudes)
63 self.assertFloatsAlmostEqual(row["p"], p)
65 def testGradient(self):
66 for row in self.data:
67 grad = numpy.zeros(4, dtype=float)
68 hess = numpy.zeros((4, 4), dtype=float)
69 self.prior.evaluateDerivatives(
70 numpy.array([row["eta1"], row["eta2"], row["lnR"]]),
71 self.amplitudes,
72 grad[:3], grad[3:],
73 hess[:3, :3], hess[3:, 3:], hess[:3, 3:]
74 )
75 self.assertFloatsAlmostEqual(row["d_eta1"], grad[0])
76 self.assertFloatsAlmostEqual(row["d_eta2"], grad[1])
77 self.assertFloatsAlmostEqual(row["d_lnR"], grad[2])
79 def testHessian(self):
80 for row in self.data:
81 grad = numpy.zeros(4, dtype=float)
82 hess = numpy.zeros((4, 4), dtype=float)
83 self.prior.evaluateDerivatives(
84 numpy.array([row["eta1"], row["eta2"], row["lnR"]]),
85 self.amplitudes,
86 grad[:3], grad[3:],
87 hess[:3, :3], hess[3:, 3:], hess[:3, 3:]
88 )
89 self.assertFloatsAlmostEqual(row["d2_eta1_eta1"], hess[0, 0])
90 self.assertFloatsAlmostEqual(row["d2_eta1_eta2"], hess[0, 1])
91 self.assertFloatsAlmostEqual(row["d2_eta1_lnR"], hess[0, 2])
92 self.assertFloatsAlmostEqual(row["d2_eta2_eta2"], hess[1, 1])
93 self.assertFloatsAlmostEqual(row["d2_eta2_lnR"], hess[1, 2])
94 self.assertFloatsAlmostEqual(row["d2_lnR_lnR"], hess[2, 2])
96 def evaluatePrior(self, eta1, eta2, lnR):
97 b = numpy.broadcast(eta1, eta2, lnR)
98 p = numpy.zeros(b.shape, dtype=lsst.meas.modelfit.Scalar)
99 for i, (eta1i, eta2i, lnRi) in enumerate(b):
100 p.flat[i] = self.prior.evaluate(numpy.array([eta1i, eta2i, lnRi]), self.amplitudes)
101 return p
104class TestMemory(lsst.utils.tests.MemoryTestCase):
105 pass
108def setup_module(module):
109 lsst.utils.tests.init()
112if __name__ == "__main__": 112 ↛ 113line 112 didn't jump to line 113, because the condition on line 112 was never true
113 lsst.utils.tests.init()
114 unittest.main()