Ejemplo n.º 1
0
    def setUp(self):
        im = afwImage.ImageF(self.monetFile("small.fits"))
        self.mi = afwImage.MaskedImageF(im, afwImage.MaskU(im.getDimensions()),
                                        afwImage.ImageF(im.getDimensions()));
        self.ds = afwDetection.FootprintSet(self.mi, afwDetection.Threshold(100))

        if display:
            ds9.mtv(self.mi.getImage())
            ds9.erase()
            
        for foot in self.ds.getFootprints():
            bbox = foot.getBBox()
            x0, y0 = bbox.getMinX(), bbox.getMinY()
            x1, y1 = bbox.getMaxX(), bbox.getMaxY()
            xc = (x0 + x1)/2.0
            yc = (y0 + y1)/2.0
            
            if display:
                ds9.dot("+", xc, yc, ctype=ds9.BLUE)

                if False:
                    x0 -= 0.5; y0 -= 0.5
                    x1 += 0.5; y1 += 0.5
                    
                    ds9.line([(x0, y0), (x1, y0), (x1, y1), (x0, y1), (x0, y0)], ctype=ds9.RED)

        self.control = algorithms.GaussianCentroidControl()
        schema = afwTable.SourceTable.makeMinimalSchema()
        self.centroider = algorithms.MeasureSourcesBuilder().addAlgorithm(self.control).build(schema)
        self.ssMeasured = afwTable.SourceCatalog(schema)
        self.ssMeasured.table.defineCentroid(self.control.name)
        self.ssTruth = afwTable.SourceCatalog(schema)
        self.readTruth(self.monetFile("positions.dat-original"))
Ejemplo n.º 2
0
Archivo: utils.py Proyecto: lsst-dm/bp
def drawFootprint(foot, borderWidth=0.5, origin=None, frame=None, ctype=None, bin=1):
    """Draw an afwDetection::Footprint on a ds9 frame with the specified ctype.  Include an extra borderWidth
pixels If origin is present, it's Added to the Footprint

All Footprint coordinates are divided by bin, as is right and proper for overlaying on a binned image
    """

    ds9.cmdBuffer.pushSize()

    borderWidth /= bin
    for s in foot.getSpans():
        y, x0, x1 = s.getY(), s.getX0(), s.getX1()

        if origin:
            x0 += origin[0]; x1 += origin[0]
            y += origin[1]

            x0 /= bin; x1 /= bin; y /= bin
    
        ds9.line([(x0 - borderWidth, y - borderWidth),
                  (x0 - borderWidth, y + borderWidth),
                  (x1 + borderWidth, y + borderWidth),
                  (x1 + borderWidth, y - borderWidth),
                  (x0 - borderWidth, y - borderWidth),
                  ], frame=frame, ctype=ctype)

    ds9.cmdBuffer.popSize()
Ejemplo n.º 3
0
def display(self, xy0=None, frame=1, ctype=None):
    """Display polygon on existing frame in ds9"""
    import lsst.afw.geom as afwGeom
    import lsst.afw.display.ds9 as ds9
    xy0 = afwGeom.Extent2D(0, 0) if xy0 is None else afwGeom.Extent2D(xy0)
    with ds9.Buffering():
        for p1, p2 in self.getEdges():
            ds9.line((p1 - xy0, p2 - xy0), frame=frame, ctype=ctype)
Ejemplo n.º 4
0
    def displayDipoles(self, exposure, sources):
        """!Display debugging information on the detected dipoles

        @param exposure  Image the dipoles were measured on
        @param sources   The set of diaSources that were measured"""

        import lsstDebug
        display = lsstDebug.Info(__name__).display
        displayDiaSources = lsstDebug.Info(__name__).displayDiaSources
        maskTransparency = lsstDebug.Info(__name__).maskTransparency
        if not maskTransparency:
            maskTransparency = 90
        ds9.setMaskTransparency(maskTransparency)
        ds9.mtv(exposure, frame=lsstDebug.frame)

        if display and displayDiaSources:
            with ds9.Buffering():
                for source in sources:
                    cenX, cenY = source.get("ipdiffim_DipolePsfFlux_centroid")
                    if np.isinf(cenX) or np.isinf(cenY):
                        cenX, cenY = source.getCentroid()

                    isdipole = source.get("classification.dipole")
                    if isdipole and np.isfinite(isdipole):
                        # Dipole
                        ctype = "green"
                    else:
                        # Not dipole
                        ctype = "red"

                    ds9.dot("o",
                            cenX,
                            cenY,
                            size=2,
                            ctype=ctype,
                            frame=lsstDebug.frame)

                    negCenX = source.get(
                        "ip_diffim_PsfDipoleFlux_neg_centroid_x")
                    negCenY = source.get(
                        "ip_diffim_PsfDipoleFlux_neg_centroid_y")
                    posCenX = source.get(
                        "ip_diffim_PsfDipoleFlux_pos_centroid_x")
                    posCenY = source.get(
                        "ip_diffim_PsfDipoleFlux_pos_centroid_y")
                    if (np.isinf(negCenX) or np.isinf(negCenY)
                            or np.isinf(posCenX) or np.isinf(posCenY)):
                        continue

                    ds9.line([(negCenX, negCenY), (posCenX, posCenY)],
                             ctype="yellow",
                             frame=lsstDebug.frame)

            lsstDebug.frame += 1
Ejemplo n.º 5
0
    def testDrawing(self):
        """Test drawing lines and glyphs"""
        ds9.erase()

        exp = afwImage.ExposureF(300, 350)
        ds9.mtv(exp, title="parent") # tells display0 about the image's xy0
        
        with ds9.Buffering():
            ds9.dot('o', 200, 220)
            vertices = [(200, 220), (210, 230), (224, 230), (214, 220), (200, 220)]
            ds9.line(vertices, ctype=ds9.CYAN)
            ds9.line(vertices[:-1], symbs="+x+x", size=3)
Ejemplo n.º 6
0
    def testDrawing(self):
        """Test drawing lines and glyphs"""
        ds9.erase()

        exp = afwImage.ExposureF(300, 350)
        ds9.mtv(exp, title="parent")  # tells display0 about the image's xy0

        with ds9.Buffering():
            ds9.dot('o', 200, 220)
            vertices = [(200, 220), (210, 230), (224, 230), (214, 220), (200, 220)]
            ds9.line(vertices, ctype=ds9.CYAN)
            ds9.line(vertices[:-1], symbs="+x+x", size=3)
Ejemplo n.º 7
0
 def testImage(self):
     """Test Polygon.createImage"""
     for i, num in enumerate(range(3, 30)):
         poly = self.polygon(num, 25, 75, 75)
         box = afwGeom.Box2I(afwGeom.Point2I(15, 15), afwGeom.Extent2I(115, 115))
         image = poly.createImage(box)
         if DEBUG:
             import lsst.afw.display.ds9 as ds9
             ds9.mtv(image, frame=i+1, title="Polygon nside=%d" % num)
             for p1, p2 in poly.getEdges():
                 ds9.line((p1, p2), frame=i+1)
         self.assertAlmostEqual(image.getArray().sum()/poly.calculateArea(), 1.0, 6)
Ejemplo n.º 8
0
def getBackground(image, backgroundConfig, nx=0, ny=0, algorithm=None):
    """
    Make a new Exposure which is exposure - background
    """
    backgroundConfig.validate()

    if not nx:
        nx = image.getWidth() // backgroundConfig.binSize + 1
    if not ny:
        ny = image.getHeight() // backgroundConfig.binSize + 1

    displayBackground = lsstDebug.Info(__name__).displayBackground
    if displayBackground:
        import itertools
        ds9.mtv(image, frame=1)
        xPosts = numpy.rint(
            numpy.linspace(0, image.getWidth() + 1, num=nx, endpoint=True))
        yPosts = numpy.rint(
            numpy.linspace(0, image.getHeight() + 1, num=ny, endpoint=True))
        with ds9.Buffering():
            for (xMin, xMax), (yMin, yMax) in itertools.product(
                    zip(xPosts[:-1], xPosts[1:]), zip(yPosts[:-1],
                                                      yPosts[1:])):
                ds9.line([(xMin, yMin), (xMin, yMax), (xMax, yMax),
                          (xMax, yMin), (xMin, yMin)],
                         frame=1)

    sctrl = afwMath.StatisticsControl()
    sctrl.setAndMask(
        reduce(lambda x, y: x | image.getMask().getPlaneBitMask(y),
               backgroundConfig.ignoredPixelMask, 0x0))
    sctrl.setNanSafe(backgroundConfig.isNanSafe)

    pl = pexLogging.Debug("meas.utils.sourceDetection.getBackground")
    pl.debug(
        3, "Ignoring mask planes: %s" %
        ", ".join(backgroundConfig.ignoredPixelMask))

    if not algorithm:
        algorithm = backgroundConfig.algorithm

    bctrl = afwMath.BackgroundControl(algorithm, nx, ny,
                                      backgroundConfig.undersampleStyle, sctrl,
                                      backgroundConfig.statisticsProperty)

    if backgroundConfig.useApprox:
        actrl = afwMath.ApproximateControl(
            afwMath.ApproximateControl.CHEBYSHEV, backgroundConfig.approxOrder)
        bctrl.setApproximateControl(actrl)

    return afwMath.makeBackground(image, bctrl)
Ejemplo n.º 9
0
 def testImage(self):
     """Test Polygon.createImage"""
     for i, num in enumerate(range(3, 30)):
         poly = self.polygon(num, 25, 75, 75)
         box = afwGeom.Box2I(afwGeom.Point2I(15, 15),
                             afwGeom.Extent2I(115, 115))
         image = poly.createImage(box)
         if DEBUG:
             import lsst.afw.display.ds9 as ds9
             ds9.mtv(image, frame=i + 1, title="Polygon nside=%d" % num)
             for p1, p2 in poly.getEdges():
                 ds9.line((p1, p2), frame=i + 1)
         self.assertAlmostEqual(
             image.getArray().sum() / poly.calculateArea(), 1.0, 6)
Ejemplo n.º 10
0
    def setUp(self):
        im = afwImage.ImageF(self.monetFile("small.fits"))
        self.mi = afwImage.MaskedImageF(im, afwImage.Mask(im.getDimensions()),
                                        afwImage.ImageF(im.getDimensions()))
        self.ds = afwDetection.FootprintSet(self.mi,
                                            afwDetection.Threshold(100))

        if display:
            ds9.mtv(self.mi.getImage())
            ds9.erase()

        for foot in self.ds.getFootprints():
            bbox = foot.getBBox()
            x0, y0 = bbox.getMinX(), bbox.getMinY()
            x1, y1 = bbox.getMaxX(), bbox.getMaxY()
            xc = (x0 + x1) / 2.0
            yc = (y0 + y1) / 2.0

            if display:
                ds9.dot("+", xc, yc, ctype=ds9.BLUE)

                if False:
                    x0 -= 0.5
                    y0 -= 0.5
                    x1 += 0.5
                    y1 += 0.5

                    ds9.line([(x0, y0), (x1, y0), (x1, y1), (x0, y1),
                              (x0, y0)],
                             ctype=ds9.RED)
        msConfig = measBase.SingleFrameMeasurementConfig()
        msConfig.algorithms.names = ["base_GaussianCentroid"]
        msConfig.plugins["base_GaussianCentroid"].doFootprintCheck = False
        msConfig.slots.centroid = "base_GaussianCentroid"
        msConfig.slots.shape = None
        msConfig.slots.apFlux = None
        msConfig.slots.modelFlux = None
        msConfig.slots.psfFlux = None
        msConfig.slots.instFlux = None
        msConfig.slots.calibFlux = None
        schema = afwTable.SourceTable.makeMinimalSchema()
        self.task = measBase.SingleFrameMeasurementTask(schema,
                                                        config=msConfig)
        self.ssMeasured = afwTable.SourceCatalog(schema)
        self.ssMeasured.table.defineCentroid("base_GaussianCentroid")
        self.ssTruth = afwTable.SourceCatalog(schema)
        self.ssTruth.table.defineCentroid("base_GaussianCentroid")

        self.readTruth(self.monetFile("positions.dat-original"))
    def measureExposure(self, exposure, positions, title="Fringe"):
        """Measure fringe amplitudes for an exposure

        The fringe amplitudes are measured as the statistic within a square
        aperture.  The statistic within a larger aperture are subtracted so
        as to remove the background.

        @param exposure    Exposure to measure
        @param positions   Array of (x,y) for fringe measurement
        @param title       Title for display
        @return Array of fringe measurements
        """
        stats = afwMath.StatisticsControl()
        stats.setNumSigmaClip(self.config.stats.clip)
        stats.setNumIter(self.config.stats.iterations)
        stats.setAndMask(exposure.getMaskedImage().getMask().getPlaneBitMask(
            self.config.stats.badMaskPlanes))

        num = self.config.num
        fringes = numpy.ndarray(num)

        for i in range(num):
            x, y = positions[i]
            small = measure(exposure.getMaskedImage(), x, y, self.config.small,
                            self.config.stats.stat, stats)
            large = measure(exposure.getMaskedImage(), x, y, self.config.large,
                            self.config.stats.stat, stats)
            fringes[i] = small - large

        import lsstDebug
        display = lsstDebug.Info(__name__).display
        if display:
            frame = getFrame()
            ds9.mtv(exposure, frame=frame, title=title)
            if False:
                with ds9.Buffering():
                    for x, y in positions:
                        corners = numpy.array([[-1, -1], [1, -1], [1, 1],
                                               [-1, 1], [-1, -1]]) + [[x, y]]
                        ds9.line(corners * self.config.small,
                                 frame=frame,
                                 ctype="green")
                        ds9.line(corners * self.config.large,
                                 frame=frame,
                                 ctype="blue")

        return fringes
Ejemplo n.º 12
0
    def testFootprintToBBoxList(self):
        """Test footprintToBBoxList"""
        region = afwGeom.Box2I(afwGeom.Point2I(0, 0), afwGeom.Extent2I(12, 10))
        foot = afwDetect.Footprint(0, region)
        for y, x0, x1 in [
            (3, 3, 5),
            (3, 7, 7),
            (4, 2, 3),
            (4, 5, 7),
            (5, 2, 3),
            (5, 5, 8),
            (6, 3, 5),
        ]:
            foot.addSpan(y, x0, x1)

        idImage = afwImage.ImageU(region.getDimensions())
        idImage.set(0)

        foot.insertIntoImage(idImage, 1)
        if display:
            ds9.mtv(idImage)

        idImageFromBBox = idImage.Factory(idImage, True)
        idImageFromBBox.set(0)
        bboxes = afwDetect.footprintToBBoxList(foot)
        for bbox in bboxes:
            x0, y0, x1, y1 = bbox.getMinX(), bbox.getMinY(), bbox.getMaxX(
            ), bbox.getMaxY()

            for y in range(y0, y1 + 1):
                for x in range(x0, x1 + 1):
                    idImageFromBBox.set(x, y, 1)

            if display:
                x0 -= 0.5
                y0 -= 0.5
                x1 += 0.5
                y1 += 0.5

                ds9.line([(x0, y0), (x1, y0), (x1, y1), (x0, y1), (x0, y0)],
                         ctype=ds9.RED)

        idImageFromBBox -= idImage  # should be blank
        stats = afwMath.makeStatistics(idImageFromBBox, afwMath.MAX)

        self.assertEqual(stats.getValue(), 0)
Ejemplo n.º 13
0
    def testCandidateList(self):
        if False and display:
            ds9.mtv(self.mi)

            for cell in self.cellSet.getCellList():
                x0, y0, x1, y1 = (cell.getBBox().getX0(),
                                  cell.getBBox().getY0(),
                                  cell.getBBox().getX1(),
                                  cell.getBBox().getY1())
                print((x0, y0, " ", x1, y1))
                x0 -= 0.5
                y0 -= 0.5
                x1 += 0.5
                y1 += 0.5

                ds9.line([(x0, y0), (x1, y0), (x1, y1), (x0, y1), (x0, y0)],
                         ctype=ds9.RED)

        self.assertFalse(self.cellSet.getCellList()[0].empty())
        self.assertTrue(self.cellSet.getCellList()[1].empty())
        self.assertFalse(self.cellSet.getCellList()[2].empty())

        stamps = []
        for cell in self.cellSet.getCellList():
            for cand in cell:
                #
                # Swig doesn't know that we PsfCandidate;  all it knows is
                # that we have a SpatialCellCandidate, and
                # SpatialCellCandidates don't know about getMaskedImage;  so
                # cast the pointer to PsfCandidate<float> and all will be well
                #
                cand = algorithms.PsfCandidateF.cast(cell[0])
                width, height = 15, 17
                cand.setWidth(width)
                cand.setHeight(height)

                im = cand.getMaskedImage()
                stamps.append(im)

                self.assertEqual(im.getWidth(), width)
                self.assertEqual(im.getHeight(), height)

        if display:
            mos = displayUtils.Mosaic()
            ds9.mtv(mos.makeMosaic(stamps), frame=1)
Ejemplo n.º 14
0
def getBackground(image, backgroundConfig, nx=0, ny=0, algorithm=None):
    """
    Make a new Exposure which is exposure - background
    """
    backgroundConfig.validate();

    if not nx:
        nx = image.getWidth()//backgroundConfig.binSize + 1
    if not ny:
        ny = image.getHeight()//backgroundConfig.binSize + 1

    displayBackground = lsstDebug.Info(__name__).displayBackground
    if displayBackground:
        import itertools
        ds9.mtv(image, frame=1)
        xPosts = numpy.rint(numpy.linspace(0, image.getWidth() + 1, num=nx, endpoint=True))
        yPosts = numpy.rint(numpy.linspace(0, image.getHeight() + 1, num=ny, endpoint=True))
        with ds9.Buffering():
            for (xMin, xMax), (yMin, yMax) in itertools.product(zip(xPosts[:-1], xPosts[1:]),
                                                                zip(yPosts[:-1], yPosts[1:])):
                ds9.line([(xMin, yMin), (xMin, yMax), (xMax, yMax), (xMax, yMin), (xMin, yMin)], frame=1)


    sctrl = afwMath.StatisticsControl()
    sctrl.setAndMask(reduce(lambda x, y: x | image.getMask().getPlaneBitMask(y),
                            backgroundConfig.ignoredPixelMask, 0x0))
    sctrl.setNanSafe(backgroundConfig.isNanSafe)

    pl = pexLogging.Debug("meas.utils.sourceDetection.getBackground")
    pl.debug(3, "Ignoring mask planes: %s" % ", ".join(backgroundConfig.ignoredPixelMask))

    if not algorithm:
        algorithm = backgroundConfig.algorithm
        
    bctrl = afwMath.BackgroundControl(algorithm, nx, ny,
                                      backgroundConfig.undersampleStyle, sctrl,
                                      backgroundConfig.statisticsProperty)

    if backgroundConfig.useApprox:
        actrl = afwMath.ApproximateControl(afwMath.ApproximateControl.CHEBYSHEV, backgroundConfig.approxOrder)
        bctrl.setApproximateControl(actrl)

    return afwMath.makeBackground(image, bctrl)
Ejemplo n.º 15
0
def drawFootprint(foot, borderWidth=0.5, origin=None, XY0=None, frame=None, ctype=None, bin=1,
                  peaks=False, symb="+", size=0.4, ctypePeak=None):
    """Draw an afwDetection::Footprint on a ds9 frame with the specified ctype.  Include an extra borderWidth
pixels If origin is present, it's Added to the Footprint; if XY0 is present is Subtracted from the Footprint

If peaks is True, also show the object's Peaks using the specified symbol and size and ctypePeak

All Footprint coordinates are divided by bin, as is right and proper for overlaying on a binned image
    """

    if XY0:
        if origin:
            raise RuntimeError("You may not specify both origin and XY0")
        origin = (-XY0[0], -XY0[1])

    with ds9.Buffering():
        borderWidth /= bin
        for s in foot.getSpans():
            y, x0, x1 = s.getY(), s.getX0(), s.getX1()

            if origin:
                x0 += origin[0]; x1 += origin[0]
                y += origin[1]

            x0 /= bin; x1 /= bin; y /= bin

            ds9.line([(x0 - borderWidth, y - borderWidth),
                      (x0 - borderWidth, y + borderWidth),
                      (x1 + borderWidth, y + borderWidth),
                      (x1 + borderWidth, y - borderWidth),
                      (x0 - borderWidth, y - borderWidth),
                      ], frame=frame, ctype=ctype)

        if peaks:
            for p in foot.getPeaks():
                x, y = p.getIx(), p.getIy()

                if origin:
                    x += origin[0]; y += origin[1]

                x /= bin; y /= bin

                ds9.dot(symb, x, y, size=size, ctype=ctypePeak, frame=frame)
Ejemplo n.º 16
0
    def testCandidateList(self):
        if False and display:
            ds9.mtv(self.mi)

            for cell in self.cellSet.getCellList():
                x0, y0, x1, y1 = (
                    cell.getBBox().getX0(), cell.getBBox().getY0(),
                    cell.getBBox().getX1(), cell.getBBox().getY1())
                print x0, y0, " ", x1, y1
                x0 -= 0.5
                y0 -= 0.5
                x1 += 0.5
                y1 += 0.5

                ds9.line([(x0, y0), (x1, y0), (x1, y1), (x0, y1), (x0, y0)], ctype=ds9.RED)

        self.assertFalse(self.cellSet.getCellList()[0].empty())
        self.assertTrue(self.cellSet.getCellList()[1].empty())
        self.assertFalse(self.cellSet.getCellList()[2].empty())

        stamps = []
        for cell in self.cellSet.getCellList():
            for cand in cell:
                #
                # Swig doesn't know that we inherited from SpatialCellMaskedImageCandidate;  all
                # it knows is that we have a SpatialCellCandidate, and SpatialCellCandidates
                # don't know about getMaskedImage;  so cast the pointer to
                # SpatialCellMaskedImageCandidate<float> and all will be well
                #
                cand = afwMath.cast_SpatialCellMaskedImageCandidateF(cell[0])
                width, height = 15, 17
                cand.setWidth(width)
                cand.setHeight(height)

                im = cand.getMaskedImage()
                stamps.append(im)

                self.assertEqual(im.getWidth(), width)
                self.assertEqual(im.getHeight(), height)

        if display:
            mos = displayUtils.Mosaic()
            ds9.mtv(mos.makeMosaic(stamps), frame=1)
Ejemplo n.º 17
0
def drawCoaddInputs(exposure, frame=None, ctype=None, bin=1):
    """Draw the bounding boxes of input exposures to a coadd on a ds9 frame with the specified ctype,
    assuming ds9.mtv() has already been called on the given exposure on this frame.


    All coordinates are divided by bin, as is right and proper for overlaying on a binned image
    """
    coaddWcs = exposure.getWcs()
    catalog = exposure.getInfo().getCoaddInputs().ccds

    offset = afwGeom.Point2D() - afwGeom.Point2D(exposure.getXY0())
    with ds9.Buffering():
        for record in catalog:
            ccdBox = afwGeom.Box2D(record.getBBox())
            ccdCorners = ccdBox.getCorners()
            coaddCorners = [coaddWcs.skyToPixel(record.getWcs().pixelToSky(point)) + offset
                            for point in ccdCorners]
            ds9.line([(coaddCorners[i].getX() / bin, coaddCorners[i].getY() / bin)
                      for i in range(-1, 4)], frame=frame, ctype=ctype)
Ejemplo n.º 18
0
def drawFootprint(foot, borderWidth=0.5, origin=None, XY0=None, frame=None, ctype=None, bin=1,
                  peaks=False, symb="+", size=0.4, ctypePeak=None):
    """Draw an afwDetection::Footprint on a ds9 frame with the specified ctype.  Include an extra borderWidth
pixels If origin is present, it's Added to the Footprint; if XY0 is present is Subtracted from the Footprint

If peaks is True, also show the object's Peaks using the specified symbol and size and ctypePeak

All Footprint coordinates are divided by bin, as is right and proper for overlaying on a binned image
    """

    if XY0:
        if origin:
            raise RuntimeError("You may not specify both origin and XY0")
        origin = (-XY0[0], -XY0[1])

    with ds9.Buffering():
        borderWidth /= bin
        for s in foot.getSpans():
            y, x0, x1 = s.getY(), s.getX0(), s.getX1()

            if origin:
                x0 += origin[0]; x1 += origin[0]
                y += origin[1]

            x0 /= bin; x1 /= bin; y /= bin

            ds9.line([(x0 - borderWidth, y - borderWidth),
                      (x0 - borderWidth, y + borderWidth),
                      (x1 + borderWidth, y + borderWidth),
                      (x1 + borderWidth, y - borderWidth),
                      (x0 - borderWidth, y - borderWidth),
                      ], frame=frame, ctype=ctype)

        if peaks:
            for p in foot.getPeaks():
                x, y = p.getIx(), p.getIy()

                if origin:
                    x += origin[0]; y += origin[1]

                x /= bin; y /= bin

                ds9.dot(symb, x, y, size=size, ctype=ctypePeak, frame=frame)
Ejemplo n.º 19
0
    def setUp(self):
	im = afwImage.ImageF(self.monetFile("small.fits"))
        self.mi = afwImage.MaskedImageF(im, afwImage.MaskU(im.getDimensions()),
                                        afwImage.ImageF(im.getDimensions()));
        self.ds = afwDetection.FootprintSet(self.mi, afwDetection.Threshold(100))

        if display:
            ds9.mtv(self.mi.getImage())
            ds9.erase()

        for foot in self.ds.getFootprints():
            bbox = foot.getBBox()
            x0, y0 = bbox.getMinX(), bbox.getMinY()
            x1, y1 = bbox.getMaxX(), bbox.getMaxY()
            xc = (x0 + x1)/2.0
            yc = (y0 + y1)/2.0

            if display:
                ds9.dot("+", xc, yc, ctype=ds9.BLUE)

                if False:
                    x0 -= 0.5; y0 -= 0.5
                    x1 += 0.5; y1 += 0.5

                    ds9.line([(x0, y0), (x1, y0), (x1, y1), (x0, y1), (x0, y0)], ctype=ds9.RED)
        msConfig = measBase.SingleFrameMeasurementConfig()
        msConfig.algorithms.names = ["base_GaussianCentroid"]
        msConfig.slots.centroid = "base_GaussianCentroid"
        msConfig.slots.shape = None
        msConfig.slots.apFlux = None
        msConfig.slots.modelFlux = None
        msConfig.slots.psfFlux = None
        msConfig.slots.instFlux = None
        msConfig.slots.calibFlux = None
        schema = afwTable.SourceTable.makeMinimalSchema()
        self.task = measBase.SingleFrameMeasurementTask(schema, config=msConfig)
        self.ssMeasured = afwTable.SourceCatalog(schema)
        self.ssMeasured.table.defineCentroid("base_GaussianCentroid")
        self.ssTruth = afwTable.SourceCatalog(schema)
        self.ssTruth.table.defineCentroid("base_GaussianCentroid")

        self.readTruth(self.monetFile("positions.dat-original"))
Ejemplo n.º 20
0
    def displayDipoles(self, exposure, sources):
        """!Display debugging information on the detected dipoles

        @param exposure  Image the dipoles were measured on
        @param sources   The set of diaSources that were measured"""

        import lsstDebug
        display = lsstDebug.Info(__name__).display
        displayDiaSources = lsstDebug.Info(__name__).displayDiaSources
        maskTransparency = lsstDebug.Info(__name__).maskTransparency
        if not maskTransparency:
            maskTransparency = 90
        ds9.setMaskTransparency(maskTransparency)
        ds9.mtv(exposure, frame=lsstDebug.frame)

        if display and displayDiaSources:
            with ds9.Buffering():
                for source in sources:
                    cenX, cenY = source.get("ipdiffim_DipolePsfFlux_centroid")
                    if np.isinf(cenX) or np.isinf(cenY):
                        cenX, cenY = source.getCentroid()

                    isdipole = source.get("classification.dipole")
                    if isdipole and np.isfinite(isdipole):
                        # Dipole
                        ctype= "green"
                    else:
                        # Not dipole
                        ctype = "red"

                    ds9.dot("o", cenX, cenY, size=2, ctype=ctype, frame=lsstDebug.frame)

                    negCenX = source.get("ip_diffim_PsfDipoleFlux_neg_centroid_x")
                    negCenY = source.get("ip_diffim_PsfDipoleFlux_neg_centroid_y")
                    posCenX = source.get("ip_diffim_PsfDipoleFlux_pos_centroid_x")
                    posCenY = source.get("ip_diffim_PsfDipoleFlux_pos_centroid_y")
                    if (np.isinf(negCenX) or np.isinf(negCenY) or np.isinf(posCenX) or np.isinf(posCenY)):
                        continue

                    ds9.line([(negCenX, negCenY), (posCenX, posCenY)], ctype="yellow", frame=lsstDebug.frame)

            lsstDebug.frame += 1
Ejemplo n.º 21
0
    def testFootprintToBBoxList(self):
        """Test footprintToBBoxList"""
        region = afwGeom.Box2I(afwGeom.Point2I(0,0), afwGeom.Extent2I(12,10))
        foot = afwDetect.Footprint(0, region)
        for y, x0, x1 in [(3, 3, 5), (3, 7, 7),
                          (4, 2, 3), (4, 5, 7),
                          (5, 2, 3), (5, 5, 8),
                          (6, 3, 5),
                          ]:
            foot.addSpan(y, x0, x1)

        idImage = afwImage.ImageU(region.getDimensions())
        idImage.set(0)

        foot.insertIntoImage(idImage, 1)
        if display:
            ds9.mtv(idImage)

        idImageFromBBox = idImage.Factory(idImage, True)
        idImageFromBBox.set(0)
        bboxes = afwDetect.footprintToBBoxList(foot)
        for bbox in bboxes:
            x0, y0, x1, y1 = bbox.getMinX(), bbox.getMinY(), bbox.getMaxX(), bbox.getMaxY()

            for y in range(y0, y1 + 1):
                for x in range(x0, x1 + 1):
                    idImageFromBBox.set(x, y, 1)

            if display:
                x0 -= 0.5
                y0 -= 0.5
                x1 += 0.5
                y1 += 0.5

                ds9.line([(x0, y0), (x1, y0), (x1, y1), (x0, y1), (x0, y0)], ctype=ds9.RED)

        idImageFromBBox -= idImage      # should be blank
        stats = afwMath.makeStatistics(idImageFromBBox, afwMath.MAX)

        self.assertEqual(stats.getValue(), 0)
Ejemplo n.º 22
0
    def testCandidateList(self):
        if False and display:
            ds9.mtv(self.mi)

            for cell in self.cellSet.getCellList():
                x0, y0, x1, y1 = (cell.getBBox().getX0(),
                                  cell.getBBox().getY0(),
                                  cell.getBBox().getX1(),
                                  cell.getBBox().getY1())
                print((x0, y0, " ", x1, y1))
                x0 -= 0.5
                y0 -= 0.5
                x1 += 0.5
                y1 += 0.5

                ds9.line([(x0, y0), (x1, y0), (x1, y1), (x0, y1), (x0, y0)],
                         ctype=ds9.RED)

        self.assertFalse(self.cellSet.getCellList()[0].empty())
        self.assertTrue(self.cellSet.getCellList()[1].empty())
        self.assertFalse(self.cellSet.getCellList()[2].empty())

        stamps = []
        for cell in self.cellSet.getCellList():
            for cand in cell:
                cand = cell[0]
                width, height = 15, 17
                cand.setWidth(width)
                cand.setHeight(height)

                im = cand.getMaskedImage()
                stamps.append(im)

                self.assertEqual(im.getWidth(), width)
                self.assertEqual(im.getHeight(), height)

        if display:
            mos = displayUtils.Mosaic()
            ds9.mtv(mos.makeMosaic(stamps), frame=1)
Ejemplo n.º 23
0
    def measureExposure(self, exposure, positions, title="Fringe"):
        """Measure fringe amplitudes for an exposure

        The fringe amplitudes are measured as the statistic within a square
        aperture.  The statistic within a larger aperture are subtracted so
        as to remove the background.

        @param exposure    Exposure to measure
        @param positions   Array of (x,y) for fringe measurement
        @param title       Title for display
        @return Array of fringe measurements
        """
        stats = afwMath.StatisticsControl()
        stats.setNumSigmaClip(self.config.stats.clip)
        stats.setNumIter(self.config.stats.iterations)
        stats.setAndMask(exposure.getMaskedImage().getMask().getPlaneBitMask(self.config.stats.badMaskPlanes))

        num = self.config.num
        fringes = numpy.ndarray(num)
        
        for i in range(num):
            x, y = positions[i]
            small = measure(exposure.getMaskedImage(), x, y, self.config.small, self.config.stats.stat, stats)
            large = measure(exposure.getMaskedImage(), x, y, self.config.large, self.config.stats.stat, stats)
            fringes[i] = small - large

        import lsstDebug
        display = lsstDebug.Info(__name__).display
        if display:
            frame = getFrame()
            ds9.mtv(exposure, frame=frame, title=title)
            if False:
                with ds9.Buffering():
                    for x,y in positions:
                        corners = numpy.array([[-1, -1], [ 1, -1], [ 1,  1], [-1,  1], [-1, -1]]) + [[x, y]]
                        ds9.line(corners * self.config.small, frame=frame, ctype="green")
                        ds9.line(corners * self.config.large, frame=frame, ctype="blue")

        return fringes
Ejemplo n.º 24
0
    def setUp(self):
        im = afwImage.ImageF(self.monetFile("small.fits"))
        self.mi = afwImage.MaskedImageF(im, afwImage.MaskU(im.getDimensions()),
                                        afwImage.ImageF(im.getDimensions()))
        self.ds = afwDetection.FootprintSet(self.mi,
                                            afwDetection.Threshold(100))

        if display:
            ds9.mtv(self.mi.getImage())
            ds9.erase()

        for foot in self.ds.getFootprints():
            bbox = foot.getBBox()
            x0, y0 = bbox.getMinX(), bbox.getMinY()
            x1, y1 = bbox.getMaxX(), bbox.getMaxY()
            xc = (x0 + x1) / 2.0
            yc = (y0 + y1) / 2.0

            if display:
                ds9.dot("+", xc, yc, ctype=ds9.BLUE)

                if False:
                    x0 -= 0.5
                    y0 -= 0.5
                    x1 += 0.5
                    y1 += 0.5

                    ds9.line([(x0, y0), (x1, y0), (x1, y1), (x0, y1),
                              (x0, y0)],
                             ctype=ds9.RED)

        self.control = algorithms.GaussianCentroidControl()
        schema = afwTable.SourceTable.makeMinimalSchema()
        self.centroider = algorithms.MeasureSourcesBuilder().addAlgorithm(
            self.control).build(schema)
        self.ssMeasured = afwTable.SourceCatalog(schema)
        self.ssMeasured.table.defineCentroid(self.control.name)
        self.ssTruth = afwTable.SourceCatalog(schema)
        self.readTruth(self.monetFile("positions.dat-original"))
Ejemplo n.º 25
0
def drawBBox(bbox, borderWidth=0.0, origin=None, frame=None, ctype=None, bin=1):
    """Draw an afwImage::BBox on a ds9 frame with the specified ctype.  Include an extra borderWidth pixels
If origin is present, it's Added to the BBox

All BBox coordinates are divided by bin, as is right and proper for overlaying on a binned image
    """
    x0, y0 = bbox.getMinX(), bbox.getMinY()
    x1, y1 = bbox.getMaxX(), bbox.getMaxY()

    if origin:
        x0 += origin[0]; x1 += origin[0]
        y0 += origin[1]; y1 += origin[1]

    x0 /= bin; y0 /= bin
    x1 /= bin; y1 /= bin
    borderWidth /= bin
    
    ds9.line([(x0 - borderWidth, y0 - borderWidth),
              (x0 - borderWidth, y1 + borderWidth),
              (x1 + borderWidth, y1 + borderWidth),
              (x1 + borderWidth, y0 - borderWidth),
              (x0 - borderWidth, y0 - borderWidth),
              ], frame=frame, ctype=ctype)
Ejemplo n.º 26
0
def drawBBox(bbox, borderWidth=0.0, origin=None, frame=None, ctype=None, bin=1):
    """Draw an afwImage::BBox on a ds9 frame with the specified ctype.  Include an extra borderWidth pixels
If origin is present, it's Added to the BBox

All BBox coordinates are divided by bin, as is right and proper for overlaying on a binned image
    """
    x0, y0 = bbox.getMinX(), bbox.getMinY()
    x1, y1 = bbox.getMaxX(), bbox.getMaxY()

    if origin:
        x0 += origin[0]; x1 += origin[0]
        y0 += origin[1]; y1 += origin[1]

    x0 /= bin; y0 /= bin
    x1 /= bin; y1 /= bin
    borderWidth /= bin
    
    ds9.line([(x0 - borderWidth, y0 - borderWidth),
              (x0 - borderWidth, y1 + borderWidth),
              (x1 + borderWidth, y1 + borderWidth),
              (x1 + borderWidth, y0 - borderWidth),
              (x0 - borderWidth, y0 - borderWidth),
              ], frame=frame, ctype=ctype)
Ejemplo n.º 27
0
    size = 1.6                          # half-size of box to draw

    with ds9.Buffering():
        for ev, success in zip(events, status):
            if not success:
                if display and (ev.grade == -1 and displayUnknown or ev.grade >= 0 and displayRejects):
                    ds9.dot("+", ev.x, ev.y, size=0.5, ctype=ctypes[ev.grade])
                    if displayGrades:
                        ds9.dot(str(ev.grade), ev.x + 1.5, ev.y, frame=0, ctype=ctypes[ev.grade])
                    
                continue

            if display:
                ds9.line([(ev.x - size, ev.y - size),
                          (ev.x + size, ev.y - size),
                          (ev.x + size, ev.y + size),
                          (ev.x - size, ev.y + size),
                          (ev.x - size, ev.y - size)], frame=0, ctype=ctypes[ev.grade])
                if displayGrades:
                    ds9.dot(str(ev.grade), ev.x + size + 1, ev.y - size, frame=0, ctype=ctypes[ev.grade])

    if outputHistFile:
        with open(outputHistFile, "w") as fd:
            table.dump_head(fd, "unknown", sum(status))
            table.dump_hist(fd)
    #table.dump_table()

    if plot:
        fe55.plot_hist({None : table}, title="Event = %g Split = %g Source = %s" % (thresh, split, "unknown"),
                       subplots=subplots)
Ejemplo n.º 28
0
    def testDetection(self):
        """Test CR detection."""
        #
        # Subtract background
        #
        bctrl = afwMath.BackgroundControl(afwMath.Interpolate.NATURAL_SPLINE)
        bctrl.setNxSample(int(self.mi.getWidth() / 256) + 1)
        bctrl.setNySample(int(self.mi.getHeight() / 256) + 1)
        bctrl.getStatisticsControl().setNumSigmaClip(3.0)
        bctrl.getStatisticsControl().setNumIter(2)

        im = self.mi.getImage()
        try:
            backobj = afwMath.makeBackground(im, bctrl)
        except Exception as e:
            print >> sys.stderr, e,

            bctrl.setInterpStyle(afwMath.Interpolate.CONSTANT)
            backobj = afwMath.makeBackground(im, bctrl)

        im -= backobj.getImageF()

        if display:
            frame = 0
            ds9.mtv(self.mi, frame=frame, title="Raw")  # raw frame
            if self.mi.getWidth() > 256:
                ds9.pan(944 - self.mi.getX0(), 260 - self.mi.getY0())
        #
        # Mask known bad pixels
        #
        measAlgorithmsDir = lsst.utils.getPackageDir('meas_algorithms')
        badPixels = defects.policyToBadRegionList(
            os.path.join(measAlgorithmsDir, "policy", "BadPixels.paf"))
        # did someone lie about the origin of the maskedImage?  If so, adjust bad pixel list
        if self.XY0.getX() != self.mi.getX0() or self.XY0.getY(
        ) != self.mi.getY0():
            dx = self.XY0.getX() - self.mi.getX0()
            dy = self.XY0.getY() - self.mi.getY0()
            for bp in badPixels:
                bp.shift(-dx, -dy)

        algorithms.interpolateOverDefects(self.mi, self.psf, badPixels)

        stats = afwMath.makeStatistics(self.mi.getImage(),
                                       afwMath.MEANCLIP | afwMath.STDEVCLIP)
        background = stats.getValue(afwMath.MEANCLIP)

        crConfig = algorithms.FindCosmicRaysConfig()
        crs = algorithms.findCosmicRays(self.mi, self.psf, background,
                                        pexConfig.makePolicy(crConfig))

        if display:
            ds9.mtv(self.mi, frame=frame + 1, title="CRs removed")
            if self.mi.getWidth() > 256:
                ds9.pan(944 - self.mi.getX0(), 260 - self.mi.getY0())

        print("Detected %d CRs" % len(crs))
        if display and False:
            for cr in crs:
                bbox = cr.getBBox()
                bbox.shift(afwGeom.ExtentI(-self.mi.getX0(), -self.mi.getY0()))
                ds9.line([(bbox.getMinX() - 0.5, bbox.getMinY() - 0.5),
                          (bbox.getMaxX() + 0.5, bbox.getMinY() - 0.5),
                          (bbox.getMaxX() + 0.5, bbox.getMaxY() + 0.5),
                          (bbox.getMinX() - 0.5, bbox.getMaxY() + 0.5),
                          (bbox.getMinX() - 0.5, bbox.getMinY() - 0.5)],
                         frame=frame + 1)

        if self.nCR is not None:
            self.assertEqual(len(crs), self.nCR)
def getBackground(image, backgroundConfig, nx=0, ny=0, algorithm=None):
    """!Estimate the background of an image (a thin layer on lsst.afw.math.makeBackground)

    \param[in] image  image whose background is to be computed
    \param[in] backgroundConfig  configuration (a BackgroundConfig)
    \param[in] nx  number of x bands; 0 for default
    \param[in] ny  number of y bands; 0 for default
    \param[in] algorithm  name of interpolation algorithm; see lsst.afw.math.BackgroundControl for details
    """
    backgroundConfig.validate();

    logger = pexLogging.getDefaultLog()
    logger = pexLogging.Log(logger,"lsst.meas.algorithms.detection.getBackground")

    if not nx:
        nx = image.getWidth()//backgroundConfig.binSize + 1
    if not ny:
        ny = image.getHeight()//backgroundConfig.binSize + 1

    displayBackground = lsstDebug.Info(__name__).displayBackground
    if displayBackground:
        import itertools
        ds9.mtv(image, frame=1)
        xPosts = numpy.rint(numpy.linspace(0, image.getWidth() + 1, num=nx, endpoint=True))
        yPosts = numpy.rint(numpy.linspace(0, image.getHeight() + 1, num=ny, endpoint=True))
        with ds9.Buffering():
            for (xMin, xMax), (yMin, yMax) in itertools.product(zip(xPosts[:-1], xPosts[1:]),
                                                                zip(yPosts[:-1], yPosts[1:])):
                ds9.line([(xMin, yMin), (xMin, yMax), (xMax, yMax), (xMax, yMin), (xMin, yMin)], frame=1)


    sctrl = afwMath.StatisticsControl()
    sctrl.setAndMask(reduce(lambda x, y: x | image.getMask().getPlaneBitMask(y),
                            backgroundConfig.ignoredPixelMask, 0x0))
    sctrl.setNanSafe(backgroundConfig.isNanSafe)

    pl = pexLogging.Debug("lsst.meas.algorithms.detection.getBackground")
    pl.debug(3, "Ignoring mask planes: %s" % ", ".join(backgroundConfig.ignoredPixelMask))

    if not algorithm:
        algorithm = backgroundConfig.algorithm

    bctrl = afwMath.BackgroundControl(algorithm, nx, ny,
                                      backgroundConfig.undersampleStyle, sctrl,
                                      backgroundConfig.statisticsProperty)

    # TODO: The following check should really be done within afw/math.  With the
    #       current code structure, it would need to be accounted for in the
    #       doGetImage() funtion in BackgroundMI.cc (which currently only checks
    #       against the interpoation settings which is not appropriate when
    #       useApprox=True) and/or the makeApproximate() function in
    #       afw/Approximate.cc.
    #       See ticket DM-2920: "Clean up code in afw for Approximate background
    #       estimation" (which includes a note to remove the following and the
    #       similar checks in pipe_tasks/matchBackgrounds.py once implemented)
    #
    # Check that config setting of approxOrder/binSize make sense
    # (i.e. ngrid (= shortDimension/binSize) > approxOrderX) and perform
    # appropriate undersampleStlye behavior.
    if backgroundConfig.useApprox:
        if not backgroundConfig.approxOrderY in (backgroundConfig.approxOrderX,-1):
            raise ValueError("Error: approxOrderY not in (approxOrderX, -1)")
        order = backgroundConfig.approxOrderX
        minNumberGridPoints = backgroundConfig.approxOrderX + 1
        if min(nx,ny) <= backgroundConfig.approxOrderX:
            logger.warn("Too few points in grid to constrain fit: min(nx, ny) < approxOrder) "+
                        "[min(%d, %d) < %d]" % (nx, ny, backgroundConfig.approxOrderX))
            if backgroundConfig.undersampleStyle == "THROW_EXCEPTION":
                raise ValueError("Too few points in grid (%d, %d) for order (%d) and binsize (%d)" % (
                        nx, ny, backgroundConfig.approxOrderX, backgroundConfig.binSize))
            elif backgroundConfig.undersampleStyle == "REDUCE_INTERP_ORDER":
                if order < 1:
                    raise ValueError("Cannot reduce approxOrder below 0.  " +
                                     "Try using undersampleStyle = \"INCREASE_NXNYSAMPLE\" instead?")
                order = min(nx, ny) - 1
                logger.warn("Reducing approxOrder to %d" % order)
            elif backgroundConfig.undersampleStyle == "INCREASE_NXNYSAMPLE":
                newBinSize = min(image.getWidth(),image.getHeight())//(minNumberGridPoints-1)
                if newBinSize < 1:
                    raise ValueError("Binsize must be greater than 0")
                newNx = image.getWidth()//newBinSize + 1
                newNy = image.getHeight()//newBinSize + 1
                bctrl.setNxSample(newNx)
                bctrl.setNySample(newNy)
                logger.warn("Decreasing binSize from %d to %d for a grid of (%d, %d)" %
                            (backgroundConfig.binSize, newBinSize, newNx, newNy))

        actrl = afwMath.ApproximateControl(afwMath.ApproximateControl.CHEBYSHEV, order, order,
                                           backgroundConfig.weighting)
        bctrl.setApproximateControl(actrl)

    return afwMath.makeBackground(image, bctrl)
Ejemplo n.º 30
0
def run(ioMgr, frameId, ccdId, config, display=False):

    data = {'visit': frameId, 'ccd':ccdId}

    raws = ioMgr.readRaw(data)[0]
    wcsIn = raws.getWcs()

    exposure = ioMgr.read('calexp', data, ignore=True)[0]
    wcsTrue = exposure.getWcs()
    exposure.setWcs(wcsIn)

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

    bscSet = ioMgr.read('bsc', data, ignore=True)[0].getSources()
    srcSet = [s for s in bscSet if goodStar(s)]
    print "# of srcSet: ", len(srcSet)

    distConfig = config['distortion']
    ccd = pipUtil.getCcd(exposure)
    dist = pipDist.createDistortion(ccd, distConfig)

    distSrc = dist.actualToIdeal(srcSet)

    if display:
        for (s, d) in zip(srcSet, distSrc):
            xs = s.getXAstrom()
            ys = s.getYAstrom()
            xd = d.getXAstrom()
            yd = d.getYAstrom()
            #ds9.dot("o", xs, ys, size=10, frame=1)
            #ds9.dot("o", xd, yd, size=10, frame=1, ctype=ds9.RED)
            #ds9.line([[xs,ys], [xd,yd]], frame=1)

    xMin, xMax, yMin, yMax = float("INF"), float("-INF"), float("INF"), float("-INF")
    for x, y in ((0.0, 0.0), (0.0, exposure.getHeight()), (exposure.getWidth(), 0.0),
                 (exposure.getWidth(), exposure.getHeight())):
        point = afwGeom.Point2D(x, y)
        point = dist.actualToIdeal(point)
        x, y = point.getX(), point.getY()
        if x < xMin: xMin = x
        if x > xMax: xMax = x
        if y < yMin: yMin = y
        if y > yMax: yMax = y
    xMin = int(xMin)
    yMin = int(yMin)
    size = (int(xMax - xMin + 0.5),
            int(yMax - yMin + 0.5))
    for s in distSrc:
        s.setXAstrom(s.getXAstrom() - xMin)
        s.setYAstrom(s.getYAstrom() - yMin)

    sortedDistSrc = sorted(distSrc, key=lambda x:x.getPsfFlux(), reverse=True)

    if display:
        for i, d in enumerate(sortedDistSrc):
            #ds9.dot('o', d.getXAstrom(), d.getYAstrom(), size=15, frame=1)
            if i >= 99: break
        ds9.line([[0, 0], [0+size[0], 0]], frame=1)
        ds9.line([[0+size[0], 0], [0+size[0], 0+size[1]]], frame=1)
        ds9.line([[0+size[0], 0+size[1]], [0, 0+size[1]]], frame=1)
        ds9.line([[0, 0+size[1]], [0, 0]], frame=1)

    log = Log.getDefaultLog()
    pol = policy.Policy()
    pol.set('matchThreshold', 30)
    solver = measAst.createSolver(pol, log)

    (W,H) = size
    filterName = 'r'
    idName = 'id'

    wcsIn.shiftReferencePixel(-xMin, -yMin)

    catSet = hscAst.queryReferenceCatalog(solver, distSrc, wcsIn, (W,H),
                                          filterName, idName)

    print "# of catSet: ", len(catSet)

    if display:
        for c in catSet:
            ds9.dot("o", c.getXAstrom()+xMin, c.getYAstrom()+yMin, size=15, ctype=ds9.RED, frame=1)

    start = datetime.datetime.today()

    matchList = hscAst.match(distSrc, catSet)

    end = datetime.datetime.today()

    wcsIn.shiftReferencePixel(xMin, yMin)

    matchList2 = []
    for m in matchList:
        for s in srcSet:
            if (s.getId() == m.second.getId()):
                x0 = m.first.getXAstrom() + xMin
                y0 = m.first.getYAstrom() + yMin
                x1 = m.second.getXAstrom()
                y1 = m.second.getYAstrom()
                x1 = s.getXAstrom()
                y1 = s.getYAstrom()
                #print x0, y0, x1, y1, m.second.getId()
                if display:
                    ds9.dot("o", x1, y1, size=10, frame=1)
                    ds9.line([[x0, y0], [x1, y1]], ctype=ds9.RED, frame=1)
                m2 = afwDet.SourceMatch(m.first, s, 0.0)
                matchList2.append(m2)

    elapsedtime = end - start

    print "%6d %d %3d %6.2f" % (frameId, ccdId, len(matchList), elapsedtime.seconds + elapsedtime.microseconds/1000000.)
Ejemplo n.º 31
0
class CosmicRayTestCase(unittest.TestCase):
    """A test case for Cosmic Ray detection"""
    def setUp(self):
        self.FWHM = 5  # pixels
        self.psf = algorithms.DoubleGaussianPsf(
            29, 29, self.FWHM / (2 * sqrt(2 * log(2))))

        self.mi = afwImage.MaskedImageF(imageFile)
        self.XY0 = afwGeom.PointI(0, 0)  # origin of the subimage we use

        if imageFile == imageFile0:
            if True:  # use full image, trimmed to data section
                self.XY0 = afwGeom.PointI(32, 2)
                self.mi = self.mi.Factory(
                    self.mi, afwGeom.BoxI(self.XY0, afwGeom.PointI(2079,
                                                                   4609)),
                    afwImage.LOCAL)
                self.mi.setXY0(afwGeom.PointI(0, 0))
                self.nCR = 1076  # number of CRs we should detect
            else:  # use sub-image
                if True:
                    self.XY0 = afwGeom.PointI(824, 140)
                    self.nCR = 10
                else:
                    self.XY0 = afwGeom.PointI(280, 2750)
                    self.nCR = 2
                self.mi = self.mi.Factory(
                    self.mi,
                    afwGeom.BoxI(self.XY0, afwGeom.ExtentI(256, 256),
                                 afwImage.LOCAL))
                self.mi.setXY0(afwGeom.PointI(0, 0))
        else:
            self.nCR = None

        self.mi.getMask().addMaskPlane("DETECTED")

    def tearDown(self):
        del self.mi
        del self.psf

    def testDetection(self):
        """Test CR detection"""
        #
        # Subtract background
        #
        bctrl = afwMath.BackgroundControl(afwMath.Interpolate.NATURAL_SPLINE)
        bctrl.setNxSample(int(self.mi.getWidth() / 256) + 1)
        bctrl.setNySample(int(self.mi.getHeight() / 256) + 1)
        bctrl.getStatisticsControl().setNumSigmaClip(3.0)
        bctrl.getStatisticsControl().setNumIter(2)

        im = self.mi.getImage()
        try:
            backobj = afwMath.makeBackground(im, bctrl)
        except Exception, e:
            print >> sys.stderr, e,

            bctrl.setInterpStyle(afwMath.Interpolate.CONSTANT)
            backobj = afwMath.makeBackground(im, bctrl)

        im -= backobj.getImageF()

        if display:
            frame = 0
            ds9.mtv(self.mi, frame=frame, title="Raw")  # raw frame
            if self.mi.getWidth() > 256:
                ds9.pan(944 - self.mi.getX0(), 260 - self.mi.getY0())
        #
        # Mask known bad pixels
        #
        badPixels = defects.policyToBadRegionList(
            os.path.join(os.environ["MEAS_ALGORITHMS_DIR"], "policy",
                         "BadPixels.paf"))
        # did someone lie about the origin of the maskedImage?  If so, adjust bad pixel list
        if self.XY0.getX() != self.mi.getX0() or self.XY0.getY(
        ) != self.mi.getY0():
            dx = self.XY0.getX() - self.mi.getX0()
            dy = self.XY0.getY() - self.mi.getY0()
            for bp in badPixels:
                bp.shift(-dx, -dy)

        algorithms.interpolateOverDefects(self.mi, self.psf, badPixels)

        stats = afwMath.makeStatistics(self.mi.getImage(),
                                       afwMath.MEANCLIP | afwMath.STDEVCLIP)
        background = stats.getValue(afwMath.MEANCLIP)

        crConfig = algorithms.FindCosmicRaysConfig()
        crs = algorithms.findCosmicRays(self.mi, self.psf, background,
                                        pexConfig.makePolicy(crConfig))

        if display:
            ds9.mtv(self.mi, frame=frame + 1, title="CRs removed")
            if self.mi.getWidth() > 256:
                ds9.pan(944 - self.mi.getX0(), 260 - self.mi.getY0())

        print "Detected %d CRs" % len(crs)
        if display and False:
            for cr in crs:
                bbox = cr.getBBox()
                bbox.shift(afwGeom.ExtentI(-self.mi.getX0(), -self.mi.getY0()))
                ds9.line([(bbox.getMinX() - 0.5, bbox.getMinY() - 0.5),
                          (bbox.getMaxX() + 0.5, bbox.getMinY() - 0.5),
                          (bbox.getMaxX() + 0.5, bbox.getMaxY() + 0.5),
                          (bbox.getMinX() - 0.5, bbox.getMaxY() + 0.5),
                          (bbox.getMinX() - 0.5, bbox.getMinY() - 0.5)],
                         frame=frame + 1)

        if self.nCR is not None:
            self.assertEqual(len(crs), self.nCR)
Ejemplo n.º 32
0
        with ds9.Buffering():
            for x, y in cenPos:
                ds9.dot("+", x, y, frame=frame)

        if showAmps:
            nx, ny = namp
            for i in range(nx):
                for j in range(ny):
                    xc = numpy.array([0, 1, 1, 0, 0])
                    yc = numpy.array([0, 0, 1, 1, 0])

                    corners = []
                    for k in range(len(xc)):
                        corners.append([psfWidth//2 + w/nx*(i + xc[k]), psfHeight//2 + h/ny*(j + yc[k])])

                    ds9.line(corners, frame=frame)

    return im

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

def saveSpatialCellSet(psfCellSet, fileName="foo.fits", frame=None):
    """Write the contents of a SpatialCellSet to a many-MEF fits file"""
    
    mode = "w"
    for cell in psfCellSet.getCellList():
        for cand in cell.begin(False):  # include bad candidates
            cand = algorithmsLib.cast_PsfCandidateF(cand)

            dx = afwImage.positionToIndex(cand.getXCenter(), True)[1]
            dy = afwImage.positionToIndex(cand.getYCenter(), True)[1]
Ejemplo n.º 33
0
        with ds9.Buffering():
            for x, y in cenPos:
                ds9.dot("+", x, y, frame=frame)

        if showAmps:
            nx, ny = namp
            for i in range(nx):
                for j in range(ny):
                    xc = numpy.array([0, 1, 1, 0, 0])
                    yc = numpy.array([0, 0, 1, 1, 0])

                    corners = []
                    for k in range(len(xc)):
                        corners.append([psfWidth//2 + w/nx*(i + xc[k]), psfHeight//2 + h/ny*(j + yc[k])])

                    ds9.line(corners, frame=frame)

    return im

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

def saveSpatialCellSet(psfCellSet, fileName="foo.fits", frame=None):
    """Write the contents of a SpatialCellSet to a many-MEF fits file"""
    
    mode = "w"
    for cell in psfCellSet.getCellList():
        for cand in cell.begin(False):  # include bad candidates
            cand = algorithmsLib.cast_PsfCandidateF(cand)

            dx = afwImage.positionToIndex(cand.getXCenter(), True)[1]
            dy = afwImage.positionToIndex(cand.getYCenter(), True)[1]
Ejemplo n.º 34
0
def SpatialCellSetDemo(filename=None):
    """A demonstration of the use of a SpatialCellSet"""

    im, fs = readImage(filename)

    if display:
        ds9.mtv(im, frame=0, title="Input")
    #
    # Create an (empty) SpatialCellSet
    #
    cellSet = afwMath.SpatialCellSet(
        afwGeom.Box2I(afwGeom.Point2I(0, 0), im.getDimensions()), 260, 200)

    if display:
        for i in range(len(cellSet.getCellList())):
            cell = cellSet.getCellList()[i]
            ds9.line([
                (cell.getBBox().getMinX(), cell.getBBox().getMinY()),
                (cell.getBBox().getMinX(), cell.getBBox().getMaxY()),
                (cell.getBBox().getMaxX(), cell.getBBox().getMaxY()),
                (cell.getBBox().getMaxX(), cell.getBBox().getMinY()),
                (cell.getBBox().getMinX(), cell.getBBox().getMinY()),
            ],
                     frame=0)
            ds9.dot(cell.getLabel(),
                    (cell.getBBox().getMinX() + cell.getBBox().getMaxX()) / 2,
                    (cell.getBBox().getMinY() + cell.getBBox().getMaxY()) / 2)
    #
    # Populate cellSet
    #
    for foot in fs.getFootprints():
        bbox = foot.getBBox()
        xc = (bbox.getMinX() + bbox.getMaxX()) / 2.0
        yc = (bbox.getMinY() + bbox.getMaxY()) / 2.0
        tc = testSpatialCellLib.ExampleCandidate(xc, yc, im, bbox)
        cellSet.insertCandidate(tc)
    #
    # OK, the SpatialCellList is populated.  Let's do something with it
    #
    visitor = testSpatialCellLib.ExampleCandidateVisitor()

    cellSet.visitCandidates(visitor)
    print("There are %d candidates" % (visitor.getN()))

    ctypes = [
        "red",
        "yellow",
        "cyan",
    ]
    for i in range(cellSet.getCellList().size()):
        cell = cellSet.getCellList()[i]
        cell.visitCandidates(visitor)

        j = 0
        for cand in cell:
            cand = cand

            w, h = cand.getBBox().getDimensions()
            if w * h < 75:
                # print "%d %5.2f %5.2f %d" % (i, cand.getXCenter(),
                # cand.getYCenter(), w*h)
                cand.setStatus(afwMath.SpatialCellCandidate.BAD)

                if display:
                    ds9.dot("o",
                            cand.getXCenter(),
                            cand.getYCenter(),
                            size=4,
                            ctype=ctypes[i % len(ctypes)])
            else:
                if display:
                    ds9.dot("%s:%d" % (cand.getId(), j),
                            cand.getXCenter(),
                            cand.getYCenter(),
                            size=4,
                            ctype=ctypes[i % len(ctypes)])
            j += 1

            im = cand.getMaskedImage()
            if 0 and display:
                ds9.mtv(im, title="Candidate", frame=1)
    #
    # Now count the good and bad candidates
    #
    for i in range(len(cellSet.getCellList())):
        cell = cellSet.getCellList()[i]
        cell.visitCandidates(visitor)

        cell.setIgnoreBad(False)  # include BAD in cell.size()
        print(
            "%s nobj=%d N_good=%d NPix_good=%d" %
            (cell.getLabel(), cell.size(), visitor.getN(), visitor.getNPix()))

    cellSet.setIgnoreBad(True)  # don't visit BAD candidates
    cellSet.visitCandidates(visitor)
    print("There are %d good candidates" % (visitor.getN()))
Ejemplo n.º 35
0
                pix = wcs.skyToPixel(m.first.getCoord())

                dr[i] = numpy.hypot(pix[0] - x, pix[1] - y)

                x, y = toObserved(x, y)
                ds9.dot("o", x,  y, size=10, frame=frame, ctype=ds9.YELLOW)
                
            print "<dr> = %.4g +- %.4g [%d matches]" % (dr.mean(), dr.std(), len(matches))
        else:
            for s in sources:
                x0, y0 = s.getX(), s.getY()
                ds9.dot("+", x0, y0, size=20, frame=frame, ctype=ds9.GREEN)
                if correctDistortion:
                    x, y = toObserved(x0, y0)
                    ds9.dot("o", x,  y,  frame=frame, ctype=ds9.GREEN)
                    ds9.line([(x0, y0), (x, y)], frame=frame, ctype=ds9.GREEN)

            for s in catalog:
                pix = wcs.skyToPixel(s.getCoord())
                ds9.dot("x", pix[0], pix[1], size=20, frame=frame, ctype=ds9.RED)

def getDistorter(exposure):
    try:
        detector = exposure.getDetector()
        distorter = detector.getDistortion()
        def toUndistort(x, y):
            dist = distorter.undistort(afwGeom.Point2D(x, y), detector)
            return dist.getX(), dist.getY()
        def toDistort(x, y):
            dist = distorter.distort(afwGeom.Point2D(x, y), detector)
            return dist.getX(), dist.getY()
Ejemplo n.º 36
0
def SpatialCellSetDemo(filename=None):
    """A demonstration of the use of a SpatialCellSet"""

    im, fs = readImage(filename)

    if display:
        ds9.mtv(im, frame=0, title="Input")
    #
    # Create an (empty) SpatialCellSet
    #
    cellSet = afwMath.SpatialCellSet(afwGeom.Box2I(afwGeom.Point2I(0, 0), im.getDimensions()),
                                     260, 200)

    if display:
        for i in range(len(cellSet.getCellList())):
            cell = cellSet.getCellList()[i]
            ds9.line([(cell.getBBox().getMinX(), cell.getBBox().getMinY()),
                      (cell.getBBox().getMinX(), cell.getBBox().getMaxY()),
                      (cell.getBBox().getMaxX(), cell.getBBox().getMaxY()),
                      (cell.getBBox().getMaxX(), cell.getBBox().getMinY()),
                      (cell.getBBox().getMinX(), cell.getBBox().getMinY()),
                      ], frame=0)
            ds9.dot(cell.getLabel(),
                    (cell.getBBox().getMinX() + cell.getBBox().getMaxX())/2,
                    (cell.getBBox().getMinY() + cell.getBBox().getMaxY())/2)
    #
    # Populate cellSet
    #
    for foot in fs.getFootprints():
        bbox = foot.getBBox()
        xc = (bbox.getMinX() + bbox.getMaxX())/2.0
        yc = (bbox.getMinY() + bbox.getMaxY())/2.0
        tc = testSpatialCellLib.ExampleCandidate(xc, yc, im, bbox)
        cellSet.insertCandidate(tc)
    #
    # OK, the SpatialCellList is populated.  Let's do something with it
    #
    visitor = testSpatialCellLib.ExampleCandidateVisitor()

    cellSet.visitCandidates(visitor)
    print "There are %d candidates" % (visitor.getN())
    
    ctypes = ["red", "yellow", "cyan", ]
    for i in range(cellSet.getCellList().size()):
        cell = cellSet.getCellList()[i]
        cell.visitCandidates(visitor)

        j = 0
        for cand in cell:
            #
            # Swig doesn't know that we're a SpatialCellImageCandidate;  all it knows is that we have
            # a SpatialCellCandidate so we need an explicit (dynamic) cast
            #
            cand = testSpatialCellLib.cast_ExampleCandidate(cand)

            w, h = cand.getBBox().getDimensions()
            if w*h < 75:
                #print "%d %5.2f %5.2f %d" % (i, cand.getXCenter(), cand.getYCenter(), w*h)
                cand.setStatus(afwMath.SpatialCellCandidate.BAD)

                if display:
                    ds9.dot("o", cand.getXCenter(), cand.getYCenter(), size=4, ctype=ctypes[i%len(ctypes)])
            else:
                if display:
                    ds9.dot("%s:%d" % (cand.getId(), j),
                            cand.getXCenter(), cand.getYCenter(), size=4, ctype=ctypes[i%len(ctypes)])
            j += 1

            im = cand.getMaskedImage()
            if 0 and display:
                ds9.mtv(im, title="Candidate", frame=1)
    #
    # Now count the good and bad candidates
    #
    for i in range(len(cellSet.getCellList())):
        cell = cellSet.getCellList()[i]
        cell.visitCandidates(visitor)

        cell.setIgnoreBad(False)        # include BAD in cell.size()
        print "%s nobj=%d N_good=%d NPix_good=%d" % \
              (cell.getLabel(), cell.size(), visitor.getN(), visitor.getNPix())


    cellSet.setIgnoreBad(True)           # don't visit BAD candidates
    cellSet.visitCandidates(visitor)
    print "There are %d good candidates" % (visitor.getN())