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

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

import os 

import unittest 

 

 

import lsst.utils.tests 

import lsst.utils 

import lsst.afw.geom as afwGeom 

import lsst.afw.image as afwImage 

import lsst.afw.math as afwMath 

import lsst.ip.diffim as ipDiffim 

import lsst.pex.config as pexConfig 

import lsst.log.utils as logUtils 

import lsst.afw.display.ds9 as ds9 

import lsst.afw.table as afwTable 

 

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

 

# known input images 

try: 

defDataDir = lsst.utils.getPackageDir('afwdata') 

except Exception: 

defDataDir = None 

 

 

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

 

def setUp(self): 

schema = afwTable.SourceTable.makeMinimalSchema() 

afwTable.Point2DKey.addFields(schema, "Centroid", "input centroid", "pixel") 

schema.addField("PsfFlux_instFlux", type=float) 

schema.addField("PsfFlux_instFluxErr", type=float) 

schema.addField("PsfFlux_flag", type="Flag") 

self.table = afwTable.SourceTable.make(schema) 

self.table.definePsfFlux("PsfFlux") 

self.table.defineCentroid("Centroid") 

self.ss = afwTable.SourceCatalog(self.table) 

 

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('fitForBackground', True) # we are testing known background recovery here 

self.policy.set('checkConditionNumber', False) # just in case 

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

 

if defDataDir: 

defSciencePath = os.path.join(defDataDir, "DC3a-Sim", "sci", "v26-e0", 

"v26-e0-c011-a10.sci.fits") 

defTemplatePath = os.path.join(defDataDir, "DC3a-Sim", "sci", "v5-e0", 

"v5-e0-c011-a10.sci.fits") 

 

scienceExposure = afwImage.ExposureF(defSciencePath) 

templateExposure = afwImage.ExposureF(defTemplatePath) 

# set XY0 = 0 

scienceExposure.setXY0(afwGeom.Point2I(0, 0)) 

templateExposure.setXY0(afwGeom.Point2I(0, 0)) 

# do the warping first so we don't have any masked pixels in the postage stamps 

warper = afwMath.Warper.fromConfig(self.subconfig.warpingConfig) 

templateExposure = warper.warpExposure(scienceExposure.getWcs(), templateExposure, 

destBBox=scienceExposure.getBBox()) 

 

# Change xy0 

# Nice star at position 276, 717 

# And should be at index 40, 40 

# No masked pixels in this one 

self.x02 = 276 

self.y02 = 717 

size = 40 

bbox2 = afwGeom.Box2I(afwGeom.Point2I(self.x02 - size, self.y02 - size), 

afwGeom.Point2I(self.x02 + size, self.y02 + size)) 

self.scienceImage2 = afwImage.ExposureF(scienceExposure, bbox2, origin=afwImage.LOCAL) 

self.templateExposure2 = afwImage.ExposureF(templateExposure, bbox2, origin=afwImage.LOCAL) 

 

def addNoise(self, mi): 

img = mi.getImage() 

seed = int(afwMath.makeStatistics(mi.getVariance(), afwMath.MEDIAN).getValue()) 

rdm = afwMath.Random(afwMath.Random.MT19937, seed) 

rdmImage = img.Factory(img.getDimensions()) 

afwMath.randomGaussianImage(rdmImage, rdm) 

img += rdmImage 

return afwMath.makeStatistics(rdmImage, afwMath.MEAN).getValue(afwMath.MEAN) 

 

def verifyDeltaFunctionSolution(self, solution, kSum=1.0, bg=0.0): 

# when kSum = 1.0, this agrees to the default precision. when 

# kSum != 1.0 I need to go to only 4 digits. 

# 

# -5.4640810225678728e-06 != 0.0 within 7 places 

# 

bgSolution = solution.getBackground() 

self.assertAlmostEqual(bgSolution, bg, 4) 

 

# again when kSum = 1.0 this agrees. otherwise 

# 

# 2.7000000605594079 != 2.7000000000000002 within 7 places 

# 

kSumSolution = solution.getKsum() 

self.assertAlmostEqual(kSumSolution, kSum, 5) 

 

kImage = solution.makeKernelImage() 

for j in range(kImage.getHeight()): 

for i in range(kImage.getWidth()): 

 

if (i == kImage.getWidth() // 2) and (j == kImage.getHeight() // 2): 

self.assertAlmostEqual(kImage[i, j, afwImage.LOCAL], kSum, 5) 

else: 

self.assertAlmostEqual(kImage[i, j, afwImage.LOCAL], 0., 5) 

 

@unittest.skipIf(not defDataDir, "Warning: afwdata is not set up") 

def testConstructor(self): 

# Original and uninitialized 

kc = ipDiffim.KernelCandidateF(self.x02, self.y02, 

self.templateExposure2.getMaskedImage(), 

self.scienceImage2.getMaskedImage(), 

self.policy) 

 

# Kernel not initialized 

self.assertEqual(kc.isInitialized(), False) 

 

# But this should be set on construction 

try: 

kc.getCandidateRating() 

except Exception as e: 

print(e) 

self.fail() 

 

# And these should be filled 

try: 

kc.getTemplateMaskedImage() 

kc.getScienceMaskedImage() 

except Exception as e: 

print(e) 

self.fail() 

 

# And of the right type 

self.assertEqual(type(kc.getTemplateMaskedImage()), afwImage.MaskedImageF) 

self.assertEqual(type(kc.getScienceMaskedImage()), afwImage.MaskedImageF) 

 

# None of these should work 

for kType in (ipDiffim.KernelCandidateF.ORIG, 

ipDiffim.KernelCandidateF.PCA, 

ipDiffim.KernelCandidateF.RECENT): 

for kMethod in (kc.getKernelSolution, 

kc.getKernel, 

kc.getBackground, 

kc.getKsum, 

kc.getKernelImage, 

kc.getDifferenceImage): 

try: 

kMethod(kType) 

except Exception as e: 

pass 

else: 

self.fail() 

try: 

kc.getImage() 

except Exception: 

pass 

else: 

self.fail() 

 

@unittest.skipIf(not defDataDir, "Warning: afwdata is not set up") 

def testSourceStats(self): 

source = self.ss.addNew() 

source.setId(1) 

source.set(self.table.getCentroidKey().getX(), 276) 

source.set(self.table.getCentroidKey().getY(), 717) 

source.set("slot_PsfFlux_instFlux", 1.) 

 

kc = ipDiffim.KernelCandidateF(source, 

self.templateExposure2.getMaskedImage(), 

self.scienceImage2.getMaskedImage(), 

self.policy) 

kList = ipDiffim.makeKernelBasisList(self.subconfig) 

 

kc.build(kList) 

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

 

@unittest.skipIf(not defDataDir, "Warning: afwdata is not set up") 

def testSourceConstructor(self): 

source = self.ss.addNew() 

source.setId(1) 

source.set(self.table.getCentroidKey().getX(), 276) 

source.set(self.table.getCentroidKey().getY(), 717) 

source.set("slot_PsfFlux_instFlux", 1.) 

 

kc = ipDiffim.KernelCandidateF(source, 

self.templateExposure2.getMaskedImage(), 

self.scienceImage2.getMaskedImage(), 

self.policy) 

 

# Kernel not initialized 

self.assertEqual(kc.isInitialized(), False) 

 

# Check that the source is set 

self.assertEqual(kc.getSource(), source) 

self.assertEqual(kc.getCandidateRating(), source.getPsfInstFlux()) 

 

# But this should be set on construction 

try: 

kc.getCandidateRating() 

except Exception as e: 

print(e) 

self.fail() 

 

# And these should be filled 

try: 

kc.getTemplateMaskedImage() 

kc.getScienceMaskedImage() 

except Exception as e: 

print(e) 

self.fail() 

 

# And of the right type 

self.assertEqual(type(kc.getTemplateMaskedImage()), afwImage.MaskedImageF) 

self.assertEqual(type(kc.getScienceMaskedImage()), afwImage.MaskedImageF) 

 

# None of these should work 

for kType in (ipDiffim.KernelCandidateF.ORIG, 

ipDiffim.KernelCandidateF.PCA, 

ipDiffim.KernelCandidateF.RECENT): 

for kMethod in (kc.getKernelSolution, 

kc.getKernel, 

kc.getBackground, 

kc.getKsum, 

kc.getKernelImage, 

kc.getDifferenceImage): 

try: 

kMethod(kType) 

except Exception as e: 

pass 

else: 

self.fail() 

try: 

kc.getImage() 

except Exception: 

pass 

else: 

self.fail() 

 

kList = ipDiffim.makeKernelBasisList(self.subconfig) 

 

kc.build(kList) 

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

 

@unittest.skipIf(not defDataDir, "Warning: afwdata is not set up") 

def testDeltaFunctionScaled(self, scaling=2.7, bg=11.3): 

sIm = afwImage.MaskedImageF(self.templateExposure2.getMaskedImage(), deep=True) 

sIm *= scaling 

kc = ipDiffim.KernelCandidateF(self.x02, self.y02, 

self.templateExposure2.getMaskedImage(), 

sIm, 

self.policy) 

 

kList = ipDiffim.makeKernelBasisList(self.subconfig) 

kc.build(kList) 

self.verifyDeltaFunctionSolution(kc.getKernelSolution(ipDiffim.KernelCandidateF.RECENT), 

kSum=scaling) 

 

sIm = afwImage.MaskedImageF(self.templateExposure2.getMaskedImage(), deep=True) 

sIm += bg 

kc = ipDiffim.KernelCandidateF(self.x02, self.y02, 

self.templateExposure2.getMaskedImage(), 

sIm, 

self.policy) 

 

kList = ipDiffim.makeKernelBasisList(self.subconfig) 

kc.build(kList) 

self.verifyDeltaFunctionSolution(kc.getKernelSolution(ipDiffim.KernelCandidateF.RECENT), 

bg=bg) 

 

@unittest.skipIf(not defDataDir, "Warning: afwdata is not set up") 

def testDeltaFunction(self): 

# Match an image to itself, with delta-function basis set 

# No regularization 

kc = ipDiffim.KernelCandidateF(self.x02, self.y02, 

self.templateExposure2.getMaskedImage(), 

self.templateExposure2.getMaskedImage(), 

self.policy) 

 

kList = ipDiffim.makeKernelBasisList(self.subconfig) 

 

kc.build(kList) 

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

 

# These should work 

for kType in (ipDiffim.KernelCandidateF.ORIG, 

ipDiffim.KernelCandidateF.RECENT): 

for kMethod in (kc.getKernelSolution, 

kc.getKernel, 

kc.getBackground, 

kc.getKsum, 

kc.getKernelImage, 

kc.getDifferenceImage): 

try: 

kMethod(kType) 

except Exception as e: 

print(kMethod, e) 

self.fail() 

else: 

pass 

try: 

kc.getImage() 

except Exception as e: 

print(kMethod, e) 

self.fail() 

else: 

pass 

 

# None of these should work 

for kType in (ipDiffim.KernelCandidateF.PCA,): 

for kMethod in (kc.getKernelSolution, 

kc.getKernel, 

kc.getBackground, 

kc.getKsum, 

kc.getKernelImage, 

kc.getImage, 

kc.getDifferenceImage): 

try: 

kMethod(kType) 

except Exception as e: 

pass 

else: 

print(kMethod) 

self.fail() 

 

self.verifyDeltaFunctionSolution(kc.getKernelSolution(ipDiffim.KernelCandidateF.RECENT)) 

 

@unittest.skipIf(not defDataDir, "Warning: afwdata is not set up") 

def testGaussianWithNoise(self): 

# Convolve a real image with a gaussian and try and recover 

# it. Add noise and perform the same test. 

 

gsize = self.policy.getInt("kernelSize") 

gaussFunction = afwMath.GaussianFunction2D(2, 3) 

gaussKernel = afwMath.AnalyticKernel(gsize, gsize, gaussFunction) 

kImageIn = afwImage.ImageD(afwGeom.Extent2I(gsize, gsize)) 

kSumIn = gaussKernel.computeImage(kImageIn, False) 

 

imX, imY = self.templateExposure2.getMaskedImage().getDimensions() 

smi = afwImage.MaskedImageF(afwGeom.Extent2I(imX, imY)) 

afwMath.convolve(smi, self.templateExposure2.getMaskedImage(), gaussKernel, False) 

 

bbox = gaussKernel.shrinkBBox(smi.getBBox(afwImage.LOCAL)) 

 

tmi2 = afwImage.MaskedImageF(self.templateExposure2.getMaskedImage(), bbox, origin=afwImage.LOCAL) 

smi2 = afwImage.MaskedImageF(smi, bbox, origin=afwImage.LOCAL) 

 

kc = ipDiffim.KernelCandidateF(self.x02, self.y02, tmi2, smi2, self.policy) 

kList = ipDiffim.makeKernelBasisList(self.subconfig) 

kc.build(kList) 

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

kImageOut = kc.getImage() 

 

# ds9.mtv(kImageIn, frame=1) 

# ds9.mtv(kImageOut, frame=2) 

 

soln = kc.getKernelSolution(ipDiffim.KernelCandidateF.RECENT) 

self.assertAlmostEqual(soln.getKsum(), kSumIn) 

# 8.7499380640430563e-06 != 0.0 within 7 places 

self.assertAlmostEqual(soln.getBackground(), 0.0, 4) 

 

for j in range(kImageOut.getHeight()): 

for i in range(kImageOut.getWidth()): 

 

# in the outskirts of the kernel, the ratio can get screwed because of low S/N 

# e.g. 7.45817359824e-09 vs. 1.18062529402e-08 

# in the guts of the kernel it should look closer 

if kImageIn[i, j, afwImage.LOCAL] > 1e-4: 

# sigh, too bad this sort of thing fails.. 

# 0.99941584433815966 != 1.0 within 3 places 

self.assertAlmostEqual(kImageOut[i, j, afwImage.LOCAL]/kImageIn[i, j, afwImage.LOCAL], 

1.0, 2) 

 

# now repeat with noise added; decrease precision of comparison 

self.addNoise(smi2) 

kc = ipDiffim.KernelCandidateF(self.x02, self.y02, tmi2, smi2, self.policy) 

kList = ipDiffim.makeKernelBasisList(self.subconfig) 

kc.build(kList) 

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

kImageOut = kc.getImage() 

 

# ds9.mtv(kImageIn, frame=3) 

# ds9.mtv(kImageOut, frame=4) 

 

soln = kc.getKernelSolution(ipDiffim.KernelCandidateF.RECENT) 

self.assertAlmostEqual(soln.getKsum(), kSumIn, 3) 

if not (self.policy.get("fitForBackground")): 

self.assertEqual(soln.getBackground(), 0.0) 

 

for j in range(kImageOut.getHeight()): 

for i in range(kImageOut.getWidth()): 

if kImageIn[i, j, afwImage.LOCAL] > 1e-2: 

self.assertAlmostEqual(kImageOut[i, j, afwImage.LOCAL], 

kImageIn[i, j, afwImage.LOCAL], 2) 

 

def testGaussian(self, imsize=50): 

# Convolve a delta function with a known gaussian; try to 

# recover using delta-function basis 

 

gsize = self.policy.getInt("kernelSize") 

tsize = imsize + gsize 

 

gaussFunction = afwMath.GaussianFunction2D(2, 3) 

gaussKernel = afwMath.AnalyticKernel(gsize, gsize, gaussFunction) 

kImageIn = afwImage.ImageD(afwGeom.Extent2I(gsize, gsize)) 

gaussKernel.computeImage(kImageIn, False) 

 

# template image with a single hot pixel in the exact center 

tmi = afwImage.MaskedImageF(afwGeom.Extent2I(tsize, tsize)) 

tmi.set(0, 0x0, 1e-4) 

cpix = tsize // 2 

tmi[cpix, cpix, afwImage.LOCAL] = (1, 0x0, 1) 

 

# science image 

smi = afwImage.MaskedImageF(tmi.getDimensions()) 

afwMath.convolve(smi, tmi, gaussKernel, False) 

 

# get the actual kernel sum (since the image is not infinite) 

gscaling = afwMath.makeStatistics(smi, afwMath.SUM).getValue(afwMath.SUM) 

 

# grab only the non-masked subregion 

bbox = gaussKernel.shrinkBBox(smi.getBBox(afwImage.LOCAL)) 

 

tmi2 = afwImage.MaskedImageF(tmi, bbox, origin=afwImage.LOCAL) 

smi2 = afwImage.MaskedImageF(smi, bbox, origin=afwImage.LOCAL) 

 

# make sure its a valid subregion! 

for j in range(tmi2.getHeight()): 

for i in range(tmi2.getWidth()): 

self.assertEqual(tmi2.mask[i, j, afwImage.LOCAL], 0) 

self.assertEqual(smi2.mask[i, j, afwImage.LOCAL], 0) 

 

kc = ipDiffim.KernelCandidateF(0.0, 0.0, tmi2, smi2, self.policy) 

kList = ipDiffim.makeKernelBasisList(self.subconfig) 

kc.build(kList) 

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

kImageOut = kc.getImage() 

 

soln = kc.getKernelSolution(ipDiffim.KernelCandidateF.RECENT) 

self.assertAlmostEqual(soln.getKsum(), gscaling) 

self.assertAlmostEqual(soln.getBackground(), 0.0) 

 

for j in range(kImageOut.getHeight()): 

for i in range(kImageOut.getWidth()): 

self.assertAlmostEqual(kImageOut[i, j, afwImage.LOCAL]/kImageIn[i, j, afwImage.LOCAL], 

1.0, 5) 

 

def testZeroVariance(self, imsize=50): 

gsize = self.policy.getInt("kernelSize") 

tsize = imsize + gsize 

 

tmi = afwImage.MaskedImageF(afwGeom.Extent2I(tsize, tsize)) 

tmi.set(0, 0x0, 1.0) 

cpix = tsize // 2 

tmi[cpix, cpix, afwImage.LOCAL] = (1, 0x0, 0.0) 

smi = afwImage.MaskedImageF(afwGeom.Extent2I(tsize, tsize)) 

smi.set(0, 0x0, 1.0) 

smi[cpix, cpix, afwImage.LOCAL] = (1, 0x0, 0.0) 

 

kList = ipDiffim.makeKernelBasisList(self.subconfig) 

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

kc = ipDiffim.KernelCandidateF(0.0, 0.0, tmi, smi, self.policy) 

try: 

kc.build(kList) 

except Exception: 

pass 

else: 

self.fail() 

 

@unittest.skipIf(not defDataDir, "Warning: afwdata is not set up") 

def testConstantWeighting(self): 

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

self.testGaussian() 

self.testGaussianWithNoise() 

 

@unittest.skipIf(not defDataDir, "Warning: afwdata is not set up") 

def testNoBackgroundFit(self): 

self.policy.set("constantVarianceWeighting", True) 

self.testGaussian() 

 

def testInsert(self): 

mi = afwImage.MaskedImageF(afwGeom.Extent2I(10, 10)) 

kc = ipDiffim.makeKernelCandidate(0., 0., mi, mi, self.policy) 

kc.setStatus(afwMath.SpatialCellCandidate.GOOD) 

 

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

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

kernelCellSet = afwMath.SpatialCellSet(afwGeom.Box2I(afwGeom.Point2I(0, 0), afwGeom.Extent2I(1, 1)), 

sizeCellX, sizeCellY) 

kernelCellSet.insertCandidate(kc) 

nSeen = 0 

for cell in kernelCellSet.getCellList(): 

for cand in cell.begin(True): 

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

nSeen += 1 

self.assertEqual(nSeen, 1) 

 

def dontTestDisp(self): 

ds9.mtv(self.scienceImage2, frame=1) 

ds9.mtv(self.templateExposure2, frame=2) 

 

def tearDown(self): 

del self.policy 

del self.table 

del self.ss 

if defDataDir: 

del self.scienceImage2 

del self.templateExposure2 

 

##### 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()