Skip to content

Commit b7e8cae

Browse files
committed
Replace matchedRefDiff/ChiContext with pexConfig.ChoiceField
1 parent dd32c9b commit b7e8cae

4 files changed

Lines changed: 105 additions & 127 deletions

File tree

pipelines/coaddDiffMatchedQualityExtended.yaml

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,53 +8,50 @@ tasks:
88

99
# plots
1010
atools.matchedRefCModelMagDiffPlot: MatchedRefCoaddCModelFluxPlot
11-
atools.matchedRefCModelMagDiffPlot.applyContext: MatchedRefDiffContext
11+
atools.matchedRefCModelMagDiffPlot.context: 'diff'
1212

1313
atools.matchedRefCModelFluxChiPlot: MatchedRefCoaddCModelFluxPlot
14-
atools.matchedRefCModelFluxChiPlot.applyContext: MatchedRefChiContext
14+
atools.matchedRefCModelFluxChiPlot.context: 'chi'
1515

1616
# TODO: Can this be a one liner?
1717
atools.matchedRefPositionXDiffPlot: MatchedRefCoaddPositionPlot
18-
# TODO: variable must be defined before applyContext; can this be enforced?
19-
# (the resulting error if not is not very informative)
2018
atools.matchedRefPositionXDiffPlot.variable: x
21-
atools.matchedRefPositionXDiffPlot.applyContext: MatchedRefDiffContext
19+
atools.matchedRefPositionXDiffPlot.context: 'diff'
2220

2321
atools.matchedRefPositionXChiPlot: MatchedRefCoaddPositionPlot
2422
atools.matchedRefPositionXChiPlot.variable: x
25-
atools.matchedRefPositionXChiPlot.applyContext: MatchedRefChiContext
23+
atools.matchedRefPositionXChiPlot.context: 'chi'
2624

2725
atools.matchedRefPositionYDiffPlot: MatchedRefCoaddPositionPlot
2826
atools.matchedRefPositionYDiffPlot.variable: y
29-
atools.matchedRefPositionYDiffPlot.applyContext: MatchedRefDiffContext
27+
atools.matchedRefPositionYDiffPlot.context: 'diff'
3028

3129
atools.matchedRefPositionYChiPlot: MatchedRefCoaddPositionPlot
3230
atools.matchedRefPositionYChiPlot.variable: y
33-
atools.matchedRefPositionYChiPlot.applyContext: MatchedRefChiContext
31+
atools.matchedRefPositionYChiPlot.context: 'chi'
3432

3533
# metrics
3634
atools.matchedRefCModelMagDiffMetric: MatchedRefCoaddCModelFluxMetric
37-
atools.matchedRefCModelMagDiffMetric.applyContext: MatchedRefDiffContext
35+
atools.matchedRefCModelMagDiffMetric.context: 'diff'
3836

3937
atools.matchedRefCModelFluxChiMetric: MatchedRefCoaddCModelFluxMetric
40-
atools.matchedRefCModelFluxChiMetric.applyContext: MatchedRefChiContext
38+
atools.matchedRefCModelFluxChiMetric.context: 'chi'
4139

4240
atools.matchedRefPositionXDiffMetric: MatchedRefCoaddPositionMetric
4341
atools.matchedRefPositionXDiffMetric.variable: x
44-
atools.matchedRefPositionXDiffMetric.applyContext: MatchedRefChiContext
42+
atools.matchedRefPositionXDiffMetric.context: 'chi'
4543

4644
atools.matchedRefPositionXChiMetric: MatchedRefCoaddPositionMetric
4745
atools.matchedRefPositionXChiMetric.variable: x
48-
atools.matchedRefPositionXChiMetric.applyContext: MatchedRefChiContext
46+
atools.matchedRefPositionXChiMetric.context: 'chi'
4947

5048
atools.matchedRefPositionYDiffMetric: MatchedRefCoaddPositionMetric
5149
atools.matchedRefPositionYDiffMetric.variable: y
52-
atools.matchedRefPositionYDiffMetric.applyContext: MatchedRefDiffContext
50+
atools.matchedRefPositionYDiffMetric.context: 'diff'
5351

5452
atools.matchedRefPositionYChiMetric: MatchedRefCoaddPositionMetric
5553
atools.matchedRefPositionYChiMetric.variable: y
56-
atools.matchedRefPositionYChiMetric.applyContext: MatchedRefChiContext
57-
54+
atools.matchedRefPositionYChiMetric.context: 'chi'
5855

5956
python: |
6057
from lsst.analysis.tools.atools.diffMatched import (
@@ -66,8 +63,3 @@ tasks:
6663
MatchedRefCoaddCModelFluxPlot,
6764
MatchedRefCoaddPositionPlot,
6865
)
69-
70-
from lsst.analysis.tools.contexts._contexts import (
71-
MatchedRefDiffContext,
72-
MatchedRefChiContext,
73-
)

python/lsst/analysis/tools/atools/diffMatched.py

Lines changed: 88 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -197,26 +197,30 @@ class MatchedRefCoaddDiffMagTool(MatchedRefCoaddToolBase):
197197
The default model flux is cModel.
198198
"""
199199

200-
def matchedRefDiffContext(self):
201-
self.process.buildActions.diff = SubtractVector(
202-
actionA=MagColumnNanoJansky(
203-
vectorKey=self.process.buildActions.fluxes_meas.vectorKey, returnMillimags=True
204-
),
205-
actionB=DivideVector(
206-
actionA=self.process.buildActions.mags_ref,
207-
# To convert to mmag
208-
actionB=ConstantValue(value=1e-3),
209-
),
210-
)
211-
212-
def matchedRefChiContext(self):
213-
self.process.buildActions.diff = DivideVector(
214-
actionA=SubtractVector(
215-
actionA=LoadVector(vectorKey=self.process.buildActions.fluxes_meas.vectorKey),
216-
actionB=LoadVector(vectorKey=self.process.buildActions.fluxes_ref.vectorKey),
217-
),
218-
actionB=LoadVector(vectorKey=f"{self.process.buildActions.fluxes_meas.vectorKey}Err"),
219-
)
200+
def finalize(self):
201+
super().finalize()
202+
if not hasattr(self.process.buildActions, "diff"):
203+
if self.context == "diff":
204+
self.process.buildActions.diff = SubtractVector(
205+
actionA=MagColumnNanoJansky(
206+
vectorKey=self.process.buildActions.fluxes_meas.vectorKey, returnMillimags=True
207+
),
208+
actionB=DivideVector(
209+
actionA=self.process.buildActions.mags_ref,
210+
# To convert to mmag
211+
actionB=ConstantValue(value=1e-3),
212+
),
213+
)
214+
elif self.context == "chi":
215+
self.process.buildActions.diff = DivideVector(
216+
actionA=SubtractVector(
217+
actionA=LoadVector(vectorKey=self.process.buildActions.fluxes_meas.vectorKey),
218+
actionB=LoadVector(vectorKey=self.process.buildActions.fluxes_ref.vectorKey),
219+
),
220+
actionB=LoadVector(vectorKey=f"{self.process.buildActions.fluxes_meas.vectorKey}Err"),
221+
)
222+
else:
223+
raise ValueError(f"Unrecognized {self.context=}")
220224

221225
def setDefaults(self):
222226
super().setDefaults()
@@ -237,20 +241,19 @@ def setDefaults(self):
237241
class MatchedRefCoaddCModelFluxMetric(MatchedRefCoaddDiffMagTool, MatchedRefCoaddMetric):
238242
"""Metric for diffs between reference and CModel coadd mags."""
239243

240-
def matchedRefDiffContext(self):
241-
super().matchedRefDiffContext()
242-
self.unit = "mmag"
243-
self.name_prefix = "photom_mag_cModelFlux_{name_class}_diff_"
244-
self.produce.metric.units = self.configureMetrics()
245-
246-
def matchedRefChiContext(self):
247-
super().matchedRefChiContext()
248-
self.unit = ""
249-
self.name_prefix = "photom_mag_cModelFlux_{name_class}_chi_"
250-
self.produce.metric.units = self.configureMetrics()
251-
252-
def setDefaults(self):
253-
super().setDefaults()
244+
def finalize(self):
245+
super().finalize()
246+
if not hasattr(self.process.metric, "units"):
247+
if self.context == "diff":
248+
self.unit = "mmag"
249+
self.name_prefix = "photom_mag_cModelFlux_{name_class}_diff_"
250+
self.produce.metric.units = self.configureMetrics()
251+
elif self.context == "chi":
252+
self.unit = ""
253+
self.name_prefix = "photom_mag_cModelFlux_{name_class}_chi_"
254+
self.produce.metric.units = self.configureMetrics()
255+
else:
256+
raise ValueError(f"Unrecognized {self.context=}")
254257

255258

256259
class MatchedRefCoaddDiffPositionTool(MatchedRefCoaddToolBase):
@@ -265,30 +268,26 @@ class MatchedRefCoaddDiffPositionTool(MatchedRefCoaddToolBase):
265268
optional=False,
266269
)
267270

268-
# TODO: Determine if this can be put back into setDefaults w/o this:
269-
# lsst.pex.config.config.FieldValidationError:
270-
# Field 'process.buildActions.pos_meas.vectorKey' failed validation:
271-
# Required value cannot be None
272-
def _setPos(self):
273-
self.process.buildActions.pos_meas = LoadVector(vectorKey=self.variable)
274-
self.process.buildActions.pos_ref = LoadVector(vectorKey=f"refcat_{self.variable}")
275-
276-
def matchedRefDiffContext(self):
277-
self._setPos()
278-
self.process.buildActions.diff = SubtractVector(
279-
actionA=self.process.buildActions.pos_meas,
280-
actionB=self.process.buildActions.pos_ref,
281-
)
282-
283-
def matchedRefChiContext(self):
284-
self._setPos()
285-
self.process.buildActions.diff = DivideVector(
286-
actionA=SubtractVector(
287-
actionA=self.process.buildActions.pos_meas,
288-
actionB=self.process.buildActions.pos_ref,
289-
),
290-
actionB=LoadVector(vectorKey=f"{self.process.buildActions.pos_meas.vectorKey}Err"),
291-
)
271+
def finalize(self):
272+
super().finalize()
273+
if not hasattr(self.process.buildActions, "pos_meas"):
274+
self.process.buildActions.pos_meas = LoadVector(vectorKey=self.variable)
275+
self.process.buildActions.pos_ref = LoadVector(vectorKey=f"refcat_{self.variable}")
276+
if self.context == "diff":
277+
self.process.buildActions.diff = SubtractVector(
278+
actionA=self.process.buildActions.pos_meas,
279+
actionB=self.process.buildActions.pos_ref,
280+
)
281+
elif self.context == "chi":
282+
self.process.buildActions.diff = DivideVector(
283+
actionA=SubtractVector(
284+
actionA=self.process.buildActions.pos_meas,
285+
actionB=self.process.buildActions.pos_ref,
286+
),
287+
actionB=LoadVector(vectorKey=f"{self.process.buildActions.pos_meas.vectorKey}Err"),
288+
)
289+
else:
290+
raise ValueError(f"Unrecognized {self.context=}")
292291

293292
def setDefaults(self):
294293
super().setDefaults()
@@ -307,20 +306,19 @@ def setDefaults(self):
307306
class MatchedRefCoaddPositionMetric(MatchedRefCoaddDiffPositionTool, MatchedRefCoaddMetric):
308307
"""Metric for diffs between reference and base coadd centroids."""
309308

310-
def matchedRefDiffContext(self):
311-
super().matchedRefDiffContext()
312-
self.unit = "pix"
313-
self.name_prefix = f"astrom_{self.variable}_{{name_class}}_diff_"
314-
self.produce.metric.units = self.configureMetrics()
315-
316-
def matchedRefChiContext(self):
317-
super().matchedRefChiContext()
318-
self.unit = ""
319-
self.name_prefix = f"astrom_{self.variable}_{{name_class}}_diff_"
320-
self.produce.metric.units = self.configureMetrics()
321-
322-
def setDefaults(self):
323-
super().setDefaults()
309+
def finalize(self):
310+
super().finalize()
311+
if not hasattr(self.process.metric, "units"):
312+
if self.context == "diff":
313+
self.unit = "pix"
314+
self.name_prefix = f"astrom_{self.variable}_{{name_class}}_diff_"
315+
self.produce.metric.units = self.configureMetrics()
316+
elif self.context == "chi":
317+
self.unit = ""
318+
self.name_prefix = f"astrom_{self.variable}_{{name_class}}_diff_"
319+
self.produce.metric.units = self.configureMetrics()
320+
else:
321+
raise ValueError(f"Unrecognized {self.context=}")
324322

325323

326324
class MatchedRefCoaddPlot(AnalysisTool):
@@ -360,26 +358,24 @@ def setDefaults(self):
360358

361359

362360
class MatchedRefCoaddCModelFluxPlot(MatchedRefCoaddCModelPlot, MatchedRefCoaddDiffMagTool):
363-
def matchedRefDiffContext(self):
364-
super().matchedRefDiffContext()
365-
self.produce.plot.yAxisLabel = "cModel - Reference mmag"
366-
367-
def matchedRefChiContext(self):
368-
super().matchedRefChiContext()
369-
self.produce.plot.yAxisLabel = "chi = (cModel - Reference mag)/error"
370-
371-
def setDefaults(self):
372-
super().setDefaults()
361+
def finalize(self):
362+
MatchedRefCoaddCModelPlot(self).finalize()
363+
MatchedRefCoaddDiffMagTool(self).finalize()
364+
if self.context == "diff":
365+
self.produce.plot.yAxisLabel = "cModel - Reference mmag"
366+
elif self.context == "chi":
367+
self.produce.plot.yAxisLabel = "chi = (cModel - Reference mag)/error"
368+
else:
369+
raise ValueError(f"Unrecognized {self.context=}")
373370

374371

375372
class MatchedRefCoaddPositionPlot(MatchedRefCoaddCModelPlot, MatchedRefCoaddDiffPositionTool):
376-
def matchedRefDiffContext(self):
377-
super().matchedRefDiffContext()
378-
self.produce.plot.yAxisLabel = f"{self.variable} position (pix)"
379-
380-
def matchedRefChiContext(self):
381-
super().matchedRefChiContext()
382-
self.produce.plot.yAxisLabel = f"chi = (slot - Reference {self.variable} position)/error"
383-
384-
def setDefaults(self):
385-
super().setDefaults()
373+
def finalize(self):
374+
MatchedRefCoaddCModelPlot(self).finalize()
375+
MatchedRefCoaddDiffPositionTool(self).finalize()
376+
if self.context == "diff":
377+
self.produce.plot.yAxisLabel = f"{self.variable} position (pix)"
378+
elif self.context == "chi":
379+
self.produce.plot.yAxisLabel = f"chi = (slot - Reference {self.variable} position)/error"
380+
else:
381+
raise ValueError(f"Unrecognized {self.context=}")

python/lsst/analysis/tools/contexts/_contexts.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,10 @@
2424
be a subclass of `Context`, and should contain a description of what the
2525
context is for as it's docstring.
2626
"""
27-
__all__ = ("MatchedRefDiffContext", "MatchedRefChiContext")
27+
__all__ = ("ExampleContext",)
2828

2929
from ._baseContext import Context
3030

3131

32-
class MatchedRefDiffContext(Context):
33-
"""A context which indicates `AnalysisAction`s are computing differences
34-
between matches to reference objects.
35-
"""
36-
37-
38-
class MatchedRefChiContext(Context):
39-
"""A context which indicates `AnalysisAction`s are computing error-scaled
40-
differences between matches to reference objects.
41-
"""
32+
class ExampleContext(Context):
33+
"""An example context which should not actually be used."""

tests/test_metrics.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,17 @@
2929
MatchedRefCoaddMetric,
3030
MatchedRefCoaddPositionMetric,
3131
)
32-
from lsst.analysis.tools.contexts import MatchedRefChiContext, MatchedRefDiffContext
3332

3433

3534
class TestDiffMatched(TestCase):
3635
def setUp(self) -> None:
3736
super().setUp()
3837
self.band_default = "analysisTools"
39-
self.contexts = (MatchedRefChiContext, MatchedRefDiffContext)
38+
self.contexts = ("chi", "diff")
4039

4140
def _testMatchedRefCoaddMetricDerived(self, type_metric: type[MatchedRefCoaddMetric], **kwargs):
4241
for context in self.contexts:
43-
tester = type_metric(**kwargs)
44-
tester.applyContext(context)
42+
tester = type_metric(**kwargs, context=context)
4543
tester.finalize()
4644

4745
keys = list(k[0] for k in tester.getInputSchema())

0 commit comments

Comments
 (0)