Coverage for tests/test_hermiteTransformMatrix.py: 32%
57 statements
« prev ^ index » next coverage.py v6.4.1, created at 2022-06-09 02:57 -0700
« prev ^ index » next coverage.py v6.4.1, created at 2022-06-09 02:57 -0700
1#
2# LSST Data Management System
3# Copyright 2008-2017 LSST Corporation.
4#
5# This product includes software developed by the
6# LSST Project (http://www.lsst.org/).
7#
8# This program is free software: you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation, either version 3 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the LSST License Statement and
19# the GNU General Public License along with this program. If not,
20# see <http://www.lsstcorp.org/LegalNotices/>.
21#
23import unittest
25import numpy as np
27try:
28 import scipy.special
29except ImportError:
30 scipy = None
32import lsst.utils.tests
33import lsst.geom
34import lsst.shapelet.tests
37class HermiteTransformMatrixTestCase(lsst.shapelet.tests.ShapeletTestCase):
39 def setUp(self):
40 np.random.seed(500)
41 self.order = 4
42 self.size = lsst.shapelet.computeSize(self.order)
43 self.htm = lsst.shapelet.HermiteTransformMatrix(self.order)
45 @staticmethod
46 def ht(n):
47 """return a scipy poly1d for the nth 'alternate' Hermite polynomial (i.e. Hermite polynomial
48 with shapeley normalization)"""
49 return (scipy.poly1d([(2**n * np.pi**0.5 * scipy.special.gamma(n+1))**(-0.5)])
50 * scipy.special.hermite(n))
52 def testCoefficientMatrices(self):
53 coeff = self.htm.getCoefficientMatrix()
54 coeffInv = self.htm.getInverseCoefficientMatrix()
55 self.assertFloatsAlmostEqual(np.identity(self.order+1), np.dot(coeff, coeffInv))
56 # Both matrices should be lower-triangular
57 for i in range(0, self.order+1):
58 for j in range(i+1, self.order+1):
59 self.assertEqual(coeff[i, j], 0.0)
60 self.assertEqual(coeffInv[i, j], 0.0)
62 @unittest.skipIf(scipy is None, "Test requires SciPy")
63 def testCoefficientsAgainstHermite(self):
64 """Test coefficient matrix values against scipy Hermite polynomials"""
65 coeff = self.htm.getCoefficientMatrix()
66 for n in range(0, self.order+1):
67 poly = self.ht(n)
68 self.assertFloatsAlmostEqual(coeff[n, :n+1], poly.c[::-1], atol=1E-15)
70 @unittest.skipIf(scipy is None, "Test requires SciPy")
71 def testTransformMatrix(self):
72 s = lsst.geom.LinearTransform.makeScaling(2.0, 1.5)
73 r = lsst.geom.LinearTransform.makeRotation(0.30*lsst.geom.radians)
74 transforms = [s, r, s*r*s]
75 testPoints = np.random.randn(10, 2)
76 for transform in transforms:
77 m = self.htm.compute(transform)
78 for testPoint in testPoints:
79 assert(testPoint.size == 2)
80 origPoint = lsst.geom.Point2D(testPoint[0], testPoint[1])
81 transPoint = transform(origPoint)
82 for i, inx, iny in lsst.shapelet.HermiteIndexGenerator(self.order):
83 v1 = self.ht(inx)(transPoint.getX()) * self.ht(iny)(transPoint.getY())
84 v2 = 0.0
85 for j, jnx, jny in lsst.shapelet.HermiteIndexGenerator(self.order):
86 v2 += m[i, j] * self.ht(jnx)(origPoint.getX()) * self.ht(jny)(origPoint.getY())
87 self.assertFloatsAlmostEqual(v1, v2, rtol=1E-11)
90class MemoryTester(lsst.utils.tests.MemoryTestCase):
91 pass
94def setup_module(module):
95 lsst.utils.tests.init()
98if __name__ == "__main__": 98 ↛ 99line 98 didn't jump to line 99, because the condition on line 98 was never true
99 lsst.utils.tests.init()
100 unittest.main()