Coverage for tests/test_compareToHotpants.py: 7%

302 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-02-10 02:44 -0800

1import unittest 

2 

3 

4import lsst.utils.tests 

5import lsst.afw.geom as afwGeom 

6import lsst.afw.image as afwImage 

7import lsst.afw.math as afwMath 

8import lsst.afw.detection as afwDet 

9import lsst.geom as geom 

10import lsst.ip.diffim as ipDiffim 

11import lsst.utils.logging as logUtils 

12import lsst.pex.config as pexConfig 

13 

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

15 

16 

17class DiffimTestCases(lsst.utils.tests.TestCase): 

18 

19 def setUp(self): 

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

21 self.config.kernel.name = "AL" 

22 self.subconfig = self.config.kernel.active 

23 

24 # Test was put together before the min size went to 21 

25 self.subconfig.kernelSize = 19 

26 

27 self.subconfig.scaleByFwhm = False 

28 self.subconfig.fitForBackground = True 

29 self.subconfig.spatialModelType = "polynomial" 

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

31 

32 self.smi = afwImage.MaskedImageF('tests/compareToHotpants/scienceMI.fits') 

33 self.tmi = afwImage.MaskedImageF('tests/compareToHotpants/templateMI.fits') 

34 self.smi.setXY0(0, 0) 

35 self.tmi.setXY0(0, 0) 

36 

37 # Run detection 

38 # detConfig = self.subconfig.detectionConfig 

39 # Note here regarding detConfig: 

40 # 

41 # If I set detThresholdType = "pixel_stdev", I get slightly 

42 # different centroids than if I use "stdev". These different 

43 # centroids screw up the testing since hotpants was hardcoded to 

44 # use the "stdev" centroids. For completeness these are: 

45 # 

46 # 32 32 

47 # 96 32 

48 # 160 32 

49 # 96 95 

50 # 31 96 

51 # 160 96 

52 # 96 160 

53 # 160 160 

54 # 32 160 

55 

56 # As of Winter2013, KernelCandidateDetectionF does not return 

57 # these exact centroids anymore, so I need to hardcode them 

58 # in. 

59 self.footprints = [] 

60 for xc, yc in [(32, 32), (96, 32), (160, 32), 

61 (96, 95), (31, 96), (160, 96), 

62 (96, 160), (160, 160), (32, 160)]: 

63 self.footprints.append(afwDet.Footprint(afwGeom.SpanSet( 

64 geom.Box2I(geom.Point2I(xc, yc), 

65 geom.Extent2I(1, 1))))) 

66 

67 # Make a basis list that hotpants has been run with 

68 nGauss = 1 

69 sGauss = [3.] 

70 dGauss = [3] 

71 self.subconfig.alardNGauss = nGauss 

72 self.subconfig.alardSigGauss = sGauss 

73 self.subconfig.alardDegGauss = dGauss 

74 basisList0 = ipDiffim.makeKernelBasisList(self.subconfig) 

75 

76 # HP does things in a different order, and with different normalization, so reorder list 

77 order = [0, 2, 5, 9, 1, 4, 8, 3, 7, 6] 

78 scaling = [1.000000e+00, 

79 8.866037e-02, 

80 1.218095e+01, 

81 5.099318e-03, 

82 8.866037e-02, 

83 4.179772e-02, 

84 1.138120e-02, 

85 1.218095e+01, 

86 1.138120e-02, 

87 5.099318e-03] 

88 

89 self.basisList = [] 

90 for i in range(len(order)): 

91 im = afwImage.ImageD(basisList0[order[i]].getDimensions()) 

92 basisList0[order[i]].computeImage(im, False) 

93 im /= scaling[i] 

94 # im.writeFits('k%d.fits' % (i)) 

95 k = afwMath.FixedKernel(im) 

96 self.basisList.append(k) 

97 

98 # And a place to put candidates 

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

100 geom.Extent2I(self.smi.getWidth(), 

101 self.smi.getHeight())), 

102 self.ps["sizeCellX"], 

103 self.ps["sizeCellY"]) 

104 

105 # There are some -1 factors that come from differences in how 

106 # convolution is done. Some resulting convovled images end up 

107 # being a factor of -1 different, therefore the coefficients 

108 # need to be a factor of -1 different as well. 

109 self.parity = [1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1] 

110 

111 def tearDown(self): 

112 del self.ps 

113 del self.tmi 

114 del self.smi 

115 del self.basisList 

116 del self.footprints 

117 del self.kernelCellSet 

118 

119 def testSingleNoVariation(self): 

120 self.ps['constantVarianceWeighting'] = True 

121 self.ps['spatialKernelOrder'] = 0 

122 self.ps['spatialBgOrder'] = 0 

123 

124 # Place candidate footprints within the spatial grid 

125 for fp in self.footprints: 

126 bbox = fp.getBBox() 

127 

128 # Grab the centers in the parent's coordinate system 

129 xC = int(0.5 * (bbox.getMinX() + bbox.getMaxX())) 

130 yC = int(0.5 * (bbox.getMinY() + bbox.getMaxY())) 

131 

132 bbox = geom.Box2I(geom.Point2I(int(xC)-24, int(yC)-24), geom.Extent2I(49, 49)) 

133 

134 tsmi = afwImage.MaskedImageF(self.tmi, bbox, origin=afwImage.LOCAL) 

135 ssmi = afwImage.MaskedImageF(self.smi, bbox, origin=afwImage.LOCAL) 

136 

137 # Hotpants centroids go from -1 to 1 

138 # Only one passes 

139 if xC > 150 and yC > 150: 

140 cand = ipDiffim.makeKernelCandidate((xC - 0.5 * self.smi.getWidth()) 

141 / (0.5 * self.smi.getWidth()), 

142 (yC - 0.5 * self.smi.getHeight()) 

143 / (0.5 * self.smi.getHeight()), 

144 tsmi, ssmi, self.ps) 

145 self.kernelCellSet.insertCandidate(cand) 

146 

147 # Visitors 

148 bbox = self.kernelCellSet.getBBox() 

149 bsikv = ipDiffim.BuildSingleKernelVisitorF(self.basisList, self.ps) 

150 bspkv = ipDiffim.BuildSpatialKernelVisitorF(self.basisList, bbox, self.ps) 

151 

152 for cell in self.kernelCellSet.getCellList(): 

153 for cand in cell.begin(False): # False = include bad candidates 

154 bsikv.processCandidate(cand) 

155 bspkv.processCandidate(cand) 

156 

157 HPsingleSolution = [0.959086, 

158 -0.000344, 

159 -0.197758, 

160 0.000019, 

161 -0.000172, 

162 0.000053, 

163 0.000018, 

164 -0.192776, 

165 0.000000, 

166 0.000001, 

167 0.602642] 

168 

169 HPspatialSolution = HPsingleSolution 

170 

171 singleSolution = cand.getKernel(ipDiffim.KernelCandidateF.RECENT).getKernelParameters() 

172 for i in range(len(singleSolution)): 

173 self.assertAlmostEqual(HPsingleSolution[i] * self.parity[i], singleSolution[i], 5) 

174 

175 bspkv.solveLinearEquation() 

176 sk, sb = bspkv.getSolutionPair() 

177 spatialSolution = sk.getKernelParameters() 

178 for i in range(len(spatialSolution)): 

179 self.assertAlmostEqual(HPspatialSolution[i] * self.parity[i], spatialSolution[i], 6) 

180 

181 self.assertAlmostEqual(sb.getParameters()[0], HPspatialSolution[-1], 5) 

182 

183 def testFourNoVariation(self): 

184 self.ps['constantVarianceWeighting'] = True 

185 self.ps['spatialKernelOrder'] = 0 

186 self.ps['spatialBgOrder'] = 0 

187 

188 # Place candidate footprints within the spatial grid 

189 for fp in self.footprints: 

190 bbox = fp.getBBox() 

191 

192 # Grab the centers in the parent's coordinate system 

193 xC = int(0.5 * (bbox.getMinX() + bbox.getMaxX())) 

194 yC = int(0.5 * (bbox.getMinY() + bbox.getMaxY())) 

195 

196 bbox = geom.Box2I(geom.Point2I(int(xC)-24, int(yC)-24), geom.Extent2I(49, 49)) 

197 

198 tsmi = afwImage.MaskedImageF(self.tmi, bbox, origin=afwImage.LOCAL) 

199 ssmi = afwImage.MaskedImageF(self.smi, bbox, origin=afwImage.LOCAL) 

200 

201 # Hotpants centroids go from -1 to 1 

202 if xC > 90 and yC > 90: 

203 cand = ipDiffim.makeKernelCandidate((xC - 0.5 * self.smi.getWidth()) 

204 / (0.5 * self.smi.getWidth()), 

205 (yC - 0.5 * self.smi.getHeight()) 

206 / (0.5 * self.smi.getHeight()), 

207 tsmi, ssmi, self.ps) 

208 

209 self.kernelCellSet.insertCandidate(cand) 

210 

211 # Visitors 

212 bbox = self.kernelCellSet.getBBox() 

213 bsikv = ipDiffim.BuildSingleKernelVisitorF(self.basisList, self.ps) 

214 bspkv = ipDiffim.BuildSpatialKernelVisitorF(self.basisList, bbox, self.ps) 

215 

216 for cell in self.kernelCellSet.getCellList(): 

217 for cand in cell.begin(False): # False = include bad candidates 

218 bsikv.processCandidate(cand) 

219 bspkv.processCandidate(cand) 

220 

221 HPspatialSolution = [0.969559, 

222 -0.000223, 

223 -0.198374, 

224 0.000012, 

225 -0.000010, 

226 0.000036, 

227 -0.000004, 

228 -0.206751, 

229 0.000012, 

230 0.000004, 

231 0.452304] 

232 

233 bspkv.solveLinearEquation() 

234 sk, sb = bspkv.getSolutionPair() 

235 spatialSolution = sk.getKernelParameters() 

236 for i in range(len(spatialSolution)): 

237 self.assertAlmostEqual(HPspatialSolution[i] * self.parity[i], spatialSolution[i], 5) 

238 

239 self.assertAlmostEqual(sb.getParameters()[0], HPspatialSolution[-1], 5) 

240 

241 def testFourKernelVariation(self): 

242 self.ps['constantVarianceWeighting'] = True 

243 self.ps['spatialKernelOrder'] = 1 

244 self.ps['spatialBgOrder'] = 0 

245 

246 # Place candidate footprints within the spatial grid 

247 for fp in self.footprints: 

248 bbox = fp.getBBox() 

249 

250 # Grab the centers in the parent's coordinate system 

251 xC = int(0.5 * (bbox.getMinX() + bbox.getMaxX())) 

252 yC = int(0.5 * (bbox.getMinY() + bbox.getMaxY())) 

253 

254 bbox = geom.Box2I(geom.Point2I(int(xC)-24, int(yC)-24), geom.Extent2I(49, 49)) 

255 

256 tsmi = afwImage.MaskedImageF(self.tmi, bbox, origin=afwImage.LOCAL) 

257 ssmi = afwImage.MaskedImageF(self.smi, bbox, origin=afwImage.LOCAL) 

258 

259 # Hotpants centroids go from -1 to 1 

260 if xC > 90 and yC > 90: 

261 cand = ipDiffim.makeKernelCandidate((xC - 0.5 * self.smi.getWidth()) 

262 / (0.5 * self.smi.getWidth()), 

263 (yC - 0.5 * self.smi.getHeight()) 

264 / (0.5 * self.smi.getHeight()), 

265 tsmi, ssmi, self.ps) 

266 self.kernelCellSet.insertCandidate(cand) 

267 

268 # Visitors 

269 bbox = self.kernelCellSet.getBBox() 

270 bsikv = ipDiffim.BuildSingleKernelVisitorF(self.basisList, self.ps) 

271 bspkv = ipDiffim.BuildSpatialKernelVisitorF(self.basisList, bbox, self.ps) 

272 

273 for cell in self.kernelCellSet.getCellList(): 

274 for cand in cell.begin(False): # False = include bad candidates 

275 bsikv.processCandidate(cand) 

276 bspkv.processCandidate(cand) 

277 

278 HPspatialSolution = [[0.969559, 

279 0., 

280 0.], 

281 [-0.000082, 

282 -0.000620, 

283 0.000185], 

284 [-0.197749, 

285 0.001418, 

286 -0.003321], 

287 [0.000002, 

288 0.000049, 

289 -0.000016], 

290 [0.000211, 

291 -0.000283, 

292 -0.000397], 

293 [0.000034, 

294 0.000002, 

295 0.000006], 

296 [-0.000013, 

297 0.000041, 

298 -0.000010], 

299 [-0.220238, 

300 0.028395, 

301 0.013148], 

302 [0.000019, 

303 -0.000025, 

304 0.000003], 

305 [0.000003, 

306 0.000000, 

307 0.000005], 

308 0.452304] 

309 

310 bspkv.solveLinearEquation() 

311 sk, sb = bspkv.getSolutionPair() 

312 spatialSolution = sk.getSpatialParameters() 

313 

314 for i in range(len(spatialSolution)): 

315 # HP and LSST switch the order x<->y 

316 self.assertAlmostEqual(HPspatialSolution[i][0] * self.parity[i], spatialSolution[i][0], 5) 

317 self.assertAlmostEqual(HPspatialSolution[i][1] * self.parity[i], spatialSolution[i][2], 5) 

318 self.assertAlmostEqual(HPspatialSolution[i][2] * self.parity[i], spatialSolution[i][1], 5) 

319 

320 self.assertAlmostEqual(sb.getParameters()[0], HPspatialSolution[-1], 5) 

321 

322 def testFourBgVariation(self): 

323 

324 # OK, so these can end up a bit different due to how HP and 

325 # LSST represent the background in the matrix math. HP has 

326 # each pixel have its own coordinate (which goes from -1 to 1 

327 # across the entire image, by the way), whereas we give all 

328 # the LSST pixels within a stamp the same coordinate. 

329 

330 # To make this comparison, I go ahead and edit the Hotpants 

331 # code to give each pixel the same weight. For reference this 

332 # is in fillStamp() and I replace: 

333 # 

334 # //xf = (i - rPixX2) / rPixX2; 

335 # xf = (xi - rPixX2) / rPixX2; 

336 # 

337 # //yf = (j - rPixY2) / rPixY2; 

338 # yf = (yi - rPixY2) / rPixY2; 

339 

340 self.ps['constantVarianceWeighting'] = True 

341 self.ps['spatialKernelOrder'] = 0 

342 self.ps['spatialBgOrder'] = 1 

343 

344 # Place candidate footprints within the spatial grid 

345 for fp in self.footprints: 

346 bbox = fp.getBBox() 

347 

348 # Grab the centers in the parent's coordinate system 

349 xC = int(0.5 * (bbox.getMinX() + bbox.getMaxX())) 

350 yC = int(0.5 * (bbox.getMinY() + bbox.getMaxY())) 

351 

352 bbox = geom.Box2I(geom.Point2I(int(xC)-24, int(yC)-24), geom.Extent2I(49, 49)) 

353 

354 tsmi = afwImage.MaskedImageF(self.tmi, bbox, origin=afwImage.LOCAL) 

355 ssmi = afwImage.MaskedImageF(self.smi, bbox, origin=afwImage.LOCAL) 

356 

357 # Hotpants centroids go from -1 to 1 

358 if xC > 90 and yC > 90: 

359 cand = ipDiffim.makeKernelCandidate((xC - 0.5 * self.smi.getWidth()) 

360 / (0.5 * self.smi.getWidth()), 

361 (yC - 0.5 * self.smi.getHeight()) 

362 / (0.5 * self.smi.getHeight()), 

363 tsmi, ssmi, self.ps) 

364 # print 'OBJECT', cand.getId(), 'AT', xC, yC, cand.getXCenter(), cand.getYCenter() 

365 self.kernelCellSet.insertCandidate(cand) 

366 

367 # Visitors 

368 bbox = self.kernelCellSet.getBBox() 

369 bsikv = ipDiffim.BuildSingleKernelVisitorF(self.basisList, self.ps) 

370 bspkv = ipDiffim.BuildSpatialKernelVisitorF(self.basisList, bbox, self.ps) 

371 

372 for cell in self.kernelCellSet.getCellList(): 

373 for cand in cell.begin(False): # False = include bad candidates 

374 bsikv.processCandidate(cand) 

375 bspkv.processCandidate(cand) 

376 

377 HPspatialSolution = [0.969559, 

378 -0.000223, 

379 -0.198374, 

380 0.000012, 

381 -0.000010, 

382 0.000036, 

383 -0.000004, 

384 -0.206751, 

385 0.000012, 

386 0.000004, 

387 [0.782113, 

388 -0.910963, 

389 -0.106636]] 

390 

391 bspkv.solveLinearEquation() 

392 sk, sb = bspkv.getSolutionPair() 

393 spatialSolution = sk.getKernelParameters() 

394 for i in range(len(spatialSolution)): 

395 self.assertAlmostEqual(HPspatialSolution[i] * self.parity[i], spatialSolution[i], 5) 

396 

397 self.assertAlmostEqual(sb.getParameters()[0], HPspatialSolution[-1][0], 5) 

398 self.assertAlmostEqual(sb.getParameters()[1], HPspatialSolution[-1][2], 5) # x<->y 

399 self.assertAlmostEqual(sb.getParameters()[2], HPspatialSolution[-1][1], 5) # x<->y 

400 

401 def testFourVariation(self): 

402 self.ps['constantVarianceWeighting'] = True 

403 self.ps['spatialKernelOrder'] = 1 

404 self.ps['spatialBgOrder'] = 1 

405 

406 # Place candidate footprints within the spatial grid 

407 for fp in self.footprints: 

408 bbox = fp.getBBox() 

409 

410 # Grab the centers in the parent's coordinate system 

411 xC = int(0.5 * (bbox.getMinX() + bbox.getMaxX())) 

412 yC = int(0.5 * (bbox.getMinY() + bbox.getMaxY())) 

413 

414 bbox = geom.Box2I(geom.Point2I(int(xC)-24, int(yC)-24), geom.Extent2I(49, 49)) 

415 

416 tsmi = afwImage.MaskedImageF(self.tmi, bbox, origin=afwImage.LOCAL) 

417 ssmi = afwImage.MaskedImageF(self.smi, bbox, origin=afwImage.LOCAL) 

418 

419 # Hotpants centroids go from -1 to 1 

420 if xC > 90 and yC > 90: 

421 cand = ipDiffim.makeKernelCandidate((xC - 0.5 * self.smi.getWidth()) 

422 / (0.5 * self.smi.getWidth()), 

423 (yC - 0.5 * self.smi.getHeight()) 

424 / (0.5 * self.smi.getHeight()), 

425 tsmi, ssmi, self.ps) 

426 self.kernelCellSet.insertCandidate(cand) 

427 

428 # Visitors 

429 bbox = self.kernelCellSet.getBBox() 

430 bsikv = ipDiffim.BuildSingleKernelVisitorF(self.basisList, self.ps) 

431 bspkv = ipDiffim.BuildSpatialKernelVisitorF(self.basisList, bbox, self.ps) 

432 

433 for cell in self.kernelCellSet.getCellList(): 

434 for cand in cell.begin(False): # False = include bad candidates 

435 bsikv.processCandidate(cand) 

436 bspkv.processCandidate(cand) 

437 

438 HPspatialSolution = [[0.969559, 

439 0., 

440 0.], 

441 [-0.000082, 

442 -0.000620, 

443 0.000185], 

444 [-0.197749, 

445 0.001418, 

446 -0.003321], 

447 [0.000002, 

448 0.000049, 

449 -0.000016], 

450 [0.000211, 

451 -0.000283, 

452 -0.000397], 

453 [0.000034, 

454 0.000002, 

455 0.000006], 

456 [-0.000013, 

457 0.000041, 

458 -0.000010], 

459 [-0.220238, 

460 0.028395, 

461 0.013148], 

462 [0.000019, 

463 -0.000025, 

464 0.000003], 

465 [0.000003, 

466 0.000000, 

467 0.000005], 

468 [0.782113, 

469 -0.910963, 

470 -0.106636]] 

471 

472 bspkv.solveLinearEquation() 

473 sk, sb = bspkv.getSolutionPair() 

474 spatialSolution = sk.getSpatialParameters() 

475 

476 for i in range(len(spatialSolution)): 

477 # HP and LSST switch the order x<->y 

478 self.assertAlmostEqual(HPspatialSolution[i][0] * self.parity[i], spatialSolution[i][0], 5) 

479 self.assertAlmostEqual(HPspatialSolution[i][1] * self.parity[i], spatialSolution[i][2], 5) 

480 self.assertAlmostEqual(HPspatialSolution[i][2] * self.parity[i], spatialSolution[i][1], 5) 

481 

482 self.assertAlmostEqual(sb.getParameters()[0], HPspatialSolution[-1][0], 5) 

483 self.assertAlmostEqual(sb.getParameters()[1], HPspatialSolution[-1][2], 5) # x<->y 

484 self.assertAlmostEqual(sb.getParameters()[2], HPspatialSolution[-1][1], 5) # x<->y 

485 

486 def testAllBgVariation2(self): 

487 # OK, I ran HP on all the things in this image. Enough for 

488 # second order spatial variation 

489 

490 self.ps['constantVarianceWeighting'] = True 

491 self.ps['spatialKernelOrder'] = 0 

492 self.ps['spatialBgOrder'] = 2 

493 

494 # Ignore the whole kernelCellSet thing 

495 cands = [] 

496 for fp in self.footprints: 

497 bbox = fp.getBBox() 

498 

499 # Grab the centers in the parent's coordinate system 

500 xC = int(0.5 * (bbox.getMinX() + bbox.getMaxX())) 

501 yC = int(0.5 * (bbox.getMinY() + bbox.getMaxY())) 

502 

503 bbox = geom.Box2I(geom.Point2I(int(xC)-24, int(yC)-24), geom.Extent2I(49, 49)) 

504 

505 tsmi = afwImage.MaskedImageF(self.tmi, bbox, origin=afwImage.LOCAL) 

506 ssmi = afwImage.MaskedImageF(self.smi, bbox, origin=afwImage.LOCAL) 

507 

508 # Hotpants centroids go from -1 to 1 

509 cand = ipDiffim.makeKernelCandidate((xC - 0.5 * self.smi.getWidth()) 

510 / (0.5 * self.smi.getWidth()), 

511 (yC - 0.5 * self.smi.getHeight()) 

512 / (0.5 * self.smi.getHeight()), 

513 tsmi, ssmi, self.ps) 

514 cands.append(cand) 

515 

516 # Visitors 

517 bbox = self.kernelCellSet.getBBox() 

518 bsikv = ipDiffim.BuildSingleKernelVisitorF(self.basisList, self.ps) 

519 bspkv = ipDiffim.BuildSpatialKernelVisitorF(self.basisList, bbox, self.ps) 

520 

521 for cand in cands: 

522 bsikv.processCandidate(cand) 

523 bspkv.processCandidate(cand) 

524 

525 HPspatialSolution = [0.968505, 

526 -0.000053, 

527 -0.206505, 

528 0.000005, 

529 0.000062, 

530 0.000028, 

531 -0.000004, 

532 -0.206135, 

533 0.000005, 

534 0.000001, 

535 [0.812488, 

536 0.096456, 

537 -1.140900, 

538 0.132670, 

539 -0.571923, 

540 -0.284670]] 

541 

542 bspkv.solveLinearEquation() 

543 sk, sb = bspkv.getSolutionPair() 

544 spatialSolution = sk.getKernelParameters() 

545 

546 # Kernel 

547 for i in range(len(spatialSolution)): 

548 self.assertAlmostEqual(HPspatialSolution[i] * self.parity[i], spatialSolution[i], 5) 

549 

550 # Bg 

551 # Ordering of second order terms is just messy 

552 spReorder = [0, 3, 1, 5, 4, 2] 

553 spatialSolution = sb.getParameters() 

554 for i in range(len(spatialSolution)): 

555 self.assertAlmostEqual(HPspatialSolution[-1][spReorder[i]], spatialSolution[i], 5) 

556 

557 def testAllKernelVariation2(self): 

558 # OK, I ran HP on all the things in this image. Enough for 

559 # second order spatial variation 

560 

561 self.ps['constantVarianceWeighting'] = True 

562 self.ps['spatialKernelOrder'] = 2 

563 self.ps['spatialBgOrder'] = 0 

564 

565 # Ignore the whole kernelCellSet thing 

566 cands = [] 

567 for fp in self.footprints: 

568 bbox = fp.getBBox() 

569 

570 # Grab the centers in the parent's coordinate system 

571 xC = int(0.5 * (bbox.getMinX() + bbox.getMaxX())) 

572 yC = int(0.5 * (bbox.getMinY() + bbox.getMaxY())) 

573 

574 bbox = geom.Box2I(geom.Point2I(int(xC)-24, int(yC)-24), geom.Extent2I(49, 49)) 

575 

576 tsmi = afwImage.MaskedImageF(self.tmi, bbox, origin=afwImage.LOCAL) 

577 ssmi = afwImage.MaskedImageF(self.smi, bbox, origin=afwImage.LOCAL) 

578 

579 # Hotpants centroids go from -1 to 1 

580 cand = ipDiffim.makeKernelCandidate((xC - 0.5 * self.smi.getWidth()) 

581 / (0.5 * self.smi.getWidth()), 

582 (yC - 0.5 * self.smi.getHeight()) 

583 / (0.5 * self.smi.getHeight()), 

584 tsmi, ssmi, self.ps) 

585 cands.append(cand) 

586 

587 # Visitors 

588 bbox = self.kernelCellSet.getBBox() 

589 bsikv = ipDiffim.BuildSingleKernelVisitorF(self.basisList, self.ps) 

590 bspkv = ipDiffim.BuildSpatialKernelVisitorF(self.basisList, bbox, self.ps) 

591 

592 for cand in cands: 

593 bsikv.processCandidate(cand) 

594 bspkv.processCandidate(cand) 

595 

596 HPspatialSolution = [[0.968505, 

597 0., 

598 0., 

599 0., 

600 0., 

601 0., 

602 0.], 

603 [-0.000094, 

604 -0.000206, 

605 -0.000027, 

606 0.000025, 

607 -0.000705, 

608 0.000162], 

609 [-0.188375, 

610 0.001801, 

611 -0.027534, 

612 0.008718, 

613 0.016346, 

614 -0.033879], 

615 [0.000004, 

616 0.000012, 

617 0.000023, 

618 0.000004, 

619 0.000017, 

620 -0.000020], 

621 [0.000128, 

622 -0.000218, 

623 0.000304, 

624 -0.000038, 

625 -0.000151, 

626 -0.000531], 

627 [-0.000011, 

628 -0.000013, 

629 0.000038, 

630 -0.000017, 

631 0.000133, 

632 0.000093], 

633 [-0.000003, 

634 0.000003, 

635 0.000006, 

636 0.000008, 

637 0.000002, 

638 -0.000010], 

639 [-0.212235, 

640 -0.000856, 

641 0.012246, 

642 -0.010893, 

643 0.049302, 

644 0.008249], 

645 [0.000014, 

646 -0.000002, 

647 -0.000050, 

648 -0.000001, 

649 0.000030, 

650 0.000020], 

651 [-0.000001, 

652 0.000010, 

653 -0.000012, 

654 -0.000007, 

655 0.000015, 

656 0.000019], 

657 0.392482] 

658 

659 bspkv.solveLinearEquation() 

660 sk, sb = bspkv.getSolutionPair() 

661 spatialSolution = sk.getSpatialParameters() 

662 

663 # Kernel 

664 spReorder = [0, 3, 1, 5, 4, 2] 

665 for i in range(len(spatialSolution)): 

666 for j in range(len(spReorder)): 

667 self.assertAlmostEqual(HPspatialSolution[i][spReorder[j]] * self.parity[i], 

668 spatialSolution[i][j], 5) 

669 

670 self.assertAlmostEqual(sb.getParameters()[0], HPspatialSolution[-1], 5) 

671 

672 def testAllVariation2(self): 

673 # OK, I ran HP on all the things in this image. Enough for 

674 # second order spatial variation 

675 

676 self.ps['constantVarianceWeighting'] = True 

677 self.ps['spatialKernelOrder'] = 2 

678 self.ps['spatialBgOrder'] = 2 

679 

680 # Ignore the whole kernelCellSet thing 

681 cands = [] 

682 for fp in self.footprints: 

683 bbox = fp.getBBox() 

684 

685 # Grab the centers in the parent's coordinate system 

686 xC = int(0.5 * (bbox.getMinX() + bbox.getMaxX())) 

687 yC = int(0.5 * (bbox.getMinY() + bbox.getMaxY())) 

688 

689 bbox = geom.Box2I(geom.Point2I(int(xC)-24, int(yC)-24), geom.Extent2I(49, 49)) 

690 

691 tsmi = afwImage.MaskedImageF(self.tmi, bbox, origin=afwImage.LOCAL) 

692 ssmi = afwImage.MaskedImageF(self.smi, bbox, origin=afwImage.LOCAL) 

693 

694 # Hotpants centroids go from -1 to 1 

695 cand = ipDiffim.makeKernelCandidate((xC - 0.5 * self.smi.getWidth()) 

696 / (0.5 * self.smi.getWidth()), 

697 (yC - 0.5 * self.smi.getHeight()) 

698 / (0.5 * self.smi.getHeight()), 

699 tsmi, ssmi, self.ps) 

700 cands.append(cand) 

701 

702 # Visitors 

703 bbox = self.kernelCellSet.getBBox() 

704 bsikv = ipDiffim.BuildSingleKernelVisitorF(self.basisList, self.ps) 

705 bspkv = ipDiffim.BuildSpatialKernelVisitorF(self.basisList, bbox, self.ps) 

706 

707 for cand in cands: 

708 bsikv.processCandidate(cand) 

709 bspkv.processCandidate(cand) 

710 

711 HPspatialSolution = [[0.968505, 

712 0., 

713 0., 

714 0., 

715 0., 

716 0.], 

717 [-0.000094, 

718 -0.000206, 

719 -0.000027, 

720 0.000025, 

721 -0.000705, 

722 0.000162], 

723 [-0.188375, 

724 0.001801, 

725 -0.027534, 

726 0.008718, 

727 0.016346, 

728 -0.033879], 

729 [0.000004, 

730 0.000012, 

731 0.000023, 

732 0.000004, 

733 0.000017, 

734 -0.000020], 

735 [0.000128, 

736 -0.000218, 

737 0.000304, 

738 -0.000038, 

739 -0.000151, 

740 -0.000531], 

741 [-0.000011, 

742 -0.000013, 

743 0.000038, 

744 -0.000017, 

745 0.000133, 

746 0.000093], 

747 [-0.000003, 

748 0.000003, 

749 0.000006, 

750 0.000008, 

751 0.000002, 

752 -0.000010], 

753 [-0.212235, 

754 -0.000856, 

755 0.012246, 

756 -0.010893, 

757 0.049302, 

758 0.008249], 

759 [0.000014, 

760 -0.000002, 

761 -0.000050, 

762 -0.000001, 

763 0.000030, 

764 0.000020], 

765 [-0.000001, 

766 0.000010, 

767 -0.000012, 

768 -0.000007, 

769 0.000015, 

770 0.000019], 

771 [0.812488, 

772 0.096456, 

773 -1.140900, 

774 0.132670, 

775 -0.571923, 

776 -0.284670]] 

777 

778 bspkv.solveLinearEquation() 

779 sk, sb = bspkv.getSolutionPair() 

780 spatialSolution = sk.getSpatialParameters() 

781 

782 # Kernel 

783 spReorder = [0, 3, 1, 5, 4, 2] 

784 for i in range(len(spatialSolution)): 

785 for j in range(len(spReorder)): 

786 self.assertAlmostEqual(HPspatialSolution[i][spReorder[j]] * self.parity[i], 

787 spatialSolution[i][j], 5) 

788 # Bg 

789 spatialSolution = sb.getParameters() 

790 for i in range(len(spatialSolution)): 

791 self.assertAlmostEqual(HPspatialSolution[-1][spReorder[i]], spatialSolution[i], 5) 

792 

793 

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

795 pass 

796 

797 

798def setup_module(module): 

799 lsst.utils.tests.init() 

800 

801 

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

803 lsst.utils.tests.init() 

804 unittest.main()