Coverage for python/lsst/analysis/tools/interfaces/_actions.py: 62%
77 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-08 13:17 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-08 13:17 +0000
1# This file is part of analysis_tools.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (https://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <https://www.gnu.org/licenses/>.
22from __future__ import annotations
24__all__ = (
25 "AnalysisAction",
26 "KeyedDataAction",
27 "VectorAction",
28 "ScalarAction",
29 "MetricResultType",
30 "MetricAction",
31 "PlotResultType",
32 "PlotAction",
33 "JointResults",
34 "JointAction",
35 "NoMetric",
36 "NoPlot",
37)
39import warnings
40from abc import abstractmethod
41from dataclasses import dataclass
42from typing import Iterable
44import lsst.pex.config as pexConfig
45from lsst.pex.config.configurableActions import ConfigurableAction, ConfigurableActionField
47from ..contexts import ContextApplier
48from ._interfaces import KeyedData, KeyedDataSchema, MetricResultType, PlotResultType, Scalar, Vector
51class AnalysisAction(ConfigurableAction):
52 """Base class interface for the various actions used in analysis tools.
54 This extends the basic `ConfigurableAction` class to include interfaces for
55 defining what an action expects to consume, and what it expects to produce.
56 """
58 def __init_subclass__(cls, **kwargs):
59 if "getInputSchema" not in dir(cls): 59 ↛ 60line 59 didn't jump to line 60, because the condition on line 59 was never true
60 raise NotImplementedError(f"Class {cls} must implement method getInputSchema")
62 # This is a descriptor that functions like a function in most contexts
63 # and can be treated as such
64 applyContext = ContextApplier()
65 r"""Apply a `Context` to an `AnalysisAction` recursively.
67 Generally this method is called from within an `AnalysisTool` to
68 configure all `AnalysisAction`\ s at one time to make sure that they
69 all are consistently configured. However, it is permitted to call this
70 method if you are aware of the effects, or from within a specific
71 execution environment like a python shell or notebook.
73 Parameters
74 ----------
75 context : `Context`
76 The specific execution context, this may be a single context or
77 a joint context, see `Context` for more info.
78 """
80 @abstractmethod
81 def getInputSchema(self) -> KeyedDataSchema:
82 """Return the schema an `AnalysisAction` expects to be present in the
83 arguments supplied to the __call__ method.
85 Returns
86 -------
87 result : `KeyedDataSchema`
88 The schema this action requires to be present when calling this
89 action, keys are unformatted.
90 """
91 raise NotImplementedError("This is not implemented on the base class")
93 def getOutputSchema(self) -> KeyedDataSchema | None:
94 """Return the schema an `AnalysisAction` will produce, if the
95 ``__call__`` method returns `KeyedData`, otherwise this may return
96 None.
98 Returns
99 -------
100 result : `KeyedDataSchema` or None
101 The schema this action will produce when returning from call. This
102 will be unformatted if any templates are present. Should return
103 None if action does not return `KeyedData`.
104 """
105 return None
107 def getFormattedInputSchema(self, **kwargs) -> KeyedDataSchema:
108 """Return input schema, with keys formatted with any arguments supplied
109 by kwargs passed to this method.
111 Returns
112 -------
113 result : `KeyedDataSchema`
114 The schema this action requires to be present when calling this
115 action, formatted with any input arguments (e.g. band='i')
116 """
117 for key, typ in self.getInputSchema():
118 yield key.format_map(kwargs), typ
120 def addInputSchema(self, inputSchema: KeyedDataSchema) -> None:
121 """Add the supplied inputSchema argument to the class such that it will
122 be returned along side any other arguments in a call to
123 ``getInputSchema``.
125 Parameters
126 ----------
127 inputSchema : `KeyedDataSchema`
128 A schema that is to be merged in with any existing schema when a
129 call to ``getInputSchema`` is made.
130 """
131 warnings.warn(
132 f"{type(self)} does not implement adding input schemas, call will do nothing, "
133 "this may be expected",
134 RuntimeWarning,
135 )
138class KeyedDataAction(AnalysisAction):
139 """A `KeyedDataAction` is an `AnalysisAction` that returns `KeyedData` when
140 called.
141 """
143 @abstractmethod
144 def __call__(self, data: KeyedData, **kwargs) -> KeyedData:
145 raise NotImplementedError("This is not implemented on the base class")
148class VectorAction(AnalysisAction):
149 """A `VectorAction` is an `AnalysisAction` that returns a `Vector` when
150 called.
151 """
153 @abstractmethod
154 def __call__(self, data: KeyedData, **kwargs) -> Vector:
155 raise NotImplementedError("This is not implemented on the base class")
158class ScalarAction(AnalysisAction):
159 """A `ScalarAction` is an `AnalysisAction` that returns a `Scalar` when
160 called.
161 """
163 @abstractmethod
164 def __call__(self, data: KeyedData, **kwargs) -> Scalar:
165 raise NotImplementedError("This is not implemented on the base class")
167 def getMask(self, **kwargs) -> Vector | slice:
168 """Extract a mask if one is passed as key word args, otherwise return
169 an empty slice object that can still be used in a getitem call.
171 Returns
172 -------
173 result : `Vector` or `slice`
174 The mask passed as a keyword, or a slice object that will return
175 a complete Vector when used in getitem.
176 """
177 if (mask := kwargs.get("mask")) is None:
178 mask = slice(None)
179 return mask
182class MetricAction(AnalysisAction):
183 """A `MetricAction` is an `AnalysisAction` that returns a `Measurement` or
184 a `Mapping` of `str` to `Measurement` when called.
185 """
187 @abstractmethod
188 def __call__(self, data: KeyedData, **kwargs) -> MetricResultType:
189 raise NotImplementedError("This is not implemented on the base class")
192class PlotAction(AnalysisAction):
193 """A `PlotAction` is an `AnalysisAction` that returns a `PlotType` or
194 a `Mapping` of `str` to `PlotType` when called.
195 """
197 def getOutputNames(self, config: pexConfig.Config | None = None) -> Iterable[str]:
198 """Returns a list of names that will be used as keys if this action's
199 call method returns a mapping. Otherwise return an empty Iterable.
201 Parameters
202 ----------
203 config : `lsst.pex.config.Config`, optional
204 Configuration of the task. This is only used if the output naming
205 needs to be config-aware.
207 Returns
208 -------
209 result : `Iterable` of `str`
210 If a `PlotAction` produces more than one plot, this should be the
211 keys the action will use in the returned `Mapping`.
212 """
213 return tuple()
215 @abstractmethod
216 def __call__(self, data: KeyedData, **kwargs) -> PlotResultType:
217 raise NotImplementedError("This is not implemented on the base class")
220class NoPlot(PlotAction):
221 """This is a sentinel class to indicate that there is no plotting action"""
224class NoMetric(MetricAction):
225 """This is a sentinel class to indicate that there is no Metric action"""
228@dataclass
229class JointResults:
230 """The `JointResults` dataclass is a container for the results of a
231 `JointAction`.
232 """
234 plot: PlotResultType | None
235 metric: MetricResultType | None
238class JointAction(AnalysisAction):
239 """A `JointAction` is an `AnalysisAction` that is a composite of a
240 `PlotAction` and a `MetricAction`.
241 """
243 metric = ConfigurableActionField[MetricAction](doc="Action to run that will produce one or more metrics")
244 plot = ConfigurableActionField[PlotAction](doc="Action to run that will produce one or more plots")
246 def __call__(self, data: KeyedData, **kwargs) -> JointResults:
247 if isinstance(self.plot, NoPlot):
248 plots = None
249 else:
250 plots = self.plot(data, **kwargs)
251 if isinstance(self.metric, NoMetric):
252 metrics = None
253 else:
254 metrics = self.metric(data, **kwargs)
255 return JointResults(plots, metrics)
257 def getInputSchema(self) -> KeyedDataSchema:
258 yield from self.metric.getInputSchema()
259 yield from self.plot.getInputSchema()
261 def getOutputNames(self, config: pexConfig.Config | None = None) -> Iterable[str]:
262 """Returns a list of names that will be used as keys if this action's
263 call method returns a mapping. Otherwise return an empty Iterable.
265 Parameters
266 ----------
267 config : `lsst.pex.config.Config`, optional
268 Configuration of the task. This is only used if the output naming
269 needs to be config-aware.
271 Returns
272 -------
273 outNames : `Iterable` of `str`
274 If a `PlotAction` produces more than one plot, this should be the
275 keys the action will use in the returned `Mapping`.
276 """
277 if config is None:
278 # `dynamicOutputNames` is set to False.
279 outNames = self.plot.getOutputNames()
280 else:
281 # `dynamicOutputNames` is set to True.
282 outNames = self.plot.getOutputNames(config=config)
283 return outNames