Example #1
0
    def testPafReader(self):
        """Test that Butler Policy can read a paf file and the keys compare the same as when the same file is
        read as a pex Policy."""
        pexPolicy = lsst.pex.policy.Policy.createPolicy(pafPolicyPath)
        policy = lsst.daf.persistence.Policy(pafPolicyPath)

        # go back through the newly created Butler Policy, and verify that values match the paf Policy
        for name in policy.names():
            if pexPolicy.isArray(name):
                pexVal = pexPolicy.getArray(name)
            else:
                pexVal = pexPolicy.get(name)
            val = policy[name]
            if isinstance(val, lsst.daf.persistence.Policy):
                self.assertEqual(pexPolicy.getValueType(name),
                                 pexPolicy.POLICY)
            else:
                self.assertEqual(val, pexVal)

        for name in pexPolicy.names():
            if pexPolicy.getValueType(name) == pexPolicy.POLICY:
                self.assertIsInstance(policy.get(name),
                                      lsst.daf.persistence.Policy)
            else:
                if pexPolicy.isArray(name):
                    pexVal = pexPolicy.getArray(name)
                else:
                    pexVal = pexPolicy.get(name)
                self.assertEqual(pexVal, policy.get(name))

        # verify a known value, just for sanity:
        self.assertEqual(policy.get('exposures.raw.template'),
                         'raw/raw_v%(visit)d_f%(filter)s.fits.gz')
    def testPafReader(self):
        """Test that Butler Policy can read a paf file and the keys compare the same as when the same file is
        read as a pex Policy."""
        pexPolicy = lsst.pex.policy.Policy.createPolicy(pafPolicyPath)
        policy = lsst.daf.persistence.Policy(pafPolicyPath)

        # go back through the newly created Butler Policy, and verify that values match the paf Policy
        for name in policy.names():
            if pexPolicy.isArray(name):
                pexVal = pexPolicy.getArray(name)
            else:
                pexVal = pexPolicy.get(name)
            val = policy[name]
            if isinstance(val, lsst.daf.persistence.Policy):
                self.assertEqual(pexPolicy.getValueType(name), pexPolicy.POLICY)
            else:
                self.assertEqual(val, pexVal)

        for name in pexPolicy.names():
            if pexPolicy.getValueType(name) == pexPolicy.POLICY:
                self.assertIsInstance(policy.get(name), lsst.daf.persistence.Policy)
            else:
                if pexPolicy.isArray(name):
                    pexVal = pexPolicy.getArray(name)
                else:
                    pexVal = pexPolicy.get(name)
                self.assertEqual(pexVal, policy.get(name))

        # verify a known value, just for sanity:
        self.assertEqual(policy.get('exposures.raw.template'), 'raw/raw_v%(visit)d_f%(filter)s.fits.gz')
    def __initFromPexPolicy(self, pexPolicy):
        """Load values from a pex policy.

        :param pexPolicy:
        :return:
        """
        names = pexPolicy.names()
        names.sort()
        for name in names:
            if pexPolicy.getValueType(name) == pexPolicy.POLICY:
                if name in self:
                    continue
                else:
                    self[name] = {}
            else:
                if pexPolicy.isArray(name):
                    self[name] = pexPolicy.getArray(name)
                else:
                    self[name] = pexPolicy.get(name)
        return self
    def __initFromPexPolicy(self, pexPolicy):
        """Load values from a pex policy.

        :param pexPolicy:
        :return:
        """
        names = pexPolicy.names()
        names.sort()
        for name in names:
            if pexPolicy.getValueType(name) == pexPolicy.POLICY:
                if name in self:
                    continue
                else:
                    self[name] = {}
            else:
                if pexPolicy.isArray(name):
                    self[name] = pexPolicy.getArray(name)
                else:
                    self[name] = pexPolicy.get(name)
        return self
Example #5
0
def detection(differenceImageExposure,
              policy,
              filterId,
              useLog=None,
              footprintList=None):
    """Detect and measure objects in an incoming difference image Exposure
    
    Inputs:
    - differenceImageExposure: an lsst.afw.image.Exposure containing a difference MaskedImage and WCS
    - policy: the policy; required elements are...?
    - footprintList: a sequence of detection footprints on which to force measurement
        
    Returns:
    - an lsst.afw.detection.SourceVector
    """

    if not (useLog):
        useLog = ScreenLog()
        useLog.setScreenVerbose(True)

    logging.Trace("lsst.detection.detection", 3, "filterId = %d" % (filterId))

    ###########
    #
    # Get directives from policy
    #
    thresh = policy.get('thresholdSigma')
    nPixMin = policy.get('numPixMinFootprint')

    ###########
    #
    # Unpack the MaskedImage from the Exposure
    #

    img = differenceImageExposure.getMaskedImage()

    ###########
    #
    # Crudely estimate noise from mean of variance image - should do sigma clipping
    #

    varImg = img.getVariance()
    noise = math.sqrt(afwImage.mean_channel_value(varImg))

    logging.Trace(
        "lsst.detection.detection", 3,
        "thresholdSigma = %r; noise = %r PixMin = %r" %
        (thresh, noise, nPixMin))

    LogRec(useLog, Log.INFO) \
                   <<  "Threshold computation" \
                   << DataProperty("thresholdSigma", thresh) \
                   << DataProperty("noise", noise) \
                   << DataProperty("threshold", thresh*noise) \
                   << LogRec.endr

    ###########
    #
    # Build the DetectionSet for positive sources
    #

    dsPositive = det.DetectionSetF(
        img, det.Threshold(thresh * noise, det.Threshold.VALUE, True), "FP+",
        nPixMin)
    fpVecPositive = dsPositive.getFootprints()
    print "Positive detections: ", len(fpVecPositive)

    LogRec(useLog, Log.INFO) \
                   <<  "Positive detections" \
                   << DataProperty("nPositive", len(fpVecPositive)) \
                   << LogRec.endr

    ###########
    #
    # Build the DetectionSet for negative sources
    #

    dsNegative = det.DetectionSetF(
        img, det.Threshold(thresh * noise, det.Threshold.VALUE, False), "FP-",
        nPixMin)
    fpVecNegative = dsNegative.getFootprints()
    print "Negative detections: ", len(fpVecNegative)

    LogRec(useLog, Log.INFO) \
                   <<  "Negative detections" \
                   << DataProperty("nNegative", len(fpVecNegative)) \
                   << LogRec.endr

    ###########
    #
    # Measure the FootPrints
    #

    imgWCS = differenceImageExposure.getWcs()

    outputDiaSources = afwDet.DiaSourceVec()

    imgMeasure = det.MeasureF(img, "FP+")

    id = 0
    for i in range(len(fpVecPositive)):
        diaPtr = afwDet.DiaSourcePtr()
        diaPtr.setId(id)
        diaPtr.setFilterId(filterId)
        imgMeasure.measureSource(
            diaPtr, fpVecPositive[i],
            0.0)  # NOTE explicit background of zero used for difference image
        pixCoord = afwImage.Coord2D(diaPtr.getColc(), diaPtr.getRowc())
        skyCoord = imgWCS.colRowToRaDec(pixCoord)
        diaPtr.setRa(skyCoord.x())
        diaPtr.setDec(skyCoord.y())
        outputDiaSources.push_back(diaPtr.get())
        id += 1

    imgMeasure = det.MeasureF(img, "FP-")

    for i in range(len(fpVecNegative)):
        diaPtr = afwDet.DiaSourcePtr()
        diaPtr.setId(id)
        diaPtr.setFilterId(filterId)
        imgMeasure.measureSource(
            diaPtr, fpVecNegative[i],
            0.0)  # NOTE explicit background of zero used for difference image
        pixCoord = afwImage.Coord2D(diaPtr.getColc(), diaPtr.getRowc())
        skyCoord = imgWCS.colRowToRaDec(pixCoord)
        diaPtr.setRa(skyCoord.x())
        diaPtr.setDec(skyCoord.y())
        outputDiaSources.push_back(diaPtr.get())
        id += 1

    ###########
    #
    # Return the DiaSources
    #

    return outputDiaSources
def detection(differenceImageExposure, policy, filterId, useLog=None, footprintList=None):
    """Detect and measure objects in an incoming difference image Exposure
    
    Inputs:
    - differenceImageExposure: an lsst.afw.image.Exposure containing a difference MaskedImage and WCS
    - policy: the policy; required elements are...?
    - footprintList: a sequence of detection footprints on which to force measurement
        
    Returns:
    - an lsst.afw.detection.SourceVector
    """

    if not(useLog):
        useLog = ScreenLog()
        useLog.setScreenVerbose(True)

    logging.Trace("lsst.detection.detection", 3,
        "filterId = %d" % (filterId))

    ###########
    #
    # Get directives from policy
    #
    thresh = policy.get('thresholdSigma')
    nPixMin = policy.get('numPixMinFootprint')

    ###########
    #
    # Unpack the MaskedImage from the Exposure
    #

    img = differenceImageExposure.getMaskedImage()

    ###########
    #
    # Crudely estimate noise from mean of variance image - should do sigma clipping
    #

    varImg = img.getVariance()
    noise = math.sqrt(afwImage.mean_channel_value(varImg))

    logging.Trace("lsst.detection.detection", 3,
        "thresholdSigma = %r; noise = %r PixMin = %r" % (thresh, noise, nPixMin))

    LogRec(useLog, Log.INFO) \
                   <<  "Threshold computation" \
                   << DataProperty("thresholdSigma", thresh) \
                   << DataProperty("noise", noise) \
                   << DataProperty("threshold", thresh*noise) \
                   << LogRec.endr

    ###########
    #
    # Build the DetectionSet for positive sources
    #

    dsPositive = det.DetectionSetF(img, det.Threshold(thresh*noise, det.Threshold.VALUE, True), "FP+", nPixMin)
    fpVecPositive = dsPositive.getFootprints()
    print "Positive detections: ", len(fpVecPositive)

    LogRec(useLog, Log.INFO) \
                   <<  "Positive detections" \
                   << DataProperty("nPositive", len(fpVecPositive)) \
                   << LogRec.endr

    ###########
    #
    # Build the DetectionSet for negative sources
    #

    dsNegative = det.DetectionSetF(img, det.Threshold(thresh*noise, det.Threshold.VALUE, False), "FP-", nPixMin)
    fpVecNegative = dsNegative.getFootprints()
    print "Negative detections: ", len(fpVecNegative)

    LogRec(useLog, Log.INFO) \
                   <<  "Negative detections" \
                   << DataProperty("nNegative", len(fpVecNegative)) \
                   << LogRec.endr


    ###########
    #
    # Measure the FootPrints
    #

    imgWCS = differenceImageExposure.getWcs()

    outputDiaSources = afwDet.DiaSourceVec()

    imgMeasure = det.MeasureF(img, "FP+")

    id = 0
    for i in range(len(fpVecPositive)):
        diaPtr = afwDet.DiaSourcePtr()
        diaPtr.setId(id)
        diaPtr.setFilterId(filterId);
        imgMeasure.measureSource(diaPtr, fpVecPositive[i], 0.0)   # NOTE explicit background of zero used for difference image
        pixCoord = afwImage.Coord2D(diaPtr.getColc(), diaPtr.getRowc())
        skyCoord = imgWCS.colRowToRaDec(pixCoord)
        diaPtr.setRa(skyCoord.x())
        diaPtr.setDec(skyCoord.y())
        outputDiaSources.push_back(diaPtr.get())
        id += 1
 
    imgMeasure = det.MeasureF(img, "FP-")

    for i in range(len(fpVecNegative)):
        diaPtr = afwDet.DiaSourcePtr()
        diaPtr.setId(id)
        diaPtr.setFilterId(filterId);
        imgMeasure.measureSource(diaPtr, fpVecNegative[i], 0.0)   # NOTE explicit background of zero used for difference image
        pixCoord = afwImage.Coord2D(diaPtr.getColc(), diaPtr.getRowc())
        skyCoord = imgWCS.colRowToRaDec(pixCoord)
        diaPtr.setRa(skyCoord.x())
        diaPtr.setDec(skyCoord.y())
        outputDiaSources.push_back(diaPtr.get())
        id += 1

    ###########
    #
    # Return the DiaSources
    #


    return outputDiaSources