def apply(self, policy, visitor, xloc, yloc, tmi, smi):
        # image statistics
        dStats = ipDiffim.ImageStatisticsF(policy)
        kc = ipDiffim.makeKernelCandidate(xloc, yloc, tmi, smi, policy)
        visitor.processCandidate(kc)
        kim = kc.getKernelImage(ipDiffim.KernelCandidateF.RECENT)
        diffIm = kc.getDifferenceImage(ipDiffim.KernelCandidateF.RECENT)
        kSum = kc.getKsum(ipDiffim.KernelCandidateF.RECENT)
        bg = kc.getBackground(ipDiffim.KernelCandidateF.RECENT)

        bbox = kc.getKernel(ipDiffim.KernelCandidateF.RECENT).shrinkBBox(
            diffIm.getBBox(afwImage.LOCAL))
        diffIm = afwImage.MaskedImageF(diffIm, bbox, origin=afwImage.LOCAL)
        dStats.apply(diffIm)

        dmean = afwMath.makeStatistics(diffIm.getImage(),
                                       afwMath.MEAN).getValue()
        dstd = afwMath.makeStatistics(diffIm.getImage(),
                                      afwMath.STDEV).getValue()
        vmean = afwMath.makeStatistics(diffIm.getVariance(),
                                       afwMath.MEAN).getValue()
        return kSum, bg, dmean, dstd, vmean, kim, diffIm, kc, dStats
Example #2
0
    def read(self, dirName="."):
        """Read self's pfsFiberTrace file from directory dirName"""
        if not pyfits:
            raise RuntimeError(
                "I failed to import astropy.io.fits, so cannot read from disk")

        fileName = PfsFiberTrace.fileNameFormat % (self.obsDate, self.visit0,
                                                   self.arm, self.spectrograph)

        self.metadata = afwFits.readMetadata(os.path.join(dirName, fileName),
                                             0, True)
        self.metadata.remove(
            "COMMENT")  # Added by FITS writer, not stripped (!)
        allTracesMI = afwImage.MaskedImageF(os.path.join(dirName, fileName))

        with pyfits.open(os.path.join(dirName, fileName)) as fd:
            hdu = fd["ID_BOX"]
            self.fiberId = hdu.data['FIBERID']
            minX = hdu.data['MINX']
            minY = hdu.data['MINY']
            maxX = hdu.data['MAXX']
            maxY = hdu.data['MAXY']

        self.traces = []
        x0 = 0
        for i in range(len(self.fiberId)):
            # bbox: BBox in full (i.e. data) image
            bbox = lsst.geom.BoxI(lsst.geom.PointI(minX[i], minY[i]),
                                  lsst.geom.PointI(maxX[i], maxY[i]))

            # bboxAllTMI: BBox in allTracesMI
            bboxAllTMI = lsst.geom.BoxI(lsst.geom.PointI(x0, bbox.getMinY()),
                                        bbox.getDimensions())

            trace = allTracesMI[bboxAllTMI].clone()
            trace.setXY0(bbox.getBegin())

            self.traces.append(trace)
            x0 += bbox.getWidth()
Example #3
0
def makeFakeImage(bbox, centerList, fluxList, fwhm, var):
    """Make a fake image containing a set of stars variance = image + var

    (It is trivial to add Poisson noise, which would be more accurate,
    but hard to make a unit test  that can reliably determine whether such an image passes a test)

    @param[in] bbox: bounding box for image
    @param[in] centerList: list of positions of center of star on image (pairs of float)
    @param[in] fluxList: flux of each star, in counts
    @param[in] fwhm: FWHM of Gaussian star, in pixels
    @param[in] var: value of variance plane (counts)
    """
    if len(centerList) != len(fluxList):
        raise RuntimeError("len(centerList) != len(fluxList)")
    maskedImage = afwImage.MaskedImageF(bbox)
    image = maskedImage.getImage()
    for center, flux in zip(centerList, fluxList):
        addStar(image, center=center, flux=flux, fwhm=fwhm)
    variance = maskedImage.getVariance()
    variance[:] = image
    variance += var
    return maskedImage
Example #4
0
    def testTranspose(self):
        """Test transposition before interpolation

        Interpolate over a bad row (not a bad column).
        """
        box = lsst.geom.Box2I(lsst.geom.Point2I(12345, 6789),
                              lsst.geom.Extent2I(123, 45))
        value = 123.45
        bad = "BAD"
        image = afwImage.MaskedImageF(box)
        image.image.set(value)
        image.mask.set(0)

        badRow = box.getHeight() // 2
        image.image.array[badRow] = 10 * value
        image.mask.array[badRow] = image.mask.getPlaneBitMask(bad)

        config = InterpImageTask.ConfigClass()
        config.transpose = True
        task = InterpImageTask(config)
        task.run(image, planeName=bad, fwhmPixels=self.FWHM)
        self.assertFloatsEqual(image.image.array, value)
    def _gridImage(self, maskedImage, binsize, statsFlag):
        """Private method to grid an image for debugging"""
        width, height = maskedImage.getDimensions()
        x0, y0 = maskedImage.getXY0()
        xedges = numpy.arange(0, width, binsize)
        yedges = numpy.arange(0, height, binsize)
        xedges = numpy.hstack((xedges, width))  # add final edge
        yedges = numpy.hstack((yedges, height))  # add final edge

        # Use lists/append to protect against the case where
        # a bin has no valid pixels and should not be included in the fit
        bgX = []
        bgY = []
        bgZ = []
        bgdZ = []

        for ymin, ymax in zip(yedges[0:-1], yedges[1:]):
            for xmin, xmax in zip(xedges[0:-1], xedges[1:]):
                subBBox = afwGeom.Box2I(
                    afwGeom.PointI(int(x0 + xmin), int(y0 + ymin)),
                    afwGeom.PointI(int(x0 + xmax - 1), int(y0 + ymax - 1)))
                subIm = afwImage.MaskedImageF(maskedImage, subBBox,
                                              afwImage.PARENT, False)
                stats = afwMath.makeStatistics(
                    subIm, afwMath.MEAN | afwMath.MEANCLIP | afwMath.MEDIAN
                    | afwMath.NPOINT | afwMath.STDEV, self.sctrl)
                npoints, _ = stats.getResult(afwMath.NPOINT)
                if npoints >= 2:
                    stdev, _ = stats.getResult(afwMath.STDEV)
                    if stdev < self.config.gridStdevEpsilon:
                        stdev = self.config.gridStdevEpsilon
                    bgX.append(0.5 * (x0 + xmin + x0 + xmax))
                    bgY.append(0.5 * (y0 + ymin + y0 + ymax))
                    bgdZ.append(stdev / numpy.sqrt(npoints))
                    est, _ = stats.getResult(statsFlag)
                    bgZ.append(est)

        return numpy.array(bgX), numpy.array(bgY), numpy.array(
            bgZ), numpy.array(bgdZ)
Example #6
0
    def testBadRows(self):
        """Test that a bad set of rows in an image doesn't cause a failure"""
        initialValue = 20
        mi = afwImage.MaskedImageF(500, 200)
        mi.set((initialValue, 0x0, 1.0))
        im = mi.getImage()
        im[:, 0:100] = np.nan
        del im
        msk = mi.getMask()
        badBits = msk.getPlaneBitMask(
            ['EDGE', 'DETECTED', 'DETECTED_NEGATIVE'])
        msk[0:400, :] |= badBits
        del msk

        if debugMode:
            ds9.mtv(mi, frame=0)

        sctrl = afwMath.StatisticsControl()
        sctrl.setAndMask(badBits)
        nx, ny = 17, 17
        bctrl = afwMath.BackgroundControl(nx, ny, sctrl, afwMath.MEANCLIP)

        bkgd = afwMath.makeBackground(mi, bctrl)
        statsImage = bkgd.getStatsImage()
        if debugMode:
            ds9.mtv(statsImage, frame=1)

        # the test is that this doesn't fail if the bug (#2297) is fixed
        for frame, interpStyle in enumerate([
                afwMath.Interpolate.CONSTANT, afwMath.Interpolate.LINEAR,
                afwMath.Interpolate.NATURAL_SPLINE,
                afwMath.Interpolate.AKIMA_SPLINE
        ], 2):
            bkgdImage = bkgd.getImageF(interpStyle,
                                       afwMath.REDUCE_INTERP_ORDER)
            self.assertEqual(np.mean(bkgdImage[0:100, 0:100].getArray()),
                             initialValue)
            if debugMode:
                ds9.mtv(bkgdImage, frame=frame)
Example #7
0
def test_ps_dmpsf_offset_smoke():
    rng = np.random.RandomState(12)

    dim = 20
    masked_image = afw_image.MaskedImageF(dim, dim)
    exp = afw_image.ExposureF(masked_image)

    psf_dim = 15
    wcs = make_sim_wcs(dim)

    pspsf = make_ps_psf(rng=rng, dim=dim)
    fpsf = make_dm_psf(psf=pspsf, psf_dim=psf_dim, wcs=wcs)
    exp.setPsf(fpsf)

    psf = exp.getPsf()

    x = 8.5
    y = 10.1
    pos = geom.Point2D(x=x, y=y)
    gs_pos = galsim.PositionD(x=x, y=y)

    # this one is shifted
    msim = psf.computeImage(pos)
    assert msim.array.shape == (psf_dim, psf_dim)

    offset_x = x - int(x + 0.5)
    offset_y = y - int(y + 0.5)

    offset = (offset_x, offset_y)

    gspsf = pspsf.getPSF(gs_pos)
    gsim = gspsf.drawImage(
        nx=psf_dim,
        ny=psf_dim,
        offset=offset,
        wcs=wcs.local(image_pos=gs_pos),
    )

    assert np.allclose(msim.array, gsim.array)
Example #8
0
    def makeRamp(self, binsize=1):
        """Make a linear ramp"""
        ramp = afwImage.MaskedImageF(20, 40)

        x = []
        for i in range(ramp.getWidth()):
            x.append((i + 0.5)*binsize - 0.5)

        y = []
        for j in range(ramp.getHeight()):
            y.append((j + 0.5)*binsize - 0.5)

        var = 1
        rampCoeffs = (1000, 1, 1)
        for i in range(ramp.getHeight()):
            for j in range(ramp.getWidth()):
                ramp.set(
                    j, i,
                    (rampCoeffs[0] + rampCoeffs[1]*x[j] + rampCoeffs[2]*y[i],
                     0x0, var))

        return ramp, rampCoeffs, x, y
    def testTransformBasedWarp(self):
        """Test warping using TransformPoint2ToPoint2
        """
        for interpLength in (0, 1, 2, 4):
            kernelName = "lanczos3"
            rtol = 4e-5
            atol = 1e-2
            warpingControl = afwMath.WarpingControl(
                warpingKernelName=kernelName,
                interpLength=interpLength,
            )

            originalExposure = afwImage.ExposureF(originalExposurePath)
            originalMetadata = afwImage.DecoratedImageF(originalExposurePath).getMetadata()
            originalSkyWcs = afwGeom.makeSkyWcs(originalMetadata)

            swarpedImageName = "medswarp1%s.fits" % (kernelName,)
            swarpedImagePath = os.path.join(dataDir, swarpedImageName)
            swarpedDecoratedImage = afwImage.DecoratedImageF(swarpedImagePath)
            swarpedImage = swarpedDecoratedImage.getImage()

            swarpedMetadata = swarpedDecoratedImage.getMetadata()
            warpedSkyWcs = afwGeom.makeSkyWcs(swarpedMetadata)

            # original image is source, warped image is destination
            srcToDest = afwGeom.makeWcsPairTransform(originalSkyWcs, warpedSkyWcs)

            afwWarpedMaskedImage = afwImage.MaskedImageF(swarpedImage.getDimensions())
            originalMaskedImage = originalExposure.getMaskedImage()

            numGoodPix = afwMath.warpImage(afwWarpedMaskedImage, originalMaskedImage,
                                           srcToDest, warpingControl)
            self.assertGreater(numGoodPix, 50)

            afwWarpedImage = afwWarpedMaskedImage.getImage()
            afwWarpedImageArr = afwWarpedImage.getArray()
            noDataMaskArr = np.isnan(afwWarpedImageArr)
            self.assertImagesAlmostEqual(afwWarpedImage, swarpedImage,
                                         skipMask=noDataMaskArr, rtol=rtol, atol=atol)
Example #10
0
    def testBadPatch(self):
        """Test that a large bad patch of an image doesn't cause an absolute failure"""
        initialValue = 20
        mi = afwImage.MaskedImageF(500, 200)
        mi.set((initialValue, 0x0, 1.0))
        mi.image[0:200, :] = np.nan
        badBits = mi.mask.getPlaneBitMask(
            ['EDGE', 'DETECTED', 'DETECTED_NEGATIVE'])
        mi.mask[0:400, :] |= badBits

        if debugMode:
            ds9.mtv(mi, frame=0)

        sctrl = afwMath.StatisticsControl()
        sctrl.setAndMask(badBits)
        nx, ny = 17, 17
        bctrl = afwMath.BackgroundControl(nx, ny, sctrl, afwMath.MEANCLIP)

        bkgd = afwMath.makeBackground(mi, bctrl)
        statsImage = bkgd.getStatsImage()
        if debugMode:
            ds9.mtv(statsImage, frame=1)

        # the test is that this doesn't fail if the bug (#2297) is fixed
        bkgdImage = bkgd.getImageF(afwMath.Interpolate.NATURAL_SPLINE,
                                   afwMath.REDUCE_INTERP_ORDER)
        self.assertEqual(np.mean(bkgdImage[0:100, 0:100].array), initialValue)
        if debugMode:
            ds9.mtv(bkgdImage, frame=2)
        # Check that we can fix the NaNs in the statsImage
        sim = statsImage.getImage().getArray()
        sim[np.isnan(sim)] = initialValue  # replace NaN by initialValue
        bkgdImage = bkgd.getImageF(afwMath.Interpolate.NATURAL_SPLINE,
                                   afwMath.REDUCE_INTERP_ORDER)

        self.assertAlmostEqual(
            np.mean(bkgdImage[0:100, 0:100].array, dtype=np.float64),
            initialValue)
Example #11
0
    def testTicket1123(self):
        """
        Ticket #1123 reported that the Statistics stack routine throws an exception
        when all pixels in a stack are masked.  Returning a NaN pixel in the stack is preferred
        """

        mean = self.images[0][1]

        ctrl = afwMath.StatisticsControl()
        ctrl.setAndMask(~0x0)

        mimg = afwImage.MaskedImageF(lsst.geom.Extent2I(10, 10))
        mimg.set([mean, 0x1, mean])

        # test the case with no valid pixels ... both mean and stdev should be
        # nan
        stat = afwMath.makeStatistics(mimg, afwMath.MEAN | afwMath.STDEV, ctrl)
        mean = stat.getValue(afwMath.MEAN)
        stdev = stat.getValue(afwMath.STDEV)
        self.assertNotEqual(mean, mean)  # NaN does not equal itself
        self.assertNotEqual(stdev, stdev)  # NaN does not equal itself

        # test the case with one valid pixel ... mean is ok, but stdev should
        # still be nan
        mimg.getMask()[1, 1] = 0x0
        stat = afwMath.makeStatistics(mimg, afwMath.MEAN | afwMath.STDEV, ctrl)
        mean = stat.getValue(afwMath.MEAN)
        stdev = stat.getValue(afwMath.STDEV)
        self.assertEqual(mean, mean)
        self.assertNotEqual(stdev, stdev)  # NaN does not equal itself

        # test the case with two valid pixels ... both mean and stdev are ok
        mimg.getMask()[1, 2] = 0x0
        stat = afwMath.makeStatistics(mimg, afwMath.MEAN | afwMath.STDEV, ctrl)
        mean = stat.getValue(afwMath.MEAN)
        stdev = stat.getValue(afwMath.STDEV)
        self.assertEqual(mean, mean)
        self.assertEqual(stdev, 0.0)
Example #12
0
    def testSetMembers(self):
        """
        Test that the MaskedImage and the WCS of an Exposure can be set.
        """
        exposure = afwImage.ExposureF()

        maskedImage = afwImage.MaskedImageF(inFilePathSmall)
        exposure.setMaskedImage(maskedImage)
        exposure.setWcs(self.wcs)
        exposure.setDetector(self.detector)
        exposure.setFilter(afwImage.Filter("g"))

        self.assertEqual(exposure.getDetector().getName(),
                         self.detector.getName())
        self.assertEqual(exposure.getDetector().getSerial(),
                         self.detector.getSerial())
        self.assertEqual(exposure.getFilter().getName(), "g")
        self.assertEqual(exposure.getWcs(), self.wcs)

        # The PhotoCalib tests are in test_photoCalib.py;
        # here we just check that it's gettable and settable.
        self.assertIsNone(exposure.getPhotoCalib())

        photoCalib = afwImage.PhotoCalib(511.1, 44.4)
        exposure.setPhotoCalib(photoCalib)
        self.assertEqual(exposure.getPhotoCalib(), photoCalib)

        # Psfs next
        self.assertFalse(exposure.hasPsf())
        exposure.setPsf(self.psf)
        self.assertTrue(exposure.hasPsf())

        exposure.setPsf(DummyPsf(1.0))  # we can reset the Psf

        # Test that we can set the MaskedImage and WCS of an Exposure
        # that already has both
        self.exposureMiWcs.setMaskedImage(maskedImage)
        exposure.setWcs(self.wcs)
Example #13
0
    def testWeightedStack(self):
        """ Test statisticsStack() function when weighting by a variance plane"""

        sctrl = afwMath.StatisticsControl()
        sctrl.setWeighted(True)
        mimgList = []
        for val in self.values:
            mimg = afwImage.MaskedImageF(afwGeom.Extent2I(self.nX, self.nY))
            mimg.set(val, 0x0, val)
            mimgList.append(mimg)
        mimgStack = afwMath.statisticsStack(mimgList, afwMath.MEAN, sctrl)

        wvalues = [1.0/q for q in self.values]
        wmean = float(len(self.values)) / reduce(lambda x, y: x + y, wvalues)
        self.assertAlmostEqual(
            mimgStack.getImage().get(self.nX//2, self.nY//2),
            wmean)

        # Test in-place stacking
        afwMath.statisticsStack(mimgStack, mimgList, afwMath.MEAN, sctrl)
        self.assertAlmostEqual(
            mimgStack.getImage().get(self.nX//2, self.nY//2),
            wmean)
Example #14
0
    def testReadWriteXY0(self):
        """Test that we read and write (X0, Y0) correctly"""
        im = afwImage.MaskedImageF(afwGeom.Extent2I(10, 20))

        x0, y0 = 1, 2
        im.setXY0(x0, y0)
        tmpFile = "foo.fits"
        im.writeFits(tmpFile)

        im2 = im.Factory(tmpFile)
        self.assertEqual(im2.getX0(), x0)
        self.assertEqual(im2.getY0(), y0)

        self.assertEqual(im2.getImage().getX0(), x0)
        self.assertEqual(im2.getImage().getY0(), y0)

        self.assertEqual(im2.getMask().getX0(), x0)
        self.assertEqual(im2.getMask().getY0(), y0)
        
        self.assertEqual(im2.getVariance().getX0(), x0)
        self.assertEqual(im2.getVariance().getY0(), y0)
        
        os.remove(tmpFile)
Example #15
0
    def testSetExposureInfo(self):
        exposureInfo = afwImage.ExposureInfo()
        exposureInfo.setWcs(self.wcs)
        exposureInfo.setDetector(self.detector)
        gFilter = afwImage.Filter("g")
        gFilterLabel = afwImage.FilterLabel(band="g")
        exposureInfo.setFilter(gFilter)
        exposureInfo.setFilterLabel(gFilterLabel)
        maskedImage = afwImage.MaskedImageF(inFilePathSmall)
        exposure = afwImage.ExposureF(maskedImage)
        self.assertFalse(exposure.hasWcs())

        exposure.setInfo(exposureInfo)

        self.assertTrue(exposure.hasWcs())
        self.assertEqual(exposure.getWcs().getPixelOrigin(),
                         self.wcs.getPixelOrigin())
        self.assertEqual(exposure.getDetector().getName(),
                         self.detector.getName())
        self.assertEqual(exposure.getDetector().getSerial(),
                         self.detector.getSerial())
        self.assertEqual(exposure.getFilter(), gFilter)
        self.assertEqual(exposure.getFilterLabel(), gFilterLabel)
Example #16
0
    def setUp(self):
        self.ms = afwImage.MaskedImageF(lsst.geom.Extent2I(12, 8))
        im = self.ms.getImage()
        #
        # Objects that we should detect
        #
        self.objects = []
        self.objects += [Object(10, [(1, 4, 4), (2, 3, 5), (3, 4, 4)])]
        self.objects += [Object(20, [(5, 7, 8), (6, 8, 8)])]
        self.objects += [Object(20, [(5, 10, 10)])]
        self.objects += [Object(30, [(6, 3, 3)])]

        im.set(0)  # clear image
        for obj in self.objects:
            obj.insert(im)

        self.NaN = float("NaN")
        im[3, 7, afwImage.LOCAL] = self.NaN
        im[0, 0, afwImage.LOCAL] = self.NaN
        im[8, 2, afwImage.LOCAL] = self.NaN

        # connects the two objects with value==20 together if NaN is detected
        im[9, 6, afwImage.LOCAL] = self.NaN
Example #17
0
def main000():
    butils = measDeblend.BaselineUtilsF
    S = 20
    mim = afwImg.MaskedImageF(S, S)
    x0, y0 = S / 2, S / 2
    im = mim.getImage().getArray()
    peakTable = afwDet.PeakTable.make(afwDet.PeakTable.makeMinimalSchema)
    peak = makePeak(x0, y0)

    im[:, :] = 5.
    im[y0, x0] = 10.
    im[y0, x0 + 2] = 1.

    if plt:
        plt.clf()
        plt.imshow(im, origin='lower', interpolation='nearest')
        plt.savefig('im2.png')
        butils.makeMonotonic(mim, peak)
        plt.clf()
        plt.imshow(mim.getImage().getArray(),
                   origin='lower',
                   interpolation='nearest')
        plt.savefig('mono2.png')
Example #18
0
def footprintToImage(fp, mi=None, mask=False):
    if fp.isHeavy():
        fp = afwDetection.cast_HeavyFootprintF(fp)
    elif mi is None:
        print >> sys.stderr, "Unable to make a HeavyFootprint as image is None"
    else:
        fp = afwDetection.makeHeavyFootprint(fp, mi)
    bb = fp.getBBox()
    if mask:
        im = afwImage.MaskedImageF(bb.getWidth(), bb.getHeight())
    else:
        im = afwImage.ImageF(bb.getWidth(), bb.getHeight())
    im.setXY0(bb.getMinX(), bb.getMinY())

    try:
        fp.insert(im)
    except AttributeError:              # we failed to make it heavy
        assert not mi
        pass
    
    if mask:
        im = im.getMask()
    return im
Example #19
0
    def setUp(self):
        maskedImage = afwImage.MaskedImageF(inFilePathSmall)
        maskedImageMD = afwImage.readMetadata(inFilePathSmall)

        self.smallExposure = afwImage.ExposureF(inFilePathSmall)
        self.width = maskedImage.getWidth()
        self.height = maskedImage.getHeight()
        self.wcs = afwImage.makeWcs(maskedImageMD)
        self.psf = DummyPsf(2.0)
        self.detector = DetectorWrapper().detector

        self.exposureBlank = afwImage.ExposureF()
        self.exposureMiOnly = afwImage.makeExposure(maskedImage)
        self.exposureMiWcs = afwImage.makeExposure(maskedImage, self.wcs)
        self.exposureCrWcs = afwImage.ExposureF(100, 100, self.wcs)         # n.b. the (100, 100, ...) form
        self.exposureCrOnly = afwImage.ExposureF(afwGeom.ExtentI(100, 100))  # test with ExtentI(100, 100) too

        afwImage.Filter.reset()
        afwImage.FilterProperty.reset()

        filterPolicy = pexPolicy.Policy()
        filterPolicy.add("lambdaEff", 470.0)
        afwImage.Filter.define(afwImage.FilterProperty("g", filterPolicy))
def result(fits):

 
    cat = pysex.run(fits, params=['X_IMAGE', 'Y_IMAGE', 'FLUX_APER'], 
                    conf_args={'PHOT_APERTURES':5}) 
    print cat['FLUX_APER']  
    
    image = afwImage.MaskedImageF(fits)
    
    binsize   = 128
    nx = int(image.getWidth()/binsize) + 1
    ny = int(image.getHeight()/binsize) + 1
    bctrl = afwMath.BackgroundControl(nx, ny)

    bkgd = afwMath.makeBackground(image, bctrl)

    statsImage = afwMath.cast_BackgroundMI(bkgd).getStatsImage()
    
    image  -= bkgd.getImageF(afwMath.Interpolate.NATURAL_SPLINE)

    return bkgd

    return 
Example #21
0
    def setUp(self):
        self.config = ipDiffim.ImagePsfMatchTask.ConfigClass()
        self.subconfig = self.config.kernel.active
        self.ps = pexConfig.makePropertySet(self.subconfig)
        self.kSize = self.ps['kernelSize']

        # gaussian reference kernel
        self.gSize = self.kSize
        self.gaussFunction = afwMath.GaussianFunction2D(2, 3)
        self.gaussKernel = afwMath.AnalyticKernel(self.gSize, self.gSize,
                                                  self.gaussFunction)

        if defDataDir:
            defImagePath = os.path.join(defDataDir, "DC3a-Sim", "sci", "v5-e0",
                                        "v5-e0-c011-a00.sci.fits")
            self.templateImage = afwImage.MaskedImageF(defImagePath)
            self.scienceImage = self.templateImage.Factory(
                self.templateImage.getDimensions())

            convolutionControl = afwMath.ConvolutionControl()
            convolutionControl.setDoNormalize(False)
            afwMath.convolve(self.scienceImage, self.templateImage,
                             self.gaussKernel, convolutionControl)
Example #22
0
    def testImageSlices(self):
        """Test image slicing, which generate sub-images using Box2I under the covers"""
        im = afwImage.MaskedImageF(10, 20)
        im[4, 10] = (10, 0x2, 100)
        im[-3:, -2:, afwImage.LOCAL] = 100
        sim = im[1:4, 6:10]
        nan = -666  # a real NaN != NaN so tests fail
        sim[:] = (-1, 0x8, nan)
        im[0:4, 0:4] = im[2:6, 8:12]

        if display:
            ds9.mtv(im)

        self.assertEqual(im[0, 6, afwImage.LOCAL], (0, 0x0, 0))
        self.assertEqual(im[6, 17, afwImage.LOCAL], (0, 0x0, 0))
        self.assertEqual(im[7, 18, afwImage.LOCAL], (100, 0x0, 0))
        self.assertEqual(im[9, 19, afwImage.LOCAL], (100, 0x0, 0))
        self.assertEqual(im[1, 6, afwImage.LOCAL], (-1, 0x8, nan))
        self.assertEqual(im[3, 9, afwImage.LOCAL], (-1, 0x8, nan))
        self.assertEqual(im[4, 10, afwImage.LOCAL], (10, 0x2, 100))
        self.assertEqual(im[4, 9, afwImage.LOCAL], (0, 0x0, 0))
        self.assertEqual(im[2, 2, afwImage.LOCAL], (10, 0x2, 100))
        self.assertEqual(im[0, 0, afwImage.LOCAL], (-1, 0x8, nan))
Example #23
0
    def testImageSlices(self):
        """Test image slicing, which generate sub-images using Box2I under the covers"""
        im = afwImage.MaskedImageF(10, 20)
        im[4, 10] = (10, 0x2, 100)
        im[-3:, -2:] = 100
        sim = im[1:4, 6:10]
        nan = -666  # a real NaN != NaN so tests fail
        sim[:] = (-1, 0x8, nan)
        im[0:4, 0:4] = im[2:6, 8:12]

        if display:
            ds9.mtv(im)

        self.assertEqual(im.get(0, 6), (0, 0x0, 0))
        self.assertEqual(im.get(6, 17), (0, 0x0, 0))
        self.assertEqual(im.get(7, 18), (100, 0x0, 0))
        self.assertEqual(im.get(9, 19), (100, 0x0, 0))
        self.assertEqual(im.get(1, 6), (-1, 0x8, nan))
        self.assertEqual(im.get(3, 9), (-1, 0x8, nan))
        self.assertEqual(im.get(4, 10), (10, 0x2, 100))
        self.assertEqual(im.get(4, 9), (0, 0x0, 0))
        self.assertEqual(im.get(2, 2), (10, 0x2, 100))
        self.assertEqual(im.get(0, 0), (-1, 0x8, nan))
Example #24
0
def makeFakeImage(nx, ny, xys, fluxes, fwhms):

    mimg = afwImage.MaskedImageF(nx, ny)
    img = mimg.getImage().getArray()
    var = mimg.getVariance().getArray()
    var[:, :] = 1.0

    nSrc = len(xys)

    n = min([nx, ny])

    for i in range(nSrc):
        x, y = nx*xys[i][0], ny*xys[i][1]
        src = measAlg.DoubleGaussianPsf(n, n, fwhms[i], fwhms[i], 0.03)
        pimg = src.computeImage(afwGeom.Point2D(x, y))
        pbox = pimg.getBBox(afwImage.PARENT)
        pbox.clip(mimg.getBBox())
        pimg = pimg.Factory(pimg, pbox, afwImage.PARENT)
        xlo, xhi = pbox.getMinX(), pbox.getMaxX() + 1
        ylo, yhi = pbox.getMinY(), pbox.getMaxY() + 1
        img[ylo:yhi, xlo:xhi] += fluxes[i]*pimg.getArray()

    return mimg
Example #25
0
    def testReturnInputs(self):
        """ Make sure that a single file put into the stacker is returned unscathed"""

        imgList = []

        img = afwImage.MaskedImageF(afwGeom.Extent2I(10, 20))
        for y in range(img.getHeight()):
            simg = img.Factory(
                img,
                afwGeom.Box2I(afwGeom.Point2I(0, y),
                              afwGeom.Extent2I(img.getWidth(), 1)),
                afwImage.LOCAL)
            simg.set(y)

        imgList.append(img)

        imgStack = afwMath.statisticsStack(imgList, afwMath.MEAN)

        if display:
            ds9.mtv(img, frame=1, title="input")
            ds9.mtv(imgStack, frame=2, title="stack")

        self.assertEqual(img.get(0, 0)[0], imgStack.get(0, 0)[0])
Example #26
0
    def testImageStatisticsMask1(self):
        # Mask value that gets ignored
        maskPlane = self.ps.getArray("badMaskPlanes")[0]
        maskVal = afwImage.Mask.getPlaneBitMask(maskPlane)
        numArray = num.ones((20, 19))
        mi = afwImage.MaskedImageF(geom.Extent2I(20, 20))
        for j in range(mi.getHeight()):
            for i in range(mi.getWidth()):
                val = i + 2.3 * j

                if i == 19:
                    mi[i, j, afwImage.LOCAL] = (val, maskVal, 1)
                else:
                    mi[i, j, afwImage.LOCAL] = (val, 0x0, 1)
                    numArray[j][i] = val

        imstat = ipDiffim.ImageStatisticsF(self.ps)
        imstat.apply(mi)

        self.assertAlmostEqual(imstat.getMean(), numArray.mean())
        # note that these don't agree exactly...
        self.assertAlmostEqual(imstat.getRms(), numArray.std(), 1)
        self.assertEqual(imstat.getNpix(), 20 * (20 - 1))
Example #27
0
    def setUp(self):
        self.mi = afwImage.MaskedImageF(
            os.path.join(afwdataDir, "CFHT", "D4",
                         "cal-53535-i-797722_1.fits"))

        self.FWHM = 5
        self.psf = algorithms.DoubleGaussianPsf(
            15, 15, self.FWHM / (2 * math.sqrt(2 * math.log(2))))

        if False:  # 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))
        else:  # use sub-image
            self.XY0 = afwGeom.PointI(824, 140)
            self.mi = self.mi.Factory(
                self.mi, afwGeom.BoxI(self.XY0, afwGeom.ExtentI(256, 256)),
                afwImage.LOCAL)

        self.mi.getMask().addMaskPlane("DETECTED")
        self.exposure = afwImage.makeExposure(self.mi)
Example #28
0
def makeFakeImage(bbox, centerList, instFluxList, fwhm, var):
    """Make a fake image containing a set of stars with variance = image + var.

    Paramters
    ---------
    bbox : `lsst.afw.image.Box2I`
        Bounding box for image.
    centerList : iterable of pairs of `float`
        list of positions of center of star on image.
    instFluxList : `list` of `float`
        instFlux of each star, in counts.
    fwhm : `float`
        FWHM of Gaussian star, in pixels.
    var : `float`
        Value of variance plane, in counts.

    Returns
    -------
    maskedImage : `lsst.afw.image.MaskedImageF`
        Resulting fake image.

    Notes
    -----
    It is trivial to add Poisson noise, which would be more accurate, but
    hard to make a unit test that can reliably determine whether such an
    image passes a test.
    """
    if len(centerList) != len(instFluxList):
        raise RuntimeError("len(centerList) != len(instFluxList)")
    maskedImage = afwImage.MaskedImageF(bbox)
    image = maskedImage.getImage()
    for center, instFlux in zip(centerList, instFluxList):
        addStar(image, center=center, instFlux=instFlux, fwhm=fwhm)
    variance = maskedImage.getVariance()
    variance[:] = image
    variance += var
    return maskedImage
Example #29
0
    def read(self, dirName="."):
        """Read self's pfsFiberTrace file from directory dirName"""
        if not pyfits:
            raise RuntimeError(
                "I failed to import pyfits, so cannot read from disk")

        fileName = PfsFiberTrace.fileNameFormat % (self.obsDate, self.arm,
                                                   self.spectrograph)

        self.metadata = dafBase.PropertySet()
        allTracesMI = afwImage.MaskedImageF(os.path.join(dirName, fileName),
                                            self.metadata)

        with pyfits.open(os.path.join(dirName, fileName)) as fd:
            hdu = fd["ID_BOX"]
            self.fiberId = hdu.data['FIBERID']
            minX = hdu.data['MINX']
            minY = hdu.data['MINY']
            maxX = hdu.data['MAXX']
            maxY = hdu.data['MAXY']

        self.traces = []
        x0 = 0
        for i in range(len(self.fiberId)):
            # bbox: BBox in full (i.e. data) image
            bbox = afwGeom.BoxI(afwGeom.PointI(minX[i], minY[i]),
                                afwGeom.PointI(maxX[i], maxY[i]))

            # bboxAllTMI: BBox in allTracesMI
            bboxAllTMI = afwGeom.BoxI(afwGeom.PointI(x0, bbox.getMinY()),
                                      bbox.getDimensions())

            trace = allTracesMI[bboxAllTMI].clone()
            trace.setXY0(bbox.getBegin())

            self.traces.append(trace)
            x0 += bbox.getWidth()
Example #30
0
    def testWeightedStats(self):
        """Test that bug from #1697 (weighted stats returning NaN) stays fixed."""

        rand = afwMath.Random()
        mu = 10000

        afwImage.MaskU.getPlaneBitMask("EDGE")

        badPixelMask = afwImage.MaskU.getPlaneBitMask("EDGE")
        statsCtrl = afwMath.StatisticsControl()
        statsCtrl.setNumSigmaClip(3.0)
        statsCtrl.setNumIter(2)
        statsCtrl.setAndMask(badPixelMask)

        for weight in (300.0, 10.0, 1.0):
            print("Testing with weight=%0.1f" % (weight, ))
            maskedImageList = afwImage.vectorMaskedImageF(
            )  # [] is rejected by afwMath.statisticsStack
            weightList = []

            nx, ny = 256, 256
            for i in range(3):
                print("Processing ", i)
                maskedImage = afwImage.MaskedImageF(nx, ny)
                maskedImageList.append(maskedImage)

                afwMath.randomPoissonImage(maskedImage.getImage(), rand, mu)
                maskedImage.getVariance().set(mu)
                weightList.append(weight)

            self.reportBadPixels(maskedImage, badPixelMask)

            print("Stack: ", end=' ')
            coaddMaskedImage = afwMath.statisticsStack(maskedImageList,
                                                       afwMath.MEANCLIP,
                                                       statsCtrl, weightList)
            self.reportBadPixels(coaddMaskedImage, badPixelMask)