Coverage for tests/test_centroid.py: 25%
114 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-11-01 10:01 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2023-11-01 10:01 +0000
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 math
23import unittest
25import lsst.geom
26import lsst.afw.image as afwImage
27import lsst.afw.geom as afwGeom
28import lsst.afw.table as afwTable
29import lsst.meas.base as measBase
30import lsst.utils.tests as utilsTests
31import lsst.afw.detection as afwDetection
33try:
34 type(verbose)
35except NameError:
36 display = False
37 verbose = 0
40class CentroidTestCase(utilsTests.TestCase):
41 """A test case for centroiding.
42 """
44 def setUp(self):
45 pass
47 def tearDown(self):
48 pass
50 def testCleanup(self):
51 """Test that tearDown does"""
52 pass
54 def do_testAstrometry(self, alg, bkgd, control):
55 """Test that we can instantiate and play with a centroiding algorithm.
56 """
58 schema = afwTable.SourceTable.makeMinimalSchema()
59 schema.getAliasMap().set("slot_Centroid", "test")
60 centroider = alg(control, "test", schema)
61 table = afwTable.SourceCatalog(schema)
63 x0, y0 = 12345, 54321
64 for imageFactory in (afwImage.MaskedImageF,):
66 im = imageFactory(lsst.geom.ExtentI(100, 100))
67 im.setXY0(lsst.geom.Point2I(x0, y0))
69 # This fixed DoubleGaussianPsf replaces a computer generated one.
70 # The values are not anything in particular, just a reasonable size.
71 psf = afwDetection.GaussianPsf(15, 15, 3.0)
72 exp = afwImage.makeExposure(im)
73 exp.setPsf(psf)
75 im.set(bkgd)
76 x, y = 30, 20
77 im.image[x, y, afwImage.LOCAL] = 1010
79 source = table.addNew()
80 spans = afwGeom.SpanSet(exp.getBBox(afwImage.LOCAL))
81 foot = afwDetection.Footprint(spans)
82 foot.addPeak(x + x0, y + y0, 1010)
83 source.setFootprint(foot)
84 centroider.measure(source, exp)
86 self.assertFloatsAlmostEqual(x + x0, source.getX(), rtol=.00001)
87 self.assertFloatsAlmostEqual(y + y0, source.getY(), rtol=.00001)
88 self.assertFalse(source.get("test_flag"))
90 im.set(bkgd)
91 im.image[10, 20, afwImage.LOCAL] = 1010
92 im.image[10, 21, afwImage.LOCAL] = 1010
93 im.image[11, 20, afwImage.LOCAL] = 1010
94 im.image[11, 21, afwImage.LOCAL] = 1010
96 x, y = 10.5 + x0, 20.5 + y0
97 source = table.addNew()
98 source.set('test_x', x)
99 source.set('test_y', y)
100 centroider.measure(source, exp)
101 self.assertFloatsAlmostEqual(x, source.getX(), rtol=.00001)
102 self.assertFloatsAlmostEqual(y, source.getY(), rtol=.00001)
103 self.assertFalse(source.get("test_flag"))
105 def testNaiveMeasureCentroid(self):
106 """Test that we can instantiate and play with naive centroids.
107 """
108 bkgd = 10.0
109 afwTable.SourceTable.makeMinimalSchema()
110 control = measBase.NaiveCentroidControl()
111 control.background = bkgd
112 control.doFootprintCheck = False
113 # Test fails even with the default of 5
114 control.maxDistToPeak = -1.0
115 self.do_testAstrometry(measBase.NaiveCentroidAlgorithm, bkgd, control)
117 def testSdssMeasureCentroid(self):
118 """Test that we can instantiate and play with SDSS centroids"""
119 control = measBase.SdssCentroidControl()
120 control.doFootprintCheck = False
121 # Test fails with the default of 1, and even 5
122 control.maxDistToPeak = -1.0
123 self.do_testAstrometry(measBase.SdssCentroidAlgorithm, 10.0, control)
126class SingleFrameMeasurementTaskTestCase(utilsTests.TestCase):
127 """A test case for the SingleFrameMeasurementTask.
128 """
130 def mySetup(self):
131 msConfig = measBase.SingleFrameMeasurementConfig()
132 msConfig.algorithms.names = ["base_SdssCentroid"]
133 msConfig.slots.centroid = "base_SdssCentroid"
134 msConfig.slots.shape = None
135 msConfig.slots.psfShape = None
136 msConfig.slots.apFlux = None
137 msConfig.slots.modelFlux = None
138 msConfig.slots.psfFlux = None
139 msConfig.slots.gaussianFlux = None
140 msConfig.slots.calibFlux = None
141 schema = afwTable.SourceTable.makeMinimalSchema()
142 task = measBase.SingleFrameMeasurementTask(schema, config=msConfig)
143 measCat = afwTable.SourceCatalog(schema)
145 source = measCat.addNew()
146 spans = afwGeom.SpanSet(self.exp.getBBox(afwImage.LOCAL))
147 fp = afwDetection.Footprint(spans)
148 fp.addPeak(50, 50, 1000.0)
149 source.setFootprint(fp)
150 # Then run the default SFM task. Results not checked
151 task.run(measCat, self.exp)
152 return source
154 def setUp(self):
155 """Make the image we'll measure.
156 """
158 self.exp = afwImage.ExposureF(100, 100)
159 psf = afwDetection.GaussianPsf(15, 15, 3.0)
160 self.exp.setPsf(psf)
161 self.I0, self.xcen, self.ycen = 1000.0, 50.5, 50.0
162 im = self.exp.getMaskedImage().getImage()
164 for i in (-1, 0, 1):
165 for j in (-1, 0, 1):
166 im[int(self.xcen) + i, int(self.ycen) + j, afwImage.LOCAL] = \
167 self.I0*(1 - 0.5*math.hypot(i - 0.5, j))
169 def tearDown(self):
170 del self.exp
172 def testCentroider(self):
173 """Measure the centroid.
174 """
175 s = self.mySetup()
177 # this does not match exactly, and it used to
178 self.assertFloatsAlmostEqual(s.getCentroid(), lsst.geom.PointD(self.xcen, self.ycen), rtol=.01)
181class MemoryTester(lsst.utils.tests.MemoryTestCase):
182 pass
185def setup_module(module):
186 lsst.utils.tests.init()
189if __name__ == "__main__": 189 ↛ 190line 189 didn't jump to line 190, because the condition on line 189 was never true
190 lsst.utils.tests.init()
191 unittest.main()