Skip to content

Commit

Permalink
Merge pull request #138 from lsst/tickets/DM-39332
Browse files Browse the repository at this point in the history
DM-39332: Fix the PF1 metric to be threshold relative to mean
  • Loading branch information
jeffcarlin committed Aug 24, 2023
2 parents da3c9a1 + 9f2f554 commit d43a604
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
20 changes: 14 additions & 6 deletions python/lsst/analysis/tools/actions/plot/histPlot.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ class HistPanel(Config):
default=None,
optional=True,
)
refRelativeToMedian = Field[bool](
doc="Is the referenceValue meant to be an offset from the median?",
default=False,
optional=True,
)
histDensity = Field[bool](
doc="Whether to plot the histogram as a normalized probability distribution. Must also "
"provide a value for referenceValue",
Expand Down Expand Up @@ -452,7 +457,7 @@ def _makePanel(self, data, panel, ax, colors, label_font_size=9, legend_font_siz
# If histDensity is True, also plot a reference PDF with
# mean = referenceValue and sigma = 1 for reference.
if self.panels[panel].referenceValue is not None:
ax = self._addReferenceLines(ax, panel, panel_range, legend_font_size=legend_font_size)
ax = self._addReferenceLines(ax, panel, panel_range, meds, legend_font_size=legend_font_size)

# Check if we should use the default stats panel or if a custom one
# has been created.
Expand Down Expand Up @@ -536,7 +541,7 @@ def _calcStats(self, data):
mad = sigmaMad(data)
return num, med, mad

def _addReferenceLines(self, ax, panel, panel_range, legend_font_size=7):
def _addReferenceLines(self, ax, panel, panel_range, meds, legend_font_size=7):
"""Draw the vertical reference line and density curve (if requested)
on the panel.
"""
Expand All @@ -548,10 +553,13 @@ def _addReferenceLines(self, ax, panel, panel_range, legend_font_size=7):
if self.panels[panel].histDensity:
reference_label = None
else:
reference_label = "${{\\mu_{{ref}}}}$: {}".format(self.panels[panel].referenceValue)
ax2.axvline(
self.panels[panel].referenceValue, ls="-", lw=1, c="black", zorder=0, label=reference_label
)
if self.panels[panel].refRelativeToMedian:
reference_value = self.panels[panel].referenceValue + meds[0]
reference_label = "${{\\mu_{{ref}}}}$: {}".format(reference_value)
else:
reference_value = self.panels[panel].referenceValue
reference_label = "${{\\mu_{{ref}}}}$: {}".format(reference_value)
ax2.axvline(reference_value, ls="-", lw=1, c="black", zorder=0, label=reference_label)
if self.panels[panel].histDensity:
ref_x = np.arange(panel_range[0], panel_range[1], (panel_range[1] - panel_range[0]) / 100.0)
ref_mean = self.panels[panel].referenceValue
Expand Down
8 changes: 7 additions & 1 deletion python/lsst/analysis/tools/actions/scalar/scalarActions.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ class FracThreshold(ScalarAction):
threshold = Field[float](doc="Threshold to apply.")
vectorKey = Field[str](doc="Name of column")
percent = Field[bool](doc="Express result as percentage", default=False)
relative_to_median = Field[bool](doc="Calculate threshold relative to " "the median?", default=False)

def getInputSchema(self) -> KeyedDataSchema:
return ((self.vectorKey, Vector),)
Expand All @@ -168,9 +169,14 @@ def __call__(self, data: KeyedData, **kwargs) -> Scalar:
values = data[self.vectorKey.format(**kwargs)]
values = values[mask] # type: ignore
values = values[np.logical_not(np.isnan(values))]
# If relative_to_median is set, shift the threshold to be median+thresh
if self.relative_to_median:
threshold = self.threshold + np.median(values)
else:
threshold = self.threshold
result = cast(
Scalar,
float(np.sum(getattr(operator, self.op)(values, self.threshold)) / len(values)), # type: ignore
float(np.sum(getattr(operator, self.op)(values, threshold)) / len(values)), # type: ignore
)
if self.percent:
return 100.0 * result
Expand Down
3 changes: 3 additions & 0 deletions python/lsst/analysis/tools/atools/photometricRepeatability.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ def setDefaults(self):
op="ge",
threshold=self.PA2Value,
percent=True,
relative_to_median=True,
)
self.process.calculateActions.photRepeatNsources = CountAction(vectorKey="perGroupStdevFiltered")

Expand All @@ -125,6 +126,8 @@ def setDefaults(self):
self.produce.plot.panels["panel_rms"].statsPanel.stat3 = ["photRepeatOutlier"]

self.produce.plot.panels["panel_rms"].referenceValue = self.PA2Value
self.produce.plot.panels["panel_rms"].refRelativeToMedian = True

self.produce.plot.panels["panel_rms"].label = "rms (mmag)"
self.produce.plot.panels["panel_rms"].hists = dict(perGroupStdevFiltered="Filtered per group rms")

Expand Down

0 comments on commit d43a604

Please sign in to comment.