Coverage for python/lsst/pipe/tasks/multiBand.py: 26%
267 statements
« prev ^ index » next coverage.py v7.3.1, created at 2023-09-17 10:04 +0000
« prev ^ index » next coverage.py v7.3.1, created at 2023-09-17 10:04 +0000
1# This file is part of pipe_tasks.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (https://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <https://www.gnu.org/licenses/>.
22__all__ = ["DetectCoaddSourcesConfig", "DetectCoaddSourcesTask"]
24from lsst.pipe.base import (Struct, PipelineTask, PipelineTaskConfig, PipelineTaskConnections)
25import lsst.pipe.base.connectionTypes as cT
26from lsst.pex.config import Field, ConfigurableField, ChoiceField
27from lsst.meas.algorithms import DynamicDetectionTask, ReferenceObjectLoader, ScaleVarianceTask
28from lsst.meas.base import (
29 SingleFrameMeasurementTask,
30 ApplyApCorrTask,
31 CatalogCalculationTask,
32 SkyMapIdGeneratorConfig,
33)
34from lsst.meas.astrom import DirectMatchTask, denormalizeMatches
35from lsst.pipe.tasks.setPrimaryFlags import SetPrimaryFlagsTask
36from lsst.pipe.tasks.propagateSourceFlags import PropagateSourceFlagsTask
37import lsst.afw.table as afwTable
38import lsst.afw.math as afwMath
39from lsst.daf.base import PropertyList
40from lsst.skymap import BaseSkyMap
42# NOTE: these imports are a convenience so multiband users only have to import this file.
43from .mergeDetections import MergeDetectionsConfig, MergeDetectionsTask # noqa: F401
44from .mergeMeasurements import MergeMeasurementsConfig, MergeMeasurementsTask # noqa: F401
45from .multiBandUtils import CullPeaksConfig # noqa: F401
46from .deblendCoaddSourcesPipeline import DeblendCoaddSourcesSingleConfig # noqa: F401
47from .deblendCoaddSourcesPipeline import DeblendCoaddSourcesSingleTask # noqa: F401
48from .deblendCoaddSourcesPipeline import DeblendCoaddSourcesMultiConfig # noqa: F401
49from .deblendCoaddSourcesPipeline import DeblendCoaddSourcesMultiTask # noqa: F401
52"""
53New set types:
54* deepCoadd_det: detections from what used to be processCoadd (tract, patch, filter)
55* deepCoadd_mergeDet: merged detections (tract, patch)
56* deepCoadd_meas: measurements of merged detections (tract, patch, filter)
57* deepCoadd_ref: reference sources (tract, patch)
58All of these have associated *_schema catalogs that require no data ID and hold no records.
60In addition, we have a schema-only dataset, which saves the schema for the PeakRecords in
61the mergeDet, meas, and ref dataset Footprints:
62* deepCoadd_peak_schema
63"""
66##############################################################################################################
67class DetectCoaddSourcesConnections(PipelineTaskConnections,
68 dimensions=("tract", "patch", "band", "skymap"),
69 defaultTemplates={"inputCoaddName": "deep", "outputCoaddName": "deep"}):
70 detectionSchema = cT.InitOutput(
71 doc="Schema of the detection catalog",
72 name="{outputCoaddName}Coadd_det_schema",
73 storageClass="SourceCatalog",
74 )
75 exposure = cT.Input(
76 doc="Exposure on which detections are to be performed",
77 name="{inputCoaddName}Coadd",
78 storageClass="ExposureF",
79 dimensions=("tract", "patch", "band", "skymap")
80 )
81 outputBackgrounds = cT.Output(
82 doc="Output Backgrounds used in detection",
83 name="{outputCoaddName}Coadd_calexp_background",
84 storageClass="Background",
85 dimensions=("tract", "patch", "band", "skymap")
86 )
87 outputSources = cT.Output(
88 doc="Detected sources catalog",
89 name="{outputCoaddName}Coadd_det",
90 storageClass="SourceCatalog",
91 dimensions=("tract", "patch", "band", "skymap")
92 )
93 outputExposure = cT.Output(
94 doc="Exposure post detection",
95 name="{outputCoaddName}Coadd_calexp",
96 storageClass="ExposureF",
97 dimensions=("tract", "patch", "band", "skymap")
98 )
101class DetectCoaddSourcesConfig(PipelineTaskConfig, pipelineConnections=DetectCoaddSourcesConnections):
102 """Configuration parameters for the DetectCoaddSourcesTask
103 """
105 doScaleVariance = Field(dtype=bool, default=True, doc="Scale variance plane using empirical noise?")
106 scaleVariance = ConfigurableField(target=ScaleVarianceTask, doc="Variance rescaling")
107 detection = ConfigurableField(target=DynamicDetectionTask, doc="Source detection")
108 coaddName = Field(dtype=str, default="deep", doc="Name of coadd")
109 hasFakes = Field(
110 dtype=bool,
111 default=False,
112 doc="Should be set to True if fake sources have been inserted into the input data.",
113 )
114 idGenerator = SkyMapIdGeneratorConfig.make_field()
116 def setDefaults(self):
117 super().setDefaults()
118 self.detection.thresholdType = "pixel_stdev"
119 self.detection.isotropicGrow = True
120 # Coadds are made from background-subtracted CCDs, so any background subtraction should be very basic
121 self.detection.reEstimateBackground = False
122 self.detection.background.useApprox = False
123 self.detection.background.binSize = 4096
124 self.detection.background.undersampleStyle = 'REDUCE_INTERP_ORDER'
125 self.detection.doTempWideBackground = True # Suppress large footprints that overwhelm the deblender
126 # Include band in packed data IDs that go into object IDs (None -> "as
127 # many bands as are defined", rather than the default of zero).
128 self.idGenerator.packer.n_bands = None
131class DetectCoaddSourcesTask(PipelineTask):
132 """Detect sources on a single filter coadd.
134 Coadding individual visits requires each exposure to be warped. This
135 introduces covariance in the noise properties across pixels. Before
136 detection, we correct the coadd variance by scaling the variance plane in
137 the coadd to match the observed variance. This is an approximate
138 approach -- strictly, we should propagate the full covariance matrix --
139 but it is simple and works well in practice.
141 After scaling the variance plane, we detect sources and generate footprints
142 by delegating to the @ref SourceDetectionTask_ "detection" subtask.
144 DetectCoaddSourcesTask is meant to be run after assembling a coadded image
145 in a given band. The purpose of the task is to update the background,
146 detect all sources in a single band and generate a set of parent
147 footprints. Subsequent tasks in the multi-band processing procedure will
148 merge sources across bands and, eventually, perform forced photometry.
150 Parameters
151 ----------
152 schema : `lsst.afw.table.Schema`, optional
153 Initial schema for the output catalog, modified-in place to include all
154 fields set by this task. If None, the source minimal schema will be used.
155 **kwargs
156 Additional keyword arguments.
157 """
159 _DefaultName = "detectCoaddSources"
160 ConfigClass = DetectCoaddSourcesConfig
162 def __init__(self, schema=None, **kwargs):
163 # N.B. Super is used here to handle the multiple inheritance of PipelineTasks, the init tree
164 # call structure has been reviewed carefully to be sure super will work as intended.
165 super().__init__(**kwargs)
166 if schema is None:
167 schema = afwTable.SourceTable.makeMinimalSchema()
168 self.schema = schema
169 self.makeSubtask("detection", schema=self.schema)
170 if self.config.doScaleVariance:
171 self.makeSubtask("scaleVariance")
173 self.detectionSchema = afwTable.SourceCatalog(self.schema)
175 def runQuantum(self, butlerQC, inputRefs, outputRefs):
176 inputs = butlerQC.get(inputRefs)
177 idGenerator = self.config.idGenerator.apply(butlerQC.quantum.dataId)
178 inputs["idFactory"] = idGenerator.make_table_id_factory()
179 inputs["expId"] = idGenerator.catalog_id
180 outputs = self.run(**inputs)
181 butlerQC.put(outputs, outputRefs)
183 def run(self, exposure, idFactory, expId):
184 """Run detection on an exposure.
186 First scale the variance plane to match the observed variance
187 using ``ScaleVarianceTask``. Then invoke the ``SourceDetectionTask_`` "detection" subtask to
188 detect sources.
190 Parameters
191 ----------
192 exposure : `lsst.afw.image.Exposure`
193 Exposure on which to detect (may be backround-subtracted and scaled,
194 depending on configuration).
195 idFactory : `lsst.afw.table.IdFactory`
196 IdFactory to set source identifiers.
197 expId : `int`
198 Exposure identifier (integer) for RNG seed.
200 Returns
201 -------
202 result : `lsst.pipe.base.Struct`
203 Results as a struct with attributes:
205 ``sources``
206 Catalog of detections (`lsst.afw.table.SourceCatalog`).
207 ``backgrounds``
208 List of backgrounds (`list`).
209 """
210 if self.config.doScaleVariance:
211 varScale = self.scaleVariance.run(exposure.maskedImage)
212 exposure.getMetadata().add("VARIANCE_SCALE", varScale)
213 backgrounds = afwMath.BackgroundList()
214 table = afwTable.SourceTable.make(self.schema, idFactory)
215 detections = self.detection.run(table, exposure, expId=expId)
216 sources = detections.sources
217 if hasattr(detections, "background") and detections.background:
218 for bg in detections.background:
219 backgrounds.append(bg)
220 return Struct(outputSources=sources, outputBackgrounds=backgrounds, outputExposure=exposure)
223class MeasureMergedCoaddSourcesConnections(PipelineTaskConnections,
224 dimensions=("tract", "patch", "band", "skymap"),
225 defaultTemplates={"inputCoaddName": "deep",
226 "outputCoaddName": "deep",
227 "deblendedCatalog": "deblendedFlux"}):
228 inputSchema = cT.InitInput(
229 doc="Input schema for measure merged task produced by a deblender or detection task",
230 name="{inputCoaddName}Coadd_deblendedFlux_schema",
231 storageClass="SourceCatalog"
232 )
233 outputSchema = cT.InitOutput(
234 doc="Output schema after all new fields are added by task",
235 name="{inputCoaddName}Coadd_meas_schema",
236 storageClass="SourceCatalog"
237 )
238 refCat = cT.PrerequisiteInput(
239 doc="Reference catalog used to match measured sources against known sources",
240 name="ref_cat",
241 storageClass="SimpleCatalog",
242 dimensions=("skypix",),
243 deferLoad=True,
244 multiple=True
245 )
246 exposure = cT.Input(
247 doc="Input coadd image",
248 name="{inputCoaddName}Coadd_calexp",
249 storageClass="ExposureF",
250 dimensions=("tract", "patch", "band", "skymap")
251 )
252 skyMap = cT.Input(
253 doc="SkyMap to use in processing",
254 name=BaseSkyMap.SKYMAP_DATASET_TYPE_NAME,
255 storageClass="SkyMap",
256 dimensions=("skymap",),
257 )
258 visitCatalogs = cT.Input(
259 doc="Source catalogs for visits which overlap input tract, patch, band. Will be "
260 "further filtered in the task for the purpose of propagating flags from image calibration "
261 "and characterization to coadd objects. Only used in legacy PropagateVisitFlagsTask.",
262 name="src",
263 dimensions=("instrument", "visit", "detector"),
264 storageClass="SourceCatalog",
265 multiple=True
266 )
267 sourceTableHandles = cT.Input(
268 doc=("Source tables that are derived from the ``CalibrateTask`` sources. "
269 "These tables contain astrometry and photometry flags, and optionally "
270 "PSF flags."),
271 name="sourceTable_visit",
272 storageClass="DataFrame",
273 dimensions=("instrument", "visit"),
274 multiple=True,
275 deferLoad=True,
276 )
277 finalizedSourceTableHandles = cT.Input(
278 doc=("Finalized source tables from ``FinalizeCalibrationTask``. These "
279 "tables contain PSF flags from the finalized PSF estimation."),
280 name="finalized_src_table",
281 storageClass="DataFrame",
282 dimensions=("instrument", "visit"),
283 multiple=True,
284 deferLoad=True,
285 )
286 inputCatalog = cT.Input(
287 doc=("Name of the input catalog to use."
288 "If the single band deblender was used this should be 'deblendedFlux."
289 "If the multi-band deblender was used this should be 'deblendedModel, "
290 "or deblendedFlux if the multiband deblender was configured to output "
291 "deblended flux catalogs. If no deblending was performed this should "
292 "be 'mergeDet'"),
293 name="{inputCoaddName}Coadd_{deblendedCatalog}",
294 storageClass="SourceCatalog",
295 dimensions=("tract", "patch", "band", "skymap"),
296 )
297 scarletCatalog = cT.Input(
298 doc="Catalogs produced by multiband deblending",
299 name="{inputCoaddName}Coadd_deblendedCatalog",
300 storageClass="SourceCatalog",
301 dimensions=("tract", "patch", "skymap"),
302 )
303 scarletModels = cT.Input(
304 doc="Multiband scarlet models produced by the deblender",
305 name="{inputCoaddName}Coadd_scarletModelData",
306 storageClass="ScarletModelData",
307 dimensions=("tract", "patch", "skymap"),
308 )
309 outputSources = cT.Output(
310 doc="Source catalog containing all the measurement information generated in this task",
311 name="{outputCoaddName}Coadd_meas",
312 dimensions=("tract", "patch", "band", "skymap"),
313 storageClass="SourceCatalog",
314 )
315 matchResult = cT.Output(
316 doc="Match catalog produced by configured matcher, optional on doMatchSources",
317 name="{outputCoaddName}Coadd_measMatch",
318 dimensions=("tract", "patch", "band", "skymap"),
319 storageClass="Catalog",
320 )
321 denormMatches = cT.Output(
322 doc="Denormalized Match catalog produced by configured matcher, optional on "
323 "doWriteMatchesDenormalized",
324 name="{outputCoaddName}Coadd_measMatchFull",
325 dimensions=("tract", "patch", "band", "skymap"),
326 storageClass="Catalog",
327 )
329 def __init__(self, *, config=None):
330 super().__init__(config=config)
331 if config.doPropagateFlags is False:
332 self.inputs -= set(("visitCatalogs",))
333 self.inputs -= set(("sourceTableHandles",))
334 self.inputs -= set(("finalizedSourceTableHandles",))
335 elif config.propagateFlags.target == PropagateSourceFlagsTask:
336 # New PropagateSourceFlagsTask does not use visitCatalogs.
337 self.inputs -= set(("visitCatalogs",))
338 # Check for types of flags required.
339 if not config.propagateFlags.source_flags:
340 self.inputs -= set(("sourceTableHandles",))
341 if not config.propagateFlags.finalized_source_flags:
342 self.inputs -= set(("finalizedSourceTableHandles",))
343 else:
344 # Deprecated PropagateVisitFlagsTask uses visitCatalogs.
345 self.inputs -= set(("sourceTableHandles",))
346 self.inputs -= set(("finalizedSourceTableHandles",))
348 if config.inputCatalog == "deblendedCatalog":
349 self.inputs -= set(("inputCatalog",))
351 if not config.doAddFootprints:
352 self.inputs -= set(("scarletModels",))
353 else:
354 self.inputs -= set(("deblendedCatalog"))
355 self.inputs -= set(("scarletModels",))
357 if config.doMatchSources is False:
358 self.outputs -= set(("matchResult",))
360 if config.doWriteMatchesDenormalized is False:
361 self.outputs -= set(("denormMatches",))
364class MeasureMergedCoaddSourcesConfig(PipelineTaskConfig,
365 pipelineConnections=MeasureMergedCoaddSourcesConnections):
366 """Configuration parameters for the MeasureMergedCoaddSourcesTask
367 """
368 inputCatalog = ChoiceField(
369 dtype=str,
370 default="deblendedCatalog",
371 allowed={
372 "deblendedCatalog": "Output catalog from ScarletDeblendTask",
373 "deblendedFlux": "Output catalog from SourceDeblendTask",
374 "mergeDet": "The merged detections before deblending."
375 },
376 doc="The name of the input catalog.",
377 )
378 doAddFootprints = Field(dtype=bool,
379 default=True,
380 doc="Whether or not to add footprints to the input catalog from scarlet models. "
381 "This should be true whenever using the multi-band deblender, "
382 "otherwise this should be False.")
383 doConserveFlux = Field(dtype=bool, default=True,
384 doc="Whether to use the deblender models as templates to re-distribute the flux "
385 "from the 'exposure' (True), or to perform measurements on the deblender "
386 "model footprints.")
387 doStripFootprints = Field(dtype=bool, default=True,
388 doc="Whether to strip footprints from the output catalog before "
389 "saving to disk. "
390 "This is usually done when using scarlet models to save disk space.")
391 measurement = ConfigurableField(target=SingleFrameMeasurementTask, doc="Source measurement")
392 setPrimaryFlags = ConfigurableField(target=SetPrimaryFlagsTask, doc="Set flags for primary tract/patch")
393 doPropagateFlags = Field(
394 dtype=bool, default=True,
395 doc="Whether to match sources to CCD catalogs to propagate flags (to e.g. identify PSF stars)"
396 )
397 propagateFlags = ConfigurableField(target=PropagateSourceFlagsTask, doc="Propagate source flags to coadd")
398 doMatchSources = Field(dtype=bool, default=True, doc="Match sources to reference catalog?")
399 match = ConfigurableField(target=DirectMatchTask, doc="Matching to reference catalog")
400 doWriteMatchesDenormalized = Field(
401 dtype=bool,
402 default=False,
403 doc=("Write reference matches in denormalized format? "
404 "This format uses more disk space, but is more convenient to read."),
405 )
406 coaddName = Field(dtype=str, default="deep", doc="Name of coadd")
407 psfCache = Field(dtype=int, default=100, doc="Size of psfCache")
408 checkUnitsParseStrict = Field(
409 doc="Strictness of Astropy unit compatibility check, can be 'raise', 'warn' or 'silent'",
410 dtype=str,
411 default="raise",
412 )
413 doApCorr = Field(
414 dtype=bool,
415 default=True,
416 doc="Apply aperture corrections"
417 )
418 applyApCorr = ConfigurableField(
419 target=ApplyApCorrTask,
420 doc="Subtask to apply aperture corrections"
421 )
422 doRunCatalogCalculation = Field(
423 dtype=bool,
424 default=True,
425 doc='Run catalogCalculation task'
426 )
427 catalogCalculation = ConfigurableField(
428 target=CatalogCalculationTask,
429 doc="Subtask to run catalogCalculation plugins on catalog"
430 )
432 hasFakes = Field(
433 dtype=bool,
434 default=False,
435 doc="Should be set to True if fake sources have been inserted into the input data."
436 )
437 idGenerator = SkyMapIdGeneratorConfig.make_field()
439 @property
440 def refObjLoader(self):
441 return self.match.refObjLoader
443 def setDefaults(self):
444 super().setDefaults()
445 self.measurement.plugins.names |= ['base_InputCount',
446 'base_Variance',
447 'base_LocalPhotoCalib',
448 'base_LocalWcs']
449 self.measurement.plugins['base_PixelFlags'].masksFpAnywhere = ['CLIPPED', 'SENSOR_EDGE',
450 'INEXACT_PSF']
451 self.measurement.plugins['base_PixelFlags'].masksFpCenter = ['CLIPPED', 'SENSOR_EDGE',
452 'INEXACT_PSF']
455class MeasureMergedCoaddSourcesTask(PipelineTask):
456 """Deblend sources from main catalog in each coadd seperately and measure.
458 Use peaks and footprints from a master catalog to perform deblending and
459 measurement in each coadd.
461 Given a master input catalog of sources (peaks and footprints) or deblender
462 outputs(including a HeavyFootprint in each band), measure each source on
463 the coadd. Repeating this procedure with the same master catalog across
464 multiple coadds will generate a consistent set of child sources.
466 The deblender retains all peaks and deblends any missing peaks (dropouts in
467 that band) as PSFs. Source properties are measured and the @c is-primary
468 flag (indicating sources with no children) is set. Visit flags are
469 propagated to the coadd sources.
471 Optionally, we can match the coadd sources to an external reference
472 catalog.
474 After MeasureMergedCoaddSourcesTask has been run on multiple coadds, we
475 have a set of per-band catalogs. The next stage in the multi-band
476 processing procedure will merge these measurements into a suitable catalog
477 for driving forced photometry.
479 Parameters
480 ----------
481 schema : ``lsst.afw.table.Schema`, optional
482 The schema of the merged detection catalog used as input to this one.
483 peakSchema : ``lsst.afw.table.Schema`, optional
484 The schema of the PeakRecords in the Footprints in the merged detection catalog.
485 refObjLoader : `lsst.meas.algorithms.ReferenceObjectLoader`, optional
486 An instance of ReferenceObjectLoader that supplies an external reference
487 catalog. May be None if the loader can be constructed from the butler argument or all steps
488 requiring a reference catalog are disabled.
489 initInputs : `dict`, optional
490 Dictionary that can contain a key ``inputSchema`` containing the
491 input schema. If present will override the value of ``schema``.
492 **kwargs
493 Additional keyword arguments.
494 """
496 _DefaultName = "measureCoaddSources"
497 ConfigClass = MeasureMergedCoaddSourcesConfig
499 def __init__(self, schema=None, peakSchema=None, refObjLoader=None, initInputs=None,
500 **kwargs):
501 super().__init__(**kwargs)
502 self.deblended = self.config.inputCatalog.startswith("deblended")
503 self.inputCatalog = "Coadd_" + self.config.inputCatalog
504 if initInputs is not None:
505 schema = initInputs['inputSchema'].schema
506 if schema is None:
507 raise ValueError("Schema must be defined.")
508 self.schemaMapper = afwTable.SchemaMapper(schema)
509 self.schemaMapper.addMinimalSchema(schema)
510 self.schema = self.schemaMapper.getOutputSchema()
511 afwTable.CoordKey.addErrorFields(self.schema)
512 self.algMetadata = PropertyList()
513 self.makeSubtask("measurement", schema=self.schema, algMetadata=self.algMetadata)
514 self.makeSubtask("setPrimaryFlags", schema=self.schema)
515 if self.config.doMatchSources:
516 self.makeSubtask("match", refObjLoader=refObjLoader)
517 if self.config.doPropagateFlags:
518 self.makeSubtask("propagateFlags", schema=self.schema)
519 self.schema.checkUnits(parse_strict=self.config.checkUnitsParseStrict)
520 if self.config.doApCorr:
521 self.makeSubtask("applyApCorr", schema=self.schema)
522 if self.config.doRunCatalogCalculation:
523 self.makeSubtask("catalogCalculation", schema=self.schema)
525 self.outputSchema = afwTable.SourceCatalog(self.schema)
527 def runQuantum(self, butlerQC, inputRefs, outputRefs):
528 inputs = butlerQC.get(inputRefs)
530 refObjLoader = ReferenceObjectLoader([ref.datasetRef.dataId for ref in inputRefs.refCat],
531 inputs.pop('refCat'),
532 name=self.config.connections.refCat,
533 config=self.config.refObjLoader,
534 log=self.log)
535 self.match.setRefObjLoader(refObjLoader)
537 # Set psfcache
538 # move this to run after gen2 deprecation
539 inputs['exposure'].getPsf().setCacheCapacity(self.config.psfCache)
541 # Get unique integer ID for IdFactory and RNG seeds; only the latter
542 # should really be used as the IDs all come from the input catalog.
543 idGenerator = self.config.idGenerator.apply(butlerQC.quantum.dataId)
544 inputs['exposureId'] = idGenerator.catalog_id
546 # Transform inputCatalog
547 table = afwTable.SourceTable.make(self.schema, idGenerator.make_table_id_factory())
548 sources = afwTable.SourceCatalog(table)
549 # Load the correct input catalog
550 if "scarletCatalog" in inputs:
551 inputCatalog = inputs.pop("scarletCatalog")
552 catalogRef = inputRefs.scarletCatalog
553 else:
554 inputCatalog = inputs.pop("inputCatalog")
555 catalogRef = inputRefs.inputCatalog
556 sources.extend(inputCatalog, self.schemaMapper)
557 del inputCatalog
558 # Add the HeavyFootprints to the deblended sources
559 if self.config.doAddFootprints:
560 modelData = inputs.pop('scarletModels')
561 if self.config.doConserveFlux:
562 redistributeImage = inputs['exposure'].image
563 else:
564 redistributeImage = None
565 modelData.updateCatalogFootprints(
566 catalog=sources,
567 band=inputRefs.exposure.dataId["band"],
568 psfModel=inputs['exposure'].getPsf(),
569 redistributeImage=redistributeImage,
570 removeScarletData=True,
571 )
572 table = sources.getTable()
573 table.setMetadata(self.algMetadata) # Capture algorithm metadata to write out to the source catalog.
574 inputs['sources'] = sources
576 skyMap = inputs.pop('skyMap')
577 tractNumber = catalogRef.dataId['tract']
578 tractInfo = skyMap[tractNumber]
579 patchInfo = tractInfo.getPatchInfo(catalogRef.dataId['patch'])
580 skyInfo = Struct(
581 skyMap=skyMap,
582 tractInfo=tractInfo,
583 patchInfo=patchInfo,
584 wcs=tractInfo.getWcs(),
585 bbox=patchInfo.getOuterBBox()
586 )
587 inputs['skyInfo'] = skyInfo
589 if self.config.doPropagateFlags:
590 if self.config.propagateFlags.target == PropagateSourceFlagsTask:
591 # New version
592 ccdInputs = inputs["exposure"].getInfo().getCoaddInputs().ccds
593 inputs["ccdInputs"] = ccdInputs
595 if "sourceTableHandles" in inputs:
596 sourceTableHandles = inputs.pop("sourceTableHandles")
597 sourceTableHandleDict = {handle.dataId["visit"]: handle
598 for handle in sourceTableHandles}
599 inputs["sourceTableHandleDict"] = sourceTableHandleDict
600 if "finalizedSourceTableHandles" in inputs:
601 finalizedSourceTableHandles = inputs.pop("finalizedSourceTableHandles")
602 finalizedSourceTableHandleDict = {handle.dataId["visit"]: handle
603 for handle in finalizedSourceTableHandles}
604 inputs["finalizedSourceTableHandleDict"] = finalizedSourceTableHandleDict
605 else:
606 # Deprecated legacy version
607 # Filter out any visit catalog that is not coadd inputs
608 ccdInputs = inputs['exposure'].getInfo().getCoaddInputs().ccds
609 visitKey = ccdInputs.schema.find("visit").key
610 ccdKey = ccdInputs.schema.find("ccd").key
611 inputVisitIds = set()
612 ccdRecordsWcs = {}
613 for ccdRecord in ccdInputs:
614 visit = ccdRecord.get(visitKey)
615 ccd = ccdRecord.get(ccdKey)
616 inputVisitIds.add((visit, ccd))
617 ccdRecordsWcs[(visit, ccd)] = ccdRecord.getWcs()
619 inputCatalogsToKeep = []
620 inputCatalogWcsUpdate = []
621 for i, dataRef in enumerate(inputRefs.visitCatalogs):
622 key = (dataRef.dataId['visit'], dataRef.dataId['detector'])
623 if key in inputVisitIds:
624 inputCatalogsToKeep.append(inputs['visitCatalogs'][i])
625 inputCatalogWcsUpdate.append(ccdRecordsWcs[key])
626 inputs['visitCatalogs'] = inputCatalogsToKeep
627 inputs['wcsUpdates'] = inputCatalogWcsUpdate
628 inputs['ccdInputs'] = ccdInputs
630 outputs = self.run(**inputs)
631 # Strip HeavyFootprints to save space on disk
632 sources = outputs.outputSources
633 butlerQC.put(outputs, outputRefs)
635 def run(self, exposure, sources, skyInfo, exposureId, ccdInputs=None, visitCatalogs=None, wcsUpdates=None,
636 sourceTableHandleDict=None, finalizedSourceTableHandleDict=None):
637 """Run measurement algorithms on the input exposure, and optionally populate the
638 resulting catalog with extra information.
640 Parameters
641 ----------
642 exposure : `lsst.afw.exposure.Exposure`
643 The input exposure on which measurements are to be performed.
644 sources : `lsst.afw.table.SourceCatalog`
645 A catalog built from the results of merged detections, or
646 deblender outputs.
647 skyInfo : `lsst.pipe.base.Struct`
648 A struct containing information about the position of the input exposure within
649 a `SkyMap`, the `SkyMap`, its `Wcs`, and its bounding box.
650 exposureId : `int` or `bytes`
651 Packed unique number or bytes unique to the input exposure.
652 ccdInputs : `lsst.afw.table.ExposureCatalog`, optional
653 Catalog containing information on the individual visits which went into making
654 the coadd.
655 visitCatalogs : `list` of `lsst.afw.table.SourceCatalogs`, optional
656 A list of source catalogs corresponding to measurements made on the individual
657 visits which went into the input exposure. If None and butler is `None` then
658 the task cannot propagate visit flags to the output catalog.
659 Deprecated, to be removed with PropagateVisitFlagsTask.
660 wcsUpdates : `list` of `lsst.afw.geom.SkyWcs`, optional
661 If visitCatalogs is not `None` this should be a list of wcs objects which correspond
662 to the input visits. Used to put all coordinates to common system. If `None` and
663 butler is `None` then the task cannot propagate visit flags to the output catalog.
664 Deprecated, to be removed with PropagateVisitFlagsTask.
665 sourceTableHandleDict : `dict` [`int`, `lsst.daf.butler.DeferredDatasetHandle`], optional
666 Dict for sourceTable_visit handles (key is visit) for propagating flags.
667 These tables are derived from the ``CalibrateTask`` sources, and contain
668 astrometry and photometry flags, and optionally PSF flags.
669 finalizedSourceTableHandleDict : `dict` [`int`, `lsst.daf.butler.DeferredDatasetHandle`], optional
670 Dict for finalized_src_table handles (key is visit) for propagating flags.
671 These tables are derived from ``FinalizeCalibrationTask`` and contain
672 PSF flags from the finalized PSF estimation.
674 Returns
675 -------
676 results : `lsst.pipe.base.Struct`
677 Results of running measurement task. Will contain the catalog in the
678 sources attribute. Optionally will have results of matching to a
679 reference catalog in the matchResults attribute, and denormalized
680 matches in the denormMatches attribute.
681 """
682 self.measurement.run(sources, exposure, exposureId=exposureId)
684 if self.config.doApCorr:
685 self.applyApCorr.run(
686 catalog=sources,
687 apCorrMap=exposure.getInfo().getApCorrMap()
688 )
690 # TODO DM-11568: this contiguous check-and-copy could go away if we
691 # reserve enough space during SourceDetection and/or SourceDeblend.
692 # NOTE: sourceSelectors require contiguous catalogs, so ensure
693 # contiguity now, so views are preserved from here on.
694 if not sources.isContiguous():
695 sources = sources.copy(deep=True)
697 if self.config.doRunCatalogCalculation:
698 self.catalogCalculation.run(sources)
700 self.setPrimaryFlags.run(sources, skyMap=skyInfo.skyMap, tractInfo=skyInfo.tractInfo,
701 patchInfo=skyInfo.patchInfo)
702 if self.config.doPropagateFlags:
703 if self.config.propagateFlags.target == PropagateSourceFlagsTask:
704 # New version
705 self.propagateFlags.run(
706 sources,
707 ccdInputs,
708 sourceTableHandleDict,
709 finalizedSourceTableHandleDict
710 )
711 else:
712 # Legacy deprecated version
713 self.propagateFlags.run(
714 sources,
715 ccdInputs,
716 exposure.getWcs(),
717 visitCatalogs,
718 wcsUpdates
719 )
721 results = Struct()
723 if self.config.doMatchSources:
724 matchResult = self.match.run(sources, exposure.getInfo().getFilter().bandLabel)
725 matches = afwTable.packMatches(matchResult.matches)
726 matches.table.setMetadata(matchResult.matchMeta)
727 results.matchResult = matches
728 if self.config.doWriteMatchesDenormalized:
729 if matchResult.matches:
730 denormMatches = denormalizeMatches(matchResult.matches, matchResult.matchMeta)
731 else:
732 self.log.warning("No matches, so generating dummy denormalized matches file")
733 denormMatches = afwTable.BaseCatalog(afwTable.Schema())
734 denormMatches.setMetadata(PropertyList())
735 denormMatches.getMetadata().add("COMMENT",
736 "This catalog is empty because no matches were found.")
737 results.denormMatches = denormMatches
738 results.denormMatches = denormMatches
740 results.outputSources = sources
741 return results