Coverage for tests/test_ticketDM-433.py: 17%

154 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-01-25 02:50 -0800

1# This file is part of afw. 

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 

22""" 

23Tests for SourceTable slots with version > 0 

24""" 

25 

26import unittest 

27 

28import numpy as np 

29 

30import lsst.utils.tests 

31import lsst.pex.exceptions 

32import lsst.geom 

33import lsst.afw.table 

34import lsst.afw.geom 

35import lsst.afw.image 

36import lsst.afw.detection 

37 

38 

39def makeArray(size, dtype): 

40 return np.array(np.random.randn(*size), dtype=dtype) 

41 

42 

43def makeCov(size, dtype): 

44 m = np.array(np.random.randn(size, size), dtype=dtype) 

45 return np.dot(m, m.transpose()) 

46 

47 

48def makeWcs(): 

49 crval = lsst.geom.SpherePoint(1.606631, 5.090329, lsst.geom.degrees) 

50 crpix = lsst.geom.Point2D(2036., 2000.) 

51 cdMatrix = np.array([[5.399452e-5, -1.30770e-5], [1.30770e-5, 5.399452e-5]]) 

52 return lsst.afw.geom.makeSkyWcs(crval=crval, crpix=crpix, cdMatrix=cdMatrix) 

53 

54 

55class SourceTableTestCase(lsst.utils.tests.TestCase): 

56 

57 def fillRecord(self, record): 

58 record.set(self.instFluxKey, np.random.randn()) 

59 record.set(self.instFluxErrKey, np.random.randn()) 

60 record.set(self.centroidKey, 

61 lsst.geom.Point2D(*np.random.randn(2))) 

62 record.set(self.centroidErrKey, makeCov(2, np.float32)) 

63 record.set(self.shapeKey, 

64 lsst.afw.geom.Quadrupole(*np.random.randn(3))) 

65 record.set(self.shapeErrKey, makeCov(3, np.float32)) 

66 record.set(self.fluxFlagKey, np.random.randn() > 0) 

67 record.set(self.centroidFlagKey, np.random.randn() > 0) 

68 record.set(self.shapeFlagKey, np.random.randn() > 0) 

69 

70 def makeInstFlux(self, schema, prefix, uncertainty): 

71 self.instFluxKey = self.schema.addField(prefix+"_instFlux", type="D") 

72 if uncertainty: 

73 self.instFluxErrKey = self.schema.addField( 

74 prefix+"_instFluxErr", type="D") 

75 self.fluxFlagKey = self.schema.addField(prefix+"_flag", type="Flag") 

76 

77 def makeCentroid(self, schema, prefix, uncertainty): 

78 self.centroidXKey = self.schema.addField(prefix+"_x", type="D") 

79 self.centroidYKey = self.schema.addField(prefix+"_y", type="D") 

80 sigmaArray = [] 

81 covArray = [] 

82 if uncertainty > 0: 

83 self.centroidXErrKey = self.schema.addField( 

84 prefix+"_xErr", type="F") 

85 self.centroidYErrKey = self.schema.addField( 

86 prefix+"_yErr", type="F") 

87 sigmaArray.append(self.centroidXErrKey) 

88 sigmaArray.append(self.centroidYErrKey) 

89 if uncertainty > 1: 

90 self.centroidXYCovKey = self.schema.addField( 

91 prefix+"_x_y_Cov", type="F") 

92 covArray.append(self.centroidXYCovKey) 

93 self.centroidKey = lsst.afw.table.Point2DKey( 

94 self.centroidXKey, self.centroidYKey) 

95 self.centroidErrKey = lsst.afw.table.CovarianceMatrix2fKey( 

96 sigmaArray, covArray) 

97 self.centroidFlagKey = self.schema.addField( 

98 prefix+"_flag", type="Flag") 

99 

100 def makeShape(self, schema, prefix, uncertainty): 

101 self.shapeXXKey = self.schema.addField(prefix+"_xx", type="D") 

102 self.shapeYYKey = self.schema.addField(prefix+"_yy", type="D") 

103 self.shapeXYKey = self.schema.addField(prefix+"_xy", type="D") 

104 self.shapeKey = lsst.afw.table.QuadrupoleKey( 

105 self.shapeXXKey, self.shapeYYKey, self.shapeXYKey) 

106 sigmaArray = [] 

107 covArray = [] 

108 if uncertainty > 0: 

109 self.shapeXXErrKey = self.schema.addField( 

110 prefix+"_xxErr", type="F") 

111 self.shapeYYErrKey = self.schema.addField( 

112 prefix+"_yyErr", type="F") 

113 self.shapeXYErrKey = self.schema.addField( 

114 prefix+"_xyErr", type="F") 

115 sigmaArray.append(self.shapeXXErrKey) 

116 sigmaArray.append(self.shapeYYErrKey) 

117 sigmaArray.append(self.shapeXYErrKey) 

118 if uncertainty > 1: 

119 self.shapeXXYYCovKey = self.schema.addField( 

120 prefix+"_xx_yy_Cov", type="F") 

121 self.shapeXXXYCovKey = self.schema.addField( 

122 prefix+"_xx_xy_Cov", type="F") 

123 self.shapeYYXYCovKey = self.schema.addField( 

124 prefix+"_yy_xy_Cov", type="F") 

125 covArray.append(self.shapeXXYYCovKey) 

126 covArray.append(self.shapeXXXYCovKey) 

127 covArray.append(self.shapeYYXYCovKey) 

128 self.shapeErrKey = lsst.afw.table.CovarianceMatrix3fKey( 

129 sigmaArray, covArray) 

130 self.shapeFlagKey = self.schema.addField(prefix+"_flag", type="Flag") 

131 

132 def setUp(self): 

133 np.random.seed(1) 

134 self.schema = lsst.afw.table.SourceTable.makeMinimalSchema() 

135 self.makeInstFlux(self.schema, "a", 1) 

136 self.makeCentroid(self.schema, "b", 2) 

137 self.makeShape(self.schema, "c", 2) 

138 self.table = lsst.afw.table.SourceTable.make(self.schema) 

139 self.catalog = lsst.afw.table.SourceCatalog(self.table) 

140 self.record = self.catalog.addNew() 

141 self.fillRecord(self.record) 

142 self.record.setId(50) 

143 self.fillRecord(self.catalog.addNew()) 

144 self.fillRecord(self.catalog.addNew()) 

145 self.table.definePsfFlux("a") 

146 self.table.defineCentroid("b") 

147 self.table.defineShape("c") 

148 

149 def tearDown(self): 

150 del self.schema 

151 del self.record 

152 del self.table 

153 del self.catalog 

154 

155 def testPersisted(self): 

156 self.table.definePsfFlux("a") 

157 self.table.defineCentroid("b") 

158 self.table.defineShape("c") 

159 with lsst.utils.tests.getTempFilePath(".fits") as filename: 

160 self.catalog.writeFits(filename) 

161 catalog = lsst.afw.table.SourceCatalog.readFits(filename) 

162 table = catalog.getTable() 

163 record = catalog[0] 

164 # I'm using the keys from the non-persisted table. They should work at least in the 

165 # current implementation 

166 self.assertEqual(record.get(self.instFluxKey), record.getPsfInstFlux()) 

167 self.assertEqual(record.get(self.fluxFlagKey), 

168 record.getPsfFluxFlag()) 

169 self.assertEqual(table.getSchema().getAliasMap().get("slot_Centroid"), "b") 

170 self.assertEqual(record.get(self.centroidKey), 

171 record.getCentroid()) 

172 self.assertFloatsAlmostEqual( 

173 record.get(self.centroidErrKey), 

174 record.getCentroidErr()) 

175 self.assertEqual(table.getSchema().getAliasMap().get("slot_Shape"), "c") 

176 self.assertEqual(record.get(self.shapeKey), record.getShape()) 

177 self.assertFloatsAlmostEqual( 

178 record.get(self.shapeErrKey), 

179 record.getShapeErr()) 

180 

181 def testDefiner1(self): 

182 self.table.definePsfFlux("a") 

183 self.table.defineCentroid("b") 

184 self.table.defineShape("c") 

185 self.assertEqual(self.record.get(self.instFluxKey), 

186 self.record.getPsfInstFlux()) 

187 self.assertEqual(self.record.get(self.fluxFlagKey), 

188 self.record.getPsfFluxFlag()) 

189 self.assertEqual(self.table.getSchema().getAliasMap().get("slot_Centroid"), "b") 

190 self.assertEqual(self.record.get(self.centroidKey), 

191 self.record.getCentroid()) 

192 self.assertFloatsAlmostEqual( 

193 self.record.get(self.centroidErrKey), self.record.getCentroidErr()) 

194 self.assertEqual(self.table.getSchema().getAliasMap().get("slot_Shape"), "c") 

195 self.assertEqual(self.record.get(self.shapeKey), 

196 self.record.getShape()) 

197 self.assertFloatsAlmostEqual(self.record.get(self.shapeErrKey), 

198 self.record.getShapeErr()) 

199 

200 def testCoordUpdate(self): 

201 self.table.defineCentroid("b") 

202 wcs = makeWcs() 

203 self.record.updateCoord(wcs) 

204 coord1 = self.record.getCoord() 

205 coord2 = wcs.pixelToSky(self.record.get(self.centroidKey)) 

206 self.assertEqual(coord1, coord2) 

207 

208 def testColumnView(self): 

209 cols1 = self.catalog.getColumnView() 

210 cols2 = self.catalog.columns 

211 self.assertIs(cols1, cols2) 

212 self.assertIsInstance(cols1, lsst.afw.table.SourceColumnView) 

213 self.table.definePsfFlux("a") 

214 self.table.defineCentroid("b") 

215 self.table.defineShape("c") 

216 self.assertTrue((cols2["a_instFlux"] == cols2.getPsfInstFlux()).all()) 

217 self.assertTrue((cols2["a_instFluxErr"] == cols2.getPsfInstFluxErr()).all()) 

218 self.assertTrue((cols2["b_x"] == cols2.getX()).all()) 

219 self.assertTrue((cols2["b_y"] == cols2.getY()).all()) 

220 self.assertTrue((cols2["c_xx"] == cols2.getIxx()).all()) 

221 self.assertTrue((cols2["c_yy"] == cols2.getIyy()).all()) 

222 self.assertTrue((cols2["c_xy"] == cols2.getIxy()).all()) 

223 

224 

225class MemoryTester(lsst.utils.tests.MemoryTestCase): 

226 pass 

227 

228 

229def setup_module(module): 

230 lsst.utils.tests.init() 

231 

232 

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

234 lsst.utils.tests.init() 

235 unittest.main()