Coverage for python/lsst/pipe/base/graph/_implDetails.py: 15%
132 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-25 09:14 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-25 09:14 +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# (http://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 <http://www.gnu.org/licenses/>.
21from __future__ import annotations
23__all__ = ("_DatasetTracker", "DatasetTypeName", "_pruner")
25from collections import defaultdict
26from collections.abc import Iterable
27from itertools import chain
28from typing import Generic, NewType, TypeVar
30import networkx as nx
31from lsst.daf.butler import DatasetRef, DatasetType, NamedKeyDict, Quantum
32from lsst.pipe.base.connections import AdjustQuantumHelper
34from .._status import NoWorkFound
35from ..pipeline import TaskDef
36from .quantumNode import QuantumNode
38# NewTypes
39DatasetTypeName = NewType("DatasetTypeName", str)
41# Generic type parameters
42_T = TypeVar("_T", DatasetTypeName, DatasetRef)
43_U = TypeVar("_U", TaskDef, QuantumNode)
46class _DatasetTracker(Generic[_T, _U]):
47 r"""A generic container for tracking keys which are produced or
48 consumed by some value. In the context of a QuantumGraph, keys may be
49 `~lsst.daf.butler.DatasetRef`\ s and the values would be Quanta that either
50 produce or consume those `~lsst.daf.butler.DatasetRef`\ s.
52 Prameters
53 ---------
54 createInverse : bool
55 When adding a key associated with a producer or consumer, also create
56 and inverse mapping that allows looking up all the keys associated with
57 some value. Defaults to False.
58 """
60 def __init__(self, createInverse: bool = False):
61 self._producers: dict[_T, _U] = {}
62 self._consumers: defaultdict[_T, set[_U]] = defaultdict(set)
63 self._createInverse = createInverse
64 if self._createInverse:
65 self._itemsDict: defaultdict[_U, set[_T]] = defaultdict(set)
67 def addProducer(self, key: _T, value: _U) -> None:
68 """Add a key which is produced by some value.
70 Parameters
71 ----------
72 key : `~typing.TypeVar`
73 The type to track.
74 value : `~typing.TypeVar`
75 The type associated with the production of the key.
77 Raises
78 ------
79 ValueError
80 Raised if key is already declared to be produced by another value.
81 """
82 if (existing := self._producers.get(key)) is not None and existing != value:
83 raise ValueError(f"Only one node is allowed to produce {key}, the current producer is {existing}")
84 self._producers[key] = value
85 if self._createInverse:
86 self._itemsDict[value].add(key)
88 def removeProducer(self, key: _T, value: _U) -> None:
89 """Remove a value (e.g. `QuantumNode` or `TaskDef`) from being
90 considered a producer of the corresponding key.
92 It is not an error to remove a key that is not in the tracker.
94 Parameters
95 ----------
96 key : `~typing.TypeVar`
97 The type to track.
98 value : `~typing.TypeVar`
99 The type associated with the production of the key.
100 """
101 self._producers.pop(key, None)
102 if self._createInverse:
103 if result := self._itemsDict.get(value):
104 result.discard(key)
106 def addConsumer(self, key: _T, value: _U) -> None:
107 """Add a key which is consumed by some value.
109 Parameters
110 ----------
111 key : `~typing.TypeVar`
112 The type to track.
113 value : `~typing.TypeVar`
114 The type associated with the consumption of the key.
115 """
116 self._consumers[key].add(value)
117 if self._createInverse:
118 self._itemsDict[value].add(key)
120 def removeConsumer(self, key: _T, value: _U) -> None:
121 """Remove a value (e.g. `QuantumNode` or `TaskDef`) from being
122 considered a consumer of the corresponding key.
124 It is not an error to remove a key that is not in the tracker.
126 Parameters
127 ----------
128 key : `~typing.TypeVar`
129 The type to track.
130 value : `~typing.TypeVar`
131 The type associated with the consumption of the key.
132 """
133 if (result := self._consumers.get(key)) is not None:
134 result.discard(value)
135 if self._createInverse:
136 if result_inverse := self._itemsDict.get(value):
137 result_inverse.discard(key)
139 def getConsumers(self, key: _T) -> set[_U]:
140 """Return all values associated with the consumption of the supplied
141 key.
143 Parameters
144 ----------
145 key : `~typing.TypeVar`
146 The type which has been tracked in the `_DatasetTracker`.
147 """
148 return self._consumers.get(key, set())
150 def getProducer(self, key: _T) -> _U | None:
151 """Return the value associated with the consumption of the supplied
152 key.
154 Parameters
155 ----------
156 key : `~typing.TypeVar`
157 The type which has been tracked in the `_DatasetTracker`.
158 """
159 # This tracker may have had all nodes associated with a key removed
160 # and if there are no refs (empty set) should return None
161 return producer if (producer := self._producers.get(key)) else None
163 def getAll(self, key: _T) -> set[_U]:
164 """Return all consumers and the producer associated with the the
165 supplied key.
167 Parameters
168 ----------
169 key : `~typing.TypeVar`
170 The type which has been tracked in the `_DatasetTracker`.
171 """
172 return self.getConsumers(key).union(x for x in (self.getProducer(key),) if x is not None)
174 @property
175 def inverse(self) -> defaultdict[_U, set[_T]] | None:
176 """Return the inverse mapping if class was instantiated to create an
177 inverse, else return None.
178 """
179 return self._itemsDict if self._createInverse else None
181 def makeNetworkXGraph(self) -> nx.DiGraph:
182 """Create a NetworkX graph out of all the contained keys, using the
183 relations of producer and consumers to create the edges.
185 Returns
186 -------
187 graph : `networkx.DiGraph`
188 The graph created out of the supplied keys and their relations.
189 """
190 graph = nx.DiGraph()
191 for entry in self._producers.keys() | self._consumers.keys():
192 producer = self.getProducer(entry)
193 consumers = self.getConsumers(entry)
194 # This block is for tasks that consume existing inputs
195 if producer is None and consumers:
196 for consumer in consumers:
197 graph.add_node(consumer)
198 # This block is for tasks that produce output that is not consumed
199 # in this graph
200 elif producer is not None and not consumers:
201 graph.add_node(producer)
202 # all other connections
203 else:
204 for consumer in consumers:
205 graph.add_edge(producer, consumer)
206 return graph
208 def keys(self) -> set[_T]:
209 """Return all tracked keys."""
210 return self._producers.keys() | self._consumers.keys()
212 def remove(self, key: _T) -> None:
213 """Remove a key and its corresponding value from the tracker, this is
214 a no-op if the key is not in the tracker.
216 Parameters
217 ----------
218 key : `~typing.TypeVar`
219 A key tracked by the `_DatasetTracker`.
220 """
221 self._producers.pop(key, None)
222 self._consumers.pop(key, None)
224 def __contains__(self, key: _T) -> bool:
225 """Check if a key is in the `_DatasetTracker`.
227 Parameters
228 ----------
229 key : `~typing.TypeVar`
230 The key to check.
232 Returns
233 -------
234 contains : `bool`
235 Boolean of the presence of the supplied key.
236 """
237 return key in self._producers or key in self._consumers
240def _pruner(
241 datasetRefDict: _DatasetTracker[DatasetRef, QuantumNode],
242 refsToRemove: Iterable[DatasetRef],
243 *,
244 alreadyPruned: set[QuantumNode] | None = None,
245) -> None:
246 r"""Prune supplied dataset refs out of ``datasetRefDict`` container,
247 recursing to additional nodes dependant on pruned refs.
249 Parameters
250 ----------
251 datasetRefDict : `_DatasetTracker` [ `~lsst.daf.butler.DatasetRef`, \
252 `QuantumNode`]
253 The dataset tracker that maps `~lsst.daf.butler.DatasetRef`\ s to the
254 `QuantumNode`\s that produce/consume that
255 `~lsst.daf.butler.DatasetRef`.
256 This function modifies ``datasetRefDict`` in-place.
257 refsToRemove : `~collections.abc.Iterable` of `~lsst.daf.butler.DatasetRef`
258 The `~lsst.daf.butler.DatasetRef`\ s which should be pruned from the
259 input dataset tracker.
260 alreadyPruned : `set` of `QuantumNode`
261 A set of nodes which have been pruned from the dataset tracker.
262 """
263 if alreadyPruned is None:
264 alreadyPruned = set()
265 for ref in refsToRemove:
266 # make a copy here, because this structure will be modified in
267 # recursion, hitting a node more than once won't be much of an
268 # issue, as we skip anything that has been processed
269 nodes = set(datasetRefDict.getConsumers(ref))
270 for node in nodes:
271 # This node will never be associated with this ref
272 datasetRefDict.removeConsumer(ref, node)
273 if node in alreadyPruned:
274 continue
275 # find the connection corresponding to the input ref
276 connectionRefs = node.quantum.inputs.get(ref.datasetType)
277 if connectionRefs is None:
278 # look to see if any inputs are component refs that match the
279 # input ref to prune
280 others = ref.datasetType.makeAllComponentDatasetTypes()
281 # for each other component type check if there are assocated
282 # refs
283 for other in others:
284 connectionRefs = node.quantum.inputs.get(other)
285 if connectionRefs is not None:
286 # now search the component refs and see which one
287 # matches the ref to trim
288 for cr in connectionRefs:
289 if cr.makeCompositeRef() == ref:
290 toRemove = cr
291 break
292 else:
293 # Ref must be an initInput ref and we want to ignore those
294 raise RuntimeError(f"Cannot prune on non-Input dataset type {ref.datasetType.name}")
295 else:
296 toRemove = ref
298 tmpRefs = set(connectionRefs).difference((toRemove,))
299 tmpConnections = NamedKeyDict[DatasetType, list[DatasetRef]](node.quantum.inputs.items())
300 tmpConnections[toRemove.datasetType] = list(tmpRefs)
301 helper = AdjustQuantumHelper(inputs=tmpConnections, outputs=node.quantum.outputs)
302 assert node.quantum.dataId is not None, (
303 "assert to make the type checker happy, it should not "
304 "actually be possible to not have dataId set to None "
305 "at this point"
306 )
308 # Try to adjust the quantum with the reduced refs to make sure the
309 # node will still satisfy all its conditions.
310 #
311 # If it can't because NoWorkFound is raised, that means a
312 # connection is no longer present, and the node should be removed
313 # from the graph.
314 try:
315 helper.adjust_in_place(node.taskDef.connections, node.taskDef.label, node.quantum.dataId)
316 newQuantum = Quantum(
317 taskName=node.quantum.taskName,
318 taskClass=node.quantum.taskClass,
319 dataId=node.quantum.dataId,
320 initInputs=node.quantum.initInputs,
321 inputs=helper.inputs,
322 outputs=helper.outputs,
323 )
324 # If the inputs or outputs were adjusted to something different
325 # than what was supplied by the graph builder, dissassociate
326 # node from those refs, and if they are output refs, prune them
327 # from downstream tasks. This means that based on new inputs
328 # the task wants to produce fewer outputs, or consume fewer
329 # inputs.
330 for condition, existingMapping, newMapping, remover in (
331 (
332 helper.inputs_adjusted,
333 node.quantum.inputs,
334 helper.inputs,
335 datasetRefDict.removeConsumer,
336 ),
337 (
338 helper.outputs_adjusted,
339 node.quantum.outputs,
340 helper.outputs,
341 datasetRefDict.removeProducer,
342 ),
343 ):
344 if condition:
345 notNeeded = set()
346 for key in existingMapping:
347 if key not in newMapping:
348 compositeRefs = (
349 r if not r.isComponent() else r.makeCompositeRef()
350 for r in existingMapping[key]
351 )
352 notNeeded |= set(compositeRefs)
353 continue
354 notNeeded |= set(existingMapping[key]) - set(newMapping[key])
355 if notNeeded:
356 for ref in notNeeded:
357 if ref.isComponent():
358 ref = ref.makeCompositeRef()
359 remover(ref, node)
360 if remover is datasetRefDict.removeProducer:
361 _pruner(datasetRefDict, notNeeded, alreadyPruned=alreadyPruned)
362 object.__setattr__(node, "quantum", newQuantum)
363 noWorkFound = False
365 except NoWorkFound:
366 noWorkFound = True
368 if noWorkFound:
369 # This will throw if the length is less than the minimum number
370 for tmpRef in chain(
371 chain.from_iterable(node.quantum.inputs.values()), node.quantum.initInputs.values()
372 ):
373 if tmpRef.isComponent():
374 tmpRef = tmpRef.makeCompositeRef()
375 datasetRefDict.removeConsumer(tmpRef, node)
376 alreadyPruned.add(node)
377 # prune all outputs produced by this node
378 # mark that none of these will be produced
379 forwardPrunes = set()
380 for forwardRef in chain.from_iterable(node.quantum.outputs.values()):
381 datasetRefDict.removeProducer(forwardRef, node)
382 forwardPrunes.add(forwardRef)
383 _pruner(datasetRefDict, forwardPrunes, alreadyPruned=alreadyPruned)