Coverage for tests/test_buildSingleKernelVisitor.py: 11%

194 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2022-11-29 02:35 -0800

1import unittest 

2 

3 

4import lsst.utils.tests 

5import lsst.afw.image as afwImage 

6import lsst.afw.math as afwMath 

7import lsst.geom as geom 

8import lsst.ip.diffim as ipDiffim 

9import lsst.utils.logging as logUtils 

10import lsst.pex.config as pexConfig 

11 

12logUtils.trace_set_at("lsst.ip.diffim", 4) 

13 

14 

15class DiffimTestCases(unittest.TestCase): 

16 

17 def setUp(self): 

18 self.config = ipDiffim.ImagePsfMatchTask.ConfigClass() 

19 self.config.kernel.name = "DF" 

20 self.subconfig = self.config.kernel.active 

21 

22 self.ps = pexConfig.makePropertySet(self.subconfig) 

23 

24 self.ps["useRegularization"] = False 

25 self.ps["checkConditionNumber"] = False # I am making shady kernels by hand 

26 self.ps["useCoreStats"] = False # I am making off-center resids 

27 self.kList = ipDiffim.makeKernelBasisList(self.subconfig) 

28 self.size = 51 

29 

30 def makeCandidate(self, kSum, x, y): 

31 mi1 = afwImage.MaskedImageF(geom.Extent2I(self.size, self.size)) 

32 mi1.getVariance().set(1.0) # avoid NaNs 

33 mi1[self.size//2, self.size//2, afwImage.LOCAL] = (1, 0x0, 1) 

34 mi2 = afwImage.MaskedImageF(geom.Extent2I(self.size, self.size)) 

35 mi2.getVariance().set(1.0) # avoid NaNs 

36 mi2[self.size//2, self.size//2, afwImage.LOCAL] = (kSum, 0x0, kSum) 

37 kc = ipDiffim.makeKernelCandidate(x, y, mi1, mi2, self.ps) 

38 

39 return kc 

40 

41 def testWithOneBasis(self): 

42 self.runWithOneBasis(False) 

43 self.runWithOneBasis(True) 

44 

45 def runWithOneBasis(self, useRegularization): 

46 kc1 = self.makeCandidate(1, 0.0, 0.0) 

47 kc2 = self.makeCandidate(2, 0.0, 0.0) 

48 kc3 = self.makeCandidate(3, 0.0, 0.0) 

49 

50 if useRegularization: 

51 hMat = ipDiffim.makeRegularizationMatrix(self.ps) 

52 bskv = ipDiffim.BuildSingleKernelVisitorF(self.kList, self.ps, hMat) 

53 else: 

54 bskv = ipDiffim.BuildSingleKernelVisitorF(self.kList, self.ps) 

55 

56 bskv.processCandidate(kc1) 

57 bskv.processCandidate(kc2) 

58 bskv.processCandidate(kc3) 

59 

60 # Initialized 

61 self.assertEqual(kc1.isInitialized(), True) 

62 self.assertEqual(kc2.isInitialized(), True) 

63 self.assertEqual(kc3.isInitialized(), True) 

64 

65 # Is a solution 

66 try: 

67 kc1.getKernelSolution(ipDiffim.KernelCandidateF.RECENT) 

68 kc2.getKernelSolution(ipDiffim.KernelCandidateF.RECENT) 

69 kc3.getKernelSolution(ipDiffim.KernelCandidateF.RECENT) 

70 except Exception as e: 

71 print(e) 

72 self.fail() 

73 

74 # Its not the Pca one 

75 try: 

76 kc1.getKernelSolution(ipDiffim.KernelCandidateF.PCA) 

77 kc2.getKernelSolution(ipDiffim.KernelCandidateF.PCA) 

78 kc3.getKernelSolution(ipDiffim.KernelCandidateF.PCA) 

79 except Exception: 

80 pass 

81 else: 

82 self.fail() 

83 

84 # Processed all of them 

85 self.assertEqual(bskv.getNProcessed(), 3) 

86 

87 # Rejected none 

88 self.assertEqual(bskv.getNRejected(), 0) 

89 

90 # Skips built candidates 

91 bskv.reset() 

92 bskv.setSkipBuilt(True) 

93 bskv.processCandidate(kc1) 

94 bskv.processCandidate(kc2) 

95 bskv.processCandidate(kc3) 

96 # Processed none of them 

97 self.assertEqual(bskv.getNProcessed(), 0) 

98 

99 def testWithThreeBases(self): 

100 kc1 = self.makeCandidate(1, 0.0, 0.0) 

101 kc2 = self.makeCandidate(2, 0.0, 0.0) 

102 kc3 = self.makeCandidate(3, 0.0, 0.0) 

103 bskv1 = ipDiffim.BuildSingleKernelVisitorF(self.kList, self.ps) 

104 bskv1.processCandidate(kc1) 

105 bskv1.processCandidate(kc2) 

106 bskv1.processCandidate(kc3) 

107 self.assertEqual(bskv1.getNProcessed(), 3) 

108 

109 # make sure orig solution is the current one 

110 soln1_1 = kc1.getKernelSolution(ipDiffim.KernelCandidateF.ORIG).getId() 

111 soln2_1 = kc2.getKernelSolution(ipDiffim.KernelCandidateF.ORIG).getId() 

112 soln3_1 = kc3.getKernelSolution(ipDiffim.KernelCandidateF.ORIG).getId() 

113 self.assertEqual(soln1_1, kc1.getKernelSolution(ipDiffim.KernelCandidateF.RECENT).getId()) 

114 self.assertEqual(soln2_1, kc2.getKernelSolution(ipDiffim.KernelCandidateF.RECENT).getId()) 

115 self.assertEqual(soln3_1, kc3.getKernelSolution(ipDiffim.KernelCandidateF.RECENT).getId()) 

116 

117 # do pca basis; visit manually since visitCandidates is still broken 

118 imagePca = ipDiffim.KernelPcaD() 

119 kpv = ipDiffim.KernelPcaVisitorF(imagePca) 

120 kpv.processCandidate(kc1) 

121 kpv.processCandidate(kc2) 

122 kpv.processCandidate(kc3) 

123 kpv.subtractMean() 

124 imagePca.analyze() 

125 eigenKernels = [] 

126 eigenKernels.append(kpv.getEigenKernels()[0]) 

127 self.assertEqual(len(eigenKernels), 1) # the other eKernels are 0.0 and you can't get their coeffs! 

128 

129 # do twice to mimic a Pca loop 

130 bskv2 = ipDiffim.BuildSingleKernelVisitorF(eigenKernels, self.ps) 

131 bskv2.setSkipBuilt(False) 

132 bskv2.processCandidate(kc1) 

133 bskv2.processCandidate(kc2) 

134 bskv2.processCandidate(kc3) 

135 self.assertEqual(bskv2.getNProcessed(), 3) 

136 

137 soln1_2 = kc1.getKernelSolution(ipDiffim.KernelCandidateF.PCA).getId() 

138 soln2_2 = kc2.getKernelSolution(ipDiffim.KernelCandidateF.PCA).getId() 

139 soln3_2 = kc3.getKernelSolution(ipDiffim.KernelCandidateF.PCA).getId() 

140 # pca is recent 

141 self.assertEqual(soln1_2, kc1.getKernelSolution(ipDiffim.KernelCandidateF.RECENT).getId()) 

142 self.assertEqual(soln2_2, kc2.getKernelSolution(ipDiffim.KernelCandidateF.RECENT).getId()) 

143 self.assertEqual(soln3_2, kc3.getKernelSolution(ipDiffim.KernelCandidateF.RECENT).getId()) 

144 # orig is still orig 

145 self.assertEqual(soln1_1, kc1.getKernelSolution(ipDiffim.KernelCandidateF.ORIG).getId()) 

146 self.assertEqual(soln2_1, kc2.getKernelSolution(ipDiffim.KernelCandidateF.ORIG).getId()) 

147 self.assertEqual(soln3_1, kc3.getKernelSolution(ipDiffim.KernelCandidateF.ORIG).getId()) 

148 # pca is not orig 

149 self.assertNotEqual(soln1_2, soln1_1) 

150 self.assertNotEqual(soln2_2, soln2_1) 

151 self.assertNotEqual(soln3_2, soln3_1) 

152 

153 # do twice to mimic a Pca loop 

154 bskv3 = ipDiffim.BuildSingleKernelVisitorF(eigenKernels, self.ps) 

155 bskv3.setSkipBuilt(False) 

156 bskv3.processCandidate(kc1) 

157 bskv3.processCandidate(kc2) 

158 bskv3.processCandidate(kc3) 

159 self.assertEqual(bskv3.getNProcessed(), 3) 

160 

161 soln1_3 = kc1.getKernelSolution(ipDiffim.KernelCandidateF.PCA).getId() 

162 soln2_3 = kc2.getKernelSolution(ipDiffim.KernelCandidateF.PCA).getId() 

163 soln3_3 = kc3.getKernelSolution(ipDiffim.KernelCandidateF.PCA).getId() 

164 # pca is recent 

165 self.assertEqual(soln1_3, kc1.getKernelSolution(ipDiffim.KernelCandidateF.RECENT).getId()) 

166 self.assertEqual(soln2_3, kc2.getKernelSolution(ipDiffim.KernelCandidateF.RECENT).getId()) 

167 self.assertEqual(soln3_3, kc3.getKernelSolution(ipDiffim.KernelCandidateF.RECENT).getId()) 

168 # pca is not previous pca 

169 self.assertNotEqual(soln1_2, soln1_3) 

170 self.assertNotEqual(soln2_2, soln2_3) 

171 self.assertNotEqual(soln3_2, soln3_3) 

172 

173 def testRejection(self): 

174 # we need to construct a candidate whose shape does not 

175 # match the underlying basis 

176 # 

177 # so lets just make a kernel list with all the power in 

178 # the center, but the candidate requires some off center 

179 # power 

180 kc1 = self.makeCandidate(1, 0.0, 0.0) 

181 kc2 = self.makeCandidate(2, 0.0, 0.0) 

182 kc3 = self.makeCandidate(3, 0.0, 0.0) 

183 bskv1 = ipDiffim.BuildSingleKernelVisitorF(self.kList, self.ps) 

184 bskv1.processCandidate(kc1) 

185 bskv1.processCandidate(kc2) 

186 bskv1.processCandidate(kc3) 

187 

188 imagePca = ipDiffim.KernelPcaD() 

189 kpv = ipDiffim.KernelPcaVisitorF(imagePca) 

190 kpv.processCandidate(kc1) 

191 kpv.processCandidate(kc2) 

192 kpv.processCandidate(kc3) 

193 kpv.subtractMean() 

194 imagePca.analyze() 

195 eigenKernels = [] 

196 eigenKernels.append(kpv.getEigenKernels()[0]) 

197 self.assertEqual(len(eigenKernels), 1) 

198 

199 # bogus candidate 

200 mi1 = afwImage.MaskedImageF(geom.Extent2I(self.size, self.size)) 

201 mi1.getVariance().set(0.1) 

202 mi1[self.size//2, self.size//2, afwImage.LOCAL] = (1, 0x0, 1) 

203 mi2 = afwImage.MaskedImageF(geom.Extent2I(self.size, self.size)) 

204 mi2.getVariance().set(0.1) 

205 # make it high enough to make the mean resids large 

206 mi2[self.size//3, self.size//3, afwImage.LOCAL] = (self.size**2, 0x0, 1) 

207 kc4 = ipDiffim.makeKernelCandidate(0, 0, mi1, mi2, self.ps) 

208 self.assertEqual(kc4.getStatus(), afwMath.SpatialCellCandidate.UNKNOWN) 

209 

210 # process with eigenKernels 

211 bskv2 = ipDiffim.BuildSingleKernelVisitorF(eigenKernels, self.ps) 

212 bskv2.setSkipBuilt(False) 

213 bskv2.processCandidate(kc1) 

214 bskv2.processCandidate(kc2) 

215 bskv2.processCandidate(kc3) 

216 bskv2.processCandidate(kc4) 

217 

218 self.assertEqual(bskv2.getNProcessed(), 4) 

219 self.assertEqual(bskv2.getNRejected(), 1) 

220 

221 self.assertEqual(kc1.getStatus(), afwMath.SpatialCellCandidate.GOOD) 

222 self.assertEqual(kc2.getStatus(), afwMath.SpatialCellCandidate.GOOD) 

223 self.assertEqual(kc3.getStatus(), afwMath.SpatialCellCandidate.GOOD) 

224 self.assertEqual(kc4.getStatus(), afwMath.SpatialCellCandidate.BAD) 

225 

226 def testVisit(self, nCell=3): 

227 bskv = ipDiffim.BuildSingleKernelVisitorF(self.kList, self.ps) 

228 

229 sizeCellX = self.ps["sizeCellX"] 

230 sizeCellY = self.ps["sizeCellY"] 

231 

232 kernelCellSet = afwMath.SpatialCellSet(geom.Box2I(geom.Point2I(0, 0), 

233 geom.Extent2I(sizeCellX * nCell, 

234 sizeCellY * nCell)), 

235 sizeCellX, 

236 sizeCellY) 

237 nTot = 0 

238 for candX in range(nCell): 

239 for candY in range(nCell): 

240 if candX == nCell // 2 and candY == nCell // 2: 

241 kc = self.makeCandidate(100.0, 

242 candX * sizeCellX + sizeCellX // 2, 

243 candY * sizeCellY + sizeCellY // 2) 

244 else: 

245 kc = self.makeCandidate(1.0, 

246 candX * sizeCellX + sizeCellX // 2, 

247 candY * sizeCellY + sizeCellY // 2) 

248 kernelCellSet.insertCandidate(kc) 

249 nTot += 1 

250 

251 kernelCellSet.visitCandidates(bskv, 1) 

252 self.assertEqual(bskv.getNProcessed(), nTot) 

253 self.assertEqual(bskv.getNRejected(), 0) 

254 

255 for cell in kernelCellSet.getCellList(): 

256 for cand in cell.begin(False): 

257 self.assertEqual(cand.getStatus(), afwMath.SpatialCellCandidate.GOOD) 

258 

259 def tearDown(self): 

260 del self.config 

261 del self.ps 

262 del self.kList 

263 

264 

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

266 pass 

267 

268 

269def setup_module(module): 

270 lsst.utils.tests.init() 

271 

272 

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

274 lsst.utils.tests.init() 

275 unittest.main()