Coverage for tests/test_ImportCentroidAlgorithm.py: 25%
74 statements
« prev ^ index » next coverage.py v7.5.0, created at 2024-05-02 03:47 -0700
« prev ^ index » next coverage.py v7.5.0, created at 2024-05-02 03:47 -0700
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.image as afwImage
28import lsst.afw.table as afwTable
29import lsst.meas.base
30import lsst.utils.tests
31import testLib
33try:
34 type(verbose)
35except NameError:
36 verbose = 0
39class CentroidTestCase(lsst.utils.tests.TestCase):
40 """A test case for centroiding.
41 """
43 def testApplyCentroid(self):
44 """Test that we can instantiate and play with SillyMeasureCentroid.
45 """
47 for imageFactory in (
48 afwImage.MaskedImageF,
49 ):
50 im = imageFactory(lsst.geom.ExtentI(100, 100))
51 exp = afwImage.makeExposure(im)
52 for offset in (0, 1, 2):
53 control = testLib.SillyCentroidControl()
54 control.param = offset
55 x, y = 10, 20
56 schema = afwTable.SourceTable.makeMinimalSchema()
57 schema.addField("centroid_x", type=np.float64)
58 schema.addField("centroid_y", type=np.float64)
59 schema.getAliasMap().set("slot_Centroid", "centroid")
60 plugin = testLib.SillyCentroidAlgorithm(control, "test", schema)
61 measCat = afwTable.SourceCatalog(schema)
62 source = measCat.makeRecord()
63 source.set("centroid_x", x)
64 source.set("centroid_y", y)
65 plugin.measure(source, exp)
66 self.assertEqual(x, source.get("test_x") - offset)
67 self.assertEqual(y, source.get("test_y") - offset)
69 def testMeasureCentroid(self):
70 """Test that we can use our silly centroid through the usual Tasks.
71 """
72 testLib.SillyCentroidControl()
73 x, y = 10, 20
75 im = afwImage.MaskedImageF(lsst.geom.ExtentI(512, 512))
76 im.set(0)
77 arr = im.image.array
78 arr[y, x] = 1000
79 exp = afwImage.makeExposure(im)
81 schema = afwTable.SourceTable.makeMinimalSchema()
82 schema.addField("flags_negative", type="Flag",
83 doc="set if source was detected as significantly negative")
84 sfm_config = lsst.meas.base.sfm.SingleFrameMeasurementConfig()
85 sfm_config.plugins = ["testLib_SillyCentroid"]
86 sfm_config.plugins["testLib_SillyCentroid"].param = 5
87 sfm_config.slots.centroid = "testLib_SillyCentroid"
88 sfm_config.slots.shape = None
89 sfm_config.slots.psfShape = None
90 sfm_config.slots.psfFlux = None
91 sfm_config.slots.gaussianFlux = None
92 sfm_config.slots.calibFlux = None
93 sfm_config.slots.apFlux = None
94 sfm_config.slots.modelFlux = None
95 sfm_config.doReplaceWithNoise = False
96 task = lsst.meas.base.SingleFrameMeasurementTask(schema, config=sfm_config)
97 measCat = afwTable.SourceCatalog(schema)
98 measCat.defineCentroid("testLib_SillyCentroid")
99 source = measCat.addNew()
100 source.set("testLib_SillyCentroid_x", x)
101 source.set("testLib_SillyCentroid_y", y)
102 source.set("parent", 0)
103 source.set("flags_negative", False)
105 # now run the SFM task with the test plugin
106 task.run(measCat, exp)
107 self.assertEqual(len(measCat), 1)
108 self.assertEqual(measCat[0].getY(), y + 5)
109 self.assertEqual(measCat[0].getX(), x + 5)
112class TestMemory(lsst.utils.tests.MemoryTestCase):
113 pass
116def setup_module(module):
117 lsst.utils.tests.init()
120if __name__ == "__main__": 120 ↛ 121line 120 didn't jump to line 121, because the condition on line 120 was never true
121 lsst.utils.tests.init()
122 unittest.main()