def testFailures(self):
        """Test deblender failure flagging (#2871)

        We create a good source which is expected to pass and a bad source
        which is expected to fail because its footprint goes off the image.
        This latter case may not happen in practise, but it is useful for
        checking the plumbing of the deblender.
        """
        import lsst.meas.deblender as measDeb

        self.checkDeblender()
        xGood, yGood = 57, 86
        # Required to be in image so we can evaluate the PSF; will put neighbour just outside
        xBad, yBad = 0, 0
        flux = 100.0
        dims = geom.Extent2I(128, 128)

        mi = afwImage.MaskedImageF(dims)
        mi.getVariance().set(1.0)
        image = mi.getImage()
        image.set(0)
        image[xGood, yGood, afwImage.LOCAL] = flux

        exposure = afwImage.makeExposure(mi)
        psf = algorithms.DoubleGaussianPsf(21, 21, 3.)
        exposure.setPsf(psf)

        schema = afwTable.SourceTable.makeMinimalSchema()

        config = measDeb.SourceDeblendConfig()
        config.catchFailures = True
        task = measDeb.SourceDeblendTask(schema, config=config)

        catalog = afwTable.SourceCatalog(schema)

        def makeSource(x, y, offset=-2, size=3.0):
            """Make a source in the catalog

            Two peaks are created: one at the specified
            position, and one offset in x,y.
            The footprint is of the nominated size.
            """
            src = catalog.addNew()
            spans = afwGeom.SpanSet.fromShape(int(size), offset=(x, y))
            foot = afwDetection.Footprint(spans)
            foot.addPeak(x, y, flux)
            foot.addPeak(x + offset, y + offset, flux)
            src.setFootprint(foot)
            return src

        good = makeSource(xGood, yGood)
        bad = makeSource(xBad, yBad)

        task.run(exposure, catalog)

        self.assertFalse(good.get('deblend_failed'))
        self.assertTrue(bad.get('deblend_failed'))
Beispiel #2
0
    def testInclude(self):
        schema = afwTable.SourceTable.makeMinimalSchema()

        # Create the detection task
        config = SourceDetectionTask.ConfigClass()
        config.reEstimateBackground = False  # Turn off so that background does not change from orig
        detectionTask = SourceDetectionTask(config=config, schema=schema)

        # Create the deblender Task
        debConfig = measDeb.SourceDeblendConfig()
        debTask = measDeb.SourceDeblendTask(schema, config=debConfig)

        # Create the measurement Task
        config = SingleFrameMeasurementTask.ConfigClass()
        measureTask = SingleFrameMeasurementTask(schema, config=config)

        # Create the output table
        tab = afwTable.SourceTable.make(schema)

        # Process the data
        result = detectionTask.run(tab, self.calexp)
        sources = result.sources

        # Run the deblender
        debTask.run(self.calexp, sources)

        # Run the measurement task: this where the replace-with-noise occurs
        measureTask.run(sources, self.calexp)

        plotOnFailure = False
        if display:
            plotOnFailure = True

        # The relative differences ranged from 0.02 to ~2.  This rtol is somewhat
        # random, but will certainly catch the pathology if it occurs.
        self.assertFloatsAlmostEqual(
            self.calexpOrig.getMaskedImage().getImage().getArray(),
            self.calexp.getMaskedImage().getImage().getArray(),
            rtol=1E-3,
            printFailures=False,
            plotOnFailure=plotOnFailure)