From 3eea02d053881f721087332571462eed1188063b Mon Sep 17 00:00:00 2001 From: Dan Taranu Date: Thu, 31 Jul 2025 14:18:17 -0700 Subject: [PATCH] Add formatting to completeness plot labels --- .../tools/actions/plot/completenessPlot.py | 22 ++++++++++--------- .../lsst/analysis/tools/atools/diffMatched.py | 20 ++++++++++++++++- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/python/lsst/analysis/tools/actions/plot/completenessPlot.py b/python/lsst/analysis/tools/actions/plot/completenessPlot.py index 032babbbb..00b0e642b 100644 --- a/python/lsst/analysis/tools/actions/plot/completenessPlot.py +++ b/python/lsst/analysis/tools/actions/plot/completenessPlot.py @@ -56,10 +56,10 @@ class CompletenessHist(PlotAction): ) legendLocation = Field[str](doc="Legend position within main plot", default="lower left") mag_ref_label = Field[str]( - doc="Label for the completeness x axis.", default="{band}-band Reference Magnitude" + doc="Label for the completeness x axis.", default="{band}-band {name_flux} Magnitude" ) mag_target_label = Field[str]( - doc="Label for the purity x axis.", default="{band}-band Measured Magnitude" + doc="Label for the purity x axis.", default="{band}-band {name_flux} Magnitude" ) object_label = Field[str](doc="Label for measured objects", default="Object") reference_label = Field[str](doc="Label for reference objects", default="Reference") @@ -202,19 +202,21 @@ def makePlot(self, data, plotInfo, **kwargs): (data[names["completeness_good_match"]], False, self.color_right, "Correct Class"), ) - mag_ref_label = self.mag_ref_label - if "{band}" in mag_ref_label: - mag_ref_label = mag_ref_label.format(band=band) - mag_target_label = self.mag_target_label - if "{band}" in mag_target_label: - mag_target_label = mag_target_label.format(band=band) + # If the user left the name_flux template in the label value + # but never formatted it, apply a sensible generic default + mag_labels = { + "ref": (self.mag_ref_label, "Reference"), + "target": (self.mag_target_label, "Measured"), + } + for key, (label, name_flux_default) in mag_labels.items(): + mag_labels[key] = label.format(band=band, name_flux=name_flux_default) plots = { "Completeness": { "count_type": self.reference_label, "counts": data[names["count_ref"]], "lines": lineTuples, - "xlabel": mag_ref_label, + "xlabel": mag_labels["ref"], }, } if self.show_purity: @@ -226,7 +228,7 @@ def makePlot(self, data, plotInfo, **kwargs): (data[names["purity_bad_match"]], False, self.color_wrong, "Incorrect class"), (data[names["purity_good_match"]], False, self.color_right, "Correct class"), ), - "xlabel": mag_target_label, + "xlabel": mag_labels["target"], } # idx == 0 should be completeness; update this if that assumption diff --git a/python/lsst/analysis/tools/atools/diffMatched.py b/python/lsst/analysis/tools/atools/diffMatched.py index a4a21f5ee..53a144ed8 100644 --- a/python/lsst/analysis/tools/atools/diffMatched.py +++ b/python/lsst/analysis/tools/atools/diffMatched.py @@ -667,10 +667,28 @@ def finalize(self): overrides["color_counts"] = galaxies_color() elif name_type == self.type_stars: overrides["color_counts"] = stars_color() + plot_action = CompletenessHist( + action=completeness_plot, + ) + # Since the plot action is made on the fly, it can't be + # configured in a pipeline yaml. This makes the keys + # formattable, but (unfortunately) in a way that's hard + # to discover or document. + for label_type in ("ref", "target"): + name_mag_label = f"mag_{label_type}_label" + label = getattr(plot_action, name_mag_label) + if "{name_flux}" in label: + name_flux = getattr(self, f"config_mag_{label_type}").name_flux + # Still no support for partial formatting, i.e. + # if {band} is in label and it's not a kwarg of + # format, it will raise a KeyError + kwargs_format = {"band": "{band}"} if "{band}" in label else {} + label = label.format(name_flux=name_flux, **kwargs_format) + setattr(plot_action, name_mag_label, label) setattr( self.produce.plot.actions, object_class, - CompletenessHist(action=completeness_plot), + plot_action, ) def reconfigure_dependent_magnitudes(