Coverage for tests/test_centroid.py: 26%
112 statements
« prev ^ index » next coverage.py v6.5.0, created at 2022-11-09 11:35 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2022-11-09 11:35 +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 self.do_testAstrometry(measBase.NaiveCentroidAlgorithm, bkgd, control)
115 def testSdssMeasureCentroid(self):
116 """Test that we can instantiate and play with SDSS centroids"""
117 control = measBase.SdssCentroidControl()
118 control.doFootprintCheck = False
119 self.do_testAstrometry(measBase.SdssCentroidAlgorithm, 10.0, control)
122class SingleFrameMeasurementTaskTestCase(utilsTests.TestCase):
123 """A test case for the SingleFrameMeasurementTask.
124 """
126 def mySetup(self):
127 msConfig = measBase.SingleFrameMeasurementConfig()
128 msConfig.algorithms.names = ["base_SdssCentroid"]
129 msConfig.slots.centroid = "base_SdssCentroid"
130 msConfig.slots.shape = None
131 msConfig.slots.psfShape = None
132 msConfig.slots.apFlux = None
133 msConfig.slots.modelFlux = None
134 msConfig.slots.psfFlux = None
135 msConfig.slots.gaussianFlux = None
136 msConfig.slots.calibFlux = None
137 schema = afwTable.SourceTable.makeMinimalSchema()
138 task = measBase.SingleFrameMeasurementTask(schema, config=msConfig)
139 measCat = afwTable.SourceCatalog(schema)
141 source = measCat.addNew()
142 spans = afwGeom.SpanSet(self.exp.getBBox(afwImage.LOCAL))
143 fp = afwDetection.Footprint(spans)
144 fp.addPeak(50, 50, 1000.0)
145 source.setFootprint(fp)
146 # Then run the default SFM task. Results not checked
147 task.run(measCat, self.exp)
148 return source
150 def setUp(self):
151 """Make the image we'll measure.
152 """
154 self.exp = afwImage.ExposureF(100, 100)
155 psf = afwDetection.GaussianPsf(15, 15, 3.0)
156 self.exp.setPsf(psf)
157 self.I0, self.xcen, self.ycen = 1000.0, 50.5, 50.0
158 im = self.exp.getMaskedImage().getImage()
160 for i in (-1, 0, 1):
161 for j in (-1, 0, 1):
162 im[int(self.xcen) + i, int(self.ycen) + j, afwImage.LOCAL] = \
163 self.I0*(1 - 0.5*math.hypot(i - 0.5, j))
165 def tearDown(self):
166 del self.exp
168 def testCentroider(self):
169 """Measure the centroid.
170 """
171 s = self.mySetup()
173 # this does not match exactly, and it used to
174 self.assertFloatsAlmostEqual(s.getCentroid(), lsst.geom.PointD(self.xcen, self.ycen), rtol=.01)
177class MemoryTester(lsst.utils.tests.MemoryTestCase):
178 pass
181def setup_module(module):
182 lsst.utils.tests.init()
185if __name__ == "__main__": 185 ↛ 186line 185 didn't jump to line 186, because the condition on line 185 was never true
186 lsst.utils.tests.init()
187 unittest.main()