Coverage for tests/test_SdssShape.py: 21%

164 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-01-26 10:19 +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.afw.geom 

28import lsst.afw.table 

29import lsst.meas.base 

30import lsst.meas.base.tests 

31import lsst.utils.tests 

32 

33 

34class SdssShapeTestCase(lsst.meas.base.tests.AlgorithmTestCase, lsst.utils.tests.TestCase): 

35 

36 def setUp(self): 

37 self.bbox = lsst.geom.Box2I(lsst.geom.Point2I(-20, -30), 

38 lsst.geom.Extent2I(240, 160)) 

39 self.dataset = lsst.meas.base.tests.TestDataset(self.bbox) 

40 # first source is a point 

41 self.dataset.addSource(100000.0, lsst.geom.Point2D(50.1, 49.8)) 

42 # second source is extended 

43 self.dataset.addSource(100000.0, lsst.geom.Point2D(149.9, 50.3), 

44 lsst.afw.geom.Quadrupole(8, 9, 3)) 

45 self.config = self.makeSingleFrameMeasurementConfig("base_SdssShape") 

46 

47 def tearDown(self): 

48 del self.bbox 

49 del self.dataset 

50 del self.config 

51 

52 def makeAlgorithm(self, ctrl=None): 

53 """Construct an algorithm and return both it and its schema. 

54 """ 

55 if ctrl is None: 

56 ctrl = lsst.meas.base.SdssShapeControl() 

57 schema = lsst.meas.base.tests.TestDataset.makeMinimalSchema() 

58 algorithm = lsst.meas.base.SdssShapeAlgorithm(ctrl, "base_SdssShape", schema) 

59 return algorithm, schema 

60 

61 def assertFinite(self, value): 

62 self.assertTrue(np.isfinite(value), msg="%s is not finite" % (value,)) 

63 

64 def _runMeasurementTask(self): 

65 task = self.makeSingleFrameMeasurementTask("base_SdssShape", config=self.config) 

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

67 task.run(catalog, exposure) 

68 return exposure, catalog 

69 

70 def _checkShape(self, result, record): 

71 self.assertFloatsAlmostEqual(result.x, record.get("truth_x"), rtol=1E-2) 

72 self.assertFloatsAlmostEqual(result.y, record.get("truth_y"), rtol=1E-2) 

73 self.assertFloatsAlmostEqual(result.xx, record.get("truth_xx"), rtol=1E-2) 

74 self.assertFloatsAlmostEqual(result.yy, record.get("truth_yy"), rtol=1E-2) 

75 self.assertFloatsAlmostEqual(result.xy, record.get("truth_xy"), rtol=1E-1, atol=2E-1) 

76 self.assertFinite(result.xxErr) 

77 self.assertFinite(result.yyErr) 

78 self.assertFinite(result.xyErr) 

79 self.assertFinite(result.instFlux_xx_Cov) 

80 self.assertFinite(result.instFlux_yy_Cov) 

81 self.assertFinite(result.instFlux_xy_Cov) 

82 self.assertFinite(result.xx_yy_Cov) 

83 self.assertFinite(result.xx_xy_Cov) 

84 self.assertFinite(result.yy_xy_Cov) 

85 self.assertFalse(result.getFlag(lsst.meas.base.SdssShapeAlgorithm.FAILURE.number)) 

86 self.assertFalse(result.getFlag(lsst.meas.base.SdssShapeAlgorithm.UNWEIGHTED_BAD.number)) 

87 self.assertFalse(result.getFlag(lsst.meas.base.SdssShapeAlgorithm.UNWEIGHTED.number)) 

88 self.assertFalse(result.getFlag(lsst.meas.base.SdssShapeAlgorithm.SHIFT.number)) 

89 self.assertFalse(result.getFlag(lsst.meas.base.SdssShapeAlgorithm.MAXITER.number)) 

90 

91 def _checkPsfShape(self, result, psfResult, psfTruth): 

92 self.assertFloatsAlmostEqual(psfResult.getIxx(), psfTruth.getIxx(), rtol=1E-4) 

93 self.assertFloatsAlmostEqual(psfResult.getIyy(), psfTruth.getIyy(), rtol=1E-4) 

94 self.assertFloatsAlmostEqual(psfResult.getIxy(), psfTruth.getIxy(), rtol=1E-4) 

95 self.assertFalse(result.getFlag(lsst.meas.base.SdssShapeAlgorithm.PSF_SHAPE_BAD.number)) 

96 

97 def testMeasureGoodPsf(self): 

98 """Test that we measure shapes and record the PSF shape correctly 

99 

100 Note: Given that the PSF model here is constant over the entire image, this test 

101 would not catch an error in the potition at which base_SdssShape_psf is computed. 

102 Such a test requires a spatially varying PSF model such that different locations 

103 can be distinguished by their different PSF model shapes. Such a test exists in 

104 meas_algorithms (tests/testSdssShapePsf.py), making use of the PcaPsf algorithm 

105 to build the spatially varying PSF. 

106 """ 

107 exposure, catalog = self._runMeasurementTask() 

108 key = lsst.meas.base.SdssShapeResultKey(catalog.schema["base_SdssShape"]) 

109 psfTruth = exposure.getPsf().computeShape(catalog[0].getCentroid()) 

110 for record in catalog: 

111 result = record.get(key) 

112 self._checkShape(result, record) 

113 psfResult = key.getPsfShape(record) 

114 self._checkPsfShape(result, psfResult, psfTruth) 

115 

116 def testMeasureWithoutPsf(self): 

117 """Test that we measure shapes correctly and do not record the PSF shape when not needed.""" 

118 self.config.plugins["base_SdssShape"].doMeasurePsf = False 

119 _, catalog = self._runMeasurementTask() 

120 key = lsst.meas.base.SdssShapeResultKey(catalog.schema["base_SdssShape"]) 

121 for record in catalog: 

122 result = record.get(key) 

123 self._checkShape(result, record) 

124 self.assertNotIn("base_SdssShape_psf_xx", catalog.schema) 

125 self.assertNotIn("base_SdssShape_psf_yy", catalog.schema) 

126 self.assertNotIn("base_SdssShape_psf_xy", catalog.schema) 

127 self.assertNotIn("base_SdssShape_flag_psf", catalog.schema) 

128 

129 def testMeasureBadPsf(self): 

130 """Test that we measure shapes correctly and set a flag with the PSF is unavailable.""" 

131 self.config.plugins["base_SdssShape"].doMeasurePsf = True 

132 task = self.makeSingleFrameMeasurementTask("base_SdssShape", config=self.config) 

133 exposure, catalog = self.dataset.realize(10.0, task.schema, randomSeed=1) 

134 exposure.setPsf(None) # Set PSF to None to test no PSF case 

135 task.run(catalog, exposure) 

136 key = lsst.meas.base.SdssShapeResultKey(catalog.schema["base_SdssShape"]) 

137 for record in catalog: 

138 result = record.get(key) 

139 self._checkShape(result, record) 

140 self.assertTrue(result.getFlag(lsst.meas.base.SdssShapeAlgorithm.PSF_SHAPE_BAD.number)) 

141 

142 def testMonteCarlo(self): 

143 """Test an ideal simulation, with deterministic noise. 

144 

145 Demonstrate that: 

146 

147 - We get the right answer, and 

148 - The reported uncertainty agrees with a Monte Carlo test of the noise. 

149 """ 

150 algorithm, schema = self.makeAlgorithm() 

151 # Results are RNG dependent; we choose a seed that is known to pass. 

152 exposure, cat = self.dataset.realize(0.0, schema, randomSeed=3) 

153 record = cat[1] 

154 instFlux = record["truth_instFlux"] 

155 algorithm.measure(record, exposure) 

156 for suffix in ["xx", "yy", "xy"]: 

157 self.assertFloatsAlmostEqual(record.get("truth_"+suffix), 

158 record.get("base_SdssShape_"+suffix), rtol=1E-4) 

159 

160 for noise in (0.0001, 0.001,): 

161 nSamples = 1000 

162 catalog = lsst.afw.table.SourceCatalog(cat.schema) 

163 for i in range(nSamples): 

164 # By using ``i`` to seed the RNG, we get results which 

165 # fall within the tolerances defined below. If we allow this 

166 # test to be truly random, passing becomes RNG-dependent. 

167 exposure, cat = self.dataset.realize(noise*instFlux, schema, randomSeed=i) 

168 record = cat[1] 

169 algorithm.measure(record, exposure) 

170 catalog.append(record) 

171 

172 catalog = catalog.copy(deep=True) 

173 for suffix in ["xx", "yy", "xy"]: 

174 shapeMean = np.mean(catalog["base_SdssShape_"+suffix]) 

175 shapeErrMean = np.nanmean(catalog["base_SdssShape_"+suffix+"Err"]) 

176 shapeInterval68 = 0.5*(np.nanpercentile(catalog["base_SdssShape_"+suffix], 84) 

177 - np.nanpercentile(catalog["base_SdssShape_"+suffix], 16)) 

178 self.assertFloatsAlmostEqual(np.nanstd(catalog["base_SdssShape_"+suffix]), 

179 shapeInterval68, rtol=0.03) 

180 self.assertFloatsAlmostEqual(shapeErrMean, shapeInterval68, rtol=0.03) 

181 self.assertLess(abs(shapeMean - record.get("truth_"+suffix)), 2.0*shapeErrMean/nSamples**0.5) 

182 

183 

184class SdssShapeTransformTestCase(lsst.meas.base.tests.FluxTransformTestCase, 

185 lsst.meas.base.tests.CentroidTransformTestCase, 

186 lsst.meas.base.tests.SingleFramePluginTransformSetupHelper, 

187 lsst.utils.tests.TestCase): 

188 

189 name = "sdssShape" 

190 controlClass = lsst.meas.base.SdssShapeControl 

191 algorithmClass = lsst.meas.base.SdssShapeAlgorithm 

192 transformClass = lsst.meas.base.SdssShapeTransform 

193 flagNames = ("flag", "flag_unweighted", "flag_unweightedBad", "flag_shift", "flag_maxIter") 

194 singleFramePlugins = ('base_SdssShape',) 

195 forcedPlugins = ('base_SdssShape',) 

196 testPsf = True 

197 

198 def _setFieldsInRecords(self, records, name): 

199 lsst.meas.base.tests.FluxTransformTestCase._setFieldsInRecords(self, records, name) 

200 lsst.meas.base.tests.CentroidTransformTestCase._setFieldsInRecords(self, records, name) 

201 for record in records: 

202 for field in ('xx', 'yy', 'xy', 'xxErr', 'yyErr', 'xyErr', 'psf_xx', 'psf_yy', 'psf_xy'): 

203 if record.schema.join(name, field) in record.schema: 

204 record[record.schema.join(name, field)] = np.random.random() 

205 

206 def _compareFieldsInRecords(self, inSrc, outSrc, name): 

207 lsst.meas.base.tests.FluxTransformTestCase._compareFieldsInRecords(self, inSrc, outSrc, name) 

208 lsst.meas.base.tests.CentroidTransformTestCase._compareFieldsInRecords(self, inSrc, outSrc, name) 

209 

210 inShape = lsst.meas.base.ShapeResultKey(inSrc.schema[name]).get(inSrc) 

211 outShape = lsst.meas.base.ShapeResultKey(outSrc.schema[name]).get(outSrc) 

212 

213 centroid = lsst.meas.base.CentroidResultKey(inSrc.schema[name]).get(inSrc).getCentroid() 

214 xform = self.calexp.getWcs().linearizePixelToSky(centroid, lsst.geom.radians) 

215 

216 trInShape = inShape.getShape().transform(xform.getLinear()) 

217 self.assertEqual(trInShape.getIxx(), outShape.getShape().getIxx()) 

218 self.assertEqual(trInShape.getIyy(), outShape.getShape().getIyy()) 

219 self.assertEqual(trInShape.getIxy(), outShape.getShape().getIxy()) 

220 

221 m = lsst.meas.base.makeShapeTransformMatrix(xform.getLinear()) 

222 np.testing.assert_array_almost_equal( 

223 np.dot(np.dot(m, inShape.getShapeErr()), m.transpose()), outShape.getShapeErr() 

224 ) 

225 if self.testPsf: 

226 inPsfShape = lsst.meas.base.ShapeResultKey( 

227 inSrc.schema[inSrc.schema.join(name, "psf")] 

228 ).get(inSrc) 

229 outPsfShape = lsst.meas.base.ShapeResultKey( 

230 outSrc.schema[outSrc.schema.join(name, "psf")] 

231 ).get(outSrc) 

232 trInPsfShape = inPsfShape.getShape().transform(xform.getLinear()) 

233 

234 self.assertEqual(trInPsfShape.getIxx(), outPsfShape.getShape().getIxx()) 

235 self.assertEqual(trInPsfShape.getIyy(), outPsfShape.getShape().getIyy()) 

236 self.assertEqual(trInPsfShape.getIxy(), outPsfShape.getShape().getIxy()) 

237 else: 

238 self.assertNotIn(inSrc.schema.join(name, "psf", "xx"), inSrc.schema) 

239 self.assertNotIn(inSrc.schema.join(name, "flag", "psf"), inSrc.schema) 

240 

241 

242class PsfSdssShapeTransformTestCase(SdssShapeTransformTestCase, lsst.utils.tests.TestCase): 

243 testPsf = False 

244 

245 def makeSdssShapeControl(self): 

246 ctrl = lsst.meas.base.SdssShapeControl() 

247 ctrl.doMeasurePsf = False 

248 return ctrl 

249 controlClass = makeSdssShapeControl 

250 

251 

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

253 pass 

254 

255 

256def setup_module(module): 

257 lsst.utils.tests.init() 

258 

259 

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

261 lsst.utils.tests.init() 

262 unittest.main()