Coverage for tests/test_pixelAreaBoundedField.py: 27%
73 statements
« prev ^ index » next coverage.py v6.4.4, created at 2022-08-26 02:33 -0700
« prev ^ index » next coverage.py v6.4.4, created at 2022-08-26 02:33 -0700
1# This file is part of afw.
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (https://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
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.
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.
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <https://www.gnu.org/licenses/>.
22import unittest
24import numpy as np
26import lsst.utils.tests
27import lsst.geom
28import lsst.afw.geom
29from lsst.afw.math import PixelAreaBoundedField
32class PixelAreaBoundedFieldTestCase(lsst.utils.tests.TestCase):
34 def setUp(self):
35 # Trivial WCS:
36 crpix = lsst.geom.Point2D(100, 100)
37 crval = lsst.geom.SpherePoint(0, 45, lsst.geom.degrees)
38 cdMatrix = lsst.afw.geom.makeCdMatrix(0.25*lsst.geom.arcseconds, 60*lsst.geom.degrees)
39 self.skyWcs = lsst.afw.geom.makeSkyWcs(crpix=crpix, crval=crval, cdMatrix=cdMatrix)
40 self.bbox = lsst.geom.Box2I(lsst.geom.Point2I(0, 0), lsst.geom.Extent2I(200, 200))
41 self.points = [crpix]
42 self.points.extend(lsst.geom.Box2D(self.bbox).getCorners())
43 self.boundedField = PixelAreaBoundedField(self.bbox, self.skyWcs, unit=lsst.geom.arcseconds)
45 def testBBox(self):
46 self.assertEqual(self.boundedField.getBBox(), self.bbox)
48 def _computeExpected(self, points):
49 """Return an array with the expected result of evaluate(point)."""
50 expect = np.zeros(len(points), dtype=float)
51 for i, point in enumerate(points):
52 scale = self.skyWcs.getPixelScale(lsst.geom.Point2D(point)).asArcseconds()
53 expect[i] = scale**2
54 return expect
56 def testEvaluate(self):
57 """Test regular evaluation (including evaluation after multiplication
58 by a scalar).
59 """
60 expect = self._computeExpected(self.points)
61 result = np.zeros(len(self.points), dtype=float)
62 result2 = np.zeros(len(self.points), dtype=float)
63 product = self.boundedField * 2.5
64 for i, point in enumerate(self.points):
65 result[i] = self.boundedField.evaluate(point)
66 result2[i] = product.evaluate(point)
67 self.assertFloatsAlmostEqual(expect, result)
68 self.assertFloatsAlmostEqual(expect*2.5, result2)
70 def testEvaluateArray(self):
71 """Test vectorized evaluation (including vectorized evaluation
72 after multiplication by a scalar).
73 """
74 xx = np.linspace(self.bbox.getMinX(), self.bbox.getMaxX())
75 yy = np.linspace(self.bbox.getMinY(), self.bbox.getMaxY())
76 xv, yv = np.meshgrid(xx, yy)
77 points = list(zip(xv.flatten(), yv.flatten()))
78 expect = self._computeExpected(points)
79 result = self.boundedField.evaluate(xv.flatten(), yv.flatten())
80 self.assertFloatsAlmostEqual(expect, result, rtol=1E-15)
81 product = self.boundedField * 2.5
82 result2 = product.evaluate(xv.flatten(), yv.flatten())
83 self.assertFloatsAlmostEqual(expect*2.5, result2)
85 def testEquality(self):
86 """Test the implementation of operator== / __eq__.
87 """
88 # not equal to a different type of BoundedField
89 other = lsst.afw.math.ChebyshevBoundedField(self.bbox, np.random.random((2, 2)))
90 self.assertNotEqual(self.boundedField, other)
92 # equal to something created with the same parameters.
93 other = PixelAreaBoundedField(self.bbox, self.skyWcs, unit=lsst.geom.arcseconds)
94 self.assertEqual(self.boundedField, other)
96 # not equal to something with different bbox.
97 newBox = lsst.geom.Box2I(self.bbox)
98 newBox.grow(10)
99 other = PixelAreaBoundedField(newBox, self.skyWcs, unit=lsst.geom.arcseconds)
100 self.assertNotEqual(self.boundedField, other)
102 # not equal to something with different units.
103 other = PixelAreaBoundedField(self.bbox, self.skyWcs, unit=lsst.geom.radians)
104 self.assertNotEqual(self.boundedField, other)
106 # not equal to something with different wcs
107 crpix = self.skyWcs.getPixelOrigin()
108 crpix.scale(10)
109 newWcs = lsst.afw.geom.makeSkyWcs(crpix=crpix,
110 crval=self.skyWcs.getSkyOrigin(),
111 cdMatrix=self.skyWcs.getCdMatrix())
112 other = PixelAreaBoundedField(self.bbox, newWcs)
113 self.assertNotEqual(self.boundedField, other)
115 def testPersistence(self):
116 """Test that we can round-trip a PixelAreaBoundedField through
117 persistence.
118 """
119 with lsst.utils.tests.getTempFilePath(".fits") as filename:
120 self.boundedField.writeFits(filename)
121 out = lsst.afw.math.PixelAreaBoundedField.readFits(filename)
122 self.assertEqual(self.boundedField, out)
125class MemoryTester(lsst.utils.tests.MemoryTestCase):
126 pass
129def setup_module(module):
130 lsst.utils.tests.init()
133if __name__ == "__main__": 133 ↛ 134line 133 didn't jump to line 134, because the condition on line 133 was never true
134 lsst.utils.tests.init()
135 unittest.main()