def runDataRef(self, patchRefList):
        """Run this task via CmdLineTask and Gen2 Butler.
        Parameters
        ----------
        patchRefList : `list` of `lsst.daf.persistence.ButlerDataRef`
            A list of DataRefs for all filters in a single patch.
        """
        #import pdb;pdb.set_trace()
        images = {}
        replacers = {}
        mergedDataId = {"tract": patchRefList[0].dataId["tract"],
                        "patch": patchRefList[0].dataId["patch"]}
        butler = patchRefList[0].butlerSubset.butler
        ref = butler.get(self.config.ref, dataId=mergedDataId)
        imageId = butler.get("deepMergedCoaddId", dataId=mergedDataId)
        for patchRef in patchRefList:
            filt = getShortFilterName(patchRef.dataId["filter"])
            images[filt] = patchRef.get(self.config.images)

            fpCat = patchRef.get(self.config.deblendCatalog)
            footprints = {rec.getId(): (rec.getParent(), rec.getFootprint()) for rec in fpCat}
            replacers[filt] = NoiseReplacer(self.config.deblendReplacer, exposure=images[filt],
                                            footprints=footprints, exposureId=imageId)
        results = self.run(images, ref, imageId=imageId, replacers=replacers)
        butler.put(results.output, self.config.output, dataId=mergedDataId)
Beispiel #2
0
def rebuildNoiseReplacer(exposure, measCat):
    """ Recreate NoiseReplacer used in measurement

    Given a measurement catalog and the exposure on which the measurements were
    made, reconstruct the NoiseReplacer object that was used to mask out
    sources during measurement.

    Parameters
    ----------
    exposure : lsst.awf.exposure.Exposure
        The exposure on which measurements were made

    meaCat : lsst.afw.table.SourceCatalog
        Catalog containing the outputs of measurements on each source

    Returns
    -------
    noiseReplacer : lsst.meas.base.NoiseReplacer
        Object used to replace and or restore sources in the exposure with
        deterministic noise
    """

    algMetadata = measCat.getMetadata()
    noiseReplacerConf = NoiseReplacerConfig()
    noiseReplacerConf.noiseSeedMultiplier = \
        algMetadata.get(SFMT.NOISE_SEED_MULTIPLIER)
    noiseReplacerConf.noiseSource = algMetadata.get(SFMT.NOISE_SOURCE)
    noiseReplacerConf.noiseOffset = algMetadata.get(SFMT.NOISE_OFFSET)

    footprints = {
        src.getId(): (src.getParent(), src.getFootprint())
        for src in measCat
    }

    try:
        exposureId = algMetadata.get(SFMT.NOISE_EXPOSURE_ID)
    except Exception:
        exposureId = None

    noiseReplacer = NoiseReplacer(noiseReplacerConf,
                                  exposure,
                                  footprints,
                                  exposureId=exposureId)
    return noiseReplacer
Beispiel #3
0
    def __init__(self,
                 butler,
                 tract,
                 patch,
                 filter,
                 limit=None,
                 config=None,
                 all_filters=("HSC-G", "HSC-R", "HSC-I", "HSC-Z", "HSC-Y")):

        self.setConfig(config)
        self.butler = butler
        dataId = {'tract': tract, 'patch': patch, 'filter': filter}

        # NOTE: this is a fix to work under the main LSST obs_lsstSim,
        # this is fixed in the DESC fork and the butler can directly be used
        self.coadd_image_id = self._computeCoaddExposureId(dataId, True)
        #self.coadd_image_id = butler.get("deepCoaddId", dataId)

        self.ref = butler.get(
            "deepCoadd_ref",
            tract=tract,
            patch=patch,
            flags=afwTable.SOURCE_IO_NO_FOOTPRINTS,
        )
        self.forced = [
            butler.get("deepCoadd_forced_src",
                       tract=tract,
                       patch=patch,
                       filter=b,
                       flags=afwTable.SOURCE_IO_NO_FOOTPRINTS)
            for b in all_filters
        ]
        self.coadd = butler.get(
            "deepCoadd_calexp",
            tract=tract,
            patch=patch,
            filter=filter,
        )
        self.ccds = self.coadd.getInfo().getCoaddInputs().ccds
        self.coaddSegMap = None

        self.limit = limit

        if self.config['deblend_coadd']:
            # Use the numeric ID of the coadd to seed the RNG used to replace deblended
            # neighbors with noise: this is both deterministic and not the same for every
            # image.
            meas = self.butler.get("deepCoadd_meas",
                                   tract=tract,
                                   patch=patch,
                                   filter=filter)
            footprints = {
                r.getId(): (r.getParent(), r.getFootprint())
                for r in meas
            }
            self.noiseReplacer = NoiseReplacer(
                config=NoiseReplacer.ConfigClass(),
                exposure=self.coadd,
                footprints=footprints,
                exposureId=self.coadd_image_id,
            )
        self.loadImages()
Beispiel #4
0
def detect_and_deblend(*, exp, log):

    log = lsst.log.Log.getLogger("LSSTMEDSifier")

    thresh = 5.0
    loglevel = 'INFO'

    # This schema holds all the measurements that will be run within the
    # stack It needs to be constructed before running anything and passed
    # to algorithms that make additional measurents.
    schema = afw_table.SourceTable.makeMinimalSchema()

    # Setup algorithms to run
    meas_config = SingleFrameMeasurementConfig()
    meas_config.plugins.names = [
        "base_SdssCentroid",
        "base_PsfFlux",
        "base_SkyCoord",
        # "modelfit_ShapeletPsfApprox",
        "modelfit_DoubleShapeletPsfApprox",
        "modelfit_CModel",
        # "base_SdssShape",
        # "base_LocalBackground",
    ]

    # set these slots to none because we aren't running these algorithms
    meas_config.slots.apFlux = None
    meas_config.slots.gaussianFlux = None
    meas_config.slots.calibFlux = None
    meas_config.slots.modelFlux = None

    # goes with SdssShape above
    meas_config.slots.shape = None

    # fix odd issue where it things things are near the edge
    meas_config.plugins['base_SdssCentroid'].binmax = 1

    meas_task = SingleFrameMeasurementTask(
        config=meas_config,
        schema=schema,
    )

    # setup detection config
    detection_config = SourceDetectionConfig()
    detection_config.reEstimateBackground = False
    detection_config.thresholdValue = thresh
    detection_task = SourceDetectionTask(config=detection_config)
    detection_task.log.setLevel(getattr(lsst.log, loglevel))

    deblend_config = SourceDeblendConfig()
    deblend_task = SourceDeblendTask(config=deblend_config, schema=schema)
    deblend_task.log.setLevel(getattr(lsst.log, loglevel))

    # Detect objects
    table = afw_table.SourceTable.make(schema)
    result = detection_task.run(table, exp)
    sources = result.sources

    # run the deblender
    deblend_task.run(exp, sources)

    # Run on deblended images
    noise_replacer_config = NoiseReplacerConfig()
    footprints = {
        record.getId(): (record.getParent(), record.getFootprint())
        for record in result.sources
    }

    # This constructor will replace all detected pixels with noise in the
    # image
    replacer = NoiseReplacer(
        noise_replacer_config,
        exposure=exp,
        footprints=footprints,
    )

    nbad = 0
    ntry = 0
    kept_sources = []

    for record in result.sources:

        # Skip parent objects where all children are inserted
        if record.get('deblend_nChild') != 0:
            continue

        ntry += 1

        # This will insert a single source into the image
        replacer.insertSource(record.getId())  # Get the peak as before

        # peak = record.getFootprint().getPeaks()[0]

        # The bounding box will be for the parent object
        # bbox = record.getFootprint().getBBox()

        meas_task.callMeasure(record, exp)

        # Remove object
        replacer.removeSource(record.getId())

        if record.getCentroidFlag():
            nbad += 1

        kept_sources.append(record)

    # Insert all objects back into image
    replacer.end()

    if ntry > 0:
        log.debug('nbad center: %d frac: %d' % (nbad, nbad / ntry))

    nkeep = len(kept_sources)
    ntot = len(result.sources)
    log.debug('kept %d/%d non parents' % (nkeep, ntot))
    return kept_sources