def testNoiseCoadd(self):
        """Build a coadd from noise images and compare the histogram to a chi
        squared distribution
        """
        numImages = 4
        imShape = (150, 150)
        imSigma = 1.0
        imVariance = 1.0
        numBins = 200
        maxStdDevErr = 0.2
        maxMeanErr = 1.0e-12

        badMaskPlanes = ["EDGE"]

        np.random.seed(0)

        coadd = None
        for imInd in range(numImages):
            maskedImage = afwTestUtils.makeGaussianNoiseMaskedImage(
                dimensions=imShape, sigma=imSigma, variance=imVariance)
            exposure = afwImage.ExposureF(maskedImage)

            if not coadd:
                coadd = coaddChiSq.Coadd(bbox=exposure.getBBox(),
                                         wcs=exposure.getWcs(),
                                         badMaskPlanes=badMaskPlanes)

            coadd.addExposure(exposure)

        coadd.getWeightMap()
        coaddExposure = coadd.getCoadd()

        histX, histY, chiSqY = makeHistogram(coaddExposure.getMaskedImage(),
                                             numBins=numBins,
                                             numImages=numImages)

        if doPlot:
            pyplot.plot(histX, histY, drawstyle="steps")
            pyplot.plot(histX, chiSqY)
            pyplot.ylabel('frequency')
            pyplot.xlabel('sum of (counts/noise)^2')
            pyplot.show()

        errArr = (histY - chiSqY) * numBins  # crude scaling
        #         print "Mean error =", errArr.mean()
        #         print "Std dev error = ", errArr.std()
        if errArr.std() > maxStdDevErr:
            self.fail("Standard deviation of error = %s > %s limit" %
                      (errArr.std(), maxStdDevErr))
        if errArr.mean() > maxMeanErr:
            self.fail("Mean of error = %s > %s limit" %
                      (errArr.mean(), maxMeanErr))
    def testNoiseCoadd(self):
        """Build a coadd from noise images and compare the histogram to a chi squared distribution
        """
        numImages = 4
        imShape = (150, 150)
        imSigma = 1.0
        imVariance = 1.0
        numBins = 200
        maxStdDevErr = 0.2
        maxMeanErr = 1.0e-12

        badMaskPlanes = ["EDGE"]

        np.random.seed(0)

        coadd = None
        for imInd in range(numImages):
            maskedImage = afwTestUtils.makeGaussianNoiseMaskedImage(
                dimensions=imShape, sigma=imSigma, variance=imVariance)
            exposure = afwImage.ExposureF(maskedImage)

            if not coadd:
                coadd = coaddChiSq.Coadd(
                    bbox=exposure.getBBox(),
                    wcs=exposure.getWcs(),
                    badMaskPlanes=badMaskPlanes)

            coadd.addExposure(exposure)

        coadd.getWeightMap()
        coaddExposure = coadd.getCoadd()

        histX, histY, chiSqY = makeHistogram(
            coaddExposure.getMaskedImage(), numBins=numBins, numImages=numImages)

        if doPlot:
            pyplot.plot(histX, histY, drawstyle="steps")
            pyplot.plot(histX, chiSqY)
            pyplot.ylabel('frequency')
            pyplot.xlabel('sum of (counts/noise)^2')
            pyplot.show()

        errArr = (histY - chiSqY) * numBins  # crude scaling
#         print "Mean error =", errArr.mean()
#         print "Std dev error = ", errArr.std()
        if errArr.std() > maxStdDevErr:
            self.fail("Standard deviation of error = %s > %s limit" % (errArr.std(), maxStdDevErr))
        if errArr.mean() > maxMeanErr:
            self.fail("Mean of error = %s > %s limit" % (errArr.mean(), maxMeanErr))
    def testFilters(self):
        """Test that the coadd filter is set correctly
        """
        filterPolicyFile = pexPolicy.DefaultPolicyFile("afw",
                                                       "SdssFilters.paf",
                                                       "tests")
        filterPolicy = pexPolicy.Policy.createPolicy(
            filterPolicyFile, filterPolicyFile.getRepositoryPath(), True)
        imageUtils.defineFiltersFromPolicy(filterPolicy, reset=True)

        unkFilter = afwImage.Filter()
        gFilter = afwImage.Filter("g")
        rFilter = afwImage.Filter("r")

        imShape = (150, 150)
        imSigma = 1.0
        imVariance = 1.0

        badMaskPlanes = ["EDGE"]

        np.random.seed(0)

        coadd = None
        maskedImage = afwTestUtils.makeGaussianNoiseMaskedImage(
            dimensions=imShape, sigma=imSigma, variance=imVariance)
        inExp = afwImage.ExposureF(maskedImage)

        coadd = coaddChiSq.Coadd(
            bbox=inExp.getBBox(),
            wcs=inExp.getWcs(),
            badMaskPlanes=badMaskPlanes,
        )

        inExp.setFilter(gFilter)
        coadd.addExposure(inExp)
        self.assertEqualFilters(coadd.getCoadd().getFilter(), gFilter)
        self.assertEqualFilterSets(coadd.getFilters(), (gFilter, ))
        coadd.addExposure(inExp)
        self.assertEqualFilters(coadd.getCoadd().getFilter(), gFilter)
        self.assertEqualFilterSets(coadd.getFilters(), (gFilter, ))

        inExp.setFilter(rFilter)
        coadd.addExposure(inExp)
        self.assertEqualFilters(coadd.getCoadd().getFilter(), unkFilter)
        self.assertEqualFilterSets(coadd.getFilters(), (gFilter, rFilter))
    def testFilters(self):
        """Test that the coadd filter is set correctly
        """
        filterPolicyFile = pexPolicy.DefaultPolicyFile("afw", "SdssFilters.paf", "tests")
        filterPolicy = pexPolicy.Policy.createPolicy(
            filterPolicyFile, filterPolicyFile.getRepositoryPath(), True)
        imageUtils.defineFiltersFromPolicy(filterPolicy, reset=True)

        unkFilter = afwImage.Filter()
        gFilter = afwImage.Filter("g")
        rFilter = afwImage.Filter("r")

        imShape = (150, 150)
        imSigma = 1.0
        imVariance = 1.0

        badMaskPlanes = ["EDGE"]

        np.random.seed(0)

        coadd = None
        maskedImage = afwTestUtils.makeGaussianNoiseMaskedImage(
            dimensions=imShape, sigma=imSigma, variance=imVariance)
        inExp = afwImage.ExposureF(maskedImage)

        coadd = coaddChiSq.Coadd(
            bbox=inExp.getBBox(),
            wcs=inExp.getWcs(),
            badMaskPlanes=badMaskPlanes,
        )

        inExp.setFilter(gFilter)
        coadd.addExposure(inExp)
        self.assertEqualFilters(coadd.getCoadd().getFilter(), gFilter)
        self.assertEqualFilterSets(coadd.getFilters(), (gFilter,))
        coadd.addExposure(inExp)
        self.assertEqualFilters(coadd.getCoadd().getFilter(), gFilter)
        self.assertEqualFilterSets(coadd.getFilters(), (gFilter,))

        inExp.setFilter(rFilter)
        coadd.addExposure(inExp)
        self.assertEqualFilters(coadd.getCoadd().getFilter(), unkFilter)
        self.assertEqualFilterSets(coadd.getFilters(), (gFilter, rFilter))
    def testFilters(self):
        """Test that the coadd filter is set correctly
        """
        imageUtils.defineFilter("g", 468.6)
        imageUtils.defineFilter("r", 616.5)

        unkFilter = afwImage.Filter()
        gFilter = afwImage.Filter("g")
        rFilter = afwImage.Filter("r")

        imShape = (150, 150)
        imSigma = 1.0
        imVariance = 1.0

        badMaskPlanes = ["EDGE"]

        np.random.seed(0)

        coadd = None
        maskedImage = afwTestUtils.makeGaussianNoiseMaskedImage(
            dimensions=imShape, sigma=imSigma, variance=imVariance)
        inExp = afwImage.ExposureF(maskedImage)

        coadd = coaddChiSq.Coadd(
            bbox=inExp.getBBox(),
            wcs=inExp.getWcs(),
            badMaskPlanes=badMaskPlanes,
        )

        inExp.setFilter(gFilter)
        coadd.addExposure(inExp)
        self.assertEqualFilters(coadd.getCoadd().getFilter(), gFilter)
        self.assertEqualFilterSets(coadd.getFilters(), (gFilter, ))
        coadd.addExposure(inExp)
        self.assertEqualFilters(coadd.getCoadd().getFilter(), gFilter)
        self.assertEqualFilterSets(coadd.getFilters(), (gFilter, ))

        inExp.setFilter(rFilter)
        coadd.addExposure(inExp)
        self.assertEqualFilters(coadd.getCoadd().getFilter(), unkFilter)
        self.assertEqualFilterSets(coadd.getFilters(), (gFilter, rFilter))
    config = NoiseCoaddConfig()

    sys.stderr.write("""
coaddPath  = %s
numImages  = %d
imageShape = %s
imageSigma = %0.1f
variance   = %0.1f
""" % (coaddPath, numImages, config.imageShape, config.imageSigma, config.variance))
    
    numpy.random.seed(0)
    
    coadd = None
    for imInd in range(numImages):
        print >> sys.stderr, "Create exposure %d" % (imInd,)
        maskedImage = afwTestUtils.makeGaussianNoiseMaskedImage(
            dimensions=config.imageShape, sigma=config.imageSigma, variance=config.variance)
        # the WCS doesn't matter; the default will do
        wcs = afwImage.Wcs()
        exposure = afwImage.ExposureF(maskedImage, wcs)
        
        if not coadd:
            print >> sys.stderr, "Create coadd"
            coadd = coaddChiSq.Coadd.fromConfig(
                bbox = exposure.getBBox(),
                wcs = exposure.getWcs(),
                config = config.coadd)
            print >> sys.stderr, "badPixelMask=", coadd.getBadPixelMask()

        coadd.addExposure(exposure)

    print >> sys.stderr, "Save weight map as %s" % (weightPath,)
Esempio n. 7
0
        for lineNum, line in enumerate(infile):
            line = line.strip()
            if not line or line.startswith("#"):
                continue
            exposurePath = line
            expNum += 1

            try:
                print("Processing exposure %s" % (exposurePath, ),
                      file=sys.stderr)
                inputExposure = afwImage.ExposureF(exposurePath)

                print("Create Gaussian noise exposure", file=sys.stderr)
                maskedImage = afwTestUtils.makeGaussianNoiseMaskedImage(
                    dimensions=inputExposure.getDimensions(),
                    sigma=config.imageSigma,
                    variance=config.variance,
                )
                exposure = afwImage.ExposureF(maskedImage,
                                              inputExposure.getWcs())

                if config.saveDebugImages:
                    exposure.writeFits("exposure%d.fits" % (expNum, ))

                if not coadd:
                    print(
                        "Create warper and coadd with size and WCS matching the first exposure",
                        file=sys.stderr)
                    warper = afwMath.Warper.fromConfig(config.warp)
                    coadd = coaddChiSq.Coadd.fromConfig(
                        bbox=exposure.getBBox(),
    config = NoiseCoaddConfig()

    sys.stderr.write("""
coaddPath  = %s
numImages  = %d
imageShape = %s
imageSigma = %0.1f
variance   = %0.1f
""" % (coaddPath, numImages, config.imageShape, config.imageSigma, config.variance))

    np.random.seed(0)

    coadd = None
    for imInd in range(numImages):
        print("Create exposure %d" % (imInd,), file=sys.stderr)
        maskedImage = afwTestUtils.makeGaussianNoiseMaskedImage(
            dimensions=config.imageShape, sigma=config.imageSigma, variance=config.variance)
        exposure = afwImage.ExposureF(maskedImage)

        if not coadd:
            print("Create coadd", file=sys.stderr)
            coadd = coaddChiSq.Coadd.fromConfig(
                bbox=exposure.getBBox(),
                wcs=exposure.getWcs(),
                config=config.coadd)
            print("badPixelMask=", coadd.getBadPixelMask(), file=sys.stderr)

        coadd.addExposure(exposure)

    print("Save weight map as %s" % (weightPath,), file=sys.stderr)
    weightMap = coadd.getWeightMap()
    weightMap.writeFits(weightPath)
    with file(indata, "rU") as infile:
        for lineNum, line in enumerate(infile):
            line = line.strip()
            if not line or line.startswith("#"):
                continue
            exposurePath = line
            expNum += 1

            try:
                print("Processing exposure %s" % (exposurePath,), file=sys.stderr)
                inputExposure = afwImage.ExposureF(exposurePath)

                print("Create Gaussian noise exposure", file=sys.stderr)
                maskedImage = afwTestUtils.makeGaussianNoiseMaskedImage(
                    dimensions=inputExposure.getDimensions(),
                    sigma=config.imageSigma,
                    variance=config.variance,
                )
                exposure = afwImage.ExposureF(maskedImage, inputExposure.getWcs())

                if config.saveDebugImages:
                    exposure.writeFits("exposure%d.fits" % (expNum,))

                if not coadd:
                    print("Create warper and coadd with size and WCS matching the first exposure",
                          file=sys.stderr)
                    warper = afwMath.Warper.fromConfig(config.warp)
                    coadd = coaddChiSq.Coadd.fromConfig(
                        bbox=exposure.getBBox(),
                        wcs=exposure.getWcs(),
                        config=config.coadd,