Exemplo n.º 1
0
    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.
        """
        self.checkDeblender()
        xGood, yGood = 57, 86
        xBad, yBad = 0, 0 # Required to be in image so we can evaluate the PSF; will put neighbour just outside
        flux = 100.0
        dims = afwGeom.Extent2I(128, 128)

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

        exposure = afwImage.makeExposure(mi)
        psf = testLib.makeTestPsf(image)
        exposure.setPsf(psf)

        schema = afwTable.SourceTable.makeMinimalSchema()

        config = algorithms.SourceDeblendConfig()
        task = algorithms.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()
            foot = afwDetection.Footprint(afwGeom.Point2I(x, y), size)
            peakList = foot.getPeaks()
            peakList.push_back(afwDetection.Peak(x, y, flux))
            peakList.push_back(afwDetection.Peak(x + offset, y + offset, flux))
            src.setFootprint(foot)
            return src

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

        task.run(exposure, catalog, psf)

        self.assertFalse(good.get('deblend.failed'))
        self.assertTrue(bad.get('deblend.failed'))
    def do_testAstrometry(self, control, bkgd):
        """Test that we can instantiate and play with a centroiding algorithms"""

        x0, y0 = 12345, 54321
        for imageFactory in (
                afwImage.MaskedImageF,
                afwImage.MaskedImageD,
        ):

            im = imageFactory(afwGeom.ExtentI(100, 100))
            im.setXY0(afwGeom.Point2I(x0, y0))
            psf = testLib.makeTestPsf(im)

            exp = afwImage.makeExposure(im)
            exp.setPsf(psf)
            schema = afwTable.SourceTable.makeMinimalSchema()
            centroider = algorithms.MeasureSourcesBuilder().addAlgorithm(
                control).build(schema)

            #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

            im.set(bkgd)
            x, y = 30, 20
            im.set(x, y, (1010, ))

            table = afwTable.SourceTable.make(schema)
            table.defineCentroid(control.name)
            source = table.makeRecord()
            foot = afwDetection.Footprint(exp.getBBox())
            source.setFootprint(foot)

            centroider.apply(source, exp, afwGeom.Point2D(x + x0, y + y0))

            self.assertEqual(x + x0, source.getX())
            self.assertEqual(y + y0, source.getY())
            self.assertFalse(source.get(control.name + ".flags"))

            #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

            im.set(bkgd)
            im.set(10, 20, (1010, ))
            im.set(10, 21, (1010, ))
            im.set(11, 20, (1010, ))
            im.set(11, 21, (1010, ))

            x, y = 10.5 + x0, 20.5 + y0
            centroider.apply(source, exp,
                             afwGeom.Point2D(x + 0.123, y - 0.123))

            self.assertEqual(x, source.getX())
            self.assertEqual(y, source.getY())
            self.assertFalse(source.get(control.name + ".flags"))
Exemplo n.º 3
0
    def do_testAstrometry(self, control, bkgd):
        """Test that we can instantiate and play with a centroiding algorithms"""

        x0, y0 = 12345, 54321
        for imageFactory in (afwImage.MaskedImageF,
                             afwImage.MaskedImageD,
                             ):

            im = imageFactory(afwGeom.ExtentI(100, 100))
            im.setXY0(afwGeom.Point2I(x0, y0))
            psf = testLib.makeTestPsf(im)

            exp = afwImage.makeExposure(im)
            exp.setPsf(psf)
            schema = afwTable.SourceTable.makeMinimalSchema()
            centroider = algorithms.MeasureSourcesBuilder().addAlgorithm(control).build(schema)

            #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

            im.set(bkgd)
            x, y = 30, 20
            im.set(x, y, (1010,))

            table = afwTable.SourceTable.make(schema)
            table.defineCentroid(control.name)
            source = table.makeRecord()
            foot = afwDetection.Footprint(exp.getBBox())
            source.setFootprint(foot)

            centroider.apply(source, exp, afwGeom.Point2D(x + x0, y + y0))

            self.assertEqual(x + x0, source.getX())
            self.assertEqual(y + y0, source.getY())
            self.assertFalse(source.get(control.name + ".flags"))

            #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

            im.set(bkgd)
            im.set(10, 20, (1010,))
            im.set(10, 21, (1010,))
            im.set(11, 20, (1010,))
            im.set(11, 21, (1010,))

            x, y = 10.5 + x0, 20.5 + y0
            centroider.apply(source, exp, afwGeom.Point2D(x + 0.123, y - 0.123))

            self.assertEqual(x, source.getX())
            self.assertEqual(y, source.getY())
            self.assertFalse(source.get(control.name + ".flags"))
Exemplo n.º 4
0
    def testAlgorithms(self):
        """Test that we can instantiate and use algorithms"""

        config = measAlg.SourceMeasurementConfig()
        config.algorithms.names = measAlg.AlgorithmRegistry.all.keys()
        config.algorithms.names.discard(config.centroider.name)
        config.doReplaceWithNoise = False

        config.algorithms.names.discard("flux.peakLikelihood")

        if False:
            log = pexLog.getDefaultLog()
            log.setThreshold(log.DEBUG)

        schema = afwTable.SourceTable.makeMinimalSchema()
        task = measAlg.SourceMeasurementTask(schema, config=config)
        catalog = afwTable.SourceCatalog(schema)
        source = catalog.addNew()
        source.set("id", 12345)

        size = 256
        xStar, yStar = 65.432, 76.543
        width = 3.21
        x0, y0 = 12345, 54321
        x, y = numpy.indices((size, size))
        im = afwImage.MaskedImageF(afwGeom.ExtentI(size, size))
        im.setXY0(afwGeom.Point2I(x0, y0))
        im.getVariance().set(1.0)
        arr = im.getImage().getArray()
        arr[y, x] = numpy.exp(-0.5 * ((x - xStar) ** 2 + (y - yStar) ** 2) / width ** 2)
        psf = testLib.makeTestPsf(im)
        exp = afwImage.makeExposure(im)
        exp.setPsf(psf)
        exp.setXY0(afwGeom.Point2I(x0, y0))
        scale = 1.0e-5
        wcs = afwImage.makeWcs(
            afwCoord.Coord(0.0 * afwGeom.degrees, 0.0 * afwGeom.degrees),
            afwGeom.Point2D(0.0, 0.0),
            scale,
            0.0,
            0.0,
            scale,
        )
        exp.setWcs(wcs)

        point = afwGeom.Point2I(int(xStar + x0), int(yStar + y0))
        bbox = im.getBBox()
        bbox.shift(afwGeom.Extent2I(x0, y0))
        foot = afwDetection.Footprint(point, width, bbox)
        foot.addPeak(point.getX(), point.getY(), 1.0)
        afwDetection.setMaskFromFootprint(exp.getMaskedImage().getMask(), foot, 1)
        source.setFootprint(foot)

        if display:
            ds9.mtv(exp, frame=1)

        task.run(exp, catalog)

        for alg in config.algorithms:
            flagName = alg + ".flags"
            if False:
                print(
                    alg,
                    source.get(flagName) if flagName in schema else None,
                    source.get(alg) if alg in schema else None,
                )
            elif flagName in schema:
                self.assertFalse(source.get(alg + ".flags"))
Exemplo n.º 5
0
    def testAlgorithms(self):
        """Test that we can instantiate and use algorithms"""

        config = measAlg.SourceMeasurementConfig()
        config.algorithms.names = measAlg.AlgorithmRegistry.all.keys()
        config.algorithms.names.discard(config.centroider.name)
        config.doReplaceWithNoise = False

        config.algorithms.names.discard("flux.peakLikelihood")

        if False:
            log = pexLog.getDefaultLog()
            log.setThreshold(log.DEBUG)

        schema = afwTable.SourceTable.makeMinimalSchema()
        task = measAlg.SourceMeasurementTask(schema, config=config)
        catalog = afwTable.SourceCatalog(schema)
        source = catalog.addNew()
        source.set("id", 12345)

        size = 256
        xStar, yStar = 65.432, 76.543
        width = 3.21
        x0, y0 = 12345, 54321
        x, y = numpy.indices((size, size))
        im = afwImage.MaskedImageF(afwGeom.ExtentI(size, size))
        im.setXY0(afwGeom.Point2I(x0, y0))
        im.getVariance().set(1.0)
        arr = im.getImage().getArray()
        arr[y,
            x] = numpy.exp(-0.5 * ((x - xStar)**2 + (y - yStar)**2) / width**2)
        psf = testLib.makeTestPsf(im)
        exp = afwImage.makeExposure(im)
        exp.setPsf(psf)
        exp.setXY0(afwGeom.Point2I(x0, y0))
        scale = 1.0e-5
        wcs = afwImage.makeWcs(
            afwCoord.Coord(0.0 * afwGeom.degrees, 0.0 * afwGeom.degrees),
            afwGeom.Point2D(0.0, 0.0), scale, 0.0, 0.0, scale)
        exp.setWcs(wcs)

        point = afwGeom.Point2I(int(xStar + x0), int(yStar + y0))
        bbox = im.getBBox()
        bbox.shift(afwGeom.Extent2I(x0, y0))
        foot = afwDetection.Footprint(point, width, bbox)
        foot.addPeak(point.getX(), point.getY(), 1.0)
        afwDetection.setMaskFromFootprint(exp.getMaskedImage().getMask(), foot,
                                          1)
        source.setFootprint(foot)

        if display:
            ds9.mtv(exp, frame=1)

        task.run(exp, catalog)

        for alg in config.algorithms:
            flagName = alg + ".flags"
            if False:
                print(alg,
                      source.get(flagName) if flagName in schema else None,
                      source.get(alg) if alg in schema else None)
            elif flagName in schema:
                self.assertFalse(source.get(alg + ".flags"))