Exemplo n.º 1
0
def makeTest1(doAddNoise):
    gaussian1 = afwMath.GaussianFunction2D(1.*gScale, 1.*gScale, 0.)
    kernel1 = afwMath.AnalyticKernel(imSize, imSize, gaussian1)
    image1 = afwImage.ImageD(kernel1.getDimensions())
    kernel1.computeImage(image1, doNorm)
    image1 *= scaling  # total counts = scaling
    image1 = image1.convertF()
    mask1 = afwImage.Mask(kernel1.getDimensions())
    var1 = afwImage.ImageF(image1, True)
    mi1 = afwImage.MaskedImageF(image1, mask1, var1)
    if doAddNoise:
        addNoise(mi1)

    gaussian2 = afwMath.GaussianFunction2D(2.*gScale, 1.5*gScale, 0.5*np.pi)
    kernel2 = afwMath.AnalyticKernel(imSize, imSize, gaussian2)
    image2 = afwImage.ImageD(kernel2.getDimensions())
    kernel2.computeImage(image2, doNorm)
    image2 *= scaling  # total counts = scaling
    image2 = image2.convertF()
    mask2 = afwImage.Mask(kernel2.getDimensions())
    var2 = afwImage.ImageF(image2, True)
    mi2 = afwImage.MaskedImageF(image2, mask2, var2)
    if doAddNoise:
        addNoise(mi2)
    return mi1, mi2
Exemplo n.º 2
0
def makeTest1(doAddNoise):
    gaussian1 = afwMath.GaussianFunction2D(1., 1., 0.)
    kernel1 = afwMath.AnalyticKernel(imSize, imSize, gaussian1)
    image1 = afwImage.ImageD(kernel1.getDimensions())
    kernel1.computeImage(image1, False)
    image1 *= 10000
    image1 = image1.convertF()
    mask1 = afwImage.Mask(kernel1.getDimensions())
    var1 = afwImage.ImageF(image1, True)
    mi1 = afwImage.MaskedImageF(image1, mask1, var1)
    if doAddNoise:
        addNoise(mi1)

    gaussian2 = afwMath.GaussianFunction2D(2., 1.5, 0.5 * num.pi)
    kernel2 = afwMath.AnalyticKernel(imSize, imSize, gaussian2)
    image2 = afwImage.ImageD(kernel2.getDimensions())
    kernel2.computeImage(image2, False)
    image2 *= 10000
    image2 = image2.convertF()
    mask2 = afwImage.Mask(kernel2.getDimensions())
    var2 = afwImage.ImageF(image2, True)
    mi2 = afwImage.MaskedImageF(image2, mask2, var2)
    if doAddNoise:
        addNoise(mi2)
    return mi1, mi2
Exemplo n.º 3
0
def main():
    butils = measDeblend.BaselineUtilsF

    foot = buildExample2()

    fbb = foot.getBBox()
    mask1 = afwImg.Mask(fbb.getWidth(), fbb.getHeight())
    mask1.setXY0(fbb.getMinX(), fbb.getMinY())
    afwDet.setMaskFromFootprint(mask1, foot, 1)

    if plt:
        plt.clf()
        plt.imshow(mask1.getArray(), origin='lower', interpolation='nearest',
                   extent=(fbb.getMinX(), fbb.getMaxX(), fbb.getMinY(), fbb.getMaxY()))
        plt.gray()
        plt.savefig('foot2.png')

        sfoot = butils.symmetrizeFootprint(foot, 355, 227)

        mask2 = afwImg.Mask(fbb.getWidth(), fbb.getHeight())
        mask2.setXY0(fbb.getMinX(), fbb.getMinY())
        afwDet.setMaskFromFootprint(mask2, sfoot, 1)

        plt.clf()
        plt.imshow(mask2.getArray(), origin='lower', interpolation='nearest',
                   extent=(fbb.getMinX(), fbb.getMaxX(), fbb.getMinY(), fbb.getMaxY()))
        plt.gray()
        plt.savefig('sfoot3.png')
Exemplo n.º 4
0
    def setUp(self):
        np.random.seed(1)
        self.Mask = afwImage.Mask[afwImage.MaskPixel]

        # Store the default mask planes for later use
        maskPlaneDict = self.Mask().getMaskPlaneDict()
        self.defaultMaskPlanes = sorted(maskPlaneDict,
                                        key=maskPlaneDict.__getitem__)

        # reset so tests will be deterministic
        self.Mask.clearMaskPlaneDict()
        for p in ("BAD", "SAT", "INTRP", "CR", "EDGE"):
            self.Mask.addMaskPlane(p)

        self.BAD = self.Mask.getPlaneBitMask("BAD")
        self.CR = self.Mask.getPlaneBitMask("CR")
        self.EDGE = self.Mask.getPlaneBitMask("EDGE")

        self.val1 = self.BAD | self.CR
        self.val2 = self.val1 | self.EDGE

        self.mask1 = afwImage.Mask(100, 200)
        self.mask1.set(self.val1)
        self.mask2 = afwImage.Mask(self.mask1.getDimensions())
        self.mask2.set(self.val2)

        if afwdataDir is not None:
            self.maskFile = os.path.join(afwdataDir, "data", "small_MI.fits")
            # Below: what to expect from the mask plane in the above data.
            # For some tests, it is left-shifted by some number of mask planes.
            self.expect = np.zeros((256, 256), dtype='i8')
            self.expect[:, 0] = 1
    def testNoMask(self):
        mask = afwImage.Mask(afwGeom.Extent2I(20, 20))
        mask.set(0)
        fsb = ipDiffim.FindSetBitsU()

        bbox = afwGeom.Box2I(afwGeom.Point2I(0, 10), afwGeom.Point2I(9, 12))
        fsb.apply(afwImage.Mask(mask, bbox, afwImage.LOCAL))

        self.assertEqual(fsb.getBits(), 0)
Exemplo n.º 6
0
    def testCopyConstructors(self):
        dmask = afwImage.Mask(self.mask1, True)  # deep copy
        smask = afwImage.Mask(self.mask1)  # shallow copy

        self.mask1 |= 32767  # should only change smask
        temp = np.zeros_like(self.mask1.getArray()) | self.val1
        self.assertMasksEqual(dmask, temp)
        self.assertMasksEqual(smask, self.mask1)
        self.assertMasksEqual(smask, temp | 32767)
Exemplo n.º 7
0
    def testString(self):
        mask = afwImage.Mask(100, 100)
        self.assertIn(str(np.zeros((100, 100), dtype=mask.dtype)), str(mask))
        self.assertIn("bbox=%s"%str(mask.getBBox()), str(mask))
        self.assertIn("maskPlaneDict=%s"%str(mask.getMaskPlaneDict()), str(mask))

        smallMask = afwImage.Mask(2, 2)
        self.assertIn(str(np.zeros((2, 2), dtype=mask.dtype)), str(smallMask))

        self.assertIn("MaskX=", repr(mask))
Exemplo n.º 8
0
    def testInclude(self):
        """Test that we can expand a Footprint to include the union of itself and all others provided."""
        region = lsst.geom.Box2I(lsst.geom.Point2I(-6, -6),
                                 lsst.geom.Point2I(6, 6))
        parentSpanSet = afwGeom.SpanSet(
            lsst.geom.Box2I(lsst.geom.Point2I(-2, -2), lsst.geom.Point2I(2,
                                                                         2)))
        parent = afwDetect.Footprint(parentSpanSet, region)
        parent.addPeak(0, 0, float("NaN"))
        child1SpanSet = afwGeom.SpanSet(
            lsst.geom.Box2I(lsst.geom.Point2I(-3, 0), lsst.geom.Point2I(0, 3)))
        child1 = afwDetect.Footprint(child1SpanSet, region)
        child1.addPeak(-1, 1, float("NaN"))
        child2SpanSet = afwGeom.SpanSet(
            lsst.geom.Box2I(lsst.geom.Point2I(-4, -3),
                            lsst.geom.Point2I(-1, 0)))
        child2 = afwDetect.Footprint(child2SpanSet, region)
        child3SpanSet = afwGeom.SpanSet(
            lsst.geom.Box2I(lsst.geom.Point2I(4, -1), lsst.geom.Point2I(6, 1)))
        child3 = afwDetect.Footprint(child3SpanSet)
        merge123 = afwDetect.Footprint(parent)
        merge123.spans = merge123.spans.union(child1.spans).union(
            child2.spans).union(child3.spans)
        self.assertTrue(merge123.getBBox().contains(parent.getBBox()))
        self.assertTrue(merge123.getBBox().contains(child1.getBBox()))
        self.assertTrue(merge123.getBBox().contains(child2.getBBox()))
        self.assertTrue(merge123.getBBox().contains(child3.getBBox()))
        mask123a = afwImage.Mask(region)
        mask123b = afwImage.Mask(region)
        parent.spans.setMask(mask123a, 1)
        child1.spans.setMask(mask123a, 1)
        child2.spans.setMask(mask123a, 1)
        child3.spans.setMask(mask123a, 1)
        merge123.spans.setMask(mask123b, 1)
        self.assertEqual(mask123a.getArray().sum(), merge123.getArea())
        self.assertFloatsAlmostEqual(mask123a.getArray(),
                                     mask123b.getArray(),
                                     rtol=0,
                                     atol=0)

        # Test that ignoreSelf=True works for include
        childOnly = afwDetect.Footprint()
        childOnly.spans = childOnly.spans.union(child1.spans).union(
            child2.spans).union(child3.spans)
        merge123 = afwDetect.Footprint(parent)
        merge123.spans = child1.spans.union(child2.spans).union(child3.spans)
        maskChildren = afwImage.Mask(region)
        mask123 = afwImage.Mask(region)
        childOnly.spans.setMask(maskChildren, 1)
        merge123.spans.setMask(mask123, 1)
        self.assertTrue(np.all(maskChildren.getArray() == mask123.getArray()))
Exemplo n.º 9
0
    def preMeasureImage(self, source, ref, exp, setFlags=True):

        if self.config.calculateVariance:
            # Calculate the noise around each object, maybe I should get this from the variance plane?
            image = exp.getMaskedImage().getImage()

            maskPlanes = [
                "BAD", "CR", "DETECTED", "DETECTED_NEGATIVE", "NO_DATA", "SAT",
                "EDGE", "SUSPECT", "INTRP"
            ]
            badBit = afwImage.Mask().getPlaneBitMask(maskPlanes)
            failed = True
            meanSrc = -1
            varSrc = -1
            # try multiples of the noiseGrow parameter until there are a sufficient number of pixels
            for scale in (1, 2, 4, 8):

                fp = afwDet.Footprint(source.getFootprint())
                fp.dilate(scale * self.config.noiseGrow,
                          afwGeom.Stencil.MANHATTAN)

                if fp.getArea() == 0:
                    continue

                fp.clipTo(exp.getBBox(lsst.afw.image.PARENT))
                box = fp.getBBox()

                span = fp.spans.intersectNot(
                    exp.getMaskedImage().getMask()[box], badBit)
                maskImage = afwImage.Mask(box, 0)
                span.setMask(maskImage, 0x1)
                mask = maskImage.array == 0

                if span.getArea() < self.config.minVariancePixels:
                    continue

                varSrc = numpy.nanvar(image[box].getArray()[mask])
                meanSrc = numpy.nanmean(image[box].getArray()[mask])

                failed = False
                break

            if setFlags and failed:
                source.set('bfd.flags.variance', True)
                source.set('bfd.flags', True)
                return False

            source.set('noise_variance', float(varSrc))
            source.set('noise_mean', float(meanSrc))
        return True
Exemplo n.º 10
0
    def testOneMask(self):
        mask = afwImage.Mask(afwGeom.Extent2I(20, 20))
        mask.set(0)
        bitmaskBad = mask.getPlaneBitMask('BAD')
        fsb = ipDiffim.FindSetBitsU()

        bbox = afwGeom.Box2I(afwGeom.Point2I(9, 10), afwGeom.Point2I(11, 12))
        submask = afwImage.Mask(mask, bbox, afwImage.LOCAL)
        submask |= bitmaskBad

        bbox2 = afwGeom.Box2I(afwGeom.Point2I(8, 8), afwGeom.Point2I(19, 19))
        fsb.apply(afwImage.Mask(mask, bbox2, afwImage.LOCAL))

        self.assertEqual(fsb.getBits(), bitmaskBad)
Exemplo n.º 11
0
    def testLogicalMasksMismatch(self):
        "Test logical operations on Masks of different sizes"
        i1 = afwImage.Mask(lsst.geom.ExtentI(100, 100))
        i1.set(100)
        i2 = afwImage.Mask(lsst.geom.ExtentI(10, 10))
        i2.set(10)

        with self.assertRaises(lsst.pex.exceptions.LengthError):
            i1 |= i2

        with self.assertRaises(lsst.pex.exceptions.LengthError):
            i1 &= i2

        with self.assertRaises(lsst.pex.exceptions.LengthError):
            i1 ^= i2
Exemplo n.º 12
0
    def testFitsReadNoConform2(self):
        """Check that reading a mask doesn't invalidate the plane dictionary"""

        testMask = afwImage.Mask(self.fileName, hdu=2)

        mask = self.mi.getMask()
        mask |= testMask
Exemplo n.º 13
0
    def setUp(self):
        self.imgVal1, self.varVal1 = 100.0, 10.0
        self.imgVal2, self.varVal2 = 200.0, 15.0
        self.mimage = afwImage.MaskedImageF(100, 200)

        self.mimage.image.set(self.imgVal1)
        #
        # Set center of mask to 0, with 2 pixel border set to EDGE
        #
        self.BAD = afwImage.Mask.getPlaneBitMask("BAD")
        self.EDGE = afwImage.Mask.getPlaneBitMask("EDGE")

        self.mimage.mask.set(self.EDGE)
        centre = afwImage.Mask(
            self.mimage.mask,
            lsst.geom.Box2I(lsst.geom.Point2I(2, 2),
                            self.mimage.getDimensions() - lsst.geom.Extent2I(4)),
            afwImage.LOCAL)
        centre.set(0x0)
        #
        self.mimage.variance.set(self.varVal1)
        #
        # Second MaskedImage
        #
        self.mimage2 = afwImage.MaskedImageF(self.mimage.getDimensions())
        self.mimage2.image.set(self.imgVal2)
        self.mimage2.variance.set(self.varVal2)
        #
        # a Function2
        #
        self.function = afwMath.PolynomialFunction2D(2)
        self.function.setParameters(
            list(range(self.function.getNParameters())))
Exemplo n.º 14
0
def makeTestImage(xsize=200, ysize=100, nCR=15):

    randArr = numpy.random.poisson(1000., xsize * ysize)
    randArr = numpy.array(randArr.reshape(ysize, xsize),
                          dtype=numpy.float32)  # force to ImageF
    factory = measAlg.GaussianPsfFactory()
    factory.addWing = False
    psf = factory.apply(4)  # FWHM in pixels

    img = afwImage.makeImageFromArray(randArr)
    var = afwImage.ImageF(img, True)  # copy constructor
    mask = afwImage.Mask(xsize, ysize)

    xind = numpy.random.randint(0, xsize, nCR)
    yind = numpy.random.randint(0, ysize, nCR)

    # set some CRs
    for xi, yi in zip(xind, yind):
        xi, yi = int(xi), int(yi)
        img.set(xi, yi, 1e6)

    mi = afwImage.makeMaskedImage(img, mask, var)
    exp = afwImage.makeExposure(mi)
    exp.setPsf(psf)
    return exp
Exemplo n.º 15
0
 def testCtorWithPlaneDefs(self):
     """Test that we can create a Mask with a given MaskPlaneDict"""
     FOO, val = "FOO", 2
     mask = afwImage.Mask(100, 200, {FOO: val})
     mpd = mask.getMaskPlaneDict()
     self.assertIn(FOO, mpd.keys())
     self.assertEqual(mpd[FOO], val)
Exemplo n.º 16
0
    def makeExposure(self, im, mask=None, variance=None):
        """Method for constructing an exposure object from an image and the
        information contained in this class to construct the Detector.

        Parameters
        ----------
        im : `lsst.afw.image.Image`
            Image used to construct the exposure.
        mask : `lsst.afw.image.MaskU`
            Optional mask plane.
        variance : `lsst.afw.image.Image`
            Optional variance plance as an image of the same type as im.

        Returns
        -------
        exposure : `lsst.afw.image.Exposure`
            Constructed exposure (specific type will match that of ``im``).
        """
        if mask is None:
            mask = afwImage.Mask(im.getDimensions())
        if variance is None:
            variance = im
        mi = afwImage.makeMaskedImage(im, mask, variance)
        detector = self.buildDetector()

        exp = afwImage.makeExposure(mi)
        exp.setDetector(detector)
        return exp
Exemplo n.º 17
0
    def getSkySourceFootprints(self, mergedList, skyInfo, seed):
        """!
        @brief Return a list of Footprints of sky objects which don't overlap with anything in mergedList

        @param mergedList  The merged Footprints from all the input bands
        @param skyInfo     A description of the patch
        @param seed        Seed for the random number generator
        """
        mask = afwImage.Mask(skyInfo.patchInfo.getOuterBBox())
        detected = mask.getPlaneBitMask("DETECTED")
        for s in mergedList:
            s.getFootprint().spans.setMask(mask, detected)

        footprints = self.skyObjects.run(mask, seed)
        if not footprints:
            return footprints

        # Need to convert the peak catalog's schema so we can set the "merge_peak_<skyFilterName>" flags
        schema = self.merged.getPeakSchema()
        mergeKey = schema.find("merge_peak_%s" % self.config.skyFilterName).key
        converted = []
        for oldFoot in footprints:
            assert len(oldFoot.getPeaks()) == 1, "Should be a single peak only"
            peak = oldFoot.getPeaks()[0]
            newFoot = afwDetect.Footprint(oldFoot.spans, schema)
            newFoot.addPeak(peak.getFx(), peak.getFy(), peak.getPeakValue())
            newFoot.getPeaks()[0].set(mergeKey, True)
            converted.append(newFoot)

        return converted
Exemplo n.º 18
0
    def testFromMask(self):
        xy0 = afwGeom.Point2I(12345, 67890)  # xy0 for image
        dims = afwGeom.Extent2I(123, 45)  # Dimensions of image
        box = afwGeom.Box2I(xy0, dims)  # Bounding box of image
        value = 32
        other = 16
        assert value & other == 0  # Setting 'other' unsets 'value'
        point = afwGeom.Point2I(3 + xy0.getX(),
                                3 + xy0.getY())  # Point in the image

        mask = afwImage.Mask(box)
        mask.set(value)

        # We can create a SpanSet from bit planes
        spans = afwGeom.SpanSet.fromMask(mask, value)
        self.assertEqual(spans.getArea(), box.getArea())
        self.assertEqual(spans.getBBox(), box)
        self.assertTrue(point in spans)

        # Pixels not matching the desired bit plane are ignored as they should be
        mask.set(point.getX() - xy0.getX(),
                 point.getY() - xy0.getY(), other)  # unset one pixel
        spans = afwGeom.SpanSet.fromMask(mask, value)
        self.assertEqual(spans.getArea(), box.getArea() - 1)
        self.assertEqual(spans.getBBox(), box)
        self.assertFalse(point in spans)
Exemplo n.º 19
0
    def testClipped(self):
        """Test that we set mask bits when pixels are clipped"""
        box = lsst.geom.Box2I(lsst.geom.Point2I(12345, 67890), lsst.geom.Extent2I(3, 3))
        num = 10
        maskVal = 0xAD
        value = 0.0

        images = [afwImage.MaskedImageF(box) for _ in range(num)]
        statsCtrl = afwMath.StatisticsControl()
        statsCtrl.setAndMask(maskVal)
        clipped = 1 << afwImage.Mask().addMaskPlane("CLIPPED")

        # No clipping: check that vanilla is working
        for img in images:
            img.getImage().set(value)
            img.getMask().set(0)
        stack = afwMath.statisticsStack(images, afwMath.MEANCLIP, clipped=clipped)
        self.assertFloatsAlmostEqual(stack.getImage().getArray(), 0.0, atol=0.0)
        self.assertFloatsAlmostEqual(stack.getMask().getArray(), 0, atol=0.0)  # Not floats, but that's OK

        # Clip a pixel; the CLIPPED bit should be set
        images[0].getImage()[1, 1, afwImage.LOCAL] = value + 1.0
        stack = afwMath.statisticsStack(images, afwMath.MEANCLIP, clipped=clipped)
        self.assertFloatsAlmostEqual(stack.getImage().getArray(), 0.0, atol=0.0)
        self.assertEqual(stack.mask[1, 1, afwImage.LOCAL], clipped)

        # Mask a pixel; the CLIPPED bit should be set
        images[0].getMask()[1, 1, afwImage.LOCAL] = maskVal
        stack = afwMath.statisticsStack(images, afwMath.MEAN, statsCtrl, clipped=clipped)
        self.assertFloatsAlmostEqual(stack.getImage().getArray(), 0.0, atol=0.0)
        self.assertEqual(stack.mask[1, 1, afwImage.LOCAL], clipped)

        # Excuse that mask; the CLIPPED bit should not be set
        stack = afwMath.statisticsStack(images, afwMath.MEAN, statsCtrl, clipped=clipped, excuse=maskVal)
        self.assertFloatsAlmostEqual(stack.getImage().getArray(), 0.0, atol=0.0)
        self.assertEqual(stack.mask[1, 1, afwImage.LOCAL], 0)

        # Map that mask value to a different one.
        rejected = 1 << afwImage.Mask().addMaskPlane("REJECTED")
        maskMap = [(maskVal, rejected)]
        images[0].mask[1, 1, afwImage.LOCAL] = 0        # only want to clip, not mask, this one
        images[1].mask[1, 2, afwImage.LOCAL] = maskVal  # only want to mask, not clip, this one
        stack = afwMath.statisticsStack(images, afwMath.MEANCLIP, statsCtrl, wvector=[], clipped=clipped,
                                        maskMap=maskMap)
        self.assertFloatsAlmostEqual(stack.getImage().getArray(), 0.0, atol=0.0)
        self.assertEqual(stack.mask[1, 1, afwImage.LOCAL], clipped)
        self.assertEqual(stack.mask[1, 2, afwImage.LOCAL], rejected)
Exemplo n.º 20
0
    def getExposure(self):
        """Construct a test exposure.

        The test exposure has a simple WCS set, as well as a list of
        unlikely header keywords that can be removed during ISR
        processing to exercise that code.

        Returns
        -------
        exposure : `lsst.afw.exposure.Exposure`
            Construct exposure containing masked image of the
            appropriate size.
        """
        camera = self.getCamera()
        detector = camera[self.config.detectorIndex]
        image = afwUtils.makeImageFromCcd(detector,
                                          isTrimmed=self.config.isTrimmed,
                                          showAmpGain=False,
                                          rcMarkSize=0,
                                          binSize=1,
                                          imageFactory=afwImage.ImageF)

        var = afwImage.ImageF(image.getDimensions())
        mask = afwImage.Mask(image.getDimensions())
        image.assign(0.0)

        maskedImage = afwImage.makeMaskedImage(image, mask, var)
        exposure = afwImage.makeExposure(maskedImage)
        exposure.setDetector(detector)
        exposure.setWcs(self.getWcs())

        visitInfo = afwImage.VisitInfo(exposureTime=self.config.expTime, darkTime=self.config.darkTime)
        exposure.getInfo().setVisitInfo(visitInfo)

        metadata = exposure.getMetadata()
        metadata.add("SHEEP", 7.3, "number of sheep on farm")
        metadata.add("MONKEYS", 155, "monkeys per tree")
        metadata.add("VAMPIRES", 4, "How scary are vampires.")

        ccd = exposure.getDetector()
        newCcd = ccd.rebuild()
        newCcd.clear()
        for amp in ccd:
            newAmp = amp.rebuild()
            newAmp.setLinearityCoeffs((0., 1., 0., 0.))
            newAmp.setLinearityType("Polynomial")
            newAmp.setGain(self.config.gain)
            newAmp.setSuspectLevel(25000.0)
            newAmp.setSaturation(32000.0)
            newCcd.append(newAmp)
        exposure.setDetector(newCcd.finish())

        exposure.image.array[:] = np.zeros(exposure.getImage().getDimensions()).transpose()
        exposure.mask.array[:] = np.zeros(exposure.getMask().getDimensions()).transpose()
        exposure.variance.array[:] = np.zeros(exposure.getVariance().getDimensions()).transpose()

        return exposure
Exemplo n.º 21
0
    def testRandomMap(self):
        """Test setCoaddEdgeBits using a random depth map
        """
        imDim = geom.Extent2I(50, 55)
        coaddMask = afwImage.Mask(imDim)

        np.random.seed(12345)
        depthMapArray = np.random.randint(0, 3, list(
            (imDim[1], imDim[0]))).astype(np.uint16)
        depthMap = afwImage.makeImageFromArray(depthMapArray)

        refCoaddMask = afwImage.Mask(imDim)
        refCoaddMaskArray = refCoaddMask.getArray()
        edgeMask = afwImage.Mask.getPlaneBitMask("NO_DATA")
        refCoaddMaskArray |= np.array(np.where(depthMapArray > 0, 0, edgeMask),
                                      dtype=refCoaddMaskArray.dtype)

        coaddUtils.setCoaddEdgeBits(coaddMask, depthMap)
        self.assertMasksEqual(coaddMask, refCoaddMask)
Exemplo n.º 22
0
    def testSubmasks(self):
        smask = afwImage.Mask(
            self.mask1,
            afwGeom.Box2I(afwGeom.Point2I(1, 1), afwGeom.ExtentI(3, 2)),
            afwImage.LOCAL)
        mask2 = afwImage.Mask(smask.getDimensions())

        mask2.set(666)
        smask[:] = mask2

        del smask
        del mask2

        self.assertEqual(self.mask1.get(0, 0), self.val1)
        self.assertEqual(self.mask1.get(1, 1), 666)
        self.assertEqual(self.mask1.get(4, 1), self.val1)
        self.assertEqual(self.mask1.get(1, 2), 666)
        self.assertEqual(self.mask1.get(4, 2), self.val1)
        self.assertEqual(self.mask1.get(1, 3), self.val1)
Exemplo n.º 23
0
    def testSubmasks(self):
        smask = afwImage.Mask(
            self.mask1,
            lsst.geom.Box2I(lsst.geom.Point2I(1, 1), lsst.geom.ExtentI(3, 2)),
            afwImage.LOCAL)
        mask2 = afwImage.Mask(smask.getDimensions())

        mask2.set(666)
        smask[:] = mask2

        del smask
        del mask2

        self.assertEqual(self.mask1[0, 0, afwImage.LOCAL], self.val1)
        self.assertEqual(self.mask1[1, 1, afwImage.LOCAL], 666)
        self.assertEqual(self.mask1[4, 1, afwImage.LOCAL], self.val1)
        self.assertEqual(self.mask1[1, 2, afwImage.LOCAL], 666)
        self.assertEqual(self.mask1[4, 2, afwImage.LOCAL], self.val1)
        self.assertEqual(self.mask1[1, 3, afwImage.LOCAL], self.val1)
Exemplo n.º 24
0
    def testManyMask(self):
        mask = afwImage.Mask(geom.Extent2I(20, 20))
        mask.set(0)
        bitmaskBad = mask.getPlaneBitMask('BAD')
        bitmaskSat = mask.getPlaneBitMask('SAT')
        fsb = ipDiffim.FindSetBitsU()

        bbox = geom.Box2I(geom.Point2I(9, 10), geom.Point2I(11, 12))
        submask = afwImage.Mask(mask, bbox, afwImage.LOCAL)
        submask |= bitmaskBad

        bbox2 = geom.Box2I(geom.Point2I(8, 8), geom.Point2I(19, 19))
        submask2 = afwImage.Mask(mask, bbox2, afwImage.LOCAL)
        submask2 |= bitmaskSat

        bbox3 = geom.Box2I(geom.Point2I(0, 0), geom.Point2I(19, 19))
        fsb.apply(afwImage.Mask(mask, bbox3, afwImage.LOCAL))

        self.assertEqual(fsb.getBits(), bitmaskBad | bitmaskSat)
Exemplo n.º 25
0
def convertfpM(infile, allPlanes=False):
    hdr = pyfits.open(infile)
    run = hdr[0].header['RUN']
    camcol = hdr[0].header['CAMCOL']
    field = hdr[0].header['FIELD']
    nRows = hdr[0].header['MASKROWS']
    nCols = hdr[0].header['MASKCOLS']
    nPlane = hdr[0].header['NPLANE']

    names = hdr[-1].data.names
    if (not "attributeName" in names) or (not "Value" in names):
        raise LookupError("Missing data in fpM header")

    planes = hdr[-1].data.field("attributeName").tolist()
    values = hdr[-1].data.field("Value").tolist()
    mask = afwImage.Mask(afwGeom.ExtentI(nCols, nRows))

    # Minimal sets of mask planes needed for LSST
    interpPlane = planes.index("S_MASK_INTERP") + 1
    satPlane = planes.index("S_MASK_SATUR") + 1
    crPlane = planes.index("S_MASK_CR") + 1

    interpBitMask = afwImage.Mask.getPlaneBitMask("INTRP")
    satBitMask = afwImage.Mask.getPlaneBitMask("SAT")
    crBitMask = afwImage.Mask.getPlaneBitMask("CR")

    listToSet = [(interpPlane, interpBitMask), (satPlane, satBitMask),
                 (crPlane, crBitMask)]

    # Add the rest of the SDSS planes
    if allPlanes:
        for plane in [
                'S_MASK_NOTCHECKED', 'S_MASK_OBJECT', 'S_MASK_BRIGHTOBJECT',
                'S_MASK_BINOBJECT', 'S_MASK_CATOBJECT', 'S_MASK_SUBTRACTED',
                'S_MASK_GHOST'
        ]:
            idx = planes.index(plane) + 1
            planeName = re.sub("S_MASK_", "", plane)
            planeId = mask.addMaskPlane(planeName)
            planeBitMask = afwImage.Mask.getPlaneBitMask(planeName)
            listToSet.append((idx, planeBitMask))

    for plane, bitmask in listToSet:
        if len(hdr) < plane:
            continue

        if hdr[plane].data is None:
            continue

        nmask = len(hdr[plane].data)
        for i in range(nmask):
            frow = hdr[plane].data[i]
            Objmask(frow, bitmask).setMask(mask)

    return mask
    def runMeasurement(self, algorithmName, imageid, x, y, v):
        """Run the measurement algorithm on an image"""
        # load the test image
        imgFile = os.path.join(self.dataDir, "image.%d.fits" % imageid)
        img = afwImage.ImageF(imgFile)
        img -= self.bkgd
        nx, ny = img.getWidth(), img.getHeight()
        msk = afwImage.Mask(geom.Extent2I(nx, ny), 0x0)
        var = afwImage.ImageF(geom.Extent2I(nx, ny), v)
        mimg = afwImage.MaskedImageF(img, msk, var)
        msk.getArray()[:] = np.where(np.fabs(img.getArray()) < 1.0e-8, msk.getPlaneBitMask("BAD"), 0)

        # Put it in a bigger image, in case it matters
        big = afwImage.MaskedImageF(self.offset + mimg.getDimensions())
        big.getImage().set(0)
        big.getMask().set(0)
        big.getVariance().set(v)
        subBig = afwImage.MaskedImageF(big, geom.Box2I(big.getXY0() + self.offset, mimg.getDimensions()))
        subBig <<= mimg
        mimg = big
        mimg.setXY0(self.xy0)

        exposure = afwImage.makeExposure(mimg)
        cdMatrix = np.array([1.0/(2.53*3600.0), 0.0, 0.0, 1.0/(2.53*3600.0)])
        cdMatrix.shape = (2, 2)
        exposure.setWcs(afwGeom.makeSkyWcs(crpix=geom.Point2D(1.0, 1.0),
                                           crval=geom.SpherePoint(0, 0, geom.degrees),
                                           cdMatrix=cdMatrix))

        # load the corresponding test psf
        psfFile = os.path.join(self.dataDir, "psf.%d.fits" % imageid)
        psfImg = afwImage.ImageD(psfFile)
        psfImg -= self.bkgd

        kernel = afwMath.FixedKernel(psfImg)
        kernelPsf = algorithms.KernelPsf(kernel)
        exposure.setPsf(kernelPsf)

        # perform the shape measurement
        msConfig = base.SingleFrameMeasurementConfig()
        alg = base.SingleFramePlugin.registry[algorithmName].PluginClass.AlgClass
        control = base.SingleFramePlugin.registry[algorithmName].PluginClass.ConfigClass().makeControl()
        msConfig.algorithms.names = [algorithmName]
        # Note: It is essential to remove the floating point part of the position for the
        # Algorithm._apply.  Otherwise, when the PSF is realised it will have been warped
        # to account for the sub-pixel offset and we won't get *exactly* this PSF.
        plugin, table = makePluginAndCat(alg, algorithmName, control, centroid="centroid")
        center = geom.Point2D(int(x), int(y)) + geom.Extent2D(self.offset + geom.Extent2I(self.xy0))
        source = table.makeRecord()
        source.set("centroid_x", center.getX())
        source.set("centroid_y", center.getY())
        source.setFootprint(afwDetection.Footprint(afwGeom.SpanSet(exposure.getBBox(afwImage.PARENT))))
        plugin.measure(source, exposure)

        return source
Exemplo n.º 27
0
def plantSources(bbox, kwid, sky, coordList, addPoissonNoise=True):
    """Make an exposure with stars (modelled as Gaussians)

    @param bbox: parent bbox of exposure
    @param kwid: kernel width (and height; kernel is square)
    @param sky: amount of sky background (counts)
    @param coordList: a list of [x, y, counts, sigma], where:
        * x,y are relative to exposure origin
        * counts is the integrated counts for the star
        * sigma is the Gaussian sigma in pixels
    @param addPoissonNoise: add Poisson noise to the exposure?
    """
    # make an image with sources
    img = afwImage.ImageD(bbox)
    meanSigma = 0.0
    for coord in coordList:
        x, y, counts, sigma = coord
        meanSigma += sigma

        # make a single gaussian psf
        psf = SingleGaussianPsf(kwid, kwid, sigma)

        # make an image of it and scale to the desired number of counts
        thisPsfImg = psf.computeImage(afwGeom.PointD(x, y))
        thisPsfImg *= counts

        # bbox a window in our image and add the fake star image
        psfBox = thisPsfImg.getBBox()
        psfBox.clip(bbox)
        if psfBox != thisPsfImg.getBBox():
            thisPsfImg = thisPsfImg[psfBox, afwImage.PARENT]
        imgSeg = img[psfBox, afwImage.PARENT]
        imgSeg += thisPsfImg
    meanSigma /= len(coordList)

    img += sky

    # add Poisson noise
    if (addPoissonNoise):
        np.random.seed(seed=1)  # make results reproducible
        imgArr = img.getArray()
        imgArr[:] = np.random.poisson(imgArr)

    # bundle into a maskedimage and an exposure
    mask = afwImage.Mask(bbox)
    var = img.convertFloat()
    img -= sky
    mimg = afwImage.MaskedImageF(img.convertFloat(), mask, var)
    exposure = afwImage.makeExposure(mimg)

    # insert an approximate psf
    psf = SingleGaussianPsf(kwid, kwid, meanSigma)
    exposure.setPsf(psf)

    return exposure
Exemplo n.º 28
0
def im_to_array(imfile, hdu):
    image = afw_image.ImageF(imfile, hdu)
    mask = afw_image.Mask(image.getDimensions())
    masked_image = afw_image.MaskedImageF(image, mask)
    md = afw_image.readMetadata(imfile, hdu)
    imsec = masked_image.Factory(masked_image, get_bbox(md.get('DATASEC')))
    values, mask, var = imsec.getArrays()
    values = values.flatten()
    values = values.astype(int)
 
    return values
Exemplo n.º 29
0
 def testMismatch(self):
     """Test that we get an exception when there's a size mismatch"""
     scale = 5
     dims = self.image.getDimensions()
     mask = afwImage.Mask(dims * scale)
     mask.set(0xFF)
     ctrl = afwMath.StatisticsControl()
     ctrl.setAndMask(0xFF)
     # If it didn't raise, this would result in a NaN (the image data is
     # completely masked).
     self.assertRaises(lsst.pex.exceptions.InvalidParameterError,
                       afwMath.makeStatistics, self.image, mask,
                       afwMath.MEDIAN, ctrl)
     subMask = afwImage.Mask(
         mask, afwGeom.Box2I(afwGeom.Point2I(dims * (scale - 1)), dims))
     subMask.set(0)
     # Using subMask is successful.
     self.assertEqual(
         afwMath.makeStatistics(self.image, subMask, afwMath.MEDIAN,
                                ctrl).getValue(), self.val)
Exemplo n.º 30
0
    def makeMaskAndSpanSetForOperationTest(self):
        firstMaskPart = afwGeom.SpanSet.fromShape(2, afwGeom.Stencil.BOX).shiftedBy(3, 2)
        secondMaskPart = afwGeom.SpanSet.fromShape(2, afwGeom.Stencil.BOX).shiftedBy(3, 8)
        spanSetMaskOperation = afwGeom.SpanSet.fromShape(2, afwGeom.Stencil.BOX).shiftedBy(3, 5)

        mask = afwImage.Mask(20, 20)
        firstMaskPart.setMask(mask, 3)
        secondMaskPart.setMask(mask, 3)
        spanSetMaskOperation.setMask(mask, 4)

        return mask, spanSetMaskOperation