Coverage for tests/test_PixelFlags.py: 18%
85 statements
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-17 11:11 +0000
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-17 11:11 +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 unittest
24import numpy as np
26import lsst.geom
27import lsst.utils.tests
28import lsst.meas.base.tests
31class PixelFlagsTestCase(lsst.meas.base.tests.AlgorithmTestCase, lsst.utils.tests.TestCase):
33 def setUp(self):
34 self.center = lsst.geom.Point2D(50.1, 49.8)
35 self.bbox = lsst.geom.Box2I(lsst.geom.Point2I(-20, -30),
36 lsst.geom.Extent2I(140, 160))
37 self.dataset = lsst.meas.base.tests.TestDataset(self.bbox)
38 self.dataset.addSource(100000.0, self.center)
40 def testNoFlags(self):
41 task = self.makeSingleFrameMeasurementTask("base_PixelFlags")
42 exposure, catalog = self.dataset.realize(10.0, task.schema, randomSeed=0)
43 task.run(catalog, exposure)
44 record = catalog[0]
45 self.assertFalse(record.get("base_PixelFlags_flag"))
46 self.assertFalse(record.get("base_PixelFlags_flag_edge"))
47 self.assertFalse(record.get("base_PixelFlags_flag_interpolated"))
48 self.assertFalse(record.get("base_PixelFlags_flag_interpolatedCenter"))
49 self.assertFalse(record.get("base_PixelFlags_flag_interpolatedCenterAll"))
50 self.assertFalse(record.get("base_PixelFlags_flag_saturated"))
51 self.assertFalse(record.get("base_PixelFlags_flag_saturatedCenter"))
52 self.assertFalse(record.get("base_PixelFlags_flag_saturatedCenterAll"))
53 self.assertFalse(record.get("base_PixelFlags_flag_cr"))
54 self.assertFalse(record.get("base_PixelFlags_flag_crCenter"))
55 self.assertFalse(record.get("base_PixelFlags_flag_crCenterAll"))
56 self.assertFalse(record.get("base_PixelFlags_flag_bad"))
57 self.assertFalse(record.get("base_PixelFlags_flag_badCenter"))
58 self.assertFalse(record.get("base_PixelFlags_flag_badCenterAll"))
60 def testSomeFlags(self):
61 task = self.makeSingleFrameMeasurementTask("base_PixelFlags")
62 exposure, catalog = self.dataset.realize(10.0, task.schema, randomSeed=0)
63 # one cr pixel outside the center
64 cosmicray = exposure.mask.getPlaneBitMask("CR")
65 x = round(self.center.x)
66 y = round(self.center.y)
67 exposure.mask[x+3, y+4] |= cosmicray
68 # one interpolated pixel near the center
69 interpolated = exposure.mask.getPlaneBitMask("INTRP")
70 exposure.mask[self.center] |= interpolated
71 # all pixels in the center are bad
72 bad = exposure.mask.getPlaneBitMask("BAD")
73 exposure.mask[x-1:x+2, y-1:y+2] |= bad
74 task.run(catalog, exposure)
75 record = catalog[0]
77 self.assertTrue(record.get("base_PixelFlags_flag_cr"))
78 self.assertFalse(record.get("base_PixelFlags_flag_crCenter"))
79 self.assertFalse(record.get("base_PixelFlags_flag_crCenterAll"))
80 self.assertTrue(record.get("base_PixelFlags_flag_interpolated"))
81 self.assertTrue(record.get("base_PixelFlags_flag_interpolatedCenter"))
82 self.assertFalse(record.get("base_PixelFlags_flag_interpolatedCenterAll"))
83 self.assertTrue(record.get("base_PixelFlags_flag_bad"))
84 self.assertTrue(record.get("base_PixelFlags_flag_badCenter"))
85 self.assertTrue(record.get("base_PixelFlags_flag_badCenterAll"))
87 def testOffimageNonfinite(self):
88 """Test that flag_offimage and flag_edge are set correctly for
89 nonfinite sources.
90 """
91 # These three will be explicitly set to have a non-finite centroid; we
92 # cannot add sources off the image in TestDataset.
93 self.dataset.addSource(100000, lsst.geom.Point2D(20, 20))
94 self.dataset.addSource(100000, lsst.geom.Point2D(20, 100))
95 self.dataset.addSource(100000, lsst.geom.Point2D(100, 30))
96 task = self.makeSingleFrameMeasurementTask("base_PixelFlags")
97 exposure, catalog = self.dataset.realize(10.0, task.schema, randomSeed=0)
98 catalog[1]["slot_Centroid_x"] = np.inf
99 catalog[2]["slot_Centroid_x"] = -np.inf
100 catalog[3]["slot_Centroid_y"] = np.nan
101 task.run(catalog, exposure)
102 np.testing.assert_array_equal(catalog["base_PixelFlags_flag_offimage"],
103 np.array([False, True, True, True]))
104 np.testing.assert_array_equal(catalog["base_PixelFlags_flag_edge"],
105 np.array([False, True, True, True]))
107 def testOffimage(self):
108 """Test that sources at the boundary of the image get flag_offimage
109 and flag_edge set appropriately.
110 """
111 # These four will be explicitly set to values at the boundary; we
112 # cannot add sources off the image in TestDataset.
113 self.dataset.addSource(100000, lsst.geom.Point2D(20, 20))
114 self.dataset.addSource(100000, lsst.geom.Point2D(20, 100))
115 self.dataset.addSource(100000, lsst.geom.Point2D(80, 100))
116 self.dataset.addSource(100000, lsst.geom.Point2D(80, 30))
117 task = self.makeSingleFrameMeasurementTask("base_PixelFlags")
118 exposure, catalog = self.dataset.realize(10.0, task.schema, randomSeed=0)
119 catalog[1]["slot_Centroid_x"] = -20.5 # on image
120 catalog[2]["slot_Centroid_x"] = -20.51 # off image
121 catalog[3]["slot_Centroid_y"] = 129.4 # on image
122 catalog[4]["slot_Centroid_y"] = 129.50 # off image
123 task.run(catalog, exposure)
124 np.testing.assert_array_equal(catalog["base_PixelFlags_flag_offimage"],
125 np.array([False, False, True, False, True]))
126 np.testing.assert_array_equal(catalog["base_PixelFlags_flag_edge"],
127 np.array([False, False, True, False, True]))
130class TestMemory(lsst.utils.tests.MemoryTestCase):
131 pass
134def setup_module(module):
135 lsst.utils.tests.init()
138if __name__ == "__main__": 138 ↛ 139line 138 didn't jump to line 139, because the condition on line 138 was never true
139 lsst.utils.tests.init()
140 unittest.main()