示例#1
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))
示例#2
0
    def test_stdev(self):
        """Test that sources are detected on a simulated image with
        thresholdType='stdev'.
        """
        exposure, schema, numX, numY, starSigma = self._create_exposure()

        config = SourceDetectionTask.ConfigClass()
        # don't modify the image after detection.
        config.reEstimateBackground = False
        config.thresholdType = "stdev"
        task = SourceDetectionTask(config=config, schema=schema)

        self._check_detectFootprints(exposure,
                                     numX,
                                     numY,
                                     starSigma,
                                     task,
                                     config,
                                     doSmooth=True)
        self._check_detectFootprints(exposure,
                                     numX,
                                     numY,
                                     starSigma,
                                     task,
                                     config,
                                     doSmooth=False)
示例#3
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))
示例#4
0
    def test_detection_stdev(self):
        """Test detection and measurement on an exposure with negative sources
        for thresholdType="stdev".
        """
        exposure, numX, numY = self._create_exposure()

        if display:
            disp = afwDisplay.Display(frame=1)
            disp.mtv(exposure,
                     title=self._testMethodName + ": image with -ve sources")

        schema = afwTable.SourceTable.makeMinimalSchema()
        config = SourceDetectionTask.ConfigClass()
        config.reEstimateBackground = False
        config.thresholdPolarity = 'both'
        detection = SourceDetectionTask(config=config, schema=schema)
        algMetadata = dafBase.PropertyList()
        measurement = SourceMeasurementTask(schema=schema,
                                            algMetadata=algMetadata)

        table = afwTable.SourceTable.make(schema)
        detections = detection.run(table, exposure)
        sources = detections.sources
        fpSets = detections.fpSets

        self.assertEqual(len(sources), numX * numY)
        self.assertEqual(fpSets.numPos, numX * numY / 2)
        self.assertEqual(fpSets.numNeg, numX * numY / 2)

        measurement.run(sources, exposure)

        nGoodCent = 0
        nGoodShape = 0
        for s in sources:
            cent = s.getCentroid()
            shape = s.getShape()

            if cent[0] == cent[0] and cent[1] == cent[1]:
                nGoodCent += 1

            if (shape.getIxx() == shape.getIxx()
                    and shape.getIyy() == shape.getIyy()
                    and shape.getIxy() == shape.getIxy()):
                nGoodShape += 1

            if display:
                xy = cent[0], cent[1]
                disp.dot('+', *xy)
                disp.dot(shape, *xy, ctype=afwDisplay.RED)

        self.assertEqual(nGoodCent, numX * numY)
        self.assertEqual(nGoodShape, numX * numY)
示例#5
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)
        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)
示例#7
0
    def test_significance_stdev(self):
        """Check the non-smoothed, non-background updated peak significance
        values with thresholdType="stddev".
        """
        exposure, schema, numX, numY, starSigma = self._create_exposure()

        config = SourceDetectionTask.ConfigClass()
        # don't modify the image after detection.
        config.reEstimateBackground = False
        config.doTempLocalBackground = False
        config.thresholdType = "stdev"
        task = SourceDetectionTask(config=config, schema=schema)

        result = self._check_detectFootprints(exposure,
                                              numX,
                                              numY,
                                              starSigma,
                                              task,
                                              config,
                                              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)
        for footprint in result.positive.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,
                                                 rtol=1e-7,
                                                 msg=str(point))
示例#8
0
    def testBasics(self):
        bbox = afwGeom.Box2I(afwGeom.Point2I(256, 100),
                             afwGeom.Extent2I(128, 127))
        minCounts = 2000
        maxCounts = 20000
        starSigma = 1.5
        numX = 4
        numY = 4
        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)

        if display:
            ds9.mtv(exposure)

        schema = afwTable.SourceTable.makeMinimalSchema()
        config = SourceDetectionTask.ConfigClass()
        config.reEstimateBackground = False
        config.thresholdPolarity = 'both'
        detection = SourceDetectionTask(config=config, schema=schema)
        algMetadata = dafBase.PropertyList()
        measurement = SourceMeasurementTask(schema=schema,
                                            algMetadata=algMetadata)

        table = afwTable.SourceTable.make(schema)
        detections = detection.makeSourceCatalog(table, exposure)
        sources = detections.sources
        fpSets = detections.fpSets

        self.assertEqual(len(sources), numX * numY)
        self.assertEqual(fpSets.numPos, numX * numY / 2)
        self.assertEqual(fpSets.numNeg, numX * numY / 2)

        measurement.run(sources, exposure)

        nGoodCent = 0
        nGoodShape = 0
        for s in sources:
            cent = s.getCentroid()
            shape = s.getShape()

            if cent[0] == cent[0] and cent[1] == cent[1]:
                nGoodCent += 1

            if (shape.getIxx() == shape.getIxx()
                    and shape.getIyy() == shape.getIyy()
                    and shape.getIxy() == shape.getIxy()):
                nGoodShape += 1

            if display:
                xy = cent[0] - exposure.getX0(), cent[1] - exposure.getY0()
                ds9.dot('+', *xy)
                ds9.dot(shape, *xy, ctype=ds9.RED)

        self.assertEqual(nGoodCent, numX * numY)
        self.assertEqual(nGoodShape, numX * numY)
    subConfig[
        algName].maxSincRadius = 0  # Disable sinc photometry because we're undersampled

for subConfig in (measure_config.plugins, measure_config.undeblended):
    subConfig.names.add(algName2)
    values = [ii / 0.168 for ii in (0.65, 0.85, 1.1)]
    algConfig = subConfig[algName2]
    algConfig.seeing = values
    algConfig.aperture.radii = [3, 6, 12, 24]
    algConfig.aperture.maxSincRadius = 0

measure_config.load(
    '/tigress/rea3/lsst/DM-8059/obs_subaru/config/apertures.py')

deblend_config = SourceDeblendTask.ConfigClass()
detect_config = SourceDetectionTask.ConfigClass()
detect_config.isotropicGrow = True
detect_config.doTempLocalBackground = False
detect_config.thresholdValue = 5
detect_config.nSigmaToGrow = args.grow

schema = afwTable.SourceTable.makeMinimalSchema()
detectTask = SourceDetectionTask(schema, config=detect_config)
deblendTask = SourceDeblendTask(schema, config=deblend_config)
measureTask = SingleFrameMeasurementTask(schema, config=measure_config)

peakMinimalSchema = afwDet.PeakTable.makeMinimalSchema()
peakSchemaMapper = afwTable.SchemaMapper(peakMinimalSchema, schema)

truthKey = schema.addField('truth_index',
                           type=int,