def checkExposure(original, doTempLocalBackground, doTempWideBackground):
            config = SourceDetectionTask.ConfigClass()
            config.reEstimateBackground = False
            config.thresholdType = "pixel_stdev"
            config.doTempLocalBackground = doTempLocalBackground
            config.doTempWideBackground = doTempWideBackground
            schema = afwTable.SourceTable.makeMinimalSchema()
            task = SourceDetectionTask(config=config, schema=schema)

            exposure = original.clone()
            task.detectFootprints(exposure, sigma=3.21)

            self.assertFloatsEqual(exposure.image.array, original.image.array)
            # Mask is permitted to vary: DETECTED bit gets set
            self.assertFloatsEqual(exposure.variance.array, original.variance.array)
Example #2
0
    def testBasics(self):
        bbox = afwGeom.Box2I(afwGeom.Point2I(256, 100),
                             afwGeom.Extent2I(128, 127))
        minCounts = 5000
        maxCounts = 50000
        starSigma = 1.5
        numX = 5
        numY = 5
        coordList = self.makeCoordList(
            bbox=bbox,
            numX=numX,
            numY=numY,
            minCounts=minCounts,
            maxCounts=maxCounts,
            sigma=starSigma,
        )
        kwid = 11
        sky = 2000
        addPoissonNoise = True
        exposure = plantSources(bbox=bbox,
                                kwid=kwid,
                                sky=sky,
                                coordList=coordList,
                                addPoissonNoise=addPoissonNoise)

        schema = afwTable.SourceTable.makeMinimalSchema()
        config = SourceDetectionTask.ConfigClass()
        config.reEstimateBackground = False
        task = SourceDetectionTask(config=config, schema=schema)
        for doSmooth in (False, True):
            taskSigma = 2.2
            res = task.detectFootprints(exposure,
                                        doSmooth=doSmooth,
                                        sigma=taskSigma)
            self.assertEqual(res.numPos, numX * numY)
            self.assertEqual(res.numNeg, 0)
            self.assertEqual(task.metadata.get("sigma"), taskSigma)
            self.assertEqual(task.metadata.get("doSmooth"), doSmooth)
            self.assertEqual(task.metadata.get("nGrow"),
                             int(taskSigma * config.nSigmaToGrow + 0.5))

            res = task.detectFootprints(exposure,
                                        doSmooth=doSmooth,
                                        sigma=None)
            taskSigma = task.metadata.get("sigma")
            self.assertTrue(abs(taskSigma - starSigma) < 0.1)
            self.assertEqual(res.numPos, numX * numY)
            self.assertEqual(res.numNeg, 0)
Example #3
0
    def test_pixel_stdev(self):
        """Test that sources are detected on a simulated image with
        thresholdType='pixel_stdev', and that they have the right significance.
        """
        exposure, schema, numX, numY, starSigma = self._create_exposure()

        config = SourceDetectionTask.ConfigClass()
        config.thresholdType = "pixel_stdev"
        config.reEstimateBackground = False
        # TempLocalBackground changes the peak value of the faintest peak,
        # so disable it for this test so that we can calculate an expected
        # answer without having to try to deal with backgrounds.
        config.doTempLocalBackground = False
        task = SourceDetectionTask(config=config, schema=schema)
        # Don't smooth, so that we can directly calculate the s/n from the exposure.
        result = task.detectFootprints(exposure, doSmooth=False)
        self.assertEqual(result.numPos, numX * numY)
        self.assertEqual(result.numNeg, 0)
        # Significance values for `pixel_stdev` should match image/sqrt(variance).
        for footprint in result.positive.getFootprints():
            for peak in footprint.peaks:
                point = lsst.geom.Point2I(peak.getIx(), peak.getIy())
                value = exposure.image[point]
                stddev = np.sqrt(exposure.variance[point])
                with self.subTest(str(point)):
                    self.assertFloatsAlmostEqual(peak["significance"],
                                                 value / stddev,
                                                 rtol=1e-7,
                                                 msg=str(point))
Example #4
0
    def test_significance(self):
        """Test that negative peaks have the right significance for
        thresholdType='stdev' for the non-convolved, non-local-background case.
        """
        exposure, numX, numY = self._create_exposure()

        schema = afwTable.SourceTable.makeMinimalSchema()
        config = SourceDetectionTask.ConfigClass()
        config.thresholdPolarity = 'both'
        # don't modify the image after detection.
        config.reEstimateBackground = False
        config.doTempLocalBackground = False
        detection = SourceDetectionTask(config=config, schema=schema)
        result = detection.detectFootprints(exposure, doSmooth=False)

        bad = exposure.mask.getPlaneBitMask(config.statsMask)
        sctrl = afwMath.StatisticsControl()
        sctrl.setAndMask(bad)
        stats = afwMath.makeStatistics(exposure.maskedImage, afwMath.STDEVCLIP,
                                       sctrl)
        stddev = stats.getValue(afwMath.STDEVCLIP)
        # Don't bother checking positive footprints: those are tested more
        # thoroughly in test_detection.py.
        for footprint in result.negative.getFootprints():
            for peak in footprint.peaks:
                point = lsst.geom.Point2I(peak.getIx(), peak.getIy())
                value = exposure.image[point]
                with self.subTest(str(point)):
                    self.assertFloatsAlmostEqual(
                        peak["significance"],
                        -value / stddev,  # S/N for negative peak
                        rtol=1e-7,
                        msg=str(point))
        def checkExposure(original, doTempLocalBackground,
                          doTempWideBackground):
            config = SourceDetectionTask.ConfigClass()
            config.reEstimateBackground = False
            config.thresholdType = "pixel_stdev"
            config.doTempLocalBackground = doTempLocalBackground
            config.doTempWideBackground = doTempWideBackground
            schema = afwTable.SourceTable.makeMinimalSchema()
            task = SourceDetectionTask(config=config, schema=schema)

            exposure = original.clone()
            task.detectFootprints(exposure, sigma=3.21)

            self.assertFloatsEqual(exposure.image.array, original.image.array)
            # Mask is permitted to vary: DETECTED bit gets set
            self.assertFloatsEqual(exposure.variance.array,
                                   original.variance.array)
    def testBasics(self):
        bbox = lsst.geom.Box2I(lsst.geom.Point2I(256, 100), lsst.geom.Extent2I(128, 127))
        minCounts = 5000
        maxCounts = 50000
        starSigma = 1.5
        numX = 5
        numY = 5
        coordList = self.makeCoordList(
            bbox=bbox,
            numX=numX,
            numY=numY,
            minCounts=minCounts,
            maxCounts=maxCounts,
            sigma=starSigma,
        )
        kwid = 11
        sky = 2000
        addPoissonNoise = True
        exposure = plantSources(bbox=bbox, kwid=kwid, sky=sky, coordList=coordList,
                                addPoissonNoise=addPoissonNoise)

        schema = afwTable.SourceTable.makeMinimalSchema()
        config = SourceDetectionTask.ConfigClass()
        config.reEstimateBackground = False
        task = SourceDetectionTask(config=config, schema=schema)
        for doSmooth in (False, True):
            taskSigma = 2.2
            res = task.detectFootprints(exposure, doSmooth=doSmooth, sigma=taskSigma)
            self.assertEqual(res.numPos, numX * numY)
            self.assertEqual(res.numNeg, 0)
            self.assertEqual(task.metadata.getScalar("sigma"), taskSigma)
            self.assertEqual(task.metadata.getScalar("doSmooth"), doSmooth)
            self.assertEqual(task.metadata.getScalar("nGrow"), int(taskSigma * config.nSigmaToGrow + 0.5))

            res = task.detectFootprints(exposure, doSmooth=doSmooth, sigma=None)
            taskSigma = task.metadata.getScalar("sigma")
            self.assertLess(abs(taskSigma - starSigma), 0.1)
            self.assertEqual(res.numPos, numX * numY)
            self.assertEqual(res.numNeg, 0)