Coverage for python/lsst/verify/tasks/apdbMetricTask.py: 31%
87 statements
« prev ^ index » next coverage.py v6.4.4, created at 2022-08-20 01:56 -0700
« prev ^ index » next coverage.py v6.4.4, created at 2022-08-20 01:56 -0700
1# This file is part of verify.
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/>.
22__all__ = ["ApdbMetricTask", "ApdbMetricConfig", "ConfigApdbLoader",
23 "DirectApdbLoader", "ApdbMetricConnections"]
25import abc
27from lsst.pex.config import Config, ConfigurableField, ConfigurableInstance, \
28 ConfigDictField, ConfigChoiceField, FieldValidationError
29from lsst.pipe.base import Task, Struct, connectionTypes
30from lsst.dax.apdb import make_apdb, ApdbConfig
32from lsst.verify.tasks import MetricTask, MetricConfig, MetricConnections, \
33 MetricComputationError
36class ConfigApdbLoader(Task):
37 """A Task that takes a science task config and returns the corresponding
38 Apdb object.
40 Parameters
41 ----------
42 *args
43 **kwargs
44 Constructor parameters are the same as for `lsst.pipe.base.Task`.
45 """
46 _DefaultName = "configApdb"
47 ConfigClass = Config
49 def __init__(self, **kwargs):
50 super().__init__(**kwargs)
52 def _getApdb(self, config):
53 """Extract an Apdb object from an arbitrary task config.
55 Parameters
56 ----------
57 config : `lsst.pex.config.Config` or `None`
58 A config that may contain a `lsst.dax.apdb.ApdbConfig`.
59 Behavior is undefined if there is more than one such member.
61 Returns
62 -------
63 apdb : `lsst.dax.apdb.Apdb`-like or `None`
64 A `lsst.dax.apdb.Apdb` object or a drop-in replacement, or `None`
65 if no `lsst.dax.apdb.ApdbConfig` is present in ``config``.
66 """
67 if config is None:
68 return None
69 if isinstance(config, ApdbConfig):
70 return make_apdb(config)
72 for field in config.values():
73 if isinstance(field, ConfigurableInstance):
74 result = self._getApdbFromConfigurableField(field)
75 if result:
76 return result
77 elif isinstance(field, ConfigChoiceField.instanceDictClass):
78 try:
79 # can't test with hasattr because of non-standard getattr
80 field.names
81 except FieldValidationError:
82 result = self._getApdb(field.active)
83 else:
84 result = self._getApdbFromConfigIterable(field.active)
85 if result:
86 return result
87 elif isinstance(field, ConfigDictField.DictClass):
88 result = self._getApdbFromConfigIterable(field.values())
89 if result:
90 return result
91 elif isinstance(field, Config):
92 # Can't test for `ConfigField` more directly than this
93 result = self._getApdb(field)
94 if result:
95 return result
96 return None
98 def _getApdbFromConfigurableField(self, configurable):
99 """Extract an Apdb object from a ConfigurableField.
101 Parameters
102 ----------
103 configurable : `lsst.pex.config.ConfigurableInstance` or `None`
104 A configurable that may contain a `lsst.dax.apdb.ApdbConfig`.
106 Returns
107 -------
108 apdb : `lsst.dax.apdb.Apdb`-like or `None`
109 A `lsst.dax.apdb.Apdb` object or a drop-in replacement, if a
110 suitable config exists.
111 """
112 if configurable is None:
113 return None
115 if issubclass(configurable.ConfigClass, ApdbConfig):
116 return configurable.apply()
117 else:
118 return self._getApdb(configurable.value)
120 def _getApdbFromConfigIterable(self, configDict):
121 """Extract an Apdb object from an iterable of configs.
123 Parameters
124 ----------
125 configDict: iterable of `lsst.pex.config.Config` or `None`
126 A config iterable that may contain a `lsst.dax.apdb.ApdbConfig`.
128 Returns
129 -------
130 apdb : `lsst.dax.apdb.Apdb`-like or `None`
131 A `lsst.dax.apdb.Apdb` object or a drop-in replacement, if a
132 suitable config exists.
133 """
134 if configDict:
135 for config in configDict:
136 result = self._getApdb(config)
137 if result:
138 return result
139 return None
141 def run(self, config):
142 """Create a database consistent with a science task config.
144 Parameters
145 ----------
146 config : `lsst.pex.config.Config` or `None`
147 A config that should contain a `lsst.dax.apdb.ApdbConfig`.
148 Behavior is undefined if there is more than one such member.
150 Returns
151 -------
152 result : `lsst.pipe.base.Struct`
153 Result struct with components:
155 ``apdb``
156 A database configured the same way as in ``config``, if one
157 exists (`lsst.dax.apdb.Apdb` or `None`).
158 """
159 return Struct(apdb=self._getApdb(config))
162class DirectApdbLoader(Task):
163 """A Task that takes a Apdb config and returns the corresponding
164 Apdb object.
166 Parameters
167 ----------
168 *args
169 **kwargs
170 Constructor parameters are the same as for `lsst.pipe.base.Task`.
171 """
173 _DefaultName = "directApdb"
174 ConfigClass = Config
176 def __init__(self, **kwargs):
177 super().__init__(**kwargs)
179 def run(self, config):
180 """Create a database from a config.
182 Parameters
183 ----------
184 config : `lsst.dax.apdb.ApdbConfig` or `None`
185 A config for the database connection.
187 Returns
188 -------
189 result : `lsst.pipe.base.Struct`
190 Result struct with components:
192 ``apdb``
193 A database configured the same way as in ``config``.
194 """
195 return Struct(apdb=(make_apdb(config) if config else None))
198class ApdbMetricConnections(
199 MetricConnections,
200 dimensions={"instrument"},
201):
202 """An abstract connections class defining a database input.
204 Notes
205 -----
206 ``ApdbMetricConnections`` defines the following dataset templates:
207 ``package``
208 Name of the metric's namespace. By
209 :ref:`verify_metrics <verify-metrics-package>` convention, this is
210 the name of the package the metric is most closely
211 associated with.
212 ``metric``
213 Name of the metric, excluding any namespace.
214 """
215 dbInfo = connectionTypes.Input(
216 name="apdb_marker",
217 doc="The dataset from which an APDB instance can be constructed by "
218 "`dbLoader`. By default this is assumed to be a marker produced "
219 "by AP processing.",
220 storageClass="Config",
221 multiple=True,
222 dimensions={"instrument", "visit", "detector"},
223 )
224 # Replaces MetricConnections.measurement, which is detector-level
225 measurement = connectionTypes.Output(
226 name="metricvalue_{package}_{metric}",
227 doc="The metric value computed by this task.",
228 storageClass="MetricValue",
229 dimensions={"instrument"},
230 )
233class ApdbMetricConfig(MetricConfig,
234 pipelineConnections=ApdbMetricConnections):
235 """A base class for APDB metric task configs.
236 """
237 dbLoader = ConfigurableField(
238 target=DirectApdbLoader,
239 doc="Task for loading a database from `dbInfo`. Its run method must "
240 "take one object of the dataset type indicated by `dbInfo` and return "
241 "a Struct with an 'apdb' member."
242 )
245class ApdbMetricTask(MetricTask):
246 """A base class for tasks that compute metrics from an alert production
247 database.
249 Parameters
250 ----------
251 **kwargs
252 Constructor parameters are the same as for
253 `lsst.pipe.base.PipelineTask`.
255 Notes
256 -----
257 This class should be customized by overriding `makeMeasurement`. You
258 should not need to override `run`.
259 """
260 # Design note: makeMeasurement is an overrideable method rather than a
261 # subtask to keep the configs for `MetricsControllerTask` as simple as
262 # possible. This was judged more important than ensuring that no
263 # implementation details of MetricTask can leak into
264 # application-specific code.
266 ConfigClass = ApdbMetricConfig
268 def __init__(self, **kwargs):
269 super().__init__(**kwargs)
271 self.makeSubtask("dbLoader")
273 @abc.abstractmethod
274 def makeMeasurement(self, dbHandle, outputDataId):
275 """Compute the metric from database data.
277 Parameters
278 ----------
279 dbHandle : `lsst.dax.apdb.Apdb`
280 A database instance.
281 outputDataId : any data ID type
282 The subset of the database to which this measurement applies.
283 May be empty to represent the entire dataset.
285 Returns
286 -------
287 measurement : `lsst.verify.Measurement` or `None`
288 The measurement corresponding to the input data.
290 Raises
291 ------
292 MetricComputationError
293 Raised if an algorithmic or system error prevents calculation of
294 the metric. See `run` for expected behavior.
295 """
297 def run(self, dbInfo, outputDataId={}):
298 """Compute a measurement from a database.
300 Parameters
301 ----------
302 dbInfo : `list`
303 The datasets (of the type indicated by the config) from
304 which to load the database. If more than one dataset is provided
305 (as may be the case if DB writes are fine-grained), all are
306 assumed identical.
307 outputDataId: any data ID type, optional
308 The output data ID for the metric value. Defaults to the empty ID,
309 representing a value that covers the entire dataset.
311 Returns
312 -------
313 result : `lsst.pipe.base.Struct`
314 Result struct with component:
316 ``measurement``
317 the value of the metric (`lsst.verify.Measurement` or `None`)
319 Raises
320 ------
321 MetricComputationError
322 Raised if an algorithmic or system error prevents calculation of
323 the metric.
325 Notes
326 -----
327 This implementation calls
328 `~lsst.verify.tasks.ApdbMetricConfig.dbLoader` to acquire a database
329 handle (taking `None` if no input), then passes it and the value of
330 ``outputDataId`` to `makeMeasurement`. The result of `makeMeasurement`
331 is returned to the caller.
332 """
333 db = self.dbLoader.run(dbInfo[0] if dbInfo else None).apdb
335 if db is not None:
336 measurement = self.makeMeasurement(db, outputDataId)
337 else:
338 measurement = None
340 return Struct(measurement=measurement)
342 def runQuantum(self, butlerQC, inputRefs, outputRefs):
343 """Do Butler I/O to provide in-memory objects for run.
345 This specialization of runQuantum passes the output data ID to `run`.
346 """
347 try:
348 inputs = butlerQC.get(inputRefs)
349 outputs = self.run(**inputs,
350 outputDataId=outputRefs.measurement.dataId)
351 if outputs.measurement is not None:
352 butlerQC.put(outputs, outputRefs)
353 else:
354 self.log.debug("Skipping measurement of %r on %s "
355 "as not applicable.", self, inputRefs)
356 except MetricComputationError:
357 self.log.error(
358 "Measurement of %r failed on %s->%s",
359 self, inputRefs, outputRefs, exc_info=True)