Coverage for tests / test_PixelFlags.py: 13%

129 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-30 08:55 +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/>. 

21 

22import unittest 

23 

24import numpy as np 

25 

26import lsst.geom 

27import lsst.utils.tests 

28import lsst.meas.base.tests 

29 

30 

31class PixelFlagsTestCase(lsst.meas.base.tests.AlgorithmTestCase, lsst.utils.tests.TestCase): 

32 

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 

39 def testNoFlags(self): 

40 self.dataset.addSource(100000.0, self.center) 

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_edgeCenter")) 

48 self.assertFalse(record.get("base_PixelFlags_flag_edgeCenterAll")) 

49 self.assertFalse(record.get("base_PixelFlags_flag_nodata")) 

50 self.assertFalse(record.get("base_PixelFlags_flag_nodataCenter")) 

51 self.assertFalse(record.get("base_PixelFlags_flag_nodataCenterAll")) 

52 self.assertFalse(record.get("base_PixelFlags_flag_interpolated")) 

53 self.assertFalse(record.get("base_PixelFlags_flag_interpolatedCenter")) 

54 self.assertFalse(record.get("base_PixelFlags_flag_interpolatedCenterAll")) 

55 self.assertFalse(record.get("base_PixelFlags_flag_saturated")) 

56 self.assertFalse(record.get("base_PixelFlags_flag_saturatedCenter")) 

57 self.assertFalse(record.get("base_PixelFlags_flag_saturatedCenterAll")) 

58 self.assertFalse(record.get("base_PixelFlags_flag_cr")) 

59 self.assertFalse(record.get("base_PixelFlags_flag_crCenter")) 

60 self.assertFalse(record.get("base_PixelFlags_flag_crCenterAll")) 

61 self.assertFalse(record.get("base_PixelFlags_flag_bad")) 

62 self.assertFalse(record.get("base_PixelFlags_flag_badCenter")) 

63 self.assertFalse(record.get("base_PixelFlags_flag_badCenterAll")) 

64 

65 def testSomeFlags(self): 

66 self.dataset.addSource(100000.0, self.center) 

67 task = self.makeSingleFrameMeasurementTask("base_PixelFlags") 

68 exposure, catalog = self.dataset.realize(10.0, task.schema, randomSeed=0) 

69 # one cr pixel outside the center 

70 cosmicray = exposure.mask.getPlaneBitMask("CR") 

71 x = round(self.center.x) 

72 y = round(self.center.y) 

73 exposure.mask[x+3, y+4] |= cosmicray 

74 # one interpolated pixel near the center 

75 interpolated = exposure.mask.getPlaneBitMask("INTRP") 

76 exposure.mask[self.center] |= interpolated 

77 # all pixels in the center are bad 

78 bad = exposure.mask.getPlaneBitMask("BAD") 

79 exposure.mask[x-1:x+2, y-1:y+2] |= bad 

80 task.run(catalog, exposure) 

81 record = catalog[0] 

82 

83 self.assertFalse(record.get("base_PixelFlags_flag_edge")) 

84 self.assertFalse(record.get("base_PixelFlags_flag_edgeCenter")) 

85 self.assertFalse(record.get("base_PixelFlags_flag_edgeCenterAll")) 

86 self.assertFalse(record.get("base_PixelFlags_flag_nodata")) 

87 self.assertFalse(record.get("base_PixelFlags_flag_nodataCenter")) 

88 self.assertFalse(record.get("base_PixelFlags_flag_nodataCenterAll")) 

89 self.assertTrue(record.get("base_PixelFlags_flag_cr")) 

90 self.assertFalse(record.get("base_PixelFlags_flag_crCenter")) 

91 self.assertFalse(record.get("base_PixelFlags_flag_crCenterAll")) 

92 self.assertTrue(record.get("base_PixelFlags_flag_interpolated")) 

93 self.assertTrue(record.get("base_PixelFlags_flag_interpolatedCenter")) 

94 self.assertFalse(record.get("base_PixelFlags_flag_interpolatedCenterAll")) 

95 self.assertTrue(record.get("base_PixelFlags_flag_bad")) 

96 self.assertTrue(record.get("base_PixelFlags_flag_badCenter")) 

97 self.assertTrue(record.get("base_PixelFlags_flag_badCenterAll")) 

98 

99 def testOffimageNonfinite(self): 

100 """Test that flag_offimage and flag_edge are set correctly for 

101 nonfinite sources. 

102 """ 

103 # These three will be explicitly set to have a non-finite centroid; we 

104 # cannot add sources off the image in TestDataset. 

105 self.dataset.addSource(100000, lsst.geom.Point2D(20, 20)) 

106 self.dataset.addSource(100000, lsst.geom.Point2D(20, 100)) 

107 self.dataset.addSource(100000, lsst.geom.Point2D(100, 30)) 

108 task = self.makeSingleFrameMeasurementTask("base_PixelFlags") 

109 exposure, catalog = self.dataset.realize(10.0, task.schema, randomSeed=0) 

110 catalog[0]["slot_Centroid_x"] = np.inf 

111 catalog[1]["slot_Centroid_x"] = -np.inf 

112 catalog[2]["slot_Centroid_y"] = np.nan 

113 task.run(catalog, exposure) 

114 np.testing.assert_array_equal(catalog["base_PixelFlags_flag_offimage"], 

115 np.array([True, True, True])) 

116 np.testing.assert_array_equal(catalog["base_PixelFlags_flag_edge"], 

117 np.array([True, True, True])) 

118 np.testing.assert_array_equal(catalog["base_PixelFlags_flag_edgeCenter"], 

119 np.array([True, True, True])) 

120 np.testing.assert_array_equal(catalog["base_PixelFlags_flag_edgeCenterAll"], 

121 np.array([True, True, True])) 

122 

123 def testOffimage(self): 

124 """Test that sources at the boundary of the image get flag_offimage 

125 and flag_edge[Center[All]] set appropriately. 

126 """ 

127 # These four will be explicitly set to values at the boundary; we 

128 # cannot add sources off the image in TestDataset. 

129 self.dataset.addSource(100000, lsst.geom.Point2D(20, 20)) 

130 self.dataset.addSource(100000, lsst.geom.Point2D(20, 100)) 

131 self.dataset.addSource(100000, lsst.geom.Point2D(40, 100)) 

132 self.dataset.addSource(100000, lsst.geom.Point2D(80, 30)) 

133 

134 task = self.makeSingleFrameMeasurementTask("base_PixelFlags") 

135 exposure, catalog = self.dataset.realize(10.0, task.schema, randomSeed=0) 

136 

137 # Mask 4 pixels at the y-edges (note numpy vs. afw flipped indices!) 

138 edgeBit = exposure.mask.getPlaneBitMask("EDGE") 

139 exposure.mask.array[:4, :] |= edgeBit 

140 exposure.mask.array[-4:, ] |= edgeBit 

141 # Mask 4 pixels at the x-edges 

142 nodataBit = exposure.mask.getPlaneBitMask("NO_DATA") 

143 exposure.mask.array[:, :4] |= nodataBit 

144 exposure.mask.array[:, -4:] |= nodataBit 

145 

146 # All of these should get Center and CenterAll. 

147 catalog[0]["slot_Centroid_x"] = -20.5 # on image 

148 catalog[1]["slot_Centroid_x"] = -20.51 # off image 

149 catalog[2]["slot_Centroid_y"] = 129.4 # on image 

150 catalog[3]["slot_Centroid_y"] = 129.50 # off image 

151 

152 task.run(catalog, exposure) 

153 

154 np.testing.assert_array_equal(catalog["base_PixelFlags_flag_offimage"], 

155 np.array([False, True, False, True])) 

156 

157 # Centroid and footprints don't line up, so edge is only set for the 

158 # sources that have off-image centroids. 

159 np.testing.assert_array_equal(catalog["base_PixelFlags_flag_edge"], 

160 np.array([False, True, False, True])) 

161 # Center[All] is set for off-image and on-EDGE sources, because it's 

162 # based on the centroid location. 

163 np.testing.assert_array_equal(catalog["base_PixelFlags_flag_edgeCenter"], 

164 np.array([False, True, True, True])) 

165 np.testing.assert_array_equal(catalog["base_PixelFlags_flag_edgeCenterAll"], 

166 np.array([False, True, True, True])) 

167 

168 # Centroid and footprints don't line up, so nodata is not set. 

169 np.testing.assert_array_equal(catalog["base_PixelFlags_flag_nodata"], 

170 np.array([False, False, False, False])) 

171 # Center[All] is set for on-NO_DATA sources, because it's based on the 

172 # centroid location. 

173 np.testing.assert_array_equal(catalog["base_PixelFlags_flag_nodataCenter"], 

174 np.array([True, True, False, False])) 

175 np.testing.assert_array_equal(catalog["base_PixelFlags_flag_nodataCenterAll"], 

176 np.array([True, True, False, False])) 

177 

178 def testEdgeFlags(self): 

179 """Test that edge/edgeCenter/edgeCenterAll and the equivalent nodata 

180 flags are set appropriately. 

181 """ 

182 # Half of the Central 3x3 pixels are on the masked NO_DATA; no offimage or 

183 # nodataCenterAll, but nodata and nodataCenter. 

184 self.dataset.addSource(100000, lsst.geom.Point2D(115, 30)) 

185 # Central 3x3 pixels all masked EDGE; no offimage but all edge flags. 

186 self.dataset.addSource(100000, lsst.geom.Point2D(10, 127)) 

187 

188 task = self.makeSingleFrameMeasurementTask("base_PixelFlags") 

189 exposure, catalog = self.dataset.realize(10.0, task.schema, randomSeed=0) 

190 

191 # Mask 4 pixels at the y-edges (note numpy vs. afw flipped indices!) 

192 edgeBit = exposure.mask.getPlaneBitMask("EDGE") 

193 exposure.mask.array[:4, :] |= edgeBit 

194 exposure.mask.array[-4:, ] |= edgeBit 

195 # Mask 4 pixels at the x-edges 

196 nodataBit = exposure.mask.getPlaneBitMask("NO_DATA") 

197 exposure.mask.array[:, :4] |= nodataBit 

198 exposure.mask.array[:, -4:] |= nodataBit 

199 

200 task.run(catalog, exposure) 

201 

202 np.testing.assert_array_equal(catalog["base_PixelFlags_flag_offimage"], 

203 np.array([False, False])) 

204 

205 np.testing.assert_array_equal(catalog["base_PixelFlags_flag_nodata"], 

206 np.array([True, False])) 

207 np.testing.assert_array_equal(catalog["base_PixelFlags_flag_nodataCenter"], 

208 np.array([True, False])) 

209 np.testing.assert_array_equal(catalog["base_PixelFlags_flag_nodataCenterAll"], 

210 np.array([False, False])) 

211 

212 np.testing.assert_array_equal(catalog["base_PixelFlags_flag_edge"], 

213 np.array([False, True])) 

214 np.testing.assert_array_equal(catalog["base_PixelFlags_flag_edgeCenter"], 

215 np.array([False, True])) 

216 np.testing.assert_array_equal(catalog["base_PixelFlags_flag_edgeCenterAll"], 

217 np.array([False, True])) 

218 

219 

220class TestMemory(lsst.utils.tests.MemoryTestCase): 

221 pass 

222 

223 

224def setup_module(module): 

225 lsst.utils.tests.init() 

226 

227 

228if __name__ == "__main__": 228 ↛ 229line 228 didn't jump to line 229 because the condition on line 228 was never true

229 lsst.utils.tests.init() 

230 unittest.main()