def testGaussianWithNoise(self): # Convolve a real image with a gaussian and try and recover # it. Add noise and perform the same test. gsize = self.ps["kernelSize"] gaussFunction = afwMath.GaussianFunction2D(2, 3) gaussKernel = afwMath.AnalyticKernel(gsize, gsize, gaussFunction) kImageIn = afwImage.ImageD(geom.Extent2I(gsize, gsize)) kSumIn = gaussKernel.computeImage(kImageIn, False) imX, imY = self.templateExposure2.getMaskedImage().getDimensions() smi = afwImage.MaskedImageF(geom.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.ps) 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(), 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.ps) 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(), kSumIn, 3) if not self.ps.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 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.ps) 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.ps) kList = ipDiffim.makeKernelBasisList(self.subconfig) kc.build(kList) self.verifyDeltaFunctionSolution(kc.getKernelSolution( ipDiffim.KernelCandidateF.RECENT), bg=bg)
def testDeltaFunctionScaled(self, scaling=2.7, bg=11.3): if not self.defDataDir: print >> sys.stderr, "Warning: afwdata is not set up" return sIm = afwImage.MaskedImageF(self.templateExposure2.getMaskedImage(), 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(), 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)
def testGood(self): ti = afwImage.MaskedImageF(afwGeom.Extent2I(100, 100)) ti.getVariance().set(0.1) ti.set(50, 50, (1., 0x0, 1.)) sKernel = self.makeSpatialKernel(2) si = afwImage.MaskedImageF(ti.getDimensions()) afwMath.convolve(si, ti, sKernel, True) bbox = afwGeom.Box2I(afwGeom.Point2I(25, 25), afwGeom.Point2I(75, 75)) si = afwImage.MaskedImageF(si, bbox, origin=afwImage.LOCAL) ti = afwImage.MaskedImageF(ti, bbox, origin=afwImage.LOCAL) kc = ipDiffim.KernelCandidateF(50., 50., ti, si, self.policy) sBg = afwMath.PolynomialFunction2D(1) bgCoeffs = [0., 0., 0.] sBg.setParameters(bgCoeffs) # must be initialized bskv = ipDiffim.BuildSingleKernelVisitorF(self.kList, self.policy) bskv.processCandidate(kc) self.assertEqual(kc.isInitialized(), True) #ds9.mtv(kc.getTemplateMaskedImage(), frame=1) #ds9.mtv(kc.getScienceMaskedImage(), frame=2) #ds9.mtv(kc.getKernelImage(ipDiffim.KernelCandidateF.RECENT), frame=3) #ds9.mtv(kc.getDifferenceImage(ipDiffim.KernelCandidateF.RECENT), frame=4) askv = ipDiffim.AssessSpatialKernelVisitorF(sKernel, sBg, self.policy) askv.processCandidate(kc) self.assertEqual(askv.getNProcessed(), 1) self.assertEqual(askv.getNRejected(), 0) self.assertEqual(kc.getStatus(), afwMath.SpatialCellCandidate.GOOD)
def testDeltaFunction(self): if not self.defDataDir: print >> sys.stderr, "Warning: afwdata is not set up" return # 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, e: print kMethod, e self.fail() else: pass
def testGood(self): ti = afwImage.MaskedImageF(geom.Extent2I(100, 100)) ti.getVariance().set(0.1) ti[50, 50, afwImage.LOCAL] = (1., 0x0, 1.) sKernel = self.makeSpatialKernel(2) si = afwImage.MaskedImageF(ti.getDimensions()) convolutionControl = afwMath.ConvolutionControl() convolutionControl.setDoNormalize(True) afwMath.convolve(si, ti, sKernel, convolutionControl) bbox = geom.Box2I(geom.Point2I(25, 25), geom.Point2I(75, 75)) si = afwImage.MaskedImageF(si, bbox, origin=afwImage.LOCAL) ti = afwImage.MaskedImageF(ti, bbox, origin=afwImage.LOCAL) kc = ipDiffim.KernelCandidateF(50., 50., ti, si, self.ps) sBg = afwMath.PolynomialFunction2D(1) bgCoeffs = [0., 0., 0.] sBg.setParameters(bgCoeffs) # must be initialized bskv = ipDiffim.BuildSingleKernelVisitorF(self.kList, self.ps) bskv.processCandidate(kc) self.assertEqual(kc.isInitialized(), True) askv = ipDiffim.AssessSpatialKernelVisitorF(sKernel, sBg, self.ps) askv.processCandidate(kc) self.assertEqual(askv.getNProcessed(), 1) self.assertEqual(askv.getNRejected(), 0) self.assertEqual(kc.getStatus(), afwMath.SpatialCellCandidate.GOOD)
def testSourceConstructor(self): # Original and uninitialized if not self.defDataDir: print >> sys.stderr, "Warning: afwdata is not set up" return source = self.ss.addNew() source.setId(1) source.set(self.table.getCentroidKey().getX(), 276) source.set(self.table.getCentroidKey().getY(), 717) source.set(self.table.getPsfFluxKey(), 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.getPsfFlux()) # But this should be set on construction try: kc.getCandidateRating() except Exception, e: print e self.fail()
def testGaussian(self, imsize=50): # Convolve a delta function with a known gaussian; try to # recover using delta-function basis gsize = self.ps["kernelSize"] tsize = imsize + gsize gaussFunction = afwMath.GaussianFunction2D(2, 3) gaussKernel = afwMath.AnalyticKernel(gsize, gsize, gaussFunction) kImageIn = afwImage.ImageD(geom.Extent2I(gsize, gsize)) gaussKernel.computeImage(kImageIn, False) # template image with a single hot pixel in the exact center tmi = afwImage.MaskedImageF(geom.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()) convolutionControl = afwMath.ConvolutionControl() convolutionControl.setDoNormalize(False) afwMath.convolve(smi, tmi, gaussKernel, convolutionControl) # 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.ps) 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 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.ps) 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: pass else: print(kMethod) self.fail() self.verifyDeltaFunctionSolution(kc.getKernelSolution(ipDiffim.KernelCandidateF.RECENT))
def testConstructor(self): # Original and uninitialized kc = ipDiffim.KernelCandidateF(self.x02, self.y02, self.templateExposure2.getMaskedImage(), self.scienceImage2.getMaskedImage(), self.ps) # 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: pass else: self.fail() try: kc.getImage() except Exception: pass else: self.fail()
def testSourceStats(self): source = self.ss.addNew() source.setId(1) source.set(self.table.getCentroidSlot().getMeasKey().getX(), 276) source.set(self.table.getCentroidSlot().getMeasKey().getY(), 717) source.set("slot_PsfFlux_instFlux", 1.) kc = ipDiffim.KernelCandidateF(source, self.templateExposure2.getMaskedImage(), self.scienceImage2.getMaskedImage(), self.ps) kList = ipDiffim.makeKernelBasisList(self.subconfig) kc.build(kList) self.assertEqual(kc.isInitialized(), True)
def testSourceStats(self): # Original and uninitialized if not self.defDataDir: print >> sys.stderr, "Warning: afwdata is not set up" return source = self.ss.addNew() source.setId(1) source.set(self.table.getCentroidKey().getX(), 276) source.set(self.table.getCentroidKey().getY(), 717) source.set(self.table.getPsfFluxKey(), 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)
def testBad(self): ti = afwImage.MaskedImageF(geom.Extent2I(100, 100)) ti.getVariance().set(0.1) ti[50, 50, afwImage.LOCAL] = (1., 0x0, 1.) sKernel = self.makeSpatialKernel(2) si = afwImage.MaskedImageF(ti.getDimensions()) convolutionControl = afwMath.ConvolutionControl() convolutionControl.setDoNormalize(True) afwMath.convolve(si, ti, sKernel, convolutionControl) bbox = geom.Box2I(geom.Point2I(25, 25), geom.Point2I(75, 75)) si = afwImage.MaskedImageF(si, bbox, origin=afwImage.LOCAL) ti = afwImage.MaskedImageF(ti, bbox, origin=afwImage.LOCAL) kc = ipDiffim.KernelCandidateF(50., 50., ti, si, self.ps) badGaussian = afwMath.GaussianFunction2D(1., 1., 0.) badKernel = afwMath.AnalyticKernel(self.ksize, self.ksize, badGaussian) basisList = [] basisList.append(badKernel) badSpatialKernelFunction = afwMath.PolynomialFunction2D(0) badSpatialKernel = afwMath.LinearCombinationKernel( basisList, badSpatialKernelFunction) badSpatialKernel.setSpatialParameters([[ 1, ]]) sBg = afwMath.PolynomialFunction2D(1) bgCoeffs = [10., 10., 10.] sBg.setParameters(bgCoeffs) # must be initialized bskv = ipDiffim.BuildSingleKernelVisitorF(self.kList, self.ps) bskv.processCandidate(kc) self.assertEqual(kc.isInitialized(), True) askv = ipDiffim.AssessSpatialKernelVisitorF(badSpatialKernel, sBg, self.ps) askv.processCandidate(kc) self.assertEqual(askv.getNProcessed(), 1) self.assertEqual(askv.getNRejected(), 1) self.assertEqual(kc.getStatus(), afwMath.SpatialCellCandidate.BAD)
def testConstructor(self): # Original and uninitialized if not self.defDataDir: print >> sys.stderr, "Warning: afwdata is not set up" return 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, e: print e self.fail()
def testZeroVariance(self, imsize=50): gsize = self.ps["kernelSize"] tsize = imsize + gsize tmi = afwImage.MaskedImageF(geom.Extent2I(tsize, tsize)) tmi.set(0, 0x0, 1.0) cpix = tsize // 2 tmi[cpix, cpix, afwImage.LOCAL] = (1, 0x0, 0.0) smi = afwImage.MaskedImageF(geom.Extent2I(tsize, tsize)) smi.set(0, 0x0, 1.0) smi[cpix, cpix, afwImage.LOCAL] = (1, 0x0, 0.0) kList = ipDiffim.makeKernelBasisList(self.subconfig) self.ps["constantVarianceWeighting"] = False kc = ipDiffim.KernelCandidateF(0.0, 0.0, tmi, smi, self.ps) try: kc.build(kList) except Exception: pass else: self.fail()
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.set(cpix, cpix, (1, 0x0, 0.0)) smi = afwImage.MaskedImageF(afwGeom.Extent2I(tsize, tsize)) smi.set(0, 0x0, 1.0) smi.set(cpix, cpix, (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()
def testSourceConstructor(self): source = self.ss.addNew() source.setId(1) source.set(self.table.getCentroidSlot().getMeasKey().getX(), 276) source.set(self.table.getCentroidSlot().getMeasKey().getY(), 717) source.set("slot_PsfFlux_instFlux", 1.) kc = ipDiffim.KernelCandidateF(source, self.templateExposure2.getMaskedImage(), self.scienceImage2.getMaskedImage(), self.ps) # 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: 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)