Skip to content

DM-48045: Create template metrics and template QC process for Prompt Processing. #408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions pipelines/deepCoaddQualityCore.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
description: |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really want to add another two pipelines on top of coaddQualityCore.yaml?

Could we instead just add this to coaddQualityCore.yaml, using either deep or template as the default connections.coaddType, then override that in either drp_pipe or ap_pipe (depending on which we choose - deep or template).

I think I'd prefer for the default here to be deep, since that's what's generally found in coaddQualityCore.yaml. We'd just make sure that it wasn't run as part of drp_pipe (if we didn't want it to be).

I saw the chat on dm-science-pipelines about overriding coaddName, but I don't think that's referring to this - I think that's specifically referring to doing all the coaddQualityCore tasks with template as well as deep.

Tier1 plots and metrics to assess deep_coadd quality.
tasks:
analyzeCoaddDepthCore:
class: lsst.analysis.tools.tasks.CoaddDepthSummaryAnalysisTask
config:
connections.coaddType: deep
coaddDepthMetricTract:
class: lsst.analysis.tools.tasks.CoaddDepthTableTractAnalysisTask
config:
atools.coadd_depth_summary_metrics: CoaddQualityCheck
python: |
from lsst.analysis.tools.atools import CoaddQualityCheck
coaddDepthPlot:
class: lsst.analysis.tools.tasks.CoaddDepthSummaryPlotTask
config:
connections.coaddType: deep
# plot will be called n_image_deep_CoaddDepthPlot
# 'n_image' originates from the task outputName; 'deep' originates from the line below
atools.deep: CoaddQualityPlot
python: |
from lsst.analysis.tools.atools import CoaddQualityPlot
22 changes: 22 additions & 0 deletions pipelines/templateQualityCore.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
description: |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above comment.

Tier1 plots and metrics to assess template quality.
tasks:
analyzeCoaddDepthCore:
class: lsst.analysis.tools.tasks.CoaddDepthSummaryAnalysisTask
config:
connections.coaddType: template
coaddDepthMetricTract:
class: lsst.analysis.tools.tasks.CoaddDepthTableTractAnalysisTask
config:
atools.coadd_depth_summary_metrics: CoaddQualityCheck
python: |
from lsst.analysis.tools.atools import CoaddQualityCheck
coaddDepthPlot:
class: lsst.analysis.tools.tasks.CoaddDepthSummaryPlotTask
config:
connections.coaddType: template
# plot will be called n_image_template_CoaddDepthPlot
# 'n_image' originates from the task outputName; 'template' originates from the line below
atools.template: CoaddQualityPlot
python: |
from lsst.analysis.tools.atools import CoaddQualityPlot
1 change: 1 addition & 0 deletions python/lsst/analysis/tools/actions/plot/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from .barPlots import *
from .calculateRange import *
from .coaddDepthPlot import *
from .colorColorFitPlot import *
from .completenessPlot import *
from .diaSkyPlot import *
Expand Down
159 changes: 159 additions & 0 deletions python/lsst/analysis/tools/actions/plot/coaddDepthPlot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
# This file is part of analysis_tools.
#
# Developed for the LSST Data Management System.
# This product includes software developed by the LSST Project
# (https://www.lsst.org).
# See the COPYRIGHT file at the top-level directory of this distribution
# for details of code ownership.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

from __future__ import annotations

__all__ = ("CoaddDepthPlot",)

from typing import TYPE_CHECKING, Any, Mapping

from matplotlib.figure import Figure
from matplotlib.lines import Line2D
import matplotlib.pyplot as plt

from lsst.utils.plotting import publication_plots, set_rubin_plotstyle

from ...interfaces import PlotAction, Vector
from ..vector import BandSelector, PatchSelector
from .plotUtils import addPlotInfo

if TYPE_CHECKING:
from ...interfaces import KeyedData, KeyedDataSchema

bands_dict = publication_plots.get_band_dicts()


class CoaddDepthPlot(PlotAction):
"""Make a plot of pixels per coadd depth."""

def setDefaults(self):
super().setDefaults()

def getInputSchema(self) -> KeyedDataSchema:
base: list[tuple[str, type[Vector]]] = []
base.append(("patch", Vector))
base.append(("band", Vector))
base.append(("depth", Vector))
base.append(("pixels", Vector))
return base

def __call__(self, data: KeyedData, **kwargs) -> Figure:
self._validateInput(data)
return self.makePlot(data, **kwargs)

def _validateInput(self, data: KeyedData) -> None:
needed = set(k[0] for k in self.getInputSchema())
if not needed.issubset(data.keys()):
raise ValueError(f"Input data does not contain all required keys: {self.getInputSchema()}")

def makePlot(self, data: KeyedData, plotInfo: Mapping[str, str] | None = None, **kwargs: Any) -> Figure:
"""Make the plot.

Parameters
----------
`KeyedData`
The catalog to plot the points from.

plotInfo : `dict`
A dictionary of the plot information.

Returns
-------
fig : `~matplotlib.figure.Figure`
The resulting figure.
"""
set_rubin_plotstyle()
fig = plt.figure(dpi=300, figsize=(20, 20))

max_depth = max(data['depth'])
max_pixels = max(data['pixels'])

plt.subplots_adjust(hspace=0, wspace=0)

patch_counter = 90 # The top left corner of a tract is patch 90
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't hard-coding this risky? Just thinking that other skymaps may not have the same number of patches per tract.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class should be using the skymap tractInfo. See PerTractPropertyMapPlot for an example.

m = 0 # subplot index
while patch_counter >= 0:
for n in range(10): # column index
ax = plt.subplot(10, 10, m + 1) # there are 10x10 patches per tract
patchSelector = PatchSelector(vectorKey='patch', patches=[patch_counter])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no point making a ConfigurableAction inside a call method where it can't actually be reconfigured in a pipeline. You already put patch and band in the schema so you may as well just do patch_mask = data["patch"] == patch_counter, and same for band_mask.

patch_mask = patchSelector(data)

if patch_counter in data['patch']:
uniqueBands = set(data['band'][patch_mask])
for band in uniqueBands:
color = bands_dict['colors'][band]
markerstyle = bands_dict['symbols'][band]
bandSelector = BandSelector(vectorKey='band', bands=[band])
band_mask = bandSelector(data)

tot_mask = (patch_mask) & (band_mask)

ax.plot(data['depth'][tot_mask], data['pixels'][tot_mask],
color=color, linewidth=0, ls=None,
marker=markerstyle, ms=4, alpha=0.75,
label=f'{band}')
ax.grid(alpha=0.5)

# Chart formatting
# Need a solution for ax.set_xscale when max_depth is high,
# but not all patches/bands have quality coverage.
ax.set_yscale('log')
ax.set_xlim(0, max_depth + 5)
ax.set_ylim(5, max_pixels)
# Can we somehow generalize ax.set_xticks?
# ax.set_xticks(np.arange(0, max_depth, 20))
ax.tick_params(axis="both", which="minor")
ax.tick_params(axis='both', which="both", top=False, right=False)

# Only label axes of the farmost left and bottom row of plots
if (n != 0):
ax.set_yticklabels([])
ax.tick_params(axis='y', which='both', length=0)
if (patch_counter not in range(10)):
ax.set_xticklabels([])
ax.tick_params(axis='x', which='both', length=0)

ax.set_title(f"patch {patch_counter}", y=0.85)
patch_counter += 1
m += 1
patch_counter -= 2*(n+1)
fig.supxlabel('Number of input visits (n_image depth)', y=0.075)
fig.supylabel('Count (pixels)', x=0.075)
legend_elements = [
Line2D([0], [0], color=bands_dict['colors']['u'],
lw=0, marker=bands_dict['symbols']['u'], label='u'),
Line2D([0], [0], color=bands_dict['colors']['g'],
lw=0, marker=bands_dict['symbols']['g'], label='g'),
Line2D([0], [0], color=bands_dict['colors']['r'],
lw=0, marker=bands_dict['symbols']['r'], label='r'),
Line2D([0], [0], color=bands_dict['colors']['i'],
lw=0, marker=bands_dict['symbols']['i'], label='i'),
Line2D([0], [0], color=bands_dict['colors']['z'],
lw=0, marker=bands_dict['symbols']['z'], label='z'),
Line2D([0], [0], color=bands_dict['colors']['y'],
lw=0, marker=bands_dict['symbols']['y'], label='y'),
]
plt.legend(handles=legend_elements, loc='upper left', bbox_to_anchor=(1.05, 10))

if plotInfo is not None:
fig = addPlotInfo(fig, plotInfo)

return fig
32 changes: 32 additions & 0 deletions python/lsst/analysis/tools/actions/vector/selectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"VisitPlotFlagSelector",
"ThresholdSelector",
"BandSelector",
"PatchSelector",
"MatchingFlagSelector",
"MagSelector",
"InjectedClassSelector",
Expand Down Expand Up @@ -552,6 +553,37 @@ def __call__(self, data: KeyedData, **kwargs) -> Vector:
return cast(Vector, mask)


class PatchSelector(VectorAction):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI I added a PatchSelector recently, but it doesn't look like you actually need it.

"""Makes a mask for sources observed in a specified set of patches."""

vectorKey = Field[str](doc="Key of the Vector which defines the patch.", default="patch")
patches = ListField[int](
doc="The patches to select. `None` indicates no patch selection applied.",
default=[],
)

def getInputSchema(self) -> KeyedDataSchema:
return ((self.vectorKey, Vector),)

def __call__(self, data: KeyedData, **kwargs) -> Vector:
patches: Optional[tuple[str, ...]]
match kwargs:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I like what this is doing grammatically (patch vs. patches 😄 ), and I see that we do the same for band/bands, I wonder whether we should just keep it simple and have just patches=None as a keyword so that it's easier to see at a glance.

case {"patch": patch} if not self.patches and self.patches == []:
patches = (patch,)
case {"patches": patches} if not self.patches and self.patches == []:
patches = patches
case _ if self.patches:
patches = tuple(self.patches)
case _:
patches = None
if patches:
mask = np.isin(data[self.vectorKey], patches).flatten()
else:
# No patch selection is applied, i.e., select all rows
mask = np.full(len(data[self.vectorKey]), True) # type: ignore
return cast(Vector, mask)


class ParentObjectSelector(FlagSelector):
"""Select only parent objects that are not sky objects."""

Expand Down
15 changes: 15 additions & 0 deletions python/lsst/analysis/tools/actions/vector/vectorActions.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"RAcosDec",
"AngularSeparation",
"IsMatchedObjectSameClass",
"UniqueAction",
)

import logging
Expand Down Expand Up @@ -441,3 +442,17 @@ def getInputSchema(self) -> KeyedDataSchema:
yield self.key_is_ref_star, Vector
yield self.key_is_target_galaxy, Vector
yield self.key_is_target_star, Vector


class UniqueAction(VectorAction):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this used? I can't find anywhere in the PR where this is used. Was it added, then not needed, or have I missed something?

"""Return the unique items from a vector."""

vectorKey = Field[str](doc="The vector key to return the unique values from.")

def __call__(self, data: KeyedData, **kwargs) -> Vector:
mask = kwargs.get("mask")
result = data[self.vectorKey][mask]
return np.array(set(list(result[0])))

def getInputSchema(self) -> KeyedDataSchema:
yield self.vectorKey, Vector
Loading
Loading