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, e:
            print e
            self.fail()
    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.ps)
            bskv = ipDiffim.BuildSingleKernelVisitorF(self.kList, self.ps,
                                                      hMat)
        else:
            bskv = ipDiffim.BuildSingleKernelVisitorF(self.kList, self.ps)

        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 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.ps)
        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.ps)
        self.assertEqual(kc4.getStatus(), afwMath.SpatialCellCandidate.UNKNOWN)

        # process with eigenKernels
        bskv2 = ipDiffim.BuildSingleKernelVisitorF(eigenKernels, self.ps)
        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)
Beispiel #4
0
    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(afwGeom.Box2I(afwGeom.Point2I(0, 0),
                                                             afwGeom.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 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 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 testFourNoVariation(self):
        self.ps['constantVarianceWeighting'] = True
        self.ps['spatialKernelOrder'] = 0
        self.ps['spatialBgOrder'] = 0

        # Place candidate footprints within the spatial grid
        for fp in self.footprints:
            bbox = fp.getBBox()

            # Grab the centers in the parent's coordinate system
            xC = int(0.5 * (bbox.getMinX() + bbox.getMaxX()))
            yC = int(0.5 * (bbox.getMinY() + bbox.getMaxY()))

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

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

            # Hotpants centroids go from -1 to 1
            if xC > 90 and yC > 90:
                cand = ipDiffim.makeKernelCandidate(
                    (xC - 0.5 * self.smi.getWidth()) /
                    (0.5 * self.smi.getWidth()),
                    (yC - 0.5 * self.smi.getHeight()) /
                    (0.5 * self.smi.getHeight()), tsmi, ssmi, self.ps)

                self.kernelCellSet.insertCandidate(cand)

        # Visitors
        bbox = self.kernelCellSet.getBBox()
        bsikv = ipDiffim.BuildSingleKernelVisitorF(self.basisList, self.ps)
        bspkv = ipDiffim.BuildSpatialKernelVisitorF(self.basisList, bbox,
                                                    self.ps)

        for cell in self.kernelCellSet.getCellList():
            for cand in cell.begin(False):  # False = include bad candidates
                bsikv.processCandidate(cand)
                bspkv.processCandidate(cand)

        HPspatialSolution = [
            0.969559, -0.000223, -0.198374, 0.000012, -0.000010, 0.000036,
            -0.000004, -0.206751, 0.000012, 0.000004, 0.452304
        ]

        bspkv.solveLinearEquation()
        sk, sb = bspkv.getSolutionPair()
        spatialSolution = sk.getKernelParameters()
        for i in range(len(spatialSolution)):
            self.assertAlmostEqual(HPspatialSolution[i] * self.parity[i],
                                   spatialSolution[i], 5)

        self.assertAlmostEqual(sb.getParameters()[0], HPspatialSolution[-1], 5)
    def runAlSpatialModel(self, sko, bgo):
        basisList = ipDiffim.makeKernelBasisList(self.subconfig)
        self.policy.set('spatialKernelOrder', sko)
        self.policy.set('spatialBgOrder', bgo)
        self.policy.set('fitForBackground', True)

        bbox = afwGeom.Box2I(afwGeom.Point2I(0, 0),
                             afwGeom.Extent2I(self.size * 10, self.size * 10))

        bsikv = ipDiffim.BuildSingleKernelVisitorF(basisList, self.policy)
        bspkv = ipDiffim.BuildSpatialKernelVisitorF(basisList, bbox,
                                                    self.policy)

        for x in range(1, self.size, 10):
            for y in range(1, self.size, 10):
                cand = self.makeCandidate(1.0, x, y)
                bsikv.processCandidate(cand)
                bspkv.processCandidate(cand)

        bspkv.solveLinearEquation()
        sk, sb = bspkv.getSolutionPair()

        # Kernel
        if sko == 0:
            # Specialization for speedup
            spatialKernelSolution = sk.getKernelParameters()

            # One term for each basis function
            self.assertEqual(len(spatialKernelSolution), len(basisList))

        else:
            spatialKernelSolution = sk.getSpatialParameters()

            nSpatialTerms = int(0.5 * (sko + 1) * (sko + 2))
            # One model for each basis function
            self.assertEqual(len(spatialKernelSolution), len(basisList))
            # First basis has no spatial variation
            for i in range(1, nSpatialTerms):
                self.assertEqual(spatialKernelSolution[0][i], 0.)
            # All bases have correct number of terms
            for i in range(len(spatialKernelSolution)):
                self.assertEqual(len(spatialKernelSolution[i]), nSpatialTerms)

        # Background
        spatialBgSolution = sb.getParameters()
        nBgTerms = int(0.5 * (bgo + 1) * (bgo + 2))
        self.assertEqual(len(spatialBgSolution), nBgTerms)
    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)
Beispiel #10
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)
        if switch == 'A':
            # Default Alard Lupton
            config = subconfigAL
        elif switch == 'B':
            # Add more AL bases (typically 4 3 2)
            config = subconfigAL
            config.alardDegGauss = (8, 6, 4)
        elif switch == 'C':
            # Delta function
            config = subconfigDF
            config.useRegularization = False

        kList = ipDiffim.makeKernelBasisList(config)

        policy = pexConfig.makePolicy(config)
        bskv = ipDiffim.BuildSingleKernelVisitorF(kList, policy)

        # TEST 1
        tmi, smi = makeTest1(doAddNoise)
        kc = ipDiffim.makeKernelCandidate(0.0, 0.0, tmi, smi, policy)
        bskv.processCandidate(kc)

        kernel = kc.getKernel(ipDiffim.KernelCandidateF.ORIG)
        kimage = afwImage.ImageD(kernel.getDimensions())
        kernel.computeImage(kimage, False)
        diffim = kc.getDifferenceImage(ipDiffim.KernelCandidateF.ORIG)

        ds9.mtv(tmi, frame=fnum)
        fnum += 1
        ds9.mtv(smi, frame=fnum)
        fnum += 1
Beispiel #12
0
    def setUp(self, CFHT=True):
        lambdaValue = 1.0

        self.config1 = ipDiffim.ImagePsfMatchTask.ConfigClass()
        self.config1.kernel.name = "DF"
        self.subconfig1 = self.config1.kernel.active

        self.config2 = ipDiffim.ImagePsfMatchTask.ConfigClass()
        self.config2.kernel.name = "DF"
        self.subconfig2 = self.config2.kernel.active

        self.config3 = ipDiffim.ImagePsfMatchTask.ConfigClass()
        self.config3.kernel.name = "DF"
        self.subconfig3 = self.config3.kernel.active

        self.config4 = ipDiffim.ImagePsfMatchTask.ConfigClass()
        self.config4.kernel.name = "DF"
        self.subconfig4 = self.config4.kernel.active

        self.subconfig1.useRegularization = False

        self.subconfig2.useRegularization = True
        self.subconfig2.lambdaType = "absolute"
        self.subconfig2.lambdaValue = lambdaValue
        self.subconfig2.regularizationType = "centralDifference"
        self.subconfig2.centralRegularizationStencil = 5

        self.subconfig3.useRegularization = True
        self.subconfig3.lambdaType = "absolute"
        self.subconfig3.lambdaValue = lambdaValue
        self.subconfig3.regularizationType = "centralDifference"
        self.subconfig3.centralRegularizationStencil = 9

        self.subconfig4.useRegularization = True
        self.subconfig4.lambdaType = "absolute"
        self.subconfig4.lambdaValue = lambdaValue
        self.subconfig4.regularizationType = "forwardDifference"
        self.subconfig4.forwardRegularizationOrders = [1, 2]

        self.kList1 = ipDiffim.makeKernelBasisList(self.subconfig1)
        self.bskv1 = ipDiffim.BuildSingleKernelVisitorF(
            self.kList1, pexConfig.makePropertySet(self.subconfig1))

        self.kList2 = ipDiffim.makeKernelBasisList(self.subconfig2)
        self.hMat2 = ipDiffim.makeRegularizationMatrix(
            pexConfig.makePropertySet(self.subconfig2))
        self.bskv2 = ipDiffim.BuildSingleKernelVisitorF(
            self.kList2, pexConfig.makePropertySet(self.subconfig2),
            self.hMat2)

        self.kList3 = ipDiffim.makeKernelBasisList(self.subconfig3)
        self.hMat3 = ipDiffim.makeRegularizationMatrix(
            pexConfig.makePropertySet(self.subconfig3))
        self.bskv3 = ipDiffim.BuildSingleKernelVisitorF(
            self.kList3, pexConfig.makePropertySet(self.subconfig3),
            self.hMat3)

        self.kList4 = ipDiffim.makeKernelBasisList(self.subconfig4)
        self.hMat4 = ipDiffim.makeRegularizationMatrix(
            pexConfig.makePropertySet(self.subconfig4))
        self.bskv4 = ipDiffim.BuildSingleKernelVisitorF(
            self.kList4, pexConfig.makePropertySet(self.subconfig4),
            self.hMat4)

        # known input images
        defDataDir = lsst.utils.getPackageDir('afwdata')
        if CFHT:
            defSciencePath = os.path.join(defDataDir, 'CFHT', 'D4',
                                          CFHTTORUN + '.fits')
            defTemplatePath = os.path.join(defDataDir, 'CFHT', 'D4',
                                           CFHTTORUN + '_tmpl.fits')

            # no need to remap
            self.scienceExposure = afwImage.ExposureF(defSciencePath)
            self.templateExposure = afwImage.ExposureF(defTemplatePath)
        else:
            defSciencePath = os.path.join(defDataDir, "DC3a-Sim", "sci",
                                          "v26-e0", "v26-e0-c011-a00.sci")
            defTemplatePath = os.path.join(defDataDir, "DC3a-Sim", "sci",
                                           "v5-e0", "v5-e0-c011-a00.sci")

            self.scienceExposure = afwImage.ExposureF(defSciencePath)
            self.templateExposure = afwImage.ExposureF(defTemplatePath)
            warper = afwMath.Warper.fromConfig(self.subconfig1.warpingConfig)
            self.templateExposure = warper.warpExposure(
                self.scienceExposure.getWcs(),
                self.templateExposure,
                destBBox=self.scienceExposure.getBBox())

        diffimTools.backgroundSubtract(self.subconfig1.afwBackgroundConfig, [
            self.scienceExposure.getMaskedImage(),
            self.templateExposure.getMaskedImage()
        ])

        #
        tmi = self.templateExposure.getMaskedImage()
        smi = self.scienceExposure.getMaskedImage()

        detConfig = self.subconfig1.detectionConfig
        detps = pexConfig.makePropertySet(detConfig)
        detps["detThreshold"] = 50.
        detps["detOnTemplate"] = False
        kcDetect = ipDiffim.KernelCandidateDetectionF(detps)
        kcDetect.apply(tmi, smi)
        self.footprints = kcDetect.getFootprints()
    def testFourBgVariation(self):

        # OK, so these can end up a bit different due to how HP and
        # LSST represent the background in the matrix math.  HP has
        # each pixel have its own coordinate (which goes from -1 to 1
        # across the entire image, by the way), whereas we give all
        # the LSST pixels within a stamp the same coordinate.

        # To make this comparison, I go ahead and edit the Hotpants
        # code to give each pixel the same weight.  For reference this
        # is in fillStamp() and I replace:
        #
        #        //xf = (i - rPixX2) / rPixX2;
        #        xf = (xi - rPixX2) / rPixX2;
        #
        #            //yf = (j - rPixY2) / rPixY2;
        #            yf = (yi - rPixY2) / rPixY2;

        self.ps['constantVarianceWeighting'] = True
        self.ps['spatialKernelOrder'] = 0
        self.ps['spatialBgOrder'] = 1

        # Place candidate footprints within the spatial grid
        for fp in self.footprints:
            bbox = fp.getBBox()

            # Grab the centers in the parent's coordinate system
            xC = int(0.5 * (bbox.getMinX() + bbox.getMaxX()))
            yC = int(0.5 * (bbox.getMinY() + bbox.getMaxY()))

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

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

            # Hotpants centroids go from -1 to 1
            if xC > 90 and yC > 90:
                cand = ipDiffim.makeKernelCandidate(
                    (xC - 0.5 * self.smi.getWidth()) /
                    (0.5 * self.smi.getWidth()),
                    (yC - 0.5 * self.smi.getHeight()) /
                    (0.5 * self.smi.getHeight()), tsmi, ssmi, self.ps)
                # print 'OBJECT', cand.getId(), 'AT', xC, yC, cand.getXCenter(), cand.getYCenter()
                self.kernelCellSet.insertCandidate(cand)

        # Visitors
        bbox = self.kernelCellSet.getBBox()
        bsikv = ipDiffim.BuildSingleKernelVisitorF(self.basisList, self.ps)
        bspkv = ipDiffim.BuildSpatialKernelVisitorF(self.basisList, bbox,
                                                    self.ps)

        for cell in self.kernelCellSet.getCellList():
            for cand in cell.begin(False):  # False = include bad candidates
                bsikv.processCandidate(cand)
                bspkv.processCandidate(cand)

        HPspatialSolution = [
            0.969559, -0.000223, -0.198374, 0.000012, -0.000010, 0.000036,
            -0.000004, -0.206751, 0.000012, 0.000004,
            [0.782113, -0.910963, -0.106636]
        ]

        bspkv.solveLinearEquation()
        sk, sb = bspkv.getSolutionPair()
        spatialSolution = sk.getKernelParameters()
        for i in range(len(spatialSolution)):
            self.assertAlmostEqual(HPspatialSolution[i] * self.parity[i],
                                   spatialSolution[i], 5)

        self.assertAlmostEqual(sb.getParameters()[0], HPspatialSolution[-1][0],
                               5)
        self.assertAlmostEqual(sb.getParameters()[1], HPspatialSolution[-1][2],
                               5)  # x<->y
        self.assertAlmostEqual(sb.getParameters()[2], HPspatialSolution[-1][1],
                               5)  # x<->y
    def setUp(self):
        self.configAL = ipDiffim.ImagePsfMatchTask.ConfigClass()
        self.configAL.kernel.name = "AL"
        self.subconfigAL = self.configAL.kernel.active

        self.configDF = ipDiffim.ImagePsfMatchTask.ConfigClass()
        self.configDF.kernel.name = "DF"
        self.subconfigDF = self.configDF.kernel.active

        self.configDFr = ipDiffim.ImagePsfMatchTask.ConfigClass()
        self.configDFr.kernel.name = "DF"
        self.subconfigDFr = self.configDFr.kernel.active

        self.subconfigDF.useRegularization = False
        self.subconfigDFr.useRegularization = True
        self.subconfigDFr.lambdaValue = 1000.0

        self.subconfigAL.fitForBackground = fitForBackground
        self.subconfigDF.fitForBackground = fitForBackground
        self.subconfigDFr.fitForBackground = fitForBackground

        self.subconfigAL.constantVarianceWeighting = constantVarianceWeighting
        self.subconfigDF.constantVarianceWeighting = constantVarianceWeighting
        self.subconfigDFr.constantVarianceWeighting = constantVarianceWeighting

        self.kListAL = ipDiffim.makeKernelBasisList(self.subconfigAL)
        self.kListDF = ipDiffim.makeKernelBasisList(self.subconfigDF)
        self.kListDFr = ipDiffim.makeKernelBasisList(self.subconfigDFr)
        self.hMatDFr = ipDiffim.makeRegularizationMatrix(
            pexConfig.makePropertySet(self.subconfigDFr))

        self.bskvAL = ipDiffim.BuildSingleKernelVisitorF(
            self.kListAL, pexConfig.makePropertySet(self.subconfigAL))
        self.bskvDF = ipDiffim.BuildSingleKernelVisitorF(
            self.kListDF, pexConfig.makePropertySet(self.subconfigDF))
        self.bskvDFr = ipDiffim.BuildSingleKernelVisitorF(
            self.kListDFr, pexConfig.makePropertySet(self.subconfigDF),
            self.hMatDFr)

        defSciencePath = globals()['defSciencePath']
        defTemplatePath = globals()['defTemplatePath']
        if defSciencePath and defTemplatePath:
            self.scienceExposure = afwImage.ExposureF(defSciencePath)
            self.templateExposure = afwImage.ExposureF(defTemplatePath)
        else:
            defDataDir = lsst.utils.getPackageDir('afwdata')
            defSciencePath = os.path.join(defDataDir, "DC3a-Sim", "sci",
                                          "v26-e0", "v26-e0-c011-a00.sci.fits")
            defTemplatePath = os.path.join(defDataDir, "DC3a-Sim", "sci",
                                           "v5-e0", "v5-e0-c011-a00.sci.fits")

            self.scienceExposure = afwImage.ExposureF(defSciencePath)
            self.templateExposure = afwImage.ExposureF(defTemplatePath)
            warper = afwMath.Warper.fromConfig(self.subconfigAL.warpingConfig)
            self.templateExposure = warper.warpExposure(
                self.scienceExposure.getWcs(),
                self.templateExposure,
                destBBox=self.scienceExposure.getBBox())

        #
        tmi = self.templateExposure.getMaskedImage()
        smi = self.scienceExposure.getMaskedImage()

        # Object detection
        detConfig = self.subconfigAL.detectionConfig
        detps = pexConfig.makePropertySet(detConfig)
        detps["detThreshold"] = 50.
        detps["detThresholdType"] = "stdev"
        detps["detOnTemplate"] = False
        kcDetect = ipDiffim.KernelCandidateDetectionF(detps)
        kcDetect.apply(tmi, smi)
        self.footprints = kcDetect.getFootprints()
    def testFourVariation(self):
        self.ps['constantVarianceWeighting'] = True
        self.ps['spatialKernelOrder'] = 1
        self.ps['spatialBgOrder'] = 1

        # Place candidate footprints within the spatial grid
        for fp in self.footprints:
            bbox = fp.getBBox()

            # Grab the centers in the parent's coordinate system
            xC = int(0.5 * (bbox.getMinX() + bbox.getMaxX()))
            yC = int(0.5 * (bbox.getMinY() + bbox.getMaxY()))

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

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

            # Hotpants centroids go from -1 to 1
            if xC > 90 and yC > 90:
                cand = ipDiffim.makeKernelCandidate(
                    (xC - 0.5 * self.smi.getWidth()) /
                    (0.5 * self.smi.getWidth()),
                    (yC - 0.5 * self.smi.getHeight()) /
                    (0.5 * self.smi.getHeight()), tsmi, ssmi, self.ps)
                self.kernelCellSet.insertCandidate(cand)

        # Visitors
        bbox = self.kernelCellSet.getBBox()
        bsikv = ipDiffim.BuildSingleKernelVisitorF(self.basisList, self.ps)
        bspkv = ipDiffim.BuildSpatialKernelVisitorF(self.basisList, bbox,
                                                    self.ps)

        for cell in self.kernelCellSet.getCellList():
            for cand in cell.begin(False):  # False = include bad candidates
                bsikv.processCandidate(cand)
                bspkv.processCandidate(cand)

        HPspatialSolution = [[0.969559, 0., 0.],
                             [-0.000082, -0.000620, 0.000185],
                             [-0.197749, 0.001418, -0.003321],
                             [0.000002, 0.000049, -0.000016],
                             [0.000211, -0.000283, -0.000397],
                             [0.000034, 0.000002, 0.000006],
                             [-0.000013, 0.000041, -0.000010],
                             [-0.220238, 0.028395, 0.013148],
                             [0.000019, -0.000025, 0.000003],
                             [0.000003, 0.000000, 0.000005],
                             [0.782113, -0.910963, -0.106636]]

        bspkv.solveLinearEquation()
        sk, sb = bspkv.getSolutionPair()
        spatialSolution = sk.getSpatialParameters()

        for i in range(len(spatialSolution)):
            # HP and LSST switch the order x<->y
            self.assertAlmostEqual(HPspatialSolution[i][0] * self.parity[i],
                                   spatialSolution[i][0], 5)
            self.assertAlmostEqual(HPspatialSolution[i][1] * self.parity[i],
                                   spatialSolution[i][2], 5)
            self.assertAlmostEqual(HPspatialSolution[i][2] * self.parity[i],
                                   spatialSolution[i][1], 5)

        self.assertAlmostEqual(sb.getParameters()[0], HPspatialSolution[-1][0],
                               5)
        self.assertAlmostEqual(sb.getParameters()[1], HPspatialSolution[-1][2],
                               5)  # x<->y
        self.assertAlmostEqual(sb.getParameters()[2], HPspatialSolution[-1][1],
                               5)  # x<->y
    def testAllBgVariation2(self):
        # OK, I ran HP on all the things in this image.  Enough for
        # second order spatial variation

        self.ps['constantVarianceWeighting'] = True
        self.ps['spatialKernelOrder'] = 0
        self.ps['spatialBgOrder'] = 2

        # Ignore the whole kernelCellSet thing
        cands = []
        for fp in self.footprints:
            bbox = fp.getBBox()

            # Grab the centers in the parent's coordinate system
            xC = int(0.5 * (bbox.getMinX() + bbox.getMaxX()))
            yC = int(0.5 * (bbox.getMinY() + bbox.getMaxY()))

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

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

            # Hotpants centroids go from -1 to 1
            cand = ipDiffim.makeKernelCandidate(
                (xC - 0.5 * self.smi.getWidth()) / (0.5 * self.smi.getWidth()),
                (yC - 0.5 * self.smi.getHeight()) /
                (0.5 * self.smi.getHeight()), tsmi, ssmi, self.ps)
            cands.append(cand)

        # Visitors
        bbox = self.kernelCellSet.getBBox()
        bsikv = ipDiffim.BuildSingleKernelVisitorF(self.basisList, self.ps)
        bspkv = ipDiffim.BuildSpatialKernelVisitorF(self.basisList, bbox,
                                                    self.ps)

        for cand in cands:
            bsikv.processCandidate(cand)
            bspkv.processCandidate(cand)

        HPspatialSolution = [
            0.968505, -0.000053, -0.206505, 0.000005, 0.000062, 0.000028,
            -0.000004, -0.206135, 0.000005, 0.000001,
            [0.812488, 0.096456, -1.140900, 0.132670, -0.571923, -0.284670]
        ]

        bspkv.solveLinearEquation()
        sk, sb = bspkv.getSolutionPair()
        spatialSolution = sk.getKernelParameters()

        # Kernel
        for i in range(len(spatialSolution)):
            self.assertAlmostEqual(HPspatialSolution[i] * self.parity[i],
                                   spatialSolution[i], 5)

        # Bg
        # Ordering of second order terms is just messy
        spReorder = [0, 3, 1, 5, 4, 2]
        spatialSolution = sb.getParameters()
        for i in range(len(spatialSolution)):
            self.assertAlmostEqual(HPspatialSolution[-1][spReorder[i]],
                                   spatialSolution[i], 5)
    def testAllVariation2(self):
        # OK, I ran HP on all the things in this image.  Enough for
        # second order spatial variation

        self.ps['constantVarianceWeighting'] = True
        self.ps['spatialKernelOrder'] = 2
        self.ps['spatialBgOrder'] = 2

        # Ignore the whole kernelCellSet thing
        cands = []
        for fp in self.footprints:
            bbox = fp.getBBox()

            # Grab the centers in the parent's coordinate system
            xC = int(0.5 * (bbox.getMinX() + bbox.getMaxX()))
            yC = int(0.5 * (bbox.getMinY() + bbox.getMaxY()))

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

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

            # Hotpants centroids go from -1 to 1
            cand = ipDiffim.makeKernelCandidate(
                (xC - 0.5 * self.smi.getWidth()) / (0.5 * self.smi.getWidth()),
                (yC - 0.5 * self.smi.getHeight()) /
                (0.5 * self.smi.getHeight()), tsmi, ssmi, self.ps)
            cands.append(cand)

        # Visitors
        bbox = self.kernelCellSet.getBBox()
        bsikv = ipDiffim.BuildSingleKernelVisitorF(self.basisList, self.ps)
        bspkv = ipDiffim.BuildSpatialKernelVisitorF(self.basisList, bbox,
                                                    self.ps)

        for cand in cands:
            bsikv.processCandidate(cand)
            bspkv.processCandidate(cand)

        HPspatialSolution = [
            [0.968505, 0., 0., 0., 0., 0.],
            [-0.000094, -0.000206, -0.000027, 0.000025, -0.000705, 0.000162],
            [-0.188375, 0.001801, -0.027534, 0.008718, 0.016346, -0.033879],
            [0.000004, 0.000012, 0.000023, 0.000004, 0.000017, -0.000020],
            [0.000128, -0.000218, 0.000304, -0.000038, -0.000151, -0.000531],
            [-0.000011, -0.000013, 0.000038, -0.000017, 0.000133, 0.000093],
            [-0.000003, 0.000003, 0.000006, 0.000008, 0.000002, -0.000010],
            [-0.212235, -0.000856, 0.012246, -0.010893, 0.049302, 0.008249],
            [0.000014, -0.000002, -0.000050, -0.000001, 0.000030, 0.000020],
            [-0.000001, 0.000010, -0.000012, -0.000007, 0.000015, 0.000019],
            [0.812488, 0.096456, -1.140900, 0.132670, -0.571923, -0.284670]
        ]

        bspkv.solveLinearEquation()
        sk, sb = bspkv.getSolutionPair()
        spatialSolution = sk.getSpatialParameters()

        # Kernel
        spReorder = [0, 3, 1, 5, 4, 2]
        for i in range(len(spatialSolution)):
            for j in range(len(spReorder)):
                self.assertAlmostEqual(
                    HPspatialSolution[i][spReorder[j]] * self.parity[i],
                    spatialSolution[i][j], 5)
        # Bg
        spatialSolution = sb.getParameters()
        for i in range(len(spatialSolution)):
            self.assertAlmostEqual(HPspatialSolution[-1][spReorder[i]],
                                   spatialSolution[i], 5)
Beispiel #18
0
    def testSingleNoVariation(self):
        self.policy.set('constantVarianceWeighting', True)
        self.policy.set('spatialKernelOrder', 0)
        self.policy.set('spatialBgOrder', 0)

        # Place candidate footprints within the spatial grid
        for fp in self.footprints:
            bbox = fp.getBBox()

            # Grab the centers in the parent's coordinate system
            xC = int(0.5 * (bbox.getMinX() + bbox.getMaxX()))
            yC = int(0.5 * (bbox.getMinY() + bbox.getMaxY()))

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

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

            # Hotpants centroids go from -1 to 1
            # Only one passes
            if xC > 150 and yC > 150:
                cand = ipDiffim.makeKernelCandidate(
                    (xC - 0.5 * self.smi.getWidth()) /
                    (0.5 * self.smi.getWidth()),
                    (yC - 0.5 * self.smi.getHeight()) /
                    (0.5 * self.smi.getHeight()), tsmi, ssmi, self.policy)
                self.kernelCellSet.insertCandidate(cand)

        # Visitors
        bbox = self.kernelCellSet.getBBox()
        bsikv = ipDiffim.BuildSingleKernelVisitorF(self.basisList, self.policy)
        bspkv = ipDiffim.BuildSpatialKernelVisitorF(self.basisList, bbox,
                                                    self.policy)

        for cell in self.kernelCellSet.getCellList():
            for cand in cell.begin(False):  # False = include bad candidates
                bsikv.processCandidate(cand)
                bspkv.processCandidate(cand)

        HPsingleSolution = [
            0.959086, -0.000344, -0.197758, 0.000019, -0.000172, 0.000053,
            0.000018, -0.192776, 0.000000, 0.000001, 0.602642
        ]

        HPspatialSolution = HPsingleSolution

        singleSolution = cand.getKernel(
            ipDiffim.KernelCandidateF.RECENT).getKernelParameters()
        for i in range(len(singleSolution)):
            self.assertAlmostEqual(HPsingleSolution[i] * self.parity[i],
                                   singleSolution[i], 5)

        bspkv.solveLinearEquation()
        sk, sb = bspkv.getSolutionPair()
        spatialSolution = sk.getKernelParameters()
        for i in range(len(spatialSolution)):
            self.assertAlmostEqual(HPspatialSolution[i] * self.parity[i],
                                   spatialSolution[i], 6)

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