Skip to content

Commit

Permalink
Add summary metric creation to calibrateImage.py
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmullaney committed Jun 12, 2024
1 parent 1e125bf commit f78e4ec
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion python/lsst/pipe/tasks/calibrateImage.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,23 @@
from . import measurePsf, repair, photoCal, computeExposureSummaryStats, snapCombine


class _EmptyTargetTask(pipeBase.PipelineTask):
"""
This is a placeholder target for CreateSummaryMetrics and must be retargeted at runtime.
CreateSummaryMetrics should target an analysis tool task, but that would, at the time
of writing, result in a circular import.
As a result, this class should not be used for anything else.
"""
ConfigClass = pipeBase.PipelineTaskConfig

def __init__(self, **kwargs) -> None:
raise NotImplementedError(
"doCreateSummaryMetrics is set to True, in which case "
"createSummaryMetrics must be retargeted."
)


class CalibrateImageConnections(pipeBase.PipelineTaskConnections,
dimensions=("instrument", "visit", "detector")):

Expand Down Expand Up @@ -136,6 +153,11 @@ class CalibrateImageConnections(pipeBase.PipelineTaskConnections,
storageClass="Catalog",
dimensions=("instrument", "visit", "detector"),
)
summary_metrics = connectionTypes.Output(
name="initial_summary_metrics",
storageClass="MetricMeasurementBundle",
dimensions=("instrument", "visit", "detector"),
)

def __init__(self, *, config=None):
super().__init__(config=config)
Expand All @@ -144,6 +166,8 @@ def __init__(self, *, config=None):
del self.psf_stars_footprints
del self.astrometry_matches
del self.photometry_matches
if config.do_create_summary_metrics is False:
del self.summary_metrics


class CalibrateImageConfig(pipeBase.PipelineTaskConfig, pipelineConnections=CalibrateImageConnections):
Expand Down Expand Up @@ -255,6 +279,16 @@ class CalibrateImageConfig(pipeBase.PipelineTaskConfig, pipelineConnections=Cali
target=computeExposureSummaryStats.ComputeExposureSummaryStatsTask,
doc="Task to to compute summary statistics on the calibrated exposure."
)
do_create_summary_metrics = pexConfig.Field(
dtype=bool,
default=False,
doc="Run the subtask to create summary metrics, and then write those metrics."
)
create_summary_metrics = pexConfig.ConfigurableField(
target=_EmptyTargetTask,
doc="Subtask to create metrics from the summary stats. This must be retargeted, likely to an"
"analysis_tools task such as CalexpSummaryMetrics."
)

def setDefaults(self):
super().setDefaults()
Expand Down Expand Up @@ -406,6 +440,7 @@ def __init__(self, initial_stars_schema=None, **kwargs):
self.makeSubtask("photometry", schema=initial_stars_schema)

self.makeSubtask("compute_summary_stats")
self.makeSubtask("create_summary_metrics")

# For the butler to persist it.
self.initial_stars_schema = afwTable.SourceCatalog(initial_stars_schema)
Expand Down Expand Up @@ -539,7 +574,9 @@ def run(self, *, exposures, id_generator=None, result=None):
result.photometry_matches = lsst.meas.astrom.denormalizeMatches(photometry_matches,
photometry_meta)

self._summarize(result.exposure, result.stars_footprints, result.background)
result.summary_metrics = self._summarize(result.exposure,
result.stars_footprints,
result.background)

return result

Expand Down Expand Up @@ -849,3 +886,8 @@ def _summarize(self, exposure, stars, background):
# applied calibration). This needs to be checked.
summary = self.compute_summary_stats.run(exposure, stars, background)
exposure.info.setSummaryStats(summary)

summaryMetrics = None
if self.config.do_create_summary_metrics:
summaryMetrics = self.create_summary_metrics.run(data=summary.__dict__).metrics
return summaryMetrics

0 comments on commit f78e4ec

Please sign in to comment.