Coverage for tests/test_SdssShape.py: 25%
Shortcuts on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# This file is part of meas_base.
2#
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.
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 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.geom
27import lsst.afw.geom
28import lsst.meas.base
29import lsst.meas.base.tests
30import lsst.utils.tests
33class SdssShapeTestCase(lsst.meas.base.tests.AlgorithmTestCase, lsst.utils.tests.TestCase):
35 def setUp(self):
36 self.bbox = lsst.geom.Box2I(lsst.geom.Point2I(-20, -30),
37 lsst.geom.Extent2I(240, 160))
38 self.dataset = lsst.meas.base.tests.TestDataset(self.bbox)
39 # first source is a point
40 self.dataset.addSource(100000.0, lsst.geom.Point2D(50.1, 49.8))
41 # second source is extended
42 self.dataset.addSource(100000.0, lsst.geom.Point2D(149.9, 50.3),
43 lsst.afw.geom.Quadrupole(8, 9, 3))
44 self.config = self.makeSingleFrameMeasurementConfig("base_SdssShape")
46 def tearDown(self):
47 del self.bbox
48 del self.dataset
49 del self.config
51 def assertFinite(self, value):
52 self.assertTrue(np.isfinite(value), msg="%s is not finite" % (value,))
54 def _runMeasurementTask(self):
55 task = self.makeSingleFrameMeasurementTask("base_SdssShape", config=self.config)
56 exposure, catalog = self.dataset.realize(10.0, task.schema, randomSeed=0)
57 task.run(catalog, exposure)
58 return exposure, catalog
60 def _checkShape(self, result, record):
61 self.assertFloatsAlmostEqual(result.x, record.get("truth_x"), rtol=1E-2)
62 self.assertFloatsAlmostEqual(result.y, record.get("truth_y"), rtol=1E-2)
63 self.assertFloatsAlmostEqual(result.xx, record.get("truth_xx"), rtol=1E-2)
64 self.assertFloatsAlmostEqual(result.yy, record.get("truth_yy"), rtol=1E-2)
65 self.assertFloatsAlmostEqual(result.xy, record.get("truth_xy"), rtol=1E-1, atol=2E-1)
66 self.assertFinite(result.xxErr)
67 self.assertFinite(result.yyErr)
68 self.assertFinite(result.xyErr)
69 self.assertFinite(result.instFlux_xx_Cov)
70 self.assertFinite(result.instFlux_yy_Cov)
71 self.assertFinite(result.instFlux_xy_Cov)
72 self.assertFinite(result.xx_yy_Cov)
73 self.assertFinite(result.xx_xy_Cov)
74 self.assertFinite(result.yy_xy_Cov)
75 self.assertFalse(result.getFlag(lsst.meas.base.SdssShapeAlgorithm.FAILURE.number))
76 self.assertFalse(result.getFlag(lsst.meas.base.SdssShapeAlgorithm.UNWEIGHTED_BAD.number))
77 self.assertFalse(result.getFlag(lsst.meas.base.SdssShapeAlgorithm.UNWEIGHTED.number))
78 self.assertFalse(result.getFlag(lsst.meas.base.SdssShapeAlgorithm.SHIFT.number))
79 self.assertFalse(result.getFlag(lsst.meas.base.SdssShapeAlgorithm.MAXITER.number))
81 def _checkPsfShape(self, result, psfResult, psfTruth):
82 self.assertFloatsAlmostEqual(psfResult.getIxx(), psfTruth.getIxx(), rtol=1E-4)
83 self.assertFloatsAlmostEqual(psfResult.getIyy(), psfTruth.getIyy(), rtol=1E-4)
84 self.assertFloatsAlmostEqual(psfResult.getIxy(), psfTruth.getIxy(), rtol=1E-4)
85 self.assertFalse(result.getFlag(lsst.meas.base.SdssShapeAlgorithm.PSF_SHAPE_BAD.number))
87 def testMeasureGoodPsf(self):
88 """Test that we measure shapes and record the PSF shape correctly
90 Note: Given that the PSF model here is constant over the entire image, this test
91 would not catch an error in the potition at which base_SdssShape_psf is computed.
92 Such a test requires a spatially varying PSF model such that different locations
93 can be distinguished by their different PSF model shapes. Such a test exists in
94 meas_algorithms (tests/testSdssShapePsf.py), making use of the PcaPsf algorithm
95 to build the spatially varying PSF.
96 """
97 exposure, catalog = self._runMeasurementTask()
98 key = lsst.meas.base.SdssShapeResultKey(catalog.schema["base_SdssShape"])
99 psfTruth = exposure.getPsf().computeShape()
100 for record in catalog:
101 result = record.get(key)
102 self._checkShape(result, record)
103 psfResult = key.getPsfShape(record)
104 self._checkPsfShape(result, psfResult, psfTruth)
106 def testMeasureWithoutPsf(self):
107 """Test that we measure shapes correctly and do not record the PSF shape when not needed."""
108 self.config.plugins["base_SdssShape"].doMeasurePsf = False
109 _, catalog = self._runMeasurementTask()
110 key = lsst.meas.base.SdssShapeResultKey(catalog.schema["base_SdssShape"])
111 for record in catalog:
112 result = record.get(key)
113 self._checkShape(result, record)
114 self.assertNotIn("base_SdssShape_psf_xx", catalog.schema)
115 self.assertNotIn("base_SdssShape_psf_yy", catalog.schema)
116 self.assertNotIn("base_SdssShape_psf_xy", catalog.schema)
117 self.assertNotIn("base_SdssShape_flag_psf", catalog.schema)
119 def testMeasureBadPsf(self):
120 """Test that we measure shapes correctly and set a flag with the PSF is unavailable."""
121 self.config.plugins["base_SdssShape"].doMeasurePsf = True
122 task = self.makeSingleFrameMeasurementTask("base_SdssShape", config=self.config)
123 exposure, catalog = self.dataset.realize(10.0, task.schema, randomSeed=1)
124 exposure.setPsf(None) # Set PSF to None to test no PSF case
125 task.run(catalog, exposure)
126 key = lsst.meas.base.SdssShapeResultKey(catalog.schema["base_SdssShape"])
127 for record in catalog:
128 result = record.get(key)
129 self._checkShape(result, record)
130 self.assertTrue(result.getFlag(lsst.meas.base.SdssShapeAlgorithm.PSF_SHAPE_BAD.number))
133class SdssShapeTransformTestCase(lsst.meas.base.tests.FluxTransformTestCase,
134 lsst.meas.base.tests.CentroidTransformTestCase,
135 lsst.meas.base.tests.SingleFramePluginTransformSetupHelper,
136 lsst.utils.tests.TestCase):
138 name = "sdssShape"
139 controlClass = lsst.meas.base.SdssShapeControl
140 algorithmClass = lsst.meas.base.SdssShapeAlgorithm
141 transformClass = lsst.meas.base.SdssShapeTransform
142 flagNames = ("flag", "flag_unweighted", "flag_unweightedBad", "flag_shift", "flag_maxIter")
143 singleFramePlugins = ('base_SdssShape',)
144 forcedPlugins = ('base_SdssShape',)
145 testPsf = True
147 def _setFieldsInRecords(self, records, name):
148 lsst.meas.base.tests.FluxTransformTestCase._setFieldsInRecords(self, records, name)
149 lsst.meas.base.tests.CentroidTransformTestCase._setFieldsInRecords(self, records, name)
150 for record in records:
151 for field in ('xx', 'yy', 'xy', 'xxErr', 'yyErr', 'xyErr', 'psf_xx', 'psf_yy', 'psf_xy'):
152 if record.schema.join(name, field) in record.schema:
153 record[record.schema.join(name, field)] = np.random.random()
155 def _compareFieldsInRecords(self, inSrc, outSrc, name):
156 lsst.meas.base.tests.FluxTransformTestCase._compareFieldsInRecords(self, inSrc, outSrc, name)
157 lsst.meas.base.tests.CentroidTransformTestCase._compareFieldsInRecords(self, inSrc, outSrc, name)
159 inShape = lsst.meas.base.ShapeResultKey(inSrc.schema[name]).get(inSrc)
160 outShape = lsst.meas.base.ShapeResultKey(outSrc.schema[name]).get(outSrc)
162 centroid = lsst.meas.base.CentroidResultKey(inSrc.schema[name]).get(inSrc).getCentroid()
163 xform = self.calexp.getWcs().linearizePixelToSky(centroid, lsst.geom.radians)
165 trInShape = inShape.getShape().transform(xform.getLinear())
166 self.assertEqual(trInShape.getIxx(), outShape.getShape().getIxx())
167 self.assertEqual(trInShape.getIyy(), outShape.getShape().getIyy())
168 self.assertEqual(trInShape.getIxy(), outShape.getShape().getIxy())
170 m = lsst.meas.base.makeShapeTransformMatrix(xform.getLinear())
171 np.testing.assert_array_almost_equal(
172 np.dot(np.dot(m, inShape.getShapeErr()), m.transpose()), outShape.getShapeErr()
173 )
174 if self.testPsf:
175 inPsfShape = lsst.meas.base.ShapeResultKey(
176 inSrc.schema[inSrc.schema.join(name, "psf")]
177 ).get(inSrc)
178 outPsfShape = lsst.meas.base.ShapeResultKey(
179 outSrc.schema[outSrc.schema.join(name, "psf")]
180 ).get(outSrc)
181 trInPsfShape = inPsfShape.getShape().transform(xform.getLinear())
183 self.assertEqual(trInPsfShape.getIxx(), outPsfShape.getShape().getIxx())
184 self.assertEqual(trInPsfShape.getIyy(), outPsfShape.getShape().getIyy())
185 self.assertEqual(trInPsfShape.getIxy(), outPsfShape.getShape().getIxy())
186 else:
187 self.assertNotIn(inSrc.schema.join(name, "psf", "xx"), inSrc.schema)
188 self.assertNotIn(inSrc.schema.join(name, "flag", "psf"), inSrc.schema)
191class PsfSdssShapeTransformTestCase(SdssShapeTransformTestCase, lsst.utils.tests.TestCase):
192 testPsf = False
194 def makeSdssShapeControl(self):
195 ctrl = lsst.meas.base.SdssShapeControl()
196 ctrl.doMeasurePsf = False
197 return ctrl
198 controlClass = makeSdssShapeControl
201class TestMemory(lsst.utils.tests.MemoryTestCase):
202 pass
205def setup_module(module):
206 lsst.utils.tests.init()
209if __name__ == "__main__": 209 ↛ 210line 209 didn't jump to line 210, because the condition on line 209 was never true
210 lsst.utils.tests.init()
211 unittest.main()