Coverage for python/lsst/pipe/base/testUtils.py: 12%
138 statements
« prev ^ index » next coverage.py v6.5.0, created at 2023-06-06 10:05 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2023-06-06 10:05 +0000
1# This file is part of pipe_base.
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 "assertValidInitOutput",
26 "assertValidOutput",
27 "getInitInputs",
28 "lintConnections",
29 "makeQuantum",
30 "runTestQuantum",
31]
34import collections.abc
35import itertools
36import unittest.mock
37from collections import defaultdict
38from typing import TYPE_CHECKING, AbstractSet, Any, Dict, Mapping, Optional, Sequence, Set, Union
40from lsst.daf.butler import (
41 Butler,
42 DataCoordinate,
43 DataId,
44 DatasetRef,
45 DatasetType,
46 Dimension,
47 DimensionUniverse,
48 Quantum,
49 SkyPixDimension,
50 StorageClassFactory,
51)
52from lsst.pipe.base.connectionTypes import BaseConnection, DimensionedConnection
54from .butlerQuantumContext import ButlerQuantumContext
56if TYPE_CHECKING: 56 ↛ 57line 56 didn't jump to line 57, because the condition on line 56 was never true
57 from .config import PipelineTaskConfig
58 from .connections import PipelineTaskConnections
59 from .pipelineTask import PipelineTask
60 from .struct import Struct
63def makeQuantum(
64 task: PipelineTask,
65 butler: Butler,
66 dataId: DataId,
67 ioDataIds: Mapping[str, Union[DataId, Sequence[DataId]]],
68) -> Quantum:
69 """Create a Quantum for a particular data ID(s).
71 Parameters
72 ----------
73 task : `lsst.pipe.base.PipelineTask`
74 The task whose processing the quantum represents.
75 butler : `lsst.daf.butler.Butler`
76 The collection the quantum refers to.
77 dataId: any data ID type
78 The data ID of the quantum. Must have the same dimensions as
79 ``task``'s connections class.
80 ioDataIds : `collections.abc.Mapping` [`str`]
81 A mapping keyed by input/output names. Values must be data IDs for
82 single connections and sequences of data IDs for multiple connections.
84 Returns
85 -------
86 quantum : `lsst.daf.butler.Quantum`
87 A quantum for ``task``, when called with ``dataIds``.
88 """
89 # This is a type ignore, because `connections` is a dynamic class, but
90 # it for sure will have this property
91 connections = task.config.ConnectionsClass(config=task.config) # type: ignore
93 try:
94 _checkDimensionsMatch(butler.registry.dimensions, connections.dimensions, dataId.keys())
95 except ValueError as e:
96 raise ValueError("Error in quantum dimensions.") from e
98 inputs = defaultdict(list)
99 outputs = defaultdict(list)
100 for name in itertools.chain(connections.inputs, connections.prerequisiteInputs):
101 try:
102 connection = connections.__getattribute__(name)
103 _checkDataIdMultiplicity(name, ioDataIds[name], connection.multiple)
104 ids = _normalizeDataIds(ioDataIds[name])
105 for id in ids:
106 ref = _refFromConnection(butler, connection, id)
107 inputs[ref.datasetType].append(ref)
108 except (ValueError, KeyError) as e:
109 raise ValueError(f"Error in connection {name}.") from e
110 for name in connections.outputs:
111 try:
112 connection = connections.__getattribute__(name)
113 _checkDataIdMultiplicity(name, ioDataIds[name], connection.multiple)
114 ids = _normalizeDataIds(ioDataIds[name])
115 for id in ids:
116 ref = _refFromConnection(butler, connection, id)
117 outputs[ref.datasetType].append(ref)
118 except (ValueError, KeyError) as e:
119 raise ValueError(f"Error in connection {name}.") from e
120 quantum = Quantum(
121 taskClass=type(task),
122 dataId=DataCoordinate.standardize(dataId, universe=butler.registry.dimensions),
123 inputs=inputs,
124 outputs=outputs,
125 )
126 return quantum
129def _checkDimensionsMatch(
130 universe: DimensionUniverse,
131 expected: Union[AbstractSet[str], AbstractSet[Dimension]],
132 actual: Union[AbstractSet[str], AbstractSet[Dimension]],
133) -> None:
134 """Test whether two sets of dimensions agree after conversions.
136 Parameters
137 ----------
138 universe : `lsst.daf.butler.DimensionUniverse`
139 The set of all known dimensions.
140 expected : `Set` [`str`] or `Set` [`~lsst.daf.butler.Dimension`]
141 The dimensions expected from a task specification.
142 actual : `Set` [`str`] or `Set` [`~lsst.daf.butler.Dimension`]
143 The dimensions provided by input.
145 Raises
146 ------
147 ValueError
148 Raised if ``expected`` and ``actual`` cannot be reconciled.
149 """
150 if _simplify(universe, expected) != _simplify(universe, actual):
151 raise ValueError(f"Mismatch in dimensions; expected {expected} but got {actual}.")
154def _simplify(
155 universe: DimensionUniverse, dimensions: Union[AbstractSet[str], AbstractSet[Dimension]]
156) -> Set[str]:
157 """Reduce a set of dimensions to a string-only form.
159 Parameters
160 ----------
161 universe : `lsst.daf.butler.DimensionUniverse`
162 The set of all known dimensions.
163 dimensions : `Set` [`str`] or `Set` [`~lsst.daf.butler.Dimension`]
164 A set of dimensions to simplify.
166 Returns
167 -------
168 dimensions : `Set` [`str`]
169 A copy of ``dimensions`` reduced to string form, with all spatial
170 dimensions simplified to ``skypix``.
171 """
172 simplified: Set[str] = set()
173 for dimension in dimensions:
174 # skypix not a real Dimension, handle it first
175 if dimension == "skypix":
176 simplified.add(dimension) # type: ignore
177 else:
178 # Need a Dimension to test spatialness
179 fullDimension = universe[dimension] if isinstance(dimension, str) else dimension
180 if isinstance(fullDimension, SkyPixDimension):
181 simplified.add("skypix")
182 else:
183 simplified.add(fullDimension.name)
184 return simplified
187def _checkDataIdMultiplicity(name: str, dataIds: Union[DataId, Sequence[DataId]], multiple: bool) -> None:
188 """Test whether data IDs are scalars for scalar connections and sequences
189 for multiple connections.
191 Parameters
192 ----------
193 name : `str`
194 The name of the connection being tested.
195 dataIds : any data ID type or `~collections.abc.Sequence` [data ID]
196 The data ID(s) provided for the connection.
197 multiple : `bool`
198 The ``multiple`` field of the connection.
200 Raises
201 ------
202 ValueError
203 Raised if ``dataIds`` and ``multiple`` do not match.
204 """
205 if multiple:
206 if not isinstance(dataIds, collections.abc.Sequence):
207 raise ValueError(f"Expected multiple data IDs for {name}, got {dataIds}.")
208 else:
209 # DataCoordinate is a Mapping
210 if not isinstance(dataIds, collections.abc.Mapping):
211 raise ValueError(f"Expected single data ID for {name}, got {dataIds}.")
214def _normalizeDataIds(dataIds: Union[DataId, Sequence[DataId]]) -> Sequence[DataId]:
215 """Represent both single and multiple data IDs as a list.
217 Parameters
218 ----------
219 dataIds : any data ID type or `~collections.abc.Sequence` thereof
220 The data ID(s) provided for a particular input or output connection.
222 Returns
223 -------
224 normalizedIds : `~collections.abc.Sequence` [data ID]
225 A sequence equal to ``dataIds`` if it was already a sequence, or
226 ``[dataIds]`` if it was a single ID.
227 """
228 if isinstance(dataIds, collections.abc.Sequence):
229 return dataIds
230 else:
231 return [dataIds]
234def _refFromConnection(
235 butler: Butler, connection: DimensionedConnection, dataId: DataId, **kwargs: Any
236) -> DatasetRef:
237 """Create a DatasetRef for a connection in a collection.
239 Parameters
240 ----------
241 butler : `lsst.daf.butler.Butler`
242 The collection to point to.
243 connection : `lsst.pipe.base.connectionTypes.DimensionedConnection`
244 The connection defining the dataset type to point to.
245 dataId
246 The data ID for the dataset to point to.
247 **kwargs
248 Additional keyword arguments used to augment or construct
249 a `~lsst.daf.butler.DataCoordinate`.
251 Returns
252 -------
253 ref : `lsst.daf.butler.DatasetRef`
254 A reference to a dataset compatible with ``connection``, with ID
255 ``dataId``, in the collection pointed to by ``butler``.
256 """
257 universe = butler.registry.dimensions
258 # DatasetRef only tests if required dimension is missing, but not extras
259 _checkDimensionsMatch(universe, set(connection.dimensions), dataId.keys())
260 dataId = DataCoordinate.standardize(dataId, **kwargs, universe=universe)
262 datasetType = butler.registry.getDatasetType(connection.name)
264 try:
265 butler.registry.getDatasetType(datasetType.name)
266 except KeyError:
267 raise ValueError(f"Invalid dataset type {connection.name}.")
268 if not butler.run:
269 raise ValueError("Can not create a resolved DatasetRef since the butler has no default run defined.")
270 try:
271 registry_ref = butler.registry.findDataset(datasetType, dataId, collections=[butler.run])
272 if registry_ref:
273 ref = registry_ref
274 else:
275 ref = DatasetRef(datasetType=datasetType, dataId=dataId, run=butler.run)
276 butler.registry._importDatasets([ref])
277 return ref
278 except KeyError as e:
279 raise ValueError(f"Dataset type ({connection.name}) and ID {dataId.byName()} not compatible.") from e
282def runTestQuantum(
283 task: PipelineTask, butler: Butler, quantum: Quantum, mockRun: bool = True
284) -> Optional[unittest.mock.Mock]:
285 """Run a PipelineTask on a Quantum.
287 Parameters
288 ----------
289 task : `lsst.pipe.base.PipelineTask`
290 The task to run on the quantum.
291 butler : `lsst.daf.butler.Butler`
292 The collection to run on.
293 quantum : `lsst.daf.butler.Quantum`
294 The quantum to run.
295 mockRun : `bool`
296 Whether or not to replace ``task``'s ``run`` method. The default of
297 `True` is recommended unless ``run`` needs to do real work (e.g.,
298 because the test needs real output datasets).
300 Returns
301 -------
302 run : `unittest.mock.Mock` or `None`
303 If ``mockRun`` is set, the mock that replaced ``run``. This object can
304 be queried for the arguments ``runQuantum`` passed to ``run``.
305 """
306 butlerQc = ButlerQuantumContext(butler, quantum)
307 # This is a type ignore, because `connections` is a dynamic class, but
308 # it for sure will have this property
309 connections = task.config.ConnectionsClass(config=task.config) # type: ignore
310 inputRefs, outputRefs = connections.buildDatasetRefs(quantum)
311 if mockRun:
312 with unittest.mock.patch.object(task, "run") as mock, unittest.mock.patch(
313 "lsst.pipe.base.ButlerQuantumContext.put"
314 ):
315 task.runQuantum(butlerQc, inputRefs, outputRefs)
316 return mock
317 else:
318 task.runQuantum(butlerQc, inputRefs, outputRefs)
319 return None
322def _assertAttributeMatchesConnection(obj: Any, attrName: str, connection: BaseConnection) -> None:
323 """Test that an attribute on an object matches the specification given in
324 a connection.
326 Parameters
327 ----------
328 obj
329 An object expected to contain the attribute ``attrName``.
330 attrName : `str`
331 The name of the attribute to be tested.
332 connection : `lsst.pipe.base.connectionTypes.BaseConnection`
333 The connection, usually some type of output, specifying ``attrName``.
335 Raises
336 ------
337 AssertionError:
338 Raised if ``obj.attrName`` does not match what's expected
339 from ``connection``.
340 """
341 # name
342 try:
343 attrValue = obj.__getattribute__(attrName)
344 except AttributeError:
345 raise AssertionError(f"No such attribute on {obj!r}: {attrName}")
346 # multiple
347 if connection.multiple:
348 if not isinstance(attrValue, collections.abc.Sequence):
349 raise AssertionError(f"Expected {attrName} to be a sequence, got {attrValue!r} instead.")
350 else:
351 # use lazy evaluation to not use StorageClassFactory unless
352 # necessary
353 if isinstance(attrValue, collections.abc.Sequence) and not issubclass(
354 StorageClassFactory().getStorageClass(connection.storageClass).pytype, collections.abc.Sequence
355 ):
356 raise AssertionError(f"Expected {attrName} to be a single value, got {attrValue!r} instead.")
357 # no test for storageClass, as I'm not sure how much persistence
358 # depends on duck-typing
361def assertValidOutput(task: PipelineTask, result: Struct) -> None:
362 """Test that the output of a call to ``run`` conforms to its own
363 connections.
365 Parameters
366 ----------
367 task : `lsst.pipe.base.PipelineTask`
368 The task whose connections need validation. This is a fully-configured
369 task object to support features such as optional outputs.
370 result : `lsst.pipe.base.Struct`
371 A result object produced by calling ``task.run``.
373 Raises
374 ------
375 AssertionError:
376 Raised if ``result`` does not match what's expected from ``task's``
377 connections.
378 """
379 # This is a type ignore, because `connections` is a dynamic class, but
380 # it for sure will have this property
381 connections = task.config.ConnectionsClass(config=task.config) # type: ignore
383 for name in connections.outputs:
384 connection = connections.__getattribute__(name)
385 _assertAttributeMatchesConnection(result, name, connection)
388def assertValidInitOutput(task: PipelineTask) -> None:
389 """Test that a constructed task conforms to its own init-connections.
391 Parameters
392 ----------
393 task : `lsst.pipe.base.PipelineTask`
394 The task whose connections need validation.
396 Raises
397 ------
398 AssertionError:
399 Raised if ``task`` does not have the state expected from ``task's``
400 connections.
401 """
402 # This is a type ignore, because `connections` is a dynamic class, but
403 # it for sure will have this property
404 connections = task.config.ConnectionsClass(config=task.config) # type: ignore
406 for name in connections.initOutputs:
407 connection = connections.__getattribute__(name)
408 _assertAttributeMatchesConnection(task, name, connection)
411def getInitInputs(butler: Butler, config: PipelineTaskConfig) -> Dict[str, Any]:
412 """Return the initInputs object that would have been passed to a
413 `~lsst.pipe.base.PipelineTask` constructor.
415 Parameters
416 ----------
417 butler : `lsst.daf.butler.Butler`
418 The repository to search for input datasets. Must have
419 pre-configured collections.
420 config : `lsst.pipe.base.PipelineTaskConfig`
421 The config for the task to be constructed.
423 Returns
424 -------
425 initInputs : `dict` [`str`]
426 A dictionary of objects in the format of the ``initInputs`` parameter
427 to `lsst.pipe.base.PipelineTask`.
428 """
429 connections = config.connections.ConnectionsClass(config=config)
430 initInputs = {}
431 for name in connections.initInputs:
432 attribute = getattr(connections, name)
433 # Get full dataset type to check for consistency problems
434 dsType = DatasetType(
435 attribute.name, butler.registry.dimensions.extract(set()), attribute.storageClass
436 )
437 # All initInputs have empty data IDs
438 initInputs[name] = butler.get(dsType)
440 return initInputs
443def lintConnections(
444 connections: PipelineTaskConnections,
445 *,
446 checkMissingMultiple: bool = True,
447 checkUnnecessaryMultiple: bool = True,
448) -> None:
449 """Inspect a connections class for common errors.
451 These tests are designed to detect misuse of connections features in
452 standard designs. An unusually designed connections class may trigger
453 alerts despite being correctly written; specific checks can be turned off
454 using keywords.
456 Parameters
457 ----------
458 connections : `lsst.pipe.base.PipelineTaskConnections`-type
459 The connections class to test.
460 checkMissingMultiple : `bool`
461 Whether to test for single connections that would match multiple
462 datasets at run time.
463 checkUnnecessaryMultiple : `bool`
464 Whether to test for multiple connections that would only match
465 one dataset.
467 Raises
468 ------
469 AssertionError
470 Raised if any of the selected checks fail for any connection.
471 """
472 # Since all comparisons are inside the class, don't bother
473 # normalizing skypix.
474 quantumDimensions = connections.dimensions
476 errors = ""
477 # connectionTypes.DimensionedConnection is implementation detail,
478 # don't use it.
479 for name in itertools.chain(connections.inputs, connections.prerequisiteInputs, connections.outputs):
480 connection: DimensionedConnection = connections.allConnections[name] # type: ignore
481 connDimensions = set(connection.dimensions)
482 if checkMissingMultiple and not connection.multiple and connDimensions > quantumDimensions:
483 errors += (
484 f"Connection {name} may be called with multiple values of "
485 f"{connDimensions - quantumDimensions} but has multiple=False.\n"
486 )
487 if checkUnnecessaryMultiple and connection.multiple and connDimensions <= quantumDimensions:
488 errors += (
489 f"Connection {name} has multiple=True but can only be called with one "
490 f"value of {connDimensions} for each {quantumDimensions}.\n"
491 )
492 if errors:
493 raise AssertionError(errors)