Coverage for tests/test_shapeletFunction.py: 18%
110 statements
« prev ^ index » next coverage.py v6.5.0, created at 2023-02-07 10:55 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2023-02-07 10:55 +0000
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
24import pickle
26import numpy as np
28try:
29 import scipy.special
30except ImportError:
31 scipy = None
33import lsst.utils.tests
34import lsst.shapelet.tests
35import lsst.afw.image
36import lsst.geom as geom
37import lsst.afw.geom.ellipses as ellipses
40class ShapeletFunctionTestCase(lsst.shapelet.tests.ShapeletTestCase):
42 def setUp(self):
43 np.random.seed(500)
44 order = 4
45 self.ellipse = ellipses.Ellipse(ellipses.Axes(2.2, 0.8, 0.3), geom.Point2D(0.12, -0.08))
46 self.coefficients = np.random.randn(lsst.shapelet.computeSize(order))
47 self.x = np.random.randn(25)
48 self.y = np.random.randn(25)
49 self.bases = [
50 lsst.shapelet.BasisEvaluator(order, lsst.shapelet.HERMITE),
51 lsst.shapelet.BasisEvaluator(order, lsst.shapelet.LAGUERRE),
52 ]
53 self.functions = [
54 lsst.shapelet.ShapeletFunction(order, lsst.shapelet.HERMITE, self.coefficients),
55 lsst.shapelet.ShapeletFunction(order, lsst.shapelet.LAGUERRE, self.coefficients),
56 ]
57 for function in self.functions:
58 function.setEllipse(self.ellipse)
60 def testPickle(self):
61 for function in self.functions:
62 s = pickle.dumps(function, protocol=2)
63 function2 = pickle.loads(s)
64 self.assertEqual(function.getOrder(), function2.getOrder())
65 self.assertEqual(function.getBasisType(), function2.getBasisType())
66 self.assertFloatsAlmostEqual(function.getEllipse().getParameterVector(),
67 function2.getEllipse().getParameterVector())
68 self.assertFloatsAlmostEqual(function.getCoefficients(), function2.getCoefficients())
70 def testConversion(self):
71 for basis, function in zip(self.bases, self.functions):
72 evaluator = function.evaluate()
73 v = np.zeros(self.coefficients.shape, dtype=float)
74 t = self.ellipse.getGridTransform()
75 for x, y in zip(self.x, self.y):
76 basis.fillEvaluation(v, t(geom.Point2D(x, y)))
77 p1 = evaluator(x, y)
78 p2 = np.dot(v, self.coefficients) * t.getLinear().computeDeterminant()
79 self.assertFloatsAlmostEqual(p1, p2, rtol=1E-8)
80 v = np.zeros(self.coefficients.shape, dtype=float)
81 basis.fillIntegration(v)
82 p1 = evaluator.integrate()
83 p2 = np.dot(v, self.coefficients)
84 self.assertFloatsAlmostEqual(p1, p2, rtol=1E-8)
86 def testMoments(self):
87 x = np.linspace(-15, 15, 151)
88 y = x
89 for function in self.functions:
90 z = self.makeImage(function, x, y)
91 self.checkMoments(function, x, y, z)
93 def testDerivatives(self):
94 eps = 1E-7
95 v = np.zeros(self.coefficients.shape, dtype=float)
96 v_lo = np.zeros(self.coefficients.shape, dtype=float)
97 v_hi = np.zeros(self.coefficients.shape, dtype=float)
98 dx_a = np.zeros(self.coefficients.shape, dtype=float)
99 dy_a = np.zeros(self.coefficients.shape, dtype=float)
100 for basis in self.bases:
101 for x, y in zip(self.x, self.y):
102 basis.fillEvaluation(v, x, y, dx_a, dy_a)
103 basis.fillEvaluation(v_hi, x+eps, y)
104 basis.fillEvaluation(v_lo, x-eps, y)
105 dx_n = 0.5 * (v_hi - v_lo) / eps
106 basis.fillEvaluation(v_hi, x, y+eps)
107 basis.fillEvaluation(v_lo, x, y-eps)
108 dy_n = 0.5 * (v_hi - v_lo) / eps
109 self.assertFloatsAlmostEqual(dx_n, dx_a, rtol=1E-5)
110 self.assertFloatsAlmostEqual(dy_n, dy_a, rtol=1E-5)
112 def testAddToImage(self):
113 bbox = geom.Box2I(geom.Point2I(5, 6), geom.Extent2I(20, 30))
114 image = lsst.afw.image.ImageD(bbox)
115 x = np.arange(bbox.getBeginX(), bbox.getEndX(), dtype=float)
116 y = np.arange(bbox.getBeginY(), bbox.getEndY(), dtype=float)
117 array = np.zeros((bbox.getHeight(), bbox.getWidth()), dtype=float)
118 for f in self.functions:
119 image.getArray()[:] = 0.0
120 array[:] = 0.0
121 ev = f.evaluate()
122 ev.addToImage(image)
123 ev.addToImage(array, bbox.getMin())
124 check = self.makeImage(f, x, y)
125 self.assertFloatsAlmostEqual(image.getArray(), check)
126 self.assertFloatsAlmostEqual(array, check)
128 def testConvolution(self):
129 if scipy is None:
130 print("Skipping convolution test; scipy could not be imported.")
131 return
132 e1 = ellipses.Ellipse(ellipses.Axes(10, 8, 0.3), geom.Point2D(1.5, 2.0))
133 e2 = ellipses.Ellipse(ellipses.Axes(12, 9, -0.5), geom.Point2D(-1.0, -0.25))
134 f1 = lsst.shapelet.ShapeletFunction(3, lsst.shapelet.HERMITE, e1)
135 f2 = lsst.shapelet.ShapeletFunction(2, lsst.shapelet.LAGUERRE, e2)
136 f1.getCoefficients()[:] = np.random.randn(*f1.getCoefficients().shape)
137 f2.getCoefficients()[:] = np.random.randn(*f2.getCoefficients().shape)
138 fc1, fc2 = self.checkConvolution(f1, f2)
139 self.assertEqual(fc1.getBasisType(), lsst.shapelet.HERMITE)
140 self.assertEqual(fc2.getBasisType(), lsst.shapelet.LAGUERRE)
141 self.assertFloatsAlmostEqual(fc1.getEllipse().getParameterVector(),
142 fc2.getEllipse().getParameterVector())
143 self.assertEqual(fc1.getOrder(), fc2.getOrder())
144 fc2.changeBasisType(lsst.shapelet.HERMITE)
145 self.assertFloatsAlmostEqual(fc1.getCoefficients(), fc2.getCoefficients(), 1E-8)
148class MemoryTester(lsst.utils.tests.MemoryTestCase):
149 pass
152def setup_module(module):
153 lsst.utils.tests.init()
156if __name__ == "__main__": 156 ↛ 157line 156 didn't jump to line 157, because the condition on line 156 was never true
157 lsst.utils.tests.init()
158 unittest.main()