22__all__ = [
"DetectCoaddSourcesConfig",
"DetectCoaddSourcesTask",
23 "MeasureMergedCoaddSourcesConfig",
"MeasureMergedCoaddSourcesTask",
24 "DEEP_COADD_BACKGROUND_DOCSTRING",
33from lsst.pipe.base
import (
34 AnnotatedPartialOutputsError,
38 PipelineTaskConnections
40import lsst.pipe.base.connectionTypes
as cT
42from lsst.cell_coadds
import MultipleCellCoadd
43from lsst.images
import Mask, get_legacy_deep_coadd_mask_planes
44from lsst.images.cells
import CellCoadd
45from lsst.images.fields
import field_from_legacy_background
48 ExceedsMaxVarianceScaleError,
49 InsufficientSourcesError,
53 TooManyMaskedPixelsError,
57 SingleFrameMeasurementTask,
59 CatalogCalculationTask,
60 SkyMapIdGeneratorConfig,
62from lsst.meas.extensions.scarlet.io
import updateCatalogFootprints
71from .mergeDetections
import MergeDetectionsConfig, MergeDetectionsTask
72from .mergeMeasurements
import MergeMeasurementsConfig, MergeMeasurementsTask
73from .multiBandUtils
import CullPeaksConfig
74from .deblendCoaddSourcesPipeline
import DeblendCoaddSourcesMultiConfig
75from .deblendCoaddSourcesPipeline
import DeblendCoaddSourcesMultiTask
80* deepCoadd_det: detections from what used to be processCoadd (tract, patch, filter)
81* deepCoadd_mergeDet: merged detections (tract, patch)
82* deepCoadd_meas: measurements of merged detections (tract, patch, filter)
83* deepCoadd_ref: reference sources (tract, patch)
84All of these have associated *_schema catalogs that require no data ID and hold no records.
86In addition, we have a schema-only dataset, which saves the schema for the PeakRecords in
87the mergeDet, meas, and ref dataset Footprints:
88* deepCoadd_peak_schema
95DEEP_COADD_BACKGROUND_DOCSTRING = (
96 "Background subtracted from the image when generating the Object catalog. "
97 "This intentionally oversubtracts the background to reduce blending and ensure "
98 "scattered light is subtracted. "
99 "Restoring this background does not restore all original backgrounds, "
100 "as the coadd was built from background-subtracted visit images; in most "
101 "cases this background term is actually quite small "
106 dimensions=(
"tract",
"patch",
"band",
"skymap"),
107 defaultTemplates={
"inputCoaddName":
"deep",
"outputCoaddName":
"deep"}):
108 detectionSchema = cT.InitOutput(
109 doc=
"Schema of the detection catalog",
110 name=
"{outputCoaddName}Coadd_det_schema",
111 storageClass=
"SourceCatalog",
114 doc=
"Exposure on which detections are to be performed (if useCellCoadd=False). ",
115 name=
"{inputCoaddName}Coadd",
116 storageClass=
"ExposureF",
117 dimensions=(
"tract",
"patch",
"band",
"skymap")
119 exposure_cells = cT.Input(
120 doc=
"Exposure on which detections are to be performed (if useCellCoadd=True). ",
121 name=
"{inputCoaddName}CoaddCell",
122 storageClass=
"MultipleCellCoadd",
123 dimensions=(
"tract",
"patch",
"band",
"skymap"),
126 doc=
"Description of the skymap's tracts and patches.",
127 name=BaseSkyMap.SKYMAP_DATASET_TYPE_NAME,
128 storageClass=
"SkyMap",
129 dimensions=(
"skymap",),
131 outputBackgrounds = cT.Output(
132 doc=
"Output Backgrounds used in detection",
133 name=
"{outputCoaddName}Coadd_calexp_background",
134 storageClass=
"Background",
135 dimensions=(
"tract",
"patch",
"band",
"skymap")
137 outputSources = cT.Output(
138 doc=
"Detected sources catalog",
139 name=
"{outputCoaddName}Coadd_det",
140 storageClass=
"SourceCatalog",
141 dimensions=(
"tract",
"patch",
"band",
"skymap")
143 outputExposure = cT.Output(
144 doc=
"Exposure post detection",
145 name=
"{outputCoaddName}Coadd_calexp",
146 storageClass=
"ExposureF",
147 dimensions=(
"tract",
"patch",
"band",
"skymap")
150 def __init__(self, *, config=None):
151 super().__init__(config=config)
152 assert isinstance(config, DetectCoaddSourcesConfig)
154 if config.imageType ==
"future":
155 if config.useCellCoadds:
156 self.exposure_cells = dataclasses.replace(self.exposure_cells, storageClass=
"CellCoadd")
158 self.exposure = dataclasses.replace(self.exposure, storageClass=
"CellCoadd")
159 self.outputExposure = dataclasses.replace(self.outputExposure, storageClass=
"CellCoadd")
160 del self.outputBackgrounds
161 if config.useCellCoadds:
164 del self.exposure_cells
165 if not self.config.forceExactBinning:
167 if self.config.writeOnlyBackgrounds:
168 del self.outputExposure
169 del self.outputSources
170 del self.detectionSchema
173class DetectCoaddSourcesConfig(PipelineTaskConfig, pipelineConnections=DetectCoaddSourcesConnections):
174 """Configuration parameters for the DetectCoaddSourcesTask
177 doScaleVariance = Field(dtype=bool, default=
True, doc=
"Scale variance plane using empirical noise?")
178 scaleVariance = ConfigurableField(target=ScaleVarianceTask, doc=
"Variance rescaling")
179 detection = ConfigurableField(target=DynamicDetectionTask, doc=
"Source detection")
180 coaddName = Field(dtype=str, default=
"deep", doc=
"Name of coadd")
181 useCellCoadds = Field(dtype=bool, default=
False, doc=
"Whether to use cell coadds?")
185 doc=
"Should be set to True if fake sources have been inserted into the input data.",
187 idGenerator = SkyMapIdGeneratorConfig.make_field()
188 forceExactBinning = Field(
192 "Check that the background bin size evenly divides the patch inner region, and "
193 "crop the outer region to an integer number of bins."
196 writeOnlyBackgrounds = Field(dtype=bool, default=
False, doc=
"If true, only save the background models.")
197 writeEmptyBackgrounds = Field(
201 "If true, save a placeholder background with NaNs in all bins (but the right geometry) when "
202 "there are no pixels to compute a background from. This can be useful if a later task combines "
203 "backgrounds from multiple patches as input."
206 imageType = ChoiceField(
207 "Which image type to use for the input and output coadd. "
208 "This option only directly affects connection storage classes and hence 'runQuantum'; the 'run' "
209 "method behavior is determined by which type is actually passed in.",
212 "Read a lsst.cell_coadds.MultipleCellCoadd (if useCellCoadd) or "
213 "lsst.afw.image.Exposure (if not useCellCoadd) and write an lsst.afw.image.Exposure."
216 "Read and write lsst.images.cells.CellCoadd (useCellCoadd just "
217 "sets which of 'exposure' or 'exposure_cells' will be used for inputs). "
218 "The 'outputBackground' is deleted, writeEmptyBackgrounds is ignored, and "
219 "writeOnlyBackgrounds=True is invalid."
226 backgroundName = Field(
227 "Name of the subtracted background, to be stored with the image when the input and "
228 "output images are lsst.images.cells.CellCoadd.",
232 backgroundDescription = Field(
233 "Description of the subtracted background, to be stored with the image when the input and "
234 "output images are lsst.images.cells.CellCoadd.",
236 default=DEEP_COADD_BACKGROUND_DOCSTRING,
239 def setDefaults(self):
240 super().setDefaults()
241 self.detection.thresholdType =
"pixel_stdev"
242 self.detection.isotropicGrow =
True
244 self.detection.reEstimateBackground =
False
245 self.detection.background.useApprox =
False
246 self.detection.background.binSize = 4096
247 self.detection.background.undersampleStyle =
'REDUCE_INTERP_ORDER'
248 self.detection.doTempWideBackground =
True
251 self.idGenerator.packer.n_bands =
None
255 if self.imageType ==
"future":
256 if self.doScaleVariance:
257 raise ValueError(
"doScaleVariance=True is not compatible with imageType='future'")
258 if self.forceExactBinning:
259 raise ValueError(
"forceExactBinning=True is not compatible with imageType='future'")
260 if self.writeOnlyBackgrounds:
261 raise ValueError(
"writeOnlyBackgrounds=True is not compatible with imageType='future'")
264class DetectCoaddSourcesTask(PipelineTask):
265 """Detect sources on a single filter coadd.
267 Coadding individual visits requires each exposure to be warped. This
268 introduces covariance in the noise properties across pixels. Before
269 detection, we correct the coadd variance by scaling the variance plane in
270 the coadd to match the observed variance. This is an approximate
271 approach -- strictly, we should propagate the full covariance matrix --
272 but it is simple and works well in practice.
274 After scaling the variance plane, we detect sources and generate footprints
275 by delegating to the @ref SourceDetectionTask_ "detection" subtask.
277 DetectCoaddSourcesTask is meant to be run after assembling a coadded image
278 in a given band. The purpose of the task is to update the background,
279 detect all sources in a single band and generate a set of parent
280 footprints. Subsequent tasks in the multi-band processing procedure will
281 merge sources across bands and, eventually, perform forced photometry.
285 schema : `lsst.afw.table.Schema`, optional
286 Initial schema for the output catalog, modified-in place to include all
287 fields set by this task. If None, the source minimal schema will be used.
289 Additional keyword arguments.
292 _DefaultName =
"detectCoaddSources"
293 ConfigClass = DetectCoaddSourcesConfig
295 def __init__(self, schema=None, **kwargs):
298 super().__init__(**kwargs)
300 schema = afwTable.SourceTable.makeMinimalSchema()
302 self.makeSubtask(
"detection", schema=self.schema)
303 if self.config.doScaleVariance:
304 self.makeSubtask(
"scaleVariance")
306 self.detectionSchema = afwTable.SourceCatalog(self.schema)
308 def runQuantum(self, butlerQC, inputRefs, outputRefs):
309 inputs = butlerQC.get(inputRefs)
310 idGenerator = self.config.idGenerator.apply(butlerQC.quantum.dataId)
312 if self.config.useCellCoadds:
313 multiple_cell_coadd = inputs.pop(
"exposure_cells")
314 match self.config.imageType:
316 exposure = multiple_cell_coadd.stitch().asExposure()
318 exposure = multiple_cell_coadd
320 raise AssertionError(f
"Invalid choice {self.config.imageType!r} for imageType.")
322 exposure = inputs.pop(
"exposure")
324 skyMap = inputs.pop(
"skyMap",
None)
325 if skyMap
is not None:
326 patchInfo = skyMap[butlerQC.quantum.dataId[
"tract"]][butlerQC.quantum.dataId[
"patch"]]
330 assert not inputs,
"runQuantum got more inputs than expected."
334 idFactory=idGenerator.make_table_id_factory(),
335 expId=idGenerator.catalog_id,
339 TooManyMaskedPixelsError,
340 ExceedsMaxVarianceScaleError,
341 InsufficientSourcesError,
345 if self.config.writeEmptyBackgrounds:
346 butlerQC.put(self._makeEmptyBackground(exposure, patchInfo), outputRefs.outputBackgrounds)
348 if isinstance(exposure, CellCoadd):
355 exposure.mask.clear(
"DETECTED")
357 for maskName
in [
"DETECTED",
"DETECTED_NEGATIVE"]:
358 if maskName
in exposure.mask.getMaskPlaneDict().keys():
359 detectedMask = exposure.mask.getMaskPlane(maskName)
360 exposure.mask.clearMaskPlane(detectedMask)
361 error = AnnotatedPartialOutputsError.annotate(
367 butlerQC.put(exposure, outputRefs.outputExposure)
370 butlerQC.put(outputs, outputRefs)
372 def run(self, exposure, idFactory, expId, patchInfo=None):
373 """Run detection on an exposure.
375 First scale the variance plane to match the observed variance
376 using ``ScaleVarianceTask``. Then invoke the ``SourceDetectionTask_`` "detection" subtask to
381 exposure : `lsst.afw.image.Exposure` or `lsst.images.cells.CellCoadd`.
382 Exposure on which to detect (may be background-subtracted and scaled,
383 depending on configuration).
384 idFactory : `lsst.afw.table.IdFactory`
385 IdFactory to set source identifiers.
387 Exposure identifier (integer) for RNG seed.
388 patchInfo : `lsst.skymap.PatchInfo`, optional
389 Description of the patch geometry. Only needed if
390 `~DetectCoaddSourceConfig.forceExactBinning` is `True`.
394 result : `lsst.pipe.base.Struct`
395 Results as a struct with attributes:
398 Catalog of detections (`lsst.afw.table.SourceCatalog`).
399 ``outputBackgrounds``
400 List of backgrounds (`list`).
402 The background-subtracted coadd image, with its mask plane
403 updated to include detections. This will have the same type
407 if isinstance(exposure, CellCoadd):
408 cell_coadd = exposure
409 exposure = cell_coadd.to_legacy()
410 if self.config.forceExactBinning:
411 if cell_coadd
is not None:
412 raise ValueError(
"forceExactBinning=True is not compatible with CellCoadd inputs")
413 exposure = self._cropToExactBinning(exposure, patchInfo)
414 if self.config.doScaleVariance:
415 if cell_coadd
is not None:
416 raise ValueError(
"doScaleVariance=True is not compatible with CellCoadd inputs")
417 varScale = self.scaleVariance.run(exposure.maskedImage)
418 exposure.getMetadata().add(
"VARIANCE_SCALE", varScale)
419 backgrounds = afwMath.BackgroundList()
420 table = afwTable.SourceTable.make(self.schema, idFactory)
421 detections = self.detection.run(table, exposure, expId=expId)
422 sources = detections.sources
423 if hasattr(detections,
"background")
and detections.background:
424 for bg
in detections.background:
425 backgrounds.append(bg)
426 if len(backgrounds) == 0:
429 emptyBg = self._makeEmptyBackground(exposure, patchInfo)
430 backgrounds.append(emptyBg)
431 if cell_coadd
is not None:
432 cell_coadd.image.array[...] = exposure.image.array
433 cell_coadd.mask = Mask.from_legacy(
434 exposure.mask, plane_map=get_legacy_deep_coadd_mask_planes()
435 ).view(sky_projection=cell_coadd.sky_projection)
437 cell_coadd.backgrounds._backgrounds.clear()
438 cell_coadd.backgrounds.add(
439 self.config.backgroundName,
440 field_from_legacy_background(backgrounds, unit=astropy.units.nJy),
441 self.config.backgroundDescription,
444 exposure = cell_coadd
445 return Struct(outputSources=sources, outputBackgrounds=backgrounds, outputExposure=exposure)
448 """Crop a coadd `~lsst.afw.image.Exposure` instance to ensure exact
453 exposure : `lsst.afw.image.Exposure`
454 Exposure to crop, assumed to cover the patch outer bounding box.
455 patchInfo : `lsst.skymap.PatchInfo`
456 Description of the patch geometry.
460 cropped : `lsst.afw.image.Exposure`
461 View of ``exposure`` with background bins that evenly divide both
462 the full cropped image and the patch inner region. The bounding
463 box is guaranteed to contain the patch inner bounding box and be
464 contained by the patch outer bounding box.
469 Raised if the patch inner region width or height is not a multiple
470 of the background bin size.
472 bbox = patchInfo.getInnerBBox()
473 if bbox.width % self.detection.background.binSizeX:
475 f
"Patch inner width {bbox.width} does not evenly "
476 f
"divide bin width {self.detection.background.binSizeX}."
478 if bbox.height % self.detection.background.binSizeY:
480 f
"Patch inner height {bbox.height} does not evenly "
481 f
"divide bin height {self.detection.background.binSizeY}."
483 outer_bbox = patchInfo.getOuterBBox()
484 n_bins_grow_x = (bbox.x.begin - outer_bbox.x.begin) // self.detection.background.binSizeX
485 n_bins_grow_y = (bbox.y.begin - outer_bbox.y.begin) // self.detection.background.binSizeY
488 n_bins_grow_x*self.detection.background.binSizeX,
489 n_bins_grow_y*self.detection.background.binSizeY,
492 assert outer_bbox.contains(bbox)
493 assert bbox.contains(patchInfo.getInnerBBox())
494 assert bbox.width % self.detection.background.binSizeX == 0
495 assert bbox.height % self.detection.background.binSizeY == 0
496 return exposure[bbox]
499 """Construct an empty `lsst.afw.math.BackgroundList` with NaN values.
503 exposure : `lsst.afw.image.Exposure`
504 Exposure that the background should correspond to.
505 patchInfo : `lsst.skymap.PatchInfo`, optional
506 Description of the patch geometry. Only needed if
507 `~DetectCoaddSourceConfig.forceExactBinning` is `True`.
511 background : `lsst.afw.math.BackgroundList`
512 A background object with a single layer and the same bin geometry
513 that a background for that exposure would have had if it had enough
514 usable pixels. This object cannot actually be used for background
519 if self.config.forceExactBinning:
520 exposure = self._cropToExactBinning(exposure, patchInfo).clone()
523 bgStats = afwImage.MaskedImageF(1, 1)
524 bgStats.set(bgLevel, 0, bgLevel)
525 bg = afwMath.BackgroundMI(exposure.getBBox(), bgStats)
526 bgData = (bg, afwMath.Interpolate.LINEAR, afwMath.REDUCE_INTERP_ORDER,
527 afwMath.ApproximateControl.UNKNOWN, 0, 0,
False)
528 background = afwMath.BackgroundList()
529 background.append(bgData)
530 for bg, *_
in background:
531 stats = bg.getStatsImage()
532 stats.mask.array[:, :] = stats.mask.getPlaneBitMask(
"NO_DATA")
533 stats.variance.array[:, :] = 0.0
538 PipelineTaskConnections,
539 dimensions=(
"tract",
"patch",
"band",
"skymap"),
541 "inputCoaddName":
"deep",
542 "outputCoaddName":
"deep",
545 inputSchema = cT.InitInput(
546 doc=
"Input schema for measure merged task produced by a deblender or detection task",
547 name=
"{inputCoaddName}Coadd_deblendedFlux_schema",
548 storageClass=
"SourceCatalog"
550 outputSchema = cT.InitOutput(
551 doc=
"Output schema after all new fields are added by task",
552 name=
"{inputCoaddName}Coadd_meas_schema",
553 storageClass=
"SourceCatalog"
556 doc=
"Input non-cell-based coadd image",
557 name=
"{inputCoaddName}Coadd_calexp",
558 storageClass=
"ExposureF",
559 dimensions=(
"tract",
"patch",
"band",
"skymap")
561 exposure_cells = cT.Input(
562 doc=
"Input cell-based coadd image",
563 name=
"{inputCoaddName}CoaddCell",
564 storageClass=
"MultipleCellCoadd",
565 dimensions=(
"tract",
"patch",
"band",
"skymap"),
567 background = cT.Input(
568 doc=
"Background to subtract from cell-based coadd image",
569 name=
"{inputCoaddName}Coadd_calexp_background",
570 storageClass=
"Background",
571 dimensions=(
"tract",
"patch",
"band",
"skymap")
574 doc=
"SkyMap to use in processing",
575 name=BaseSkyMap.SKYMAP_DATASET_TYPE_NAME,
576 storageClass=
"SkyMap",
577 dimensions=(
"skymap",),
579 sourceTableHandles = cT.Input(
580 doc=(
"Source tables that are derived from the ``CalibrateTask`` sources. "
581 "These tables contain astrometry and photometry flags, and optionally "
583 name=
"sourceTable_visit",
584 storageClass=
"ArrowAstropy",
585 dimensions=(
"instrument",
"visit"),
589 finalizedSourceTableHandles = cT.Input(
590 doc=(
"Finalized source tables from ``FinalizeCalibrationTask``. These "
591 "tables contain PSF flags from the finalized PSF estimation."),
592 name=
"finalized_src_table",
593 storageClass=
"ArrowAstropy",
594 dimensions=(
"instrument",
"visit"),
598 finalVisitSummaryHandles = cT.Input(
599 doc=
"Final visit summary table",
600 name=
"finalVisitSummary",
601 storageClass=
"ExposureCatalog",
602 dimensions=(
"instrument",
"visit"),
606 scarletCatalog = cT.Input(
607 doc=
"Catalogs produced by multiband deblending",
608 name=
"{inputCoaddName}Coadd_deblendedCatalog",
609 storageClass=
"SourceCatalog",
610 dimensions=(
"tract",
"patch",
"skymap"),
612 scarletModels = cT.Input(
613 doc=
"Multiband scarlet models produced by the deblender",
614 name=
"{inputCoaddName}Coadd_scarletModelData",
615 storageClass=
"LsstScarletModelData",
616 dimensions=(
"tract",
"patch",
"skymap"),
618 outputSources = cT.Output(
619 doc=
"Source catalog containing all the measurement information generated in this task",
620 name=
"{outputCoaddName}Coadd_meas",
621 dimensions=(
"tract",
"patch",
"band",
"skymap"),
622 storageClass=
"SourceCatalog",
625 def __init__(self, *, config=None):
626 super().__init__(config=config)
627 if not config.doPropagateFlags:
628 del self.sourceTableHandles
629 del self.finalizedSourceTableHandles
630 del self.finalVisitSummaryHandles
633 if not config.propagateFlags.source_flags:
634 del self.sourceTableHandles
635 if not config.propagateFlags.finalized_source_flags:
636 del self.finalizedSourceTableHandles
637 if not config.doAddFootprints:
638 del self.scarletModels
639 if self.config.imageType ==
"future":
640 self.exposure = dataclasses.replace(self.exposure, storageClass=
"CellCoadd")
641 del self.exposure_cells
643 elif self.config.useCellCoadds:
646 del self.exposure_cells
650class MeasureMergedCoaddSourcesConfig(PipelineTaskConfig,
651 pipelineConnections=MeasureMergedCoaddSourcesConnections):
652 """Configuration parameters for the MeasureMergedCoaddSourcesTask
654 doAddFootprints = Field(dtype=bool,
656 doc=
"Whether or not to add footprints to the input catalog from scarlet models. "
657 "This should be true whenever using the multi-band deblender, "
658 "otherwise this should be False.")
659 doConserveFlux = Field(dtype=bool, default=
True,
660 doc=
"Whether to use the deblender models as templates to re-distribute the flux "
661 "from the 'exposure' (True), or to perform measurements on the deblender "
663 doStripFootprints = Field(dtype=bool, default=
True,
664 doc=
"Whether to strip footprints from the output catalog before "
666 "This is usually done when using scarlet models to save disk space.")
667 useCellCoadds = Field(dtype=bool, default=
False, doc=
"Whether to use cell coadds?")
668 measurement = ConfigurableField(target=SingleFrameMeasurementTask, doc=
"Source measurement")
669 setPrimaryFlags = ConfigurableField(target=SetPrimaryFlagsTask, doc=
"Set flags for primary tract/patch")
670 doPropagateFlags = Field(
671 dtype=bool, default=
True,
672 doc=
"Whether to match sources to CCD catalogs to propagate flags (to e.g. identify PSF stars)"
674 propagateFlags = ConfigurableField(target=PropagateSourceFlagsTask, doc=
"Propagate source flags to coadd")
675 coaddName = Field(dtype=str, default=
"deep", doc=
"Name of coadd")
676 psfCache = Field(dtype=int, default=100, doc=
"Size of psfCache")
677 checkUnitsParseStrict = Field(
678 doc=
"Strictness of Astropy unit compatibility check, can be 'raise', 'warn' or 'silent'",
685 doc=
"Apply aperture corrections"
687 applyApCorr = ConfigurableField(
688 target=ApplyApCorrTask,
689 doc=
"Subtask to apply aperture corrections"
691 doRunCatalogCalculation = Field(
694 doc=
'Run catalogCalculation task'
696 catalogCalculation = ConfigurableField(
697 target=CatalogCalculationTask,
698 doc=
"Subtask to run catalogCalculation plugins on catalog"
704 doc=
"Should be set to True if fake sources have been inserted into the input data."
706 idGenerator = SkyMapIdGeneratorConfig.make_field()
707 imageType = ChoiceField(
708 "Which image type to expect for the input coadd. "
709 "This option only directly affects connection storage classes and hence 'runQuantum'; the 'run' "
710 "method behavior is determined by which type is actually passed in.",
713 "Read a lsst.cell_coadds.MultipleCellCoadd via 'exposure_cells` and restore 'background' "
714 "(if useCellCoadd) or lsst.afw.image.Exposure via `exposure` (if not useCellCoadd)."
717 "Read lsst.images.cells.CellCoadd via the 'exposure' connection. useCellCoadd is ignored."
725 def setDefaults(self):
726 super().setDefaults()
727 self.measurement.plugins.names |= [
'base_InputCount',
729 'base_LocalPhotoCalib',
735 self.measurement.plugins[
'base_PixelFlags'].masksFpAnywhere = [
'CLIPPED',
'SENSOR_EDGE',
737 self.measurement.plugins[
'base_PixelFlags'].masksFpCenter = [
'CLIPPED',
'SENSOR_EDGE',
741class MeasureMergedCoaddSourcesTask(PipelineTask):
742 """Deblend sources from main catalog in each coadd seperately and measure.
744 Use peaks and footprints from a master catalog to perform deblending and
745 measurement in each coadd.
747 Given a master input catalog of sources (peaks and footprints) or deblender
748 outputs(including a HeavyFootprint in each band), measure each source on
749 the coadd. Repeating this procedure with the same master catalog across
750 multiple coadds will generate a consistent set of child sources.
752 The deblender retains all peaks and deblends any missing peaks (dropouts in
753 that band) as PSFs. Source properties are measured and the @c is-primary
754 flag (indicating sources with no children) is set. Visit flags are
755 propagated to the coadd sources.
757 After MeasureMergedCoaddSourcesTask has been run on multiple coadds, we
758 have a set of per-band catalogs. The next stage in the multi-band
759 processing procedure will merge these measurements into a suitable catalog
760 for driving forced photometry.
764 schema : ``lsst.afw.table.Schema`, optional
765 The schema of the merged detection catalog used as input to this one.
766 peakSchema : ``lsst.afw.table.Schema`, optional
767 The schema of the PeakRecords in the Footprints in the merged detection catalog.
768 initInputs : `dict`, optional
769 Dictionary that can contain a key ``inputSchema`` containing the
770 input schema. If present will override the value of ``schema``.
772 Additional keyword arguments.
775 _DefaultName =
"measureCoaddSources"
776 ConfigClass = MeasureMergedCoaddSourcesConfig
778 def __init__(self, schema=None, peakSchema=None, initInputs=None, **kwargs):
779 super().__init__(**kwargs)
780 if initInputs
is not None:
781 schema = initInputs[
'inputSchema'].schema
783 raise ValueError(
"Schema must be defined.")
784 self.schemaMapper = afwTable.SchemaMapper(schema)
785 self.schemaMapper.addMinimalSchema(schema)
786 self.schema = self.schemaMapper.getOutputSchema()
788 self.makeSubtask(
"measurement", schema=self.schema, algMetadata=self.algMetadata)
789 self.makeSubtask(
"setPrimaryFlags", schema=self.schema)
790 if self.config.doPropagateFlags:
791 self.makeSubtask(
"propagateFlags", schema=self.schema)
792 self.schema.checkUnits(parse_strict=self.config.checkUnitsParseStrict)
793 if self.config.doApCorr:
794 self.makeSubtask(
"applyApCorr", schema=self.schema)
795 if self.config.doRunCatalogCalculation:
796 self.makeSubtask(
"catalogCalculation", schema=self.schema)
798 self.outputSchema = afwTable.SourceCatalog(self.schema)
800 def runQuantum(self, butlerQC, inputRefs, outputRefs):
801 inputs = butlerQC.get(inputRefs)
803 if self.config.imageType ==
"future":
804 coadd = inputs.pop(
"exposure")
805 band = inputRefs.exposure.dataId[
"band"]
812 coadd = coadd.to_legacy_cell_coadd()
813 elif self.config.useCellCoadds:
814 coadd = inputs.pop(
"exposure_cells")
815 band = inputRefs.exposure_cells.dataId[
"band"]
817 coadd = inputs.pop(
"exposure")
818 band = inputRefs.exposure.dataId[
"band"]
819 if isinstance(coadd, MultipleCellCoadd):
820 stitched_coadd = coadd.stitch()
821 exposure = stitched_coadd.asExposure()
822 if self.config.imageType ==
"legacy":
823 background = inputs.pop(
"background")
824 exposure.image -= background.getImage()
825 ccdInputs = stitched_coadd.ccds
826 apCorrMap = stitched_coadd.ap_corr_map
830 exposure.getPsf().setCacheCapacity(self.config.psfCache)
832 ccdInputs = exposure.getInfo().getCoaddInputs().ccds
833 apCorrMap = exposure.getInfo().getApCorrMap()
837 idGenerator = self.config.idGenerator.apply(butlerQC.quantum.dataId)
840 table = afwTable.SourceTable.make(self.schema, idGenerator.make_table_id_factory())
841 sources = afwTable.SourceCatalog(table)
843 inputCatalog = inputs.pop(
"scarletCatalog")
844 catalogRef = inputRefs.scarletCatalog
845 sources.extend(inputCatalog, self.schemaMapper)
848 if self.config.doAddFootprints:
849 modelData = inputs.pop(
'scarletModels')
850 if self.config.doConserveFlux:
851 imageForRedistribution = exposure
853 imageForRedistribution =
None
854 updateCatalogFootprints(
858 imageForRedistribution=imageForRedistribution,
859 removeScarletData=
True,
860 updateFluxColumns=
True,
862 table = sources.getTable()
863 table.setMetadata(self.algMetadata)
865 skyMap = inputs.pop(
'skyMap')
866 tractNumber = catalogRef.dataId[
'tract']
867 tractInfo = skyMap[tractNumber]
868 patchInfo = tractInfo.getPatchInfo(catalogRef.dataId[
'patch'])
873 wcs=tractInfo.getWcs(),
874 bbox=patchInfo.getOuterBBox()
877 sourceTableHandleDict =
None
878 finalizedSourceTableHandleDict =
None
879 finalVisitSummaryHandleDict =
None
880 if self.config.doPropagateFlags:
881 if "sourceTableHandles" in inputs:
882 sourceTableHandles = inputs.pop(
"sourceTableHandles")
883 sourceTableHandleDict = {handle.dataId[
"visit"]: handle
for handle
in sourceTableHandles}
884 if "finalizedSourceTableHandles" in inputs:
885 finalizedSourceTableHandles = inputs.pop(
"finalizedSourceTableHandles")
886 finalizedSourceTableHandleDict = {handle.dataId[
"visit"]: handle
887 for handle
in finalizedSourceTableHandles}
888 if "finalVisitSummaryHandles" in inputs:
889 finalVisitSummaryHandles = inputs.pop(
"finalVisitSummaryHandles")
890 finalVisitSummaryHandleDict = {handle.dataId[
"visit"]: handle
891 for handle
in finalVisitSummaryHandles}
893 assert not inputs,
"runQuantum got more inputs than expected."
898 exposureId=idGenerator.catalog_id,
900 sourceTableHandleDict=sourceTableHandleDict,
901 finalizedSourceTableHandleDict=finalizedSourceTableHandleDict,
902 finalVisitSummaryHandleDict=finalVisitSummaryHandleDict,
906 if self.config.doStripFootprints:
907 sources = outputs.outputSources
908 for source
in sources[sources[
"parent"] != 0]:
909 source.setFootprint(
None)
910 butlerQC.put(outputs, outputRefs)
912 def run(self, exposure, sources, skyInfo, exposureId, ccdInputs=None,
913 sourceTableHandleDict=None, finalizedSourceTableHandleDict=None, finalVisitSummaryHandleDict=None,
915 """Run measurement algorithms on the input exposure, and optionally populate the
916 resulting catalog with extra information.
920 exposure : `lsst.afw.image.Exposure`
921 The input exposure on which measurements are to be performed.
922 sources : `lsst.afw.table.SourceCatalog`
923 A catalog built from the results of merged detections, or
925 parentCatalog : `lsst.afw.table.SourceCatalog`
926 Catalog of parent sources corresponding to sources.
927 skyInfo : `lsst.pipe.base.Struct`
928 A struct containing information about the position of the input exposure within
929 a `SkyMap`, the `SkyMap`, its `Wcs`, and its bounding box.
930 exposureId : `int` or `bytes`
931 Packed unique number or bytes unique to the input exposure.
932 ccdInputs : `lsst.afw.table.ExposureCatalog`, optional
933 Catalog containing information on the individual visits which went into making
935 sourceTableHandleDict : `dict` [`int`, `lsst.daf.butler.DeferredDatasetHandle`], optional
936 Dict for sourceTable_visit handles (key is visit) for propagating flags.
937 These tables contain astrometry and photometry flags, and optionally PSF flags.
938 finalizedSourceTableHandleDict : `dict` [`int`, `lsst.daf.butler.DeferredDatasetHandle`], optional
939 Dict for finalized_src_table handles (key is visit) for propagating flags.
940 These tables contain PSF flags from the finalized PSF estimation.
941 finalVisitSummaryHandleDict : `dict` [`int`, `lsst.daf.butler.DeferredDatasetHandle`], optional
942 Dict for visit_summary handles (key is visit) for visit-level information.
943 These tables contain the WCS information of the single-visit input images.
944 apCorrMap : `lsst.afw.image.ApCorrMap`, optional
945 Aperture correction map attached to the ``exposure``. If None, it
946 will be read from the ``exposure``.
950 results : `lsst.pipe.base.Struct`
951 Results of running measurement task. Will contain the catalog in the
954 if self.config.doPropagateFlags:
957 for maskPlane
in self.config.measurement.plugins[
"base_PixelFlags"].masksFpAnywhere:
958 exposure.mask.addMaskPlane(maskPlane)
959 for maskPlane
in self.config.measurement.plugins[
"base_PixelFlags"].masksFpCenter:
960 exposure.mask.addMaskPlane(maskPlane)
962 self._ensureMaskPlanes()
964 self.measurement.run(sources, exposure, exposureId=exposureId)
966 if self.config.doApCorr:
967 if apCorrMap
is None:
968 apCorrMap = exposure.getInfo().getApCorrMap()
969 self.applyApCorr.run(
978 if not sources.isContiguous():
979 sources = sources.copy(deep=
True)
981 if self.config.doRunCatalogCalculation:
982 self.catalogCalculation.run(sources)
984 self.setPrimaryFlags.run(sources, skyMap=skyInfo.skyMap, tractInfo=skyInfo.tractInfo,
985 patchInfo=skyInfo.patchInfo)
986 if self.config.doPropagateFlags:
987 self.propagateFlags.run(
990 sourceTableHandleDict,
991 finalizedSourceTableHandleDict,
992 finalVisitSummaryHandleDict,
996 results.outputSources = sources
999 def _ensureMaskPlanes(self):
1000 """Ensure the global mask dictionary has all of the mask planes
1001 needed for PixelFlags algorithms.
1003 When mask planes are added, this essentially guarantees that the
1004 corresponding PixelFlags columns will be wholly False, and usually
1005 we'd prefer to remove them from the configuration. But those config
1006 changes imply a schema changes, and that's not always viable (e.g. on
1009 needed = set(self.measurement.plugins[
"base_PixelFlags"].config.masksFpCenter)
1010 needed.update(self.measurement.plugins[
"base_PixelFlags"].config.masksFpAnywhere)
1011 existing = afwImage.MaskX().getMaskPlaneDict().keys()
1012 for plane
in sorted(needed - existing):
1014 "Adding mask plane %r with no pixel set to satisfy PixelFlags configuration.", plane
1016 afwImage.MaskX.addMaskPlane(plane)
_makeEmptyBackground(self, exposure, patchInfo=None)
_cropToExactBinning(self, exposure, patchInfo)
run(self, exposure, idFactory, expId, patchInfo=None)