Coverage for tests/test_ApplyApCorr.py: 19%

130 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2022-12-06 11:53 +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.utils.tests 

27import lsst.geom 

28import lsst.meas.base.tests 

29import lsst.afw.image as afwImage 

30import lsst.afw.table as afwTable 

31import lsst.meas.base.applyApCorr as applyApCorr 

32from lsst.afw.math import ChebyshevBoundedField 

33from lsst.meas.base.apCorrRegistry import addApCorrName 

34 

35 

36def initializeSourceCatalog(schema=None, name=None, instFlux=None, sigma=None, centroid=None): 

37 instFluxName = name + "_instFlux" 

38 instFluxErrName = name + "_instFluxErr" 

39 instFluxKey = schema.find(instFluxName).key 

40 centroidKey = afwTable.Point2DKey(schema["slot_Centroid"]) 

41 sourceCat = afwTable.SourceCatalog(schema) 

42 source = sourceCat.addNew() 

43 source.set(instFluxKey, instFlux) 

44 source.set(instFluxErrName, sigma) 

45 source.set(centroidKey, centroid) 

46 return(sourceCat) 

47 

48 

49class ApplyApCorrTestCase(lsst.meas.base.tests.AlgorithmTestCase, lsst.utils.tests.TestCase): 

50 

51 def setUp(self): 

52 schema = afwTable.SourceTable.makeMinimalSchema() 

53 names = ["test2", "test"] 

54 for name in names: 

55 addApCorrName(name) 

56 schema.addField(name + "_instFlux", type=np.float64) 

57 schema.addField(name + "_instFluxErr", type=np.float64) 

58 schema.addField(name + "_flag", type=np.float64) 

59 schema.addField(name + "_Centroid_x", type=np.float64) 

60 schema.addField(name + "_Centroid_y", type=np.float64) 

61 schema.getAliasMap().set('slot_Centroid', name + '_Centroid') 

62 self.ap_corr_task = applyApCorr.ApplyApCorrTask(schema=schema) 

63 self.name = name # just use 'test' prefix for most of the tests 

64 self.schema = schema 

65 

66 def tearDown(self): 

67 del self.schema 

68 del self.ap_corr_task 

69 

70 def testAddFields(self): 

71 # Check that the required fields have been added to the schema 

72 self.assertIn(self.name + "_apCorr", self.schema.getNames()) 

73 self.assertIn(self.name + "_apCorrErr", self.schema.getNames()) 

74 self.assertIn(self.name + "_flag_apCorr", self.schema.getNames()) 

75 self.assertLess(self.schema.find("test_apCorr").key.getOffset(), 

76 self.schema.find("test2_apCorr").key.getOffset()) 

77 

78 def testSuccessUnflagged(self): 

79 # Check that the aperture correction flag is set to False if aperture 

80 # correction was successfully run 

81 flagName = self.name + "_flag_apCorr" 

82 flagKey = self.schema.find(flagName).key 

83 source_test_instFlux = 5.1 

84 source_test_centroid = lsst.geom.Point2D(5, 7.1) 

85 sourceCat = initializeSourceCatalog(schema=self.schema, name=self.name, instFlux=source_test_instFlux, 

86 sigma=0, centroid=source_test_centroid) 

87 instFluxName = self.name + "_instFlux" 

88 instFluxErrName = self.name + "_instFluxErr" 

89 

90 apCorrMap = afwImage.ApCorrMap() 

91 bbox = lsst.geom.Box2I(lsst.geom.Point2I(0, 0), lsst.geom.ExtentI(10, 10)) 

92 coefficients = np.ones((1, 1), dtype=np.float64) 

93 coefficients_sigma = np.zeros((1, 1), dtype=np.float64) 

94 apCorrMap[instFluxName] = ChebyshevBoundedField(bbox, coefficients) 

95 apCorrMap[instFluxErrName] = ChebyshevBoundedField(bbox, coefficients_sigma) 

96 self.ap_corr_task.run(sourceCat, apCorrMap) 

97 self.assertFalse(sourceCat[flagKey]) 

98 

99 def testFailureFlagged(self): 

100 # Check that aperture correction flag is set to True if aperture 

101 # correction is invalid (negative) 

102 flagName = self.name + "_flag_apCorr" 

103 flagKey = self.schema.find(flagName).key 

104 source_test_instFlux = 5.2 

105 source_test_centroid = lsst.geom.Point2D(5, 7.1) 

106 sourceCat = initializeSourceCatalog(schema=self.schema, name=self.name, instFlux=source_test_instFlux, 

107 sigma=0, centroid=source_test_centroid) 

108 instFluxName = self.name + "_instFlux" 

109 instFluxErrName = self.name + "_instFluxErr" 

110 

111 apCorrMap = afwImage.ApCorrMap() 

112 bbox = lsst.geom.Box2I(lsst.geom.Point2I(0, 0), lsst.geom.ExtentI(10, 10)) 

113 coefficients = -(np.ones((1, 1), dtype=np.float64)) 

114 coefficients_sigma = np.zeros((1, 1), dtype=np.float64) 

115 apCorrMap[instFluxName] = ChebyshevBoundedField(bbox, coefficients) 

116 apCorrMap[instFluxErrName] = ChebyshevBoundedField(bbox, coefficients_sigma) 

117 self.ap_corr_task.run(sourceCat, apCorrMap) 

118 self.assertTrue(sourceCat[flagKey]) 

119 

120 def testCatFluxUnchanged(self): 

121 # Pick arbitrary but unique values for the test case 

122 source_test_instFlux = 5.3 

123 source_test_centroid = lsst.geom.Point2D(5, 7.1) 

124 sourceCat = initializeSourceCatalog(schema=self.schema, name=self.name, instFlux=source_test_instFlux, 

125 sigma=0, centroid=source_test_centroid) 

126 instFluxName = self.name + "_instFlux" 

127 instFluxErrName = self.name + "_instFluxErr" 

128 instFluxKey = self.schema.find(instFluxName).key 

129 

130 apCorrMap = afwImage.ApCorrMap() 

131 bbox = lsst.geom.Box2I(lsst.geom.Point2I(0, 0), lsst.geom.ExtentI(10, 10)) 

132 coefficients = np.ones((1, 1), dtype=np.float64) 

133 coefficients_sigma = np.zeros((1, 1), dtype=np.float64) 

134 apCorrMap[instFluxName] = ChebyshevBoundedField(bbox, coefficients) 

135 apCorrMap[instFluxErrName] = ChebyshevBoundedField(bbox, coefficients_sigma) 

136 self.ap_corr_task.run(sourceCat, apCorrMap) 

137 

138 self.assertEqual(sourceCat[instFluxKey], source_test_instFlux) 

139 

140 def testCatFluxHalf(self): 

141 # Pick arbitrary but unique values for the test case 

142 source_test_instFlux = 5.4 

143 source_test_centroid = lsst.geom.Point2D(5, 7.1) 

144 sourceCat = initializeSourceCatalog(schema=self.schema, name=self.name, instFlux=source_test_instFlux, 

145 sigma=0, centroid=source_test_centroid) 

146 instFluxName = self.name + "_instFlux" 

147 instFluxErrName = self.name + "_instFluxErr" 

148 instFluxKey = self.schema.find(instFluxName).key 

149 

150 apCorrMap = afwImage.ApCorrMap() 

151 bbox = lsst.geom.Box2I(lsst.geom.Point2I(0, 0), lsst.geom.ExtentI(10, 10)) 

152 coefficients = np.ones((1, 1), dtype=np.float64) 

153 coefficients /= 2. 

154 coefficients_sigma = np.zeros((1, 1), dtype=np.float64) 

155 apCorrMap[instFluxName] = ChebyshevBoundedField(bbox, coefficients) 

156 apCorrMap[instFluxErrName] = ChebyshevBoundedField(bbox, coefficients_sigma) 

157 self.ap_corr_task.run(sourceCat, apCorrMap) 

158 

159 self.assertAlmostEqual(sourceCat[instFluxKey], source_test_instFlux / 2) 

160 

161 def testCatFluxErr(self): 

162 """Test catalog flux errors. 

163 

164 Notes 

165 ----- 

166 This test will break if ``UseNaiveFluxErr = False``~ 

167 

168 The alternate method significantly overestimates noise, causing this 

169 test to fail. It is likely that this test will need to be modified if 

170 the noise calculation is updated. 

171 """ 

172 # Pick arbitrary but unique values for the test case 

173 source_test_instFlux = 5.5 

174 source_test_sigma = 0.23 

175 source_test_centroid = lsst.geom.Point2D(5, 7.3) 

176 sourceCat = initializeSourceCatalog(schema=self.schema, name=self.name, instFlux=source_test_instFlux, 

177 sigma=source_test_sigma, centroid=source_test_centroid) 

178 

179 instFluxName = self.name + "_instFlux" 

180 instFluxErrName = self.name + "_instFluxErr" 

181 instFluxErrKey = self.schema.find(instFluxErrName).key 

182 

183 apCorrMap = afwImage.ApCorrMap() 

184 bbox = lsst.geom.Box2I(lsst.geom.Point2I(0, 0), lsst.geom.ExtentI(10, 10)) 

185 coefficients = np.ones((1, 1), dtype=np.float64) 

186 coefficients_sigma = np.ones((1, 1), dtype=np.float64) 

187 apCorrMap[instFluxName] = ChebyshevBoundedField(bbox, coefficients) 

188 apCorrMap[instFluxErrName] = ChebyshevBoundedField(bbox, coefficients_sigma) 

189 self.ap_corr_task.run(sourceCat, apCorrMap) 

190 

191 self.assertAlmostEqual(sourceCat[instFluxErrKey], source_test_sigma) 

192 

193 

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

195 pass 

196 

197 

198def setup_module(module): 

199 lsst.utils.tests.init() 

200 

201 

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

203 lsst.utils.tests.init() 

204 unittest.main()