def testFourNoVariation(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, afwImage.LOCAL)
            ssmi  = afwImage.MaskedImageF(self.smi, bbox, 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.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
                cand  = ipDiffim.cast_KernelCandidateF(cand)
                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 makeCandidate(self, kSum, x, y, size=51):
     mi1 = afwImage.MaskedImageF(geom.Extent2I(size, size))
     mi1.getVariance().set(1.0)  # avoid NaNs
     mi1[size // 2, size // 2, afwImage.LOCAL] = (1, 0x0, 1)
     mi2 = afwImage.MaskedImageF(geom.Extent2I(size, size))
     mi2.getVariance().set(1.0)  # avoid NaNs
     mi2[size // 2, size // 2, afwImage.LOCAL] = (kSum, 0x0, kSum)
     kc = ipDiffim.makeKernelCandidate(x, y, mi1, mi2, self.ps)
     return kc
Beispiel #3
0
 def makeCandidate(self, kSum, x, y):
     mi1 = afwImage.MaskedImageF(afwGeom.Extent2I(self.size, self.size))
     mi1.getVariance().set(1.0) # avoid NaNs
     mi1.set(self.size//2, self.size//2, (1, 0x0, 1))
     mi2 = afwImage.MaskedImageF(afwGeom.Extent2I(self.size, self.size))
     mi2.getVariance().set(1.0) # avoid NaNs
     mi2.set(self.size//2, self.size//2, (kSum, 0x0, kSum))
     kc = ipDiffim.makeKernelCandidate(x, y, mi1, mi2, self.policy)
     return kc
    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 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)
    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.policy)
        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(afwGeom.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(afwGeom.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.policy)
        self.assertEqual(kc4.getStatus(), afwMath.SpatialCellCandidate.UNKNOWN)

        # process with eigenKernels
        bskv2 = ipDiffim.BuildSingleKernelVisitorF(eigenKernels, self.policy)
        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)
    def testInsert(self):
        mi = afwImage.MaskedImageF(geom.Extent2I(10, 10))
        kc = ipDiffim.makeKernelCandidate(0., 0., mi, mi, self.ps)
        kc.setStatus(afwMath.SpatialCellCandidate.GOOD)

        sizeCellX = self.ps["sizeCellX"]
        sizeCellY = self.ps["sizeCellY"]
        kernelCellSet = afwMath.SpatialCellSet(geom.Box2I(geom.Point2I(0, 0), geom.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 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 apply(self, policy, visitor, xloc, yloc, tmi, smi):
        kc = ipDiffim.makeKernelCandidate(xloc, yloc, tmi, smi, policy)
        visitor.processCandidate(kc)
        kim = kc.getKernelImage(ipDiffim.KernelCandidateF.RECENT)
        diffIm = kc.getDifferenceImage(ipDiffim.KernelCandidateF.RECENT)
        kSum = kc.getKsum(ipDiffim.KernelCandidateF.RECENT)
        bg = kc.getBackground(ipDiffim.KernelCandidateF.RECENT)

        bbox = kc.getKernel(ipDiffim.KernelCandidateF.RECENT).shrinkBBox(diffIm.getBBox(afwImage.LOCAL))
        diffIm = afwImage.MaskedImageF(diffIm, bbox, origin=afwImage.LOCAL)
        self.dStats.apply(diffIm)

        dmean = afwMath.makeStatistics(diffIm.getImage(), afwMath.MEAN).getValue()
        dstd = afwMath.makeStatistics(diffIm.getImage(), afwMath.STDEV).getValue()
        vmean = afwMath.makeStatistics(diffIm.getVariance(), afwMath.MEAN).getValue()
        return kSum, bg, dmean, dstd, vmean, kim, diffIm, kc
    def apply(self, policy, visitor, xloc, yloc, tmi, smi):
        kc     = ipDiffim.makeKernelCandidate(xloc, yloc, tmi, smi, policy)
        visitor.processCandidate(kc)
        kim    = kc.getKernelImage(ipDiffim.KernelCandidateF.RECENT)
        diffIm = kc.getDifferenceImage(ipDiffim.KernelCandidateF.RECENT)
        kSum   = kc.getKsum(ipDiffim.KernelCandidateF.RECENT)
        bg     = kc.getBackground(ipDiffim.KernelCandidateF.RECENT)

        bbox = kc.getKernel(ipDiffim.KernelCandidateF.RECENT).shrinkBBox(diffIm.getBBox(afwImage.LOCAL))
        diffIm = afwImage.MaskedImageF(diffIm, bbox, afwImage.LOCAL)
        self.dStats.apply(diffIm)
        
        dmean = afwMath.makeStatistics(diffIm.getImage(),    afwMath.MEAN).getValue()
        dstd  = afwMath.makeStatistics(diffIm.getImage(),    afwMath.STDEV).getValue()
        vmean = afwMath.makeStatistics(diffIm.getVariance(), afwMath.MEAN).getValue()
        return kSum, bg, dmean, dstd, vmean, kim, diffIm, kc
Beispiel #11
0
    def stamps(self, exp1, exp2):
        """Find suitable stamps

        @param[in] exp1 First exposure of interest
        @param[in] exp2 Second exposure of interest
        @output Cell set
        """
        policy = self.config['diff'].getPolicy()

        # XXX The following was cut from lsst.ip.diffim.createPsfMatchingKernel, since that does the stamp
        # identification and kernel solution within the same function, while one might imagine overriding one
        # of these with some other method.

        # Object to store the KernelCandidates for spatial modeling
        kernelCellSet = afwMath.SpatialCellSet(exp1.getBBox(afwImage.PARENT),
                                               policy.getInt("sizeCellX"),
                                               policy.getInt("sizeCellY"))

        # Candidate source footprints to use for Psf matching
        footprints = diffim.getCollectionOfFootprintsForPsfMatching(
            exp2, exp1, policy)

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

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

            # Since the footprint is in the parent's coordinate system,
            # while the BBox uses the child's coordinate system.
            bbox.shift(-afwGeom.Extent2I(exp2.getXY0()))

            tmi = afwImage.MaskedImageF(exp2, bbox)
            smi = afwImage.MaskedImageF(exp1, bbox)

            cand = diffim.makeKernelCandidate(xC, yC, tmi, smi)
            kernelCellSet.insertCandidate(cand)

        return kernelCellSet
    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)
        elif switch == 'B':
            # AL with ~320 bases
            config = subconfigAL
            config.alardDegGauss = (15, 10, 5)
        elif switch == 'C':
            config = subconfigDF
            config.useRegularization = False

        kList = ipDiffim.makeKernelBasisList(config)

        ps = pexConfig.makePropertySet(config)
        bskv = ipDiffim.BuildSingleKernelVisitorF(kList, ps)

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

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

        afwDisplay.Display(frame=fnum).mtv(tmi)
        fnum += 1
        afwDisplay.Display(frame=fnum).mtv(smi)
        fnum += 1
        afwDisplay.Display(frame=fnum).mtv(kimage)
        fnum += 1
        afwDisplay.Display(frame=fnum).mtv(diffim)
        fnum += 1
    def testAllVariation2(self):
        # OK, I ran HP on all the things in this image.  Enough for
        # second order spatial variation

        self.policy.set('constantVarianceWeighting', True)
        self.policy.set('spatialKernelOrder', 2)
        self.policy.set('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 = 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
            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)
            cands.append(cand)

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

        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)
    def testAllBgVariation2(self):
        # OK, I ran HP on all the things in this image.  Enough for
        # second order spatial variation

        self.policy.set('constantVarianceWeighting', True)
        self.policy.set('spatialKernelOrder', 0)
        self.policy.set('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 = 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
            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)
            cands.append(cand)

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

        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 testFourVariation(self):
        self.policy.set('constantVarianceWeighting', True)
        self.policy.set('spatialKernelOrder', 1)
        self.policy.set('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 = 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
            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.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)

        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 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.policy.set('constantVarianceWeighting', True)
        self.policy.set('spatialKernelOrder', 0)
        self.policy.set('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 = 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
            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.policy)
                # 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.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)

        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
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)
    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
        elif switch == 'B':
            # AL with ~320 bases
            config = subconfigAL
            config.alardDegGauss = (15, 10, 5)
        elif switch == 'C':
            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)

        afwDisplay.Display(frame=fnum).mtv(tmi)
        fnum += 1
        afwDisplay.Display(frame=fnum).mtv(smi)
        fnum += 1
        afwDisplay.Display(frame=fnum).mtv(kimage)
        fnum += 1
        afwDisplay.Display(frame=fnum).mtv(diffim)
        fnum += 1
    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
            # 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
        ds9.mtv(kimage, frame=fnum)
        fnum += 1
        ds9.mtv(diffim, frame=fnum)
        fnum += 1
    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)