def testModelType(self):
        bbox = afwGeom.Box2I(afwGeom.Point2I(10, 10), afwGeom.Extent2I(10, 10))
        basisList = ipDiffim.makeKernelBasisList(self.subconfig)

        self.policy.set("spatialModelType", "polynomial")
        ipDiffim.BuildSpatialKernelVisitorF(basisList, bbox, self.policy)

        self.policy.set("spatialModelType", "chebyshev1")
        ipDiffim.BuildSpatialKernelVisitorF(basisList, bbox, self.policy)

        try:
            self.policy.set("spatialModelType", "foo")
            ipDiffim.BuildSpatialKernelVisitorF(basisList, bbox, self.policy)
        except Exception:
            pass
        else:
            self.fail()
    def testModelType(self):
        bbox = geom.Box2I(geom.Point2I(10, 10), geom.Extent2I(10, 10))
        basisList = ipDiffim.makeKernelBasisList(self.subconfig)

        self.ps["spatialModelType"] = "polynomial"
        ipDiffim.BuildSpatialKernelVisitorF(basisList, bbox, self.ps)

        self.ps["spatialModelType"] = "chebyshev1"
        ipDiffim.BuildSpatialKernelVisitorF(basisList, bbox, self.ps)

        try:
            self.ps["spatialModelType"] = "foo"
            ipDiffim.BuildSpatialKernelVisitorF(basisList, bbox, self.ps)
        except Exception:
            pass
        else:
            self.fail()
Esempio n. 3
0
    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 jackknifeResample(self, psfmatch, results):
        
        kernel  = results.psfMatchingKernel
        bg      = results.backgroundModel
        cellSet = results.kernelCellSet
        
        goodList = []
        for cell in cellSet.getCellList():
            print
            for cand in cell.begin(False):
                cand = ipDiffim.cast_KernelCandidateF(cand)

                if cand.getStatus() == afwMath.SpatialCellCandidate.GOOD:
                    goodList.append(cand.getId())
                else:
                    # This is so that UNKNOWNs are not processed
                    cand.setStatus(afwMath.SpatialCellCandidate.BAD)

        nStarPerCell = self.config.nStarPerCell
        policy = pexConfig.makePolicy(self.config)
        for idx in range(len(goodList)):
            cid   = goodList[idx]

            print # clear the screen
            pexLog.Trace("lsst.ip.diffim.JackknifeResampleKernel", 1,
                         "Removing candidate %d" % (cid))
            
            cand = self.setStatus(cellSet, cid, afwMath.SpatialCellCandidate.BAD)

            # From _solve
            regionBBox = cellSet.getBBox()
            spatialkv  = ipDiffim.BuildSpatialKernelVisitorF(kernel.getKernelList(), regionBBox, policy)
            cellSet.visitCandidates(spatialkv, nStarPerCell)
            spatialkv.solveLinearEquation()
            jkKernel, jkBg = spatialkv.getSolutionPair()

            #jkResults = psfmatch._solve(cellSet, kernel.getKernelList())
            #jkKernel  = jkResults[1]
            #jkBg      = jkResults[2]

            # lots of windows
            # self.assess(cand, kernel, bg, jkKernel, jkBg, 6*idx+1)

            # only 6 windows
            self.assess(cand, kernel, bg, jkKernel, jkBg, 1)

            self.setStatus(cellSet, cid, afwMath.SpatialCellCandidate.GOOD)
Esempio n. 6
0
    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)
Esempio n. 7
0
    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)
Esempio n. 8
0
    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
Esempio n. 9
0
    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
Esempio n. 10
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)