Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

import unittest 

 

 

import lsst.utils.tests 

import lsst.afw.image as afwImage 

import lsst.afw.math as afwMath 

import lsst.geom as geom 

import lsst.ip.diffim as ipDiffim 

import lsst.log.utils as logUtils 

import lsst.pex.config as pexConfig 

 

logUtils.traceSetAt("ip.diffim", 4) 

 

 

class DiffimTestCases(unittest.TestCase): 

 

def setUp(self): 

self.config = ipDiffim.ImagePsfMatchTask.ConfigClass() 

self.config.kernel.name = "DF" 

self.subconfig = self.config.kernel.active 

 

self.policy = pexConfig.makePolicy(self.subconfig) 

 

self.policy.set("useRegularization", False) 

self.policy.set("checkConditionNumber", False) # I am making shady kernels by hand 

self.policy.set("useCoreStats", False) # I am making off-center resids 

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

self.size = 51 

 

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

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

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

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

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

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

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

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

 

return kc 

 

def testWithOneBasis(self): 

self.runWithOneBasis(False) 

self.runWithOneBasis(True) 

 

def runWithOneBasis(self, useRegularization): 

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

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

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

 

if useRegularization: 

hMat = ipDiffim.makeRegularizationMatrix(self.policy) 

bskv = ipDiffim.BuildSingleKernelVisitorF(self.kList, self.policy, hMat) 

else: 

bskv = ipDiffim.BuildSingleKernelVisitorF(self.kList, self.policy) 

 

bskv.processCandidate(kc1) 

bskv.processCandidate(kc2) 

bskv.processCandidate(kc3) 

 

# Initialized 

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

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

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

 

# Is a solution 

try: 

kc1.getKernelSolution(ipDiffim.KernelCandidateF.RECENT) 

kc2.getKernelSolution(ipDiffim.KernelCandidateF.RECENT) 

kc3.getKernelSolution(ipDiffim.KernelCandidateF.RECENT) 

except Exception as e: 

print(e) 

self.fail() 

 

# Its not the Pca one 

try: 

kc1.getKernelSolution(ipDiffim.KernelCandidateF.PCA) 

kc2.getKernelSolution(ipDiffim.KernelCandidateF.PCA) 

kc3.getKernelSolution(ipDiffim.KernelCandidateF.PCA) 

except Exception: 

pass 

else: 

self.fail() 

 

# Processed all of them 

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

 

# Rejected none 

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

 

# Skips built candidates 

bskv.reset() 

bskv.setSkipBuilt(True) 

bskv.processCandidate(kc1) 

bskv.processCandidate(kc2) 

bskv.processCandidate(kc3) 

# Processed none of them 

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

 

def testWithThreeBases(self): 

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

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

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

bskv1 = ipDiffim.BuildSingleKernelVisitorF(self.kList, self.policy) 

bskv1.processCandidate(kc1) 

bskv1.processCandidate(kc2) 

bskv1.processCandidate(kc3) 

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

 

# make sure orig solution is the current one 

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

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

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

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

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

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

 

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

imagePca = ipDiffim.KernelPcaD() 

kpv = ipDiffim.KernelPcaVisitorF(imagePca) 

kpv.processCandidate(kc1) 

kpv.processCandidate(kc2) 

kpv.processCandidate(kc3) 

kpv.subtractMean() 

imagePca.analyze() 

eigenKernels = [] 

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

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

 

# do twice to mimic a Pca loop 

bskv2 = ipDiffim.BuildSingleKernelVisitorF(eigenKernels, self.policy) 

bskv2.setSkipBuilt(False) 

bskv2.processCandidate(kc1) 

bskv2.processCandidate(kc2) 

bskv2.processCandidate(kc3) 

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

 

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

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

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

# pca is recent 

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

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

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

# orig is still orig 

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

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

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

# pca is not orig 

self.assertNotEqual(soln1_2, soln1_1) 

self.assertNotEqual(soln2_2, soln2_1) 

self.assertNotEqual(soln3_2, soln3_1) 

 

# do twice to mimic a Pca loop 

bskv3 = ipDiffim.BuildSingleKernelVisitorF(eigenKernels, self.policy) 

bskv3.setSkipBuilt(False) 

bskv3.processCandidate(kc1) 

bskv3.processCandidate(kc2) 

bskv3.processCandidate(kc3) 

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

 

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

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

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

# pca is recent 

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

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

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

# pca is not previous pca 

self.assertNotEqual(soln1_2, soln1_3) 

self.assertNotEqual(soln2_2, soln2_3) 

self.assertNotEqual(soln3_2, soln3_3) 

 

def testRejection(self): 

# we need to construct a candidate whose shape does not 

# match the underlying basis 

# 

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

# the center, but the candidate requires some off center 

# power 

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

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

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

bskv1 = ipDiffim.BuildSingleKernelVisitorF(self.kList, self.policy) 

bskv1.processCandidate(kc1) 

bskv1.processCandidate(kc2) 

bskv1.processCandidate(kc3) 

 

imagePca = ipDiffim.KernelPcaD() 

kpv = ipDiffim.KernelPcaVisitorF(imagePca) 

kpv.processCandidate(kc1) 

kpv.processCandidate(kc2) 

kpv.processCandidate(kc3) 

kpv.subtractMean() 

imagePca.analyze() 

eigenKernels = [] 

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

self.assertEqual(len(eigenKernels), 1) 

 

# bogus candidate 

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

mi1.getVariance().set(0.1) 

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

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

mi2.getVariance().set(0.1) 

# make it high enough to make the mean resids large 

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

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

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

 

# process with eigenKernels 

bskv2 = ipDiffim.BuildSingleKernelVisitorF(eigenKernels, self.policy) 

bskv2.setSkipBuilt(False) 

bskv2.processCandidate(kc1) 

bskv2.processCandidate(kc2) 

bskv2.processCandidate(kc3) 

bskv2.processCandidate(kc4) 

 

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

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

 

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

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

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

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

 

def testVisit(self, nCell=3): 

bskv = ipDiffim.BuildSingleKernelVisitorF(self.kList, self.policy) 

 

sizeCellX = self.policy.get("sizeCellX") 

sizeCellY = self.policy.get("sizeCellY") 

 

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

geom.Extent2I(sizeCellX * nCell, 

sizeCellY * nCell)), 

sizeCellX, 

sizeCellY) 

nTot = 0 

for candX in range(nCell): 

for candY in range(nCell): 

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

kc = self.makeCandidate(100.0, 

candX * sizeCellX + sizeCellX // 2, 

candY * sizeCellY + sizeCellY // 2) 

else: 

kc = self.makeCandidate(1.0, 

candX * sizeCellX + sizeCellX // 2, 

candY * sizeCellY + sizeCellY // 2) 

kernelCellSet.insertCandidate(kc) 

nTot += 1 

 

kernelCellSet.visitCandidates(bskv, 1) 

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

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

 

for cell in kernelCellSet.getCellList(): 

for cand in cell.begin(False): 

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

 

def tearDown(self): 

del self.config 

del self.policy 

del self.kList 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()