23 __all__ = [
"assertValidInitOutput",
31 from collections
import defaultdict
36 from lsst.daf.butler
import DataCoordinate, DatasetRef, DatasetType, Quantum, StorageClassFactory
41 """Create a Quantum for a particular data ID(s).
45 task : `lsst.pipe.base.PipelineTask`
46 The task whose processing the quantum represents.
47 butler : `lsst.daf.butler.Butler`
48 The collection the quantum refers to.
49 dataId: any data ID type
50 The data ID of the quantum. Must have the same dimensions as
51 ``task``'s connections class.
52 ioDataIds : `collections.abc.Mapping` [`str`]
53 A mapping keyed by input/output names. Values must be data IDs for
54 single connections and sequences of data IDs for multiple connections.
58 quantum : `lsst.daf.butler.Quantum`
59 A quantum for ``task``, when called with ``dataIds``.
61 connections = task.config.ConnectionsClass(config=task.config)
64 inputs = defaultdict(list)
65 outputs = defaultdict(list)
66 for name
in itertools.chain(connections.inputs, connections.prerequisiteInputs):
67 connection = connections.__getattribute__(name)
68 _checkDataIdMultiplicity(name, ioDataIds[name], connection.multiple)
69 ids = _normalizeDataIds(ioDataIds[name])
71 ref = _refFromConnection(butler, connection, id)
72 inputs[ref.datasetType].
append(ref)
73 for name
in connections.outputs:
74 connection = connections.__getattribute__(name)
75 _checkDataIdMultiplicity(name, ioDataIds[name], connection.multiple)
76 ids = _normalizeDataIds(ioDataIds[name])
78 ref = _refFromConnection(butler, connection, id)
79 outputs[ref.datasetType].
append(ref)
80 quantum = Quantum(taskClass=
type(task),
86 raise ValueError(
"Mismatch in input data.")
from e
89 def _checkDataIdMultiplicity(name, dataIds, multiple):
90 """Test whether data IDs are scalars for scalar connections and sequences
91 for multiple connections.
96 The name of the connection being tested.
97 dataIds : any data ID type or `~collections.abc.Sequence` [data ID]
98 The data ID(s) provided for the connection.
100 The ``multiple`` field of the connection.
105 Raised if ``dataIds`` and ``multiple`` do not match.
108 if not isinstance(dataIds, collections.abc.Sequence):
109 raise ValueError(f
"Expected multiple data IDs for {name}, got {dataIds}.")
112 if not isinstance(dataIds, collections.abc.Mapping):
113 raise ValueError(f
"Expected single data ID for {name}, got {dataIds}.")
116 def _normalizeDataIds(dataIds):
117 """Represent both single and multiple data IDs as a list.
121 dataIds : any data ID type or `~collections.abc.Sequence` thereof
122 The data ID(s) provided for a particular input or output connection.
126 normalizedIds : `~collections.abc.Sequence` [data ID]
127 A sequence equal to ``dataIds`` if it was already a sequence, or
128 ``[dataIds]`` if it was a single ID.
130 if isinstance(dataIds, collections.abc.Sequence):
136 def _refFromConnection(butler, connection, dataId, **kwargs):
137 """Create a DatasetRef for a connection in a collection.
141 butler : `lsst.daf.butler.Butler`
142 The collection to point to.
143 connection : `lsst.pipe.base.connectionTypes.DimensionedConnection`
144 The connection defining the dataset type to point to.
146 The data ID for the dataset to point to.
148 Additional keyword arguments used to augment or construct
149 a `~lsst.daf.butler.DataCoordinate`.
153 ref : `lsst.daf.butler.DatasetRef`
154 A reference to a dataset compatible with ``connection``, with ID
155 ``dataId``, in the collection pointed to by ``butler``.
157 universe = butler.registry.dimensions
158 dataId = DataCoordinate.standardize(dataId, **kwargs, universe=universe)
162 if "skypix" in connection.dimensions:
163 datasetType = butler.registry.getDatasetType(connection.name)
165 datasetType = connection.makeDatasetType(universe)
168 butler.registry.getDatasetType(datasetType.name)
170 raise ValueError(f
"Invalid dataset type {connection.name}.")
172 ref = DatasetRef(datasetType=datasetType, dataId=dataId)
174 except KeyError
as e:
175 raise ValueError(f
"Dataset type ({connection.name}) and ID {dataId.byName()} not compatible.") \
179 def _resolveTestQuantumInputs(butler, quantum):
180 """Look up all input datasets a test quantum in the `Registry` to resolve
181 all `DatasetRef` objects (i.e. ensure they have not-`None` ``id`` and
186 quantum : `~lsst.daf.butler.Quantum`
187 Single Quantum instance.
188 butler : `~lsst.daf.butler.Butler`
197 for refsForDatasetType
in quantum.inputs.values():
198 newRefsForDatasetType = []
199 for ref
in refsForDatasetType:
201 resolvedRef = butler.registry.findDataset(ref.datasetType, ref.dataId,
202 collections=butler.collections)
203 if resolvedRef
is None:
205 f
"Cannot find {ref.datasetType.name} with id {ref.dataId} "
206 f
"in collections {butler.collections}."
208 newRefsForDatasetType.append(resolvedRef)
210 newRefsForDatasetType.append(ref)
211 refsForDatasetType[:] = newRefsForDatasetType
215 """Run a PipelineTask on a Quantum.
219 task : `lsst.pipe.base.PipelineTask`
220 The task to run on the quantum.
221 butler : `lsst.daf.butler.Butler`
222 The collection to run on.
223 quantum : `lsst.daf.butler.Quantum`
226 Whether or not to replace ``task``'s ``run`` method. The default of
227 `True` is recommended unless ``run`` needs to do real work (e.g.,
228 because the test needs real output datasets).
232 run : `unittest.mock.Mock` or `None`
233 If ``mockRun`` is set, the mock that replaced ``run``. This object can
234 be queried for the arguments ``runQuantum`` passed to ``run``.
236 _resolveTestQuantumInputs(butler, quantum)
238 connections = task.config.ConnectionsClass(config=task.config)
239 inputRefs, outputRefs = connections.buildDatasetRefs(quantum)
241 with unittest.mock.patch.object(task,
"run")
as mock, \
242 unittest.mock.patch(
"lsst.pipe.base.ButlerQuantumContext.put"):
243 task.runQuantum(butlerQc, inputRefs, outputRefs)
246 task.runQuantum(butlerQc, inputRefs, outputRefs)
250 def _assertAttributeMatchesConnection(obj, attrName, connection):
251 """Test that an attribute on an object matches the specification given in
257 An object expected to contain the attribute ``attrName``.
259 The name of the attribute to be tested.
260 connection : `lsst.pipe.base.connectionTypes.BaseConnection`
261 The connection, usually some type of output, specifying ``attrName``.
266 Raised if ``obj.attrName`` does not match what's expected
271 attrValue = obj.__getattribute__(attrName)
272 except AttributeError:
273 raise AssertionError(f
"No such attribute on {obj!r}: {attrName}")
275 if connection.multiple:
276 if not isinstance(attrValue, collections.abc.Sequence):
277 raise AssertionError(f
"Expected {attrName} to be a sequence, got {attrValue!r} instead.")
281 if isinstance(attrValue, collections.abc.Sequence) \
283 StorageClassFactory().getStorageClass(connection.storageClass).pytype,
284 collections.abc.Sequence):
285 raise AssertionError(f
"Expected {attrName} to be a single value, got {attrValue!r} instead.")
291 """Test that the output of a call to ``run`` conforms to its own
296 task : `lsst.pipe.base.PipelineTask`
297 The task whose connections need validation. This is a fully-configured
298 task object to support features such as optional outputs.
299 result : `lsst.pipe.base.Struct`
300 A result object produced by calling ``task.run``.
305 Raised if ``result`` does not match what's expected from ``task's``
308 connections = task.config.ConnectionsClass(config=task.config)
310 for name
in connections.outputs:
311 connection = connections.__getattribute__(name)
312 _assertAttributeMatchesConnection(result, name, connection)
316 """Test that a constructed task conforms to its own init-connections.
320 task : `lsst.pipe.base.PipelineTask`
321 The task whose connections need validation.
326 Raised if ``task`` does not have the state expected from ``task's``
329 connections = task.config.ConnectionsClass(config=task.config)
331 for name
in connections.initOutputs:
332 connection = connections.__getattribute__(name)
333 _assertAttributeMatchesConnection(task, name, connection)
337 """Return the initInputs object that would have been passed to a
338 `~lsst.pipe.base.PipelineTask` constructor.
342 butler : `lsst.daf.butler.Butler`
343 The repository to search for input datasets. Must have
344 pre-configured collections.
345 config : `lsst.pipe.base.PipelineTaskConfig`
346 The config for the task to be constructed.
350 initInputs : `dict` [`str`]
351 A dictionary of objects in the format of the ``initInputs`` parameter
352 to `lsst.pipe.base.PipelineTask`.
354 connections = config.connections.ConnectionsClass(config=config)
356 for name
in connections.initInputs:
357 attribute = getattr(connections, name)
359 dsType = DatasetType(attribute.name, butler.registry.dimensions.extract(
set()),
360 attribute.storageClass)
362 initInputs[name] = butler.get(dsType)
daf::base::PropertySet * set
std::shared_ptr< FrameSet > append(FrameSet const &first, FrameSet const &second)
Construct a FrameSet that performs two transformations in series.
def makeQuantum(task, butler, dataId, ioDataIds)
def runTestQuantum(task, butler, quantum, mockRun=True)
def assertValidInitOutput(task)
def assertValidOutput(task, result)
def getInitInputs(butler, config)