def testSubMap(self):
        if hasattr(afwGeom, 'makePointI'):
            # old afw (pre-#1556) interface
            bbox = afwGeom.BoxI(afwGeom.makePointI(200, 100),
                                afwGeom.makeExtentI(300, 400))
        else:
            # new afw (post-#1556) interface
            bbox = afwGeom.BoxI(afwGeom.Point2I(200, 100),
                                afwGeom.Extent2I(300, 400))
        mapper = MinMapper2(root=ROOT)
        loc = mapper.map("raw_sub", {"ccd": 13, "bbox": bbox}, write=True)
        self.assertEqual(loc.getPythonType(), "lsst.afw.image.ExposureU")
        self.assertEqual(loc.getCppType(), "ImageU")
        self.assertEqual(loc.getStorageName(), "FitsStorage")
        self.assertEqual(loc.getLocations(), ["foo-13.fits"])
        self.assertEqual(loc.getStorage().root, ROOT)
        self.assertEqual(
            loc.getAdditionalData().toString(),
            'ccd = 13\nheight = 400\nllcX = 200\nllcY = 100\nwidth = 300\n')

        loc = mapper.map("raw_sub", {
            "ccd": 13,
            "bbox": bbox,
            "imageOrigin": "PARENT"
        },
                         write=True)
        self.assertEqual(loc.getPythonType(), "lsst.afw.image.ExposureU")
        self.assertEqual(loc.getCppType(), "ImageU")
        self.assertEqual(loc.getStorageName(), "FitsStorage")
        self.assertEqual(loc.getLocations(), ["foo-13.fits"])
        self.assertEqual(loc.getStorage().root, ROOT)
        self.assertEqual(
            loc.getAdditionalData().toString(),
            'ccd = 13\nheight = 400\nimageOrigin = "PARENT"\n'
            'llcX = 200\nllcY = 100\nwidth = 300\n')
Exemple #2
0
    def setUp(self):
        self.FWHM = 5  # pixels
        self.psf = algorithms.DoubleGaussianPsf(
            29, 29, self.FWHM / (2 * math.sqrt(2 * math.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")
Exemple #3
0
    def testAddTrimmedAmp(self):
        """Test that building a Ccd from trimmed Amps leads to a trimmed Ccd"""

        dataSec = afwGeom.BoxI(afwGeom.PointI(1, 0), afwGeom.ExtentI(10, 20))
        biasSec = afwGeom.BoxI(afwGeom.PointI(0, 0), afwGeom.ExtentI(1, 1))
        allPixelsInAmp = afwGeom.BoxI(afwGeom.PointI(0, 0), afwGeom.ExtentI(11, 21))
        eParams = cameraGeom.ElectronicParams(1.0, 1.0, 65000)

        ccd = cameraGeom.Ccd(cameraGeom.Id(0))
        self.assertFalse(ccd.isTrimmed())

        for i in range(2):
            amp = cameraGeom.Amp(cameraGeom.Id(i, "", i, 0),
                                 allPixelsInAmp, biasSec, dataSec, eParams)
            amp.setTrimmed(True)

            if i%2 == 0:                # check both APIs
                ccd.addAmp(afwGeom.PointI(i, 0), amp)
            else:
                ccd.addAmp(amp)
            self.assertTrue(ccd.isTrimmed())

        # should fail to add non-trimmed Amp to a trimmed Ccd
        i += 1
        amp = cameraGeom.Amp(cameraGeom.Id(i, "", i, 0),
                             allPixelsInAmp, biasSec, dataSec, eParams)
        self.assertFalse(amp.isTrimmed())

        utilsTests.assertRaisesLsstCpp(self, pexExcept.InvalidParameterException, ccd.addAmp, amp)

        # should fail to add trimmed Amp to a non-trimmed Ccd
        ccd.setTrimmed(False)
        amp.setTrimmed(True)
        utilsTests.assertRaisesLsstCpp(self, pexExcept.InvalidParameterException, ccd.addAmp, amp)
Exemple #4
0
def subtractXTalk(mi, coeffs, minPixelToMask=45000, crosstalkStr="CROSSTALK"):
    """Subtract the crosstalk from MaskedImage mi given a set of coefficients

The pixels affected by signal over minPixelToMask have the crosstalkStr bit set
    """
    sctrl = afwMath.StatisticsControl()
    sctrl.setAndMask(mi.getMask().getPlaneBitMask("DETECTED"))
    bkgd = afwMath.makeStatistics(mi, afwMath.MEDIAN, sctrl).getValue()
    #
    # These are the pixels that are bright enough to cause crosstalk (more precisely,
    # the ones that we label as causing crosstalk; in reality all pixels cause crosstalk)
    #
    tempStr = "TEMP"                    # mask plane used to record the bright pixels that we need to mask
    mi.getMask().addMaskPlane(tempStr)
    try:
        fs = afwDetect.FootprintSet(mi, afwDetect.Threshold(minPixelToMask), tempStr)

        mi.getMask().addMaskPlane(crosstalkStr)
        afwDisplay.getDisplay().setMaskPlaneColor(crosstalkStr, afwDisplay.MAGENTA)
        fs.setMask(mi.getMask(), crosstalkStr) # the crosstalkStr bit will now be set whenever
                                               # we subtract crosstalk
        crosstalk = mi.getMask().getPlaneBitMask(crosstalkStr)

        width, height = mi.getDimensions()
        for i in range(nAmp):
            bbox = afwGeom.BoxI(afwGeom.PointI(i*(width//nAmp), 0), afwGeom.ExtentI(width//nAmp, height))
            ampI = mi.Factory(mi, bbox)
            for j in range(nAmp):
                if i == j:
                    continue

                bbox = afwGeom.BoxI(afwGeom.PointI(j*(width//nAmp), 0), afwGeom.ExtentI(width//nAmp, height))
                if (i + j)%2 == 1:
                    ampJ = afwMath.flipImage(mi.Factory(mi, bbox), True, False) # no need for a deep copy
                else:
                    ampJ = mi.Factory(mi, bbox, afwImage.LOCAL, True)

                msk = ampJ.getMask()
                if np.all(msk.getArray() & msk.getPlaneBitMask("SAT")):
                    # Bad amplifier; ignore it completely --- its effect will come out in the bias
                    continue
                msk &= crosstalk

                ampJ -= bkgd
                ampJ *= coeffs[j][i]

                ampI -= ampJ
        #
        # Clear the crosstalkStr bit in the original bright pixels, where tempStr is set
        #
        msk = mi.getMask()
        temp = msk.getPlaneBitMask(tempStr)
        xtalk_temp = crosstalk | temp
        np_msk = msk.getArray()
        mask_indicies = np.where(np.bitwise_and(np_msk, xtalk_temp) == xtalk_temp)
        np_msk[mask_indicies] &= getattr(np, np_msk.dtype.name)(~crosstalk)

    finally:
        msk.removeAndClearMaskPlane(tempStr, True) # added in afw #1853
Exemple #5
0
def showPsfResiduals(exposure,
                     sourceSet,
                     magType="psf",
                     scale=10,
                     frame=None,
                     showAmps=False):
    mimIn = exposure.getMaskedImage()
    mimIn = mimIn.Factory(mimIn, True)  # make a copy to subtract from

    psf = exposure.getPsf()
    psfWidth, psfHeight = psf.getLocalKernel().getDimensions()
    #
    # Make the image that we'll paste our residuals into.  N.b. they can overlap the edges
    #
    w, h = int(mimIn.getWidth() / scale), int(mimIn.getHeight() / scale)

    im = mimIn.Factory(w + psfWidth, h + psfHeight)

    cenPos = []
    for s in sourceSet:
        x, y = s.getX(), s.getY()

        sx, sy = int(x / scale + 0.5), int(y / scale + 0.5)

        smim = im.Factory(
            im,
            afwGeom.BoxI(afwGeom.PointI(sx, sy),
                         afwGeom.ExtentI(psfWidth, psfHeight)),
            afwImage.PARENT)
        sim = smim.getImage()

        try:
            if magType == "ap":
                flux = s.getApFlux()
            elif magType == "model":
                flux = s.getModelFlux()
            elif magType == "psf":
                flux = s.getPsfFlux()
            else:
                raise RuntimeError("Unknown flux type %s" % magType)

            algorithmsLib.subtractPsf(psf, mimIn, x, y, flux)
        except Exception, e:
            print e

        try:
            expIm = mimIn.getImage().Factory(
                mimIn.getImage(),
                afwGeom.BoxI(
                    afwGeom.PointI(
                        int(x) - psfWidth // 2,
                        int(y) - psfHeight // 2),
                    afwGeom.ExtentI(psfWidth, psfHeight)), afwImage.PARENT)
        except pexExcept.LsstCppException:
            continue

        cenPos.append([x - expIm.getX0() + sx, y - expIm.getY0() + sy])

        sim += expIm
 def testValid(self):
     test_data = {
         "[1:1084,1:1024]":
         afwGeom.BoxI(afwGeom.PointI(0, 0), afwGeom.PointI(1083, 1023)),
         "[0:0,0:0]":
         afwGeom.BoxI(afwGeom.PointI(-1, -1), afwGeom.PointI(-1, -1))
     }
     for val, result in test_data.items():
         self.assertEqual(obsBase.bboxFromIraf(val), result)
Exemple #7
0
 def _makeDefaultAmpMap(self):
     """Make the default map from header information to amplifier information
        @return  The HeaderAmpMap object containing the mapping
     """
     hMap = HeaderAmpMap()
     emptyBBox = afwGeom.BoxI()
     mapList = [
         ('EXTNAME', 'setName'),
         ('DETSEC', 'setBBox', None, self._makeBbox),
         ('GAIN', 'setGain', 1.),
         ('RDNOISE', 'setReadNoise', 0.),
         ('SATURATE', 'setSaturation', 2 << 15),
         ('RDCRNR', 'setReadoutCorner', int(afwTable.ReadoutCorner.LL),
          afwTable.ReadoutCorner),
         ('LINCOEFF', 'setLinearityCoeffs', [0., 1.]),
         ('LINTYPE', 'setLinearityType', 'POLY'),
         ('RAWBBOX', 'setRawBBox', None, self._makeBbox),
         ('DATASEC', 'setRawDataBBox', None, self._makeBbox),
         ('FLIPX', 'setRawFlipX', False),
         ('FLIPY', 'setRawFlipY', False),
         ('XYOFF', 'setRawXYOffset', afwGeom.ExtentI(0, 0), self._makeExt),
         ('HOSCAN', 'setRawHorizontalOverscanBBox', emptyBBox,
          self._makeBbox),
         ('VOSCAN', 'setRawVerticalOverscanBBox', emptyBBox,
          self._makeBbox),
         ('PRESCAN', 'setRawPrescanBBox', emptyBBox, self._makeBbox),
     ]
     for tup in mapList:
         hMap.addEntry(*tup)
     return hMap
Exemple #8
0
def policyToBadRegionList(policyFile):
    """Given a Policy file describing a CCD's bad pixels, return a vector of BadRegion::Ptr"""

    badPixelsPolicy = policy.Policy.createPolicy(policyFile)
    badPixels = algorithmsLib.DefectListT()

    if badPixelsPolicy.exists("Defects"):
        d = badPixelsPolicy.getArray("Defects")
        for reg in d:
            x0 = reg.get("x0")
            width = reg.get("width")
            if not width:
                x1 = reg.get("x1")
                width = x1 - x0 - 1

            y0 = reg.get("y0")
            if reg.exists("height"):
                height = reg.get("height")
            else:
                y1 = reg.get("y1")
                height = y1 - y0 - 1

            bbox = afwGeom.BoxI(afwGeom.PointI(x0, y0),
                                afwGeom.ExtentI(width, height))
            badPixels.push_back(algorithmsLib.Defect(bbox))

    del badPixelsPolicy

    return badPixels
Exemple #9
0
    def setEdgeBits(maskedImage, goodBBox, edgeBitmask):
        """Set the edgeBitmask bits for all of maskedImage outside goodBBox"""
        msk = maskedImage.getMask()

        mx0, my0 = maskedImage.getXY0()
        for x0, y0, w, h in (
            [0, 0, msk.getWidth(),
             goodBBox.getBeginY() - my0],
            [
                0,
                goodBBox.getEndY() - my0,
                msk.getWidth(),
                maskedImage.getHeight() - (goodBBox.getEndY() - my0)
            ],
            [0, 0, goodBBox.getBeginX() - mx0,
             msk.getHeight()],
            [
                goodBBox.getEndX() - mx0, 0,
                maskedImage.getWidth() - (goodBBox.getEndX() - mx0),
                msk.getHeight()
            ],
        ):
            edgeMask = msk.Factory(
                msk, afwGeom.BoxI(afwGeom.PointI(x0, y0),
                                  afwGeom.ExtentI(w, h)), afwImage.LOCAL)
            edgeMask |= edgeBitmask
Exemple #10
0
    def setUp(self):
        config = SingleFrameMeasurementTask.ConfigClass()
        config.slots.apFlux = 'base_CircularApertureFlux_12_0'
        self.schema = afwTable.SourceTable.makeMinimalSchema()

        self.measureSources = SingleFrameMeasurementTask(self.schema,
                                                         config=config)

        bbox = afwGeom.BoxI(afwGeom.PointI(0, 0),
                            afwGeom.ExtentI(self.width, self.height))
        self.cellSet = afwMath.SpatialCellSet(bbox, 100)

        self.footprintSet = afwDetection.FootprintSet(
            self.mi, afwDetection.Threshold(self.detectThresh), "DETECTED")

        self.catalog = self.measure(self.footprintSet, self.exposure)

        for source in self.catalog:
            try:
                cand = measAlg.makePsfCandidate(source, self.exposure)
                self.cellSet.insertCandidate(cand)

            except Exception as e:
                print(e)
                continue
Exemple #11
0
    def _getCoaddExpTime(self, coadd):
        """
        Get the total exposure time associated with a coadd patch by adding up
        the exposure times of the individual visits used to build the coadd.
        """
        ccdInputs = coadd.getInfo().getCoaddInputs().ccds

        totalExpTime = 0.0
        expTimeVisits = set()

        for i in range(len(ccdInputs)):
            input = ccdInputs[i]
            ccd = input.get("ccd")
            v = input.get("visit")
            bbox = input.getBBox()
            # It's quicker to not read all the pixels, so just read 1
            calexp = self.butler.get("calexp_sub",
                                     bbox=afwGeom.BoxI(afwGeom.PointI(0, 0),
                                                       afwGeom.ExtentI(1, 1)),
                                     visit=int(v),
                                     ccd=ccd)
            calib = calexp.getCalib()
            exptime = calib.getExptime()
            if v not in expTimeVisits:
                totalExpTime += exptime
                expTimeVisits.add(v)
        return totalExpTime
Exemple #12
0
def makeAmp(i):
    height = 2048
    width = 4096
    allPixels = afwGeom.BoxI(
        afwGeom.PointI(0, 0),
        afwGeom.ExtentI(width + nExtended + nOverclock, height))
    return cameraGeom.Amp(cameraGeom.Id(i), allPixels, None, None, None)
Exemple #13
0
    def _computePsfSigma(self, butler, visitCat):
        """
        """
        startTime = time.time()

        camera = butler.get('camera')

        bbox = afwGeom.BoxI(afwGeom.PointI(0, 0), afwGeom.PointI(1, 1))

        psfSigma = np.zeros((len(visitCat), len(camera)))

        for visitIndex, vis in enumerate(visitCat):
            self.log.info(' Working on %d' % (vis['visit']))
            visit = vis['visit']
            for ccdIndex, detector in enumerate(camera):
                dataId = {'visit': int(visit), 'ccd': detector.getId()}

                if not butler.datasetExists('calexp', dataId=dataId):
                    continue
                exp = butler.get('calexp_sub',
                                 dataId=dataId,
                                 bbox=bbox,
                                 flags=afwTable.SOURCE_IO_NO_FOOTPRINTS)
                psfSigma[visitIndex, ccdIndex] = exp.getPsf().computeShape(
                ).getDeterminantRadius()

        self.log.info("Computed psfs from %d visits in %.2f s" %
                      (len(visitCat), time.time() - startTime))
        return psfSigma
Exemple #14
0
    def setEdgeBits(maskedImage, goodBBox, edgeBitmask):
        """!Set the edgeBitmask bits for all of maskedImage outside goodBBox

        \param[in,out] maskedImage  image on which to set edge bits in the mask
        \param[in] goodBBox  bounding box of good pixels, in LOCAL coordinates
        \param[in] edgeBitmask  bit mask to OR with the existing mask bits in the region outside goodBBox
        """
        msk = maskedImage.getMask()

        mx0, my0 = maskedImage.getXY0()
        for x0, y0, w, h in (
            [0, 0, msk.getWidth(),
             goodBBox.getBeginY() - my0],
            [
                0,
                goodBBox.getEndY() - my0,
                msk.getWidth(),
                maskedImage.getHeight() - (goodBBox.getEndY() - my0)
            ],
            [0, 0, goodBBox.getBeginX() - mx0,
             msk.getHeight()],
            [
                goodBBox.getEndX() - mx0, 0,
                maskedImage.getWidth() - (goodBBox.getEndX() - mx0),
                msk.getHeight()
            ],
        ):
            edgeMask = msk.Factory(
                msk, afwGeom.BoxI(afwGeom.PointI(x0, y0),
                                  afwGeom.ExtentI(w, h)), afwImage.LOCAL)
            edgeMask |= edgeBitmask
Exemple #15
0
    def setEdgeBits(maskedImage, goodBBox, edgeBitmask):
        """Set the edgeBitmask bits for all of maskedImage outside goodBBox

        Parameters
        ----------
        maskedImage : `lsst.afw.image.MaskedImage`
            Image on which to set edge bits in the mask.
        goodBBox : `lsst.afw.geom.Box2I`
            Bounding box of good pixels, in ``LOCAL`` coordinates.
        edgeBitmask : `lsst.afw.image.MaskPixel`
            Bit mask to OR with the existing mask bits in the region
            outside ``goodBBox``.
        """
        msk = maskedImage.getMask()

        mx0, my0 = maskedImage.getXY0()
        for x0, y0, w, h in ([0, 0,
                              msk.getWidth(), goodBBox.getBeginY() - my0],
                             [0, goodBBox.getEndY() - my0, msk.getWidth(),
                              maskedImage.getHeight() - (goodBBox.getEndY() - my0)],
                             [0, 0,
                              goodBBox.getBeginX() - mx0, msk.getHeight()],
                             [goodBBox.getEndX() - mx0, 0,
                              maskedImage.getWidth() - (goodBBox.getEndX() - mx0), msk.getHeight()],
                             ):
            edgeMask = msk.Factory(msk, afwGeom.BoxI(afwGeom.PointI(x0, y0),
                                                     afwGeom.ExtentI(w, h)), afwImage.LOCAL)
            edgeMask |= edgeBitmask
def create_postage_stamp(butler,
                         out_path,
                         dataid,
                         xpix,
                         ypix,
                         side_length,
                         dataset_type='deepDiff_differenceExp'):
    """Create a singe postage stamp and save it to file

    Args:
        butler     (Butler): A data access butler
        out_path      (str): Output path of fits file
        dataid       (dict): A valid data identifier
        xpix        (float): x pixel coordinate of cutout in degrees
        ypix        (float): y pixel coordinate of cutout in degrees
        side_length (float): Side length of cutout in pixels
        dataset_type  (str): Name of data set to create postage stamp for
    """

    cutout_size = afw_geom.ExtentI(side_length, side_length)
    xy = afw_geom.PointI(xpix, ypix)
    bbox = afw_geom.BoxI(xy - cutout_size // 2, cutout_size)

    try:
        cutout_image = butler.get(datasetType=f'{dataset_type}_sub',
                                  bbox=bbox,
                                  immediate=True,
                                  dataId=dataid)

    except LengthError:
        pass

    else:
        cutout_image.writeFits(str(out_path))
Exemple #17
0
    def testNaNFromMaskedImage(self):
        """Check that an extensively masked image doesn't lead to NaNs in the background estimation
        """
        image = afwImage.MaskedImageF(800, 800)
        msk = image.getMask()
        bbox = afwGeom.BoxI(afwGeom.PointI(560, 0), afwGeom.PointI(799, 335))
        smsk = msk.Factory(msk, bbox)
        smsk.set(msk.getPlaneBitMask("DETECTED"))

        binSize = 256
        nx = image.getWidth()//binSize + 1
        ny = image.getHeight()//binSize + 1

        sctrl = afwMath.StatisticsControl()
        sctrl.setAndMask(reduce(lambda x, y: x | image.getMask().getPlaneBitMask(y),
                                ['EDGE', 'DETECTED', 'DETECTED_NEGATIVE'], 0x0))

        bctrl = afwMath.BackgroundControl(nx, ny, sctrl, "MEANCLIP")

        bkgd = afwMath.makeBackground(image, bctrl)
        bkgdImage = bkgd.getImageF("NATURAL_SPLINE", "THROW_EXCEPTION")
        if display:
            ds9.mtv(image)
            ds9.mtv(bkgdImage, frame=1)

        self.assertFalse(np.isnan(bkgdImage.get(0,0)))

        # Check that the non-string API works too
        bkgdImage = bkgd.getImageF(afwMath.Interpolate.NATURAL_SPLINE, afwMath.THROW_EXCEPTION)
Exemple #18
0
    def testCopy(self):
        bbox = afwGeom.BoxI(afwGeom.PointI(0, 2), afwGeom.PointI(5, 6))

        fp = afwDetect.Footprint(bbox, bbox)

        # test copy construct
        fp2 = afwDetect.Footprint(fp)

        self.assertEqual(fp2.getBBox(), bbox)
        self.assertEqual(fp2.getRegion(), bbox)
        self.assertEqual(fp2.getArea(), bbox.getArea())
        self.assertEqual(fp2.isNormalized(), True)

        y = bbox.getMinY()
        for s in fp2.getSpans():
            self.assertEqual(s.getY(), y)
            self.assertEqual(s.getX0(), bbox.getMinX())
            self.assertEqual(s.getX1(), bbox.getMaxX())
            y += 1

        # test assignment
        fp3 = afwDetect.Footprint()
        fp3.assign(fp)
        self.assertEqual(fp3.getBBox(), bbox)
        self.assertEqual(fp3.getRegion(), bbox)
        self.assertEqual(fp3.getArea(), bbox.getArea())
        self.assertEqual(fp3.isNormalized(), True)

        y = bbox.getMinY()
        for s in fp3.getSpans():
            self.assertEqual(s.getY(), y)
            self.assertEqual(s.getX0(), bbox.getMinX())
            self.assertEqual(s.getX1(), bbox.getMaxX())
            y += 1
Exemple #19
0
 def _makeBBoxFromList(ylist):
     """Given a list [(x0, y0), (xsize, ysize)], probably from a yaml file,
     return a BoxI
     """
     (x0, y0), (xsize, ysize) = ylist
     return afwGeom.BoxI(afwGeom.PointI(x0, y0),
                         afwGeom.ExtentI(xsize, ysize))
Exemple #20
0
    def testBin(self):
        """Test that we can bin images anisotropically"""

        inImage = afwImage.ImageF(203, 131)
        val = 1
        inImage.set(val)
        binX, binY = 2, 4

        outImage = afwMath.binImage(inImage, binX, binY)

        self.assertEqual(outImage.getWidth(), inImage.getWidth() // binX)
        self.assertEqual(outImage.getHeight(), inImage.getHeight() // binY)

        stats = afwMath.makeStatistics(outImage, afwMath.MAX | afwMath.MIN)
        self.assertEqual(stats.getValue(afwMath.MIN), val)
        self.assertEqual(stats.getValue(afwMath.MAX), val)

        inImage.set(0)
        subImg = inImage.Factory(
            inImage, afwGeom.BoxI(afwGeom.PointI(4, 4), afwGeom.ExtentI(4, 8)),
            afwImage.LOCAL)
        subImg.set(100)
        del subImg
        outImage = afwMath.binImage(inImage, binX, binY)

        if display:
            ds9.mtv(inImage, frame=2, title="unbinned")
            ds9.mtv(outImage, frame=3, title="binned %dx%d" % (binX, binY))
Exemple #21
0
    def testLinearRamp(self):
        """Fit a ramp"""

        binsize = 1
        ramp, rampCoeffs, xVec, yVec = self.makeRamp(binsize)
        # Add a (labelled) bad value
        ramp.set(ramp.getWidth()//2, ramp.getHeight()//2, (0, 0x1, np.nan))

        if display:
            ds9.mtv(ramp, title="Input", frame=0)
        # Here's the range that the approximation should be valid (and also the
        # bbox of the image returned by getImage)
        bbox = afwGeom.BoxI(afwGeom.PointI(0, 0),
                            afwGeom.PointI(binsize*ramp.getWidth() - 1,
                                           binsize*ramp.getHeight() - 1))

        order = 3                       # 1 would be enough to fit the ramp
        actrl = afwMath.ApproximateControl(
            afwMath.ApproximateControl.CHEBYSHEV, order)
        approx = afwMath.makeApproximate(xVec, yVec, ramp, bbox, actrl)

        for i, aim in enumerate([approx.getImage(),
                                 approx.getMaskedImage().getImage(),
                                 ]):
            if i == 0 and display:
                ds9.mtv(aim, title="interpolated", frame=1)
                with ds9.Buffering():
                    for x in xVec:
                        for y in yVec:
                            ds9.dot('+', x, y, size=0.4, frame=1)

            for x, y in aim.getBBox().getCorners():
                self.assertEqual(
                    aim.get(x, y), rampCoeffs[0] + rampCoeffs[1]*x + rampCoeffs[1]*y)
    def testPsfDeterminerSubimageObjectSizeStarSelector(self):
        """Test the (PCA) psfDeterminer on subImages."""
        w, h = self.exposure.getDimensions()
        x0, y0 = int(0.35 * w), int(0.45 * h)
        bbox = afwGeom.BoxI(afwGeom.PointI(x0, y0),
                            afwGeom.ExtentI(w - x0, h - y0))
        subExp = self.exposure.Factory(self.exposure, bbox, afwImage.LOCAL)

        self.setupDeterminer(subExp, starSelectorAlg="objectSize")
        metadata = dafBase.PropertyList()

        #
        # Only keep the sources that lie within the subregion (avoiding lots of log messages)
        #

        def trimCatalogToImage(exp, catalog):
            trimmedCatalog = afwTable.SourceCatalog(catalog.table.clone())
            for s in catalog:
                if exp.getBBox().contains(afwGeom.PointI(s.getCentroid())):
                    trimmedCatalog.append(trimmedCatalog.table.copyRecord(s))

            return trimmedCatalog

        stars = self.starSelector.run(trimCatalogToImage(subExp, self.catalog),
                                      exposure=subExp)
        psfCandidateList = self.makePsfCandidates.run(stars.sourceCat,
                                                      subExp).psfCandidates

        psf, cellSet = self.psfDeterminer.determinePsf(subExp,
                                                       psfCandidateList,
                                                       metadata)
        subExp.setPsf(psf)

        # Test how well we can subtract the PSF model.  N.b. using self.exposure is an extrapolation
        for exp, chi_lim in [
            (subExp, 4.5),
            (self.exposure.Factory(
                self.exposure,
                afwGeom.BoxI(afwGeom.PointI(0, 100),
                             (afwGeom.PointI(w - 1, h - 1))),
                afwImage.LOCAL), 7.5),
            (self.exposure, 19),
        ]:
            cat = trimCatalogToImage(exp, self.catalog)
            exp.setPsf(psf)
            self.subtractStars(exp, cat, chi_lim)
    def testOtherAliases(self):
        with self.assertWarns(FutureWarning):
            self.assertIsInstance(afwGeom.BoxI(), lsst.geom.BoxI)
        with self.assertWarns(FutureWarning):
            self.assertIsInstance(afwGeom.BoxI(), lsst.geom.Box2I)
        with self.assertWarns(FutureWarning):
            self.assertIsInstance(afwGeom.BoxD(), lsst.geom.BoxD)
        with self.assertWarns(FutureWarning):
            self.assertIsInstance(afwGeom.BoxD(), lsst.geom.Box2D)

        with self.assertWarns(FutureWarning):
            self.assertIsInstance(afwGeom.SpherePoint(), lsst.geom.SpherePoint)

        with self.assertWarns(FutureWarning):
            self.assertIsInstance(afwGeom.AffineTransform(), lsst.geom.AffineTransform)
        with self.assertWarns(FutureWarning):
            self.assertIsInstance(afwGeom.LinearTransform(), lsst.geom.LinearTransform)
Exemple #24
0
    def testAllocateLargeImages(self):
        """Try to allocate a Very large image"""
        bbox = afwGeom.BoxI(afwGeom.PointI(-1<<30, -1<<30), afwGeom.PointI(1<<30, 1<<30))

        def tst():
            afwImage.ImageF(bbox)

        self.assertRaises(lsst.pex.exceptions.LengthError, tst)
Exemple #25
0
    def testAllocateLargeImages(self):
        """Try to allocate a Very large image"""
        bbox = afwGeom.BoxI(afwGeom.PointI(-1<<30, -1<<30), afwGeom.PointI(1<<30, 1<<30))

        def tst():
            im = afwImage.ImageF(bbox)

        utilsTests.assertRaisesLsstCpp(self, lsst.pex.exceptions.LengthErrorException, tst)
Exemple #26
0
    def testIntersectMask(self):
        bbox = afwGeom.BoxI(afwGeom.PointI(0,0), afwGeom.ExtentI(10))
        fp = afwDetect.Footprint(bbox)
        maskBBox = afwGeom.BoxI(bbox)
        maskBBox.grow(-2)
        mask = afwImage.MaskU(maskBBox)
        innerBBox = afwGeom.BoxI(maskBBox)
        innerBBox.grow(-2)
        subMask = mask.Factory(mask, innerBBox)
        subMask.set(1)

        fp.intersectMask(mask)
        fpBBox = fp.getBBox()
        self.assertEqual(fpBBox.getMinX(), maskBBox.getMinX())
        self.assertEqual(fpBBox.getMinY(), maskBBox.getMinY())
        self.assertEqual(fpBBox.getMaxX(), maskBBox.getMaxX())
        self.assertEqual(fpBBox.getMaxY(), maskBBox.getMaxY())

        self.assertEqual(fp.getArea(), maskBBox.getArea() - innerBBox.getArea())
Exemple #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)
Exemple #28
0
def cutout_ra_dec(butler,
                  data_id,
                  ra,
                  dec,
                  dataset_type='deepDiff_differenceExp',
                  cutout_size=75,
                  warp_to_exposure=None,
                  **kwargs):
    """
    Produce a cutout from dataset_type from the given butler at 
    the given ra, dec
    
    Notes
    -----
    Trivial wrapper around 'cutout_spherepoint'
    
    Parameters
    ----------
    butler: lsst.daf.persistence.Butler
        Loaded DM Butler providing access to a data repository
    data_id: Butler data ID
        E.g., {'visit': 1181556, 'detector': 45, 'filter': 'r'}
    ra: float
        Right ascension of the center of the cutout, degrees
    dec: float
        Declination of the center of the cutout, degrees
    cutout_size: int [optional] 
        Side of the cutout region in pixels.  Region will be cutout_size x cutout_size.
    warp_to_exposure: optional
        Warp coadd to system of specified 'exposure', e.g., the visit image, to warp the coadd to
        before making the cutout.  The goal is to that a cut out of a coadd image
        and a cutout of a visit image should line up.
        'warp_to_exposure' overrides setting of 'cutout_size'.
         
    Returns
    -------
    MaskedImage
    """
    cutout_extent = afwGeom.ExtentI(cutout_size, cutout_size)
    radec = afwGeom.SpherePoint(ra, dec, afwGeom.degrees)

    image = butler.get(dataset_type, dataId=data_id)

    xy = afwGeom.PointI(image.getWcs().skyToPixel(radec))
    bbox = afwGeom.BoxI(xy - cutout_extent // 2, cutout_extent)

    if warp_to_exposure is not None:
        warper = Warper(warpingKernelName='lanczos4')
        cutout_image = warper.warpExposure(warp_to_exposure.getWcs(),
                                           image,
                                           destBBox=warp_to_exposure.getBBox())
    else:
        cutout_image = image.getCutout(radec, cutout_extent)

    return cutout_image
Exemple #29
0
    def testSubMap(self):
        if hasattr(afwGeom, 'makePointI'):
            # old afw (pre-#1556) interface
            bbox = afwGeom.BoxI(afwGeom.makePointI(200, 100),
                                afwGeom.makeExtentI(300, 400))
        else:
            # new afw (post-#1556) interface
            bbox = afwGeom.BoxI(afwGeom.Point2I(200, 100),
                                afwGeom.Extent2I(300, 400))
        mapper = MinMapper2(root=ROOT)
        loc = mapper.map("raw_sub", {"ccd": 13, "bbox": bbox}, write=True)
        self.assertEqual(loc.getPythonType(), "lsst.afw.image.ExposureU")
        self.assertEqual(loc.getCppType(), "ImageU")
        self.assertEqual(loc.getStorageName(), "FitsStorage")
        self.assertEqual(loc.getLocations(), ["foo-13.fits"])
        self.assertEqual(loc.getStorage().root, ROOT)
        self.assertEqual(loc.getAdditionalData().getScalar("ccd"), 13)
        self.assertEqual(loc.getAdditionalData().getScalar("width"), 300)
        self.assertEqual(loc.getAdditionalData().getScalar("height"), 400)
        self.assertEqual(loc.getAdditionalData().getScalar("llcX"), 200)
        self.assertEqual(loc.getAdditionalData().getScalar("llcY"), 100)
        checkCompression(self, loc.getAdditionalData())

        loc = mapper.map("raw_sub", {
            "ccd": 13,
            "bbox": bbox,
            "imageOrigin": "PARENT"
        },
                         write=True)
        self.assertEqual(loc.getPythonType(), "lsst.afw.image.ExposureU")
        self.assertEqual(loc.getCppType(), "ImageU")
        self.assertEqual(loc.getStorageName(), "FitsStorage")
        self.assertEqual(loc.getLocations(), ["foo-13.fits"])
        self.assertEqual(loc.getStorage().root, ROOT)
        self.assertEqual(loc.getAdditionalData().getScalar("ccd"), 13)
        self.assertEqual(loc.getAdditionalData().getScalar("width"), 300)
        self.assertEqual(loc.getAdditionalData().getScalar("height"), 400)
        self.assertEqual(loc.getAdditionalData().getScalar("llcX"), 200)
        self.assertEqual(loc.getAdditionalData().getScalar("llcY"), 100)
        self.assertEqual(loc.getAdditionalData().getScalar("imageOrigin"),
                         "PARENT")
        checkCompression(self, loc.getAdditionalData())
def addAmp(ampCatalog, i, rN, gain_s):
    record = ampCatalog.addNew()

    width = 4096
    height = 4096

    os = 0  # pixels of overscan

    bbox = afwGeom.Box2I(afwGeom.Point2I(0, 0),
                         afwGeom.Extent2I(width, height))
    bbox.shift(afwGeom.Extent2I(width * i, 0))

    gain = gain_s
    saturation = 65535
    readNoise = rN
    readoutCorner = afwTable.LL if i == 0 else afwTable.LR
    linearityCoeffs = (1.0, np.nan, np.nan, np.nan)
    linearityType = "None"
    rawBBox = afwGeom.Box2I(afwGeom.Point2I(0, 0),
                            afwGeom.Extent2I(width, height))
    rawXYOffset = afwGeom.Extent2I(0, 0)
    rawDataBBox = afwGeom.Box2I(afwGeom.Point2I(0 if i == 0 else 0, 0),
                                afwGeom.Extent2I(width, height))
    rawHorizontalOverscanBBox = afwGeom.Box2I(
        afwGeom.Point2I(0 if i == 0 else width - os - 1, 0),
        afwGeom.Extent2I(os, 6220),
    )
    # rawVerticalOverscanBBox = afwGeom.Box2I(afwGeom.Point2I(50, 6132), afwGeom.Extent2I(0, 0))
    # rawPrescanBBox = afwGeom.Box2I(afwGeom.Point2I(0, 0), afwGeom.Extent2I(0, 0))
    emptyBox = afwGeom.BoxI()

    shiftp = afwGeom.Extent2I((width) * i, 0)
    rawBBox.shift(shiftp)
    rawDataBBox.shift(shiftp)
    rawHorizontalOverscanBBox.shift(shiftp)

    record.setHasRawInfo(True)  # Sets the first Flag=True
    record.setRawFlipX(False)  # Sets the second Flag=False
    record.setRawFlipY(False)  # Sets the third Flag=False
    record.setBBox(bbox)
    record.setName("left" if i == 0 else "right")
    record.setGain(gain)
    record.setSaturation(saturation)
    record.setReadNoise(readNoise)
    record.setReadoutCorner(readoutCorner)
    record.setLinearityCoeffs(linearityCoeffs)
    record.setLinearityType(linearityType)
    record.setRawBBox(rawBBox)
    record.setRawXYOffset(rawXYOffset)
    record.setRawDataBBox(rawDataBBox)
    record.setRawHorizontalOverscanBBox(rawHorizontalOverscanBBox)
    record.setRawVerticalOverscanBBox(emptyBox)
    record.setRawPrescanBBox(emptyBox)