Exemplo n.º 1
0
    def testSimReverseZogy(self):
        refPsf = 2.
        sciPsfBase = 2.
        sciNoise = 5.
        refNoise = 1.5
        seed = 18
        fluxLevel = 500
        rng = np.random.RandomState(seed)
        zogyConfig = ZogyConfig()

        for s in range(self.nRandIter):
            sciPsf = sciPsfBase + rng.random() * 2.
            ref, _ = self.makeTestImages(seed=seed + s,
                                         nSrc=20,
                                         psfSize=refPsf,
                                         noiseLevel=refNoise,
                                         fluxLevel=fluxLevel)
            sci, src = self.makeTestImages(seed=seed + s,
                                           nSrc=20,
                                           psfSize=sciPsf,
                                           noiseLevel=sciNoise,
                                           fluxLevel=fluxLevel)

            res = self.wrapZogyDiffim(zogyConfig, ref, sci)
            resR = self.wrapZogyDiffim(zogyConfig, sci, ref)
            metric = self.diffimMetricBasic(res, src, sigma=3)
            metricR = self.diffimMetricBasic(resR, src, sigma=3)
            self.assertFloatsAlmostEqual(metric, -metricR)
Exemplo n.º 2
0
    def _testZogyDiffimMapReduced(self, inImageSpace=False, doScorr=False, **kwargs):
        """Test running Zogy using ImageMapReduceTask framework.

        Compare map-reduced version with non-map-reduced version.
        Do it for pure Fourier-based calc. and also for real-space.
        Also for computing pure diffim D and corrected likelihood image Scorr.
        """
        config = ZogyMapReduceConfig()
        config.gridStepX = config.gridStepY = 9
        config.borderSizeX = config.borderSizeY = 3
        if inImageSpace:
            config.gridStepX = config.gridStepY = 8
            config.borderSizeX = config.borderSizeY = 6  # need larger border size for image-space run
        config.reducer.reduceOperation = 'average'
        task = ImageMapReduceTask(config=config)
        D_mapReduced = task.run(self.im1ex, template=self.im2ex, inImageSpace=inImageSpace,
                                doScorr=doScorr, forceEvenSized=False, **kwargs).exposure

        config = ZogyConfig()
        task = ZogyTask(templateExposure=self.im2ex, scienceExposure=self.im1ex, config=config)
        if not doScorr:
            D = task.computeDiffim(inImageSpace=inImageSpace, **kwargs).D
        else:
            D = task.computeScorr(inImageSpace=inImageSpace, **kwargs).S

        self._compareExposures(D_mapReduced, D, tol=0.04, Scorr=doScorr)
Exemplo n.º 3
0
    def testSimZogySciRefNotModified(self):
        "Image differencing should not modify the original images."
        refPsf = 2.
        sciPsfBase = 2.
        sciNoise = 5.
        refNoise = 1.5
        seed = 37
        fluxLevel = 500
        rng = np.random.RandomState(seed)
        zogyConfig = ZogyConfig()

        sciPsf = sciPsfBase + rng.random() * 2.
        refOriginal, _ = self.makeTestImages(seed=seed,
                                             nSrc=20,
                                             psfSize=refPsf,
                                             noiseLevel=refNoise,
                                             fluxLevel=fluxLevel)
        sciOriginal, src = self.makeTestImages(seed=seed,
                                               nSrc=20,
                                               psfSize=sciPsf,
                                               noiseLevel=sciNoise,
                                               fluxLevel=fluxLevel)
        # Make a deep copy of the images first
        sciTest1 = sciOriginal.clone()
        refTest1 = refOriginal.clone()

        # Basic ZOGY, but we don't care about the result.
        self.wrapZogyDiffim(zogyConfig, refTest1, sciTest1)
        self.assertMaskedImagesEqual(refOriginal.maskedImage,
                                     refTest1.maskedImage)
        self.assertMaskedImagesEqual(sciOriginal.maskedImage,
                                     sciTest1.maskedImage)
Exemplo n.º 4
0
    def _testZogyImagePsfMatchTask(self,
                                   spatiallyVarying=False,
                                   inImageSpace=False,
                                   doScorr=False,
                                   **kwargs):
        """Test running Zogy using ZogyImagePsfMatchTask framework.

        Compare resulting diffim version with original, non-spatially-varying version.
        """
        config = ZogyImagePsfMatchConfig()
        config.zogyMapReduceConfig.gridStepX = config.zogyMapReduceConfig.gridStepY = 9
        config.zogyMapReduceConfig.borderSizeX = config.zogyMapReduceConfig.borderSizeY = 3
        if inImageSpace:  # need larger border size for image-space run
            config.zogyMapReduceConfig.gridStepX = config.zogyMapReduceConfig.gridStepY = 8
            config.zogyMapReduceConfig.borderSizeX = config.zogyMapReduceConfig.borderSizeY = 6
        task = ZogyImagePsfMatchTask(config=config)
        result = task.subtractExposures(self.im2ex,
                                        self.im1ex,
                                        inImageSpace=inImageSpace,
                                        doWarping=False,
                                        spatiallyVarying=spatiallyVarying)
        D_fromTask = result.subtractedExposure

        config = ZogyConfig()
        task = ZogyTask(templateExposure=self.im2ex,
                        scienceExposure=self.im1ex,
                        config=config)
        D = task.computeDiffim(inImageSpace=inImageSpace, **kwargs).D
        self._compareExposures(D_fromTask, D, tol=0.04, Scorr=doScorr)
Exemplo n.º 5
0
    def testZogyNewImplementation(self):
        """DM-25115 implementation test.

        Notes
        -----
        See diffimTests: tickets/DM-25115_zogy_implementation/DM-25115_zogy_unit_test_development.ipynb
        """

        # self.svar = svar  # variance of noise in science image
        # self.tvar = tvar  # variance of noise in template image

        # Sourceless case
        self.im1ex, self.im2ex \
            = makeFakeImages(size=(256, 256), svar=100., tvar=100.,
                             psf1=self.psf1_sigma, psf2=self.psf2_sigma,
                             n_sources=0, psf_yvary_factor=0, varSourceChange=0.1,
                             seed=1, verbose=False)

        config = ZogyConfig()
        config.scaleByCalibration = False
        task = ZogyTask(config=config)
        res = task.run(self.im1ex, self.im2ex)

        bbox = res.diffExp.getBBox()
        subBbox = bbox.erodedBy(lsst.geom.Extent2I(25, 25))
        subExp = res.diffExp[subBbox]
        pixvar = self._computePixelVariance(subExp.maskedImage)
        varmean = self._computeVarianceMean(subExp.maskedImage)
        # Due to 3 sigma clipping, this is not so precise
        self.assertFloatsAlmostEqual(pixvar, 200, rtol=0.1, atol=None)
        self.assertFloatsAlmostEqual(varmean, 200, rtol=0.05, atol=None)
        S = res.scoreExp.image.array / np.sqrt(res.scoreExp.variance.array)
        self.assertLess(np.amax(S), 5.)  # Source not detected

        # ==========
        self.im1ex, self.im2ex \
            = makeFakeImages(size=(256, 256), svar=10., tvar=10.,
                             psf1=self.psf1_sigma, psf2=self.psf2_sigma,
                             n_sources=10, psf_yvary_factor=0, varSourceChange=0.1,
                             seed=1, verbose=False)
        task = ZogyTask(config=config)
        res = task.run(self.im1ex, self.im2ex)
        S = res.scoreExp.image.array / np.sqrt(res.scoreExp.variance.array)
        self.assertGreater(np.amax(S), 5.)  # Source detected
Exemplo n.º 6
0
    def _testZogyScorr(self, varAst=0.):
        """Compute Zogy likelihood images (Scorr) using Fourier- and Real-space methods.

        Compare the images. They are not identical but should be similar (within ~2%).
        """
        config = ZogyConfig()
        task = ZogyTask(templateExposure=self.im2ex, scienceExposure=self.im1ex, config=config)
        D_F = task.computeScorr(inImageSpace=False, xVarAst=varAst, yVarAst=varAst)
        D_R = task.computeScorr(inImageSpace=True, xVarAst=varAst, yVarAst=varAst)
        self._compareExposures(D_F.S, D_R.S, Scorr=True)
Exemplo n.º 7
0
    def testPixelOffset(self):
        """ Test whether the peak position is at [300][300].
        """
        self._setUpImages()
        config = ZogyConfig(scaleByCalibration=False)
        task = ZogyTask(config=config)
        D_F = task.run(self.imnex, self.imrex, calculateScore=False)
        max_loc = PixelOffsetTest._find_max(D_F.diffExp.image.array)

        self.assertEqual(max_loc, (300, 300))
Exemplo n.º 8
0
 def testWholeImageGrid(self):
     """Test that a 1-cell `grid` is actually the whole image"""
     config = ZogyConfig()
     task = ZogyTask(config=config)
     bb = geom.Box2I(geom.Point2I(5, 10), geom.Extent2I(200, 300))
     D = afwImage.ImageI(bb)
     grid = task.generateGrid(bb, geom.Extent2I(15, 15), bb.getDimensions())
     self.assertTrue(len(grid) == 1, "Grid length is not 1")
     x = grid[0]
     D[x.innerBox] += 1
     self.assertTrue(np.all(D.array == 1),
                     "Single cell does not cover the original image.")
Exemplo n.º 9
0
    def testZogyDiffim(self):
        """Compute Zogy diffims using Fourier- and Real-space methods.

        Compare the images.  They are not identical but should be
        similar (within ~2%).
        """
        self._setUpImages()
        config = ZogyConfig()
        task = ZogyTask(templateExposure=self.im2ex, scienceExposure=self.im1ex, config=config)
        D_F = task.computeDiffim(inImageSpace=False)
        D_R = task.computeDiffim(inImageSpace=True)
        # Fourier-space and image-space versions are not identical, so up the tolerance.
        # This is a known issue with the image-space version.
        self._compareExposures(D_F.D, D_R.D, tol=0.03)
Exemplo n.º 10
0
    def testZogyDiffim(self):
        """Compute Zogy diffims using Fourier- and Real-space methods.

        Compare the images.  They are not identical but should be
        similar (within ~2%).
        """
        self._setUpImages()
        config = ZogyConfig()
        task = ZogyTask(templateExposure=self.im2ex,
                        scienceExposure=self.im1ex,
                        config=config)
        D_F = task.computeDiffim(inImageSpace=False)
        D_R = task.computeDiffim(inImageSpace=True)
        self._compareExposures(D_F, D_R)
Exemplo n.º 11
0
    def testSplitBorder(self):
        """Test outer border box splitting around an inner box"""
        config = ZogyConfig()
        task = ZogyTask(config=config)

        bb = geom.Box2I(geom.Point2I(5, 10), geom.Extent2I(20, 30))
        D = afwImage.ImageI(bb)
        innerbox = bb.erodedBy(geom.Extent2I(3, 4))
        D[innerbox] = 1

        borderboxes = task.splitBorder(innerbox, bb)
        for x in borderboxes:
            D[x] += 1
        # The splitting should cover all border pixels exactly once
        self.assertTrue(np.all(D.array == 1),
                        "Border does not cover all pixels exactly once.")
Exemplo n.º 12
0
 def testGenerateGrid(self):
     """Test that the generated grid covers the whole image"""
     config = ZogyConfig()
     task = ZogyTask(config=config)
     bb = geom.Box2I(geom.Point2I(5, 10), geom.Extent2I(200, 300))
     D = afwImage.ImageI(bb)
     grid = task.generateGrid(bb,
                              geom.Extent2I(15, 15),
                              geom.Extent2I(20, 30),
                              powerOfTwo=True)
     for x in grid:
         h = x.outerBox.getHeight()
         w = x.outerBox.getWidth()
         self.assertTrue(isPowerOfTwo(h), "Box height is not power of two")
         self.assertTrue(isPowerOfTwo(w), "Box width is not power of two")
         D[x.innerBox] += 1
     self.assertTrue(
         np.all(D.array == 1),
         "Grid inner boxes do not cover all pixels exactly once.")
Exemplo n.º 13
0
    def testSimDiffim(self):
        "Basic smoke test to verify that the test code itself can run."
        refPsf = 2.4
        sciPsfBase = 2.
        sciNoise = 5.
        refNoise = 1.5
        seed = 8
        fluxLevel = 500
        decorrelate = DecorrelateALKernelTask()
        zogyConfig = ZogyConfig()
        alConfig = ImagePsfMatchConfig()

        for s in range(self.nRandIter):
            sciPsf = sciPsfBase + s * 0.2
            ref, _ = self.makeTestImages(seed=seed + s,
                                         nSrc=20,
                                         psfSize=refPsf,
                                         noiseLevel=refNoise,
                                         fluxLevel=fluxLevel)
            sci, src = self.makeTestImages(seed=seed + s,
                                           nSrc=20,
                                           psfSize=sciPsf,
                                           noiseLevel=sciNoise,
                                           fluxLevel=fluxLevel)
            # The diffim tasks might modify the images,
            # so make a deep copy to make sure they are independent
            sci2 = sci.clone()
            ref2 = ref.clone()

            resAl = self.wrapAlDiffim(alConfig, ref, sci)
            resZogy = self.wrapZogyDiffim(zogyConfig, ref2, sci2)
            metricZogy = self.diffimMetricBasic(resZogy, src, sigma=3)
            metricAl = self.diffimMetricBasic(resAl, src, sigma=3)
            mKernel = self.wrapAlDiffim(alConfig, ref, sci, returnKernel=True)
            resDecorr = decorrelate.run(sci, ref, resAl,
                                        mKernel).correctedExposure
            metricDecorr = self.diffimMetricBasic(resDecorr, src, sigma=3)
            self.assertGreaterEqual(metricZogy, -1)
            self.assertGreaterEqual(metricAl, -1)
            self.assertGreaterEqual(metricDecorr, -1)