Coverage for python / lsst / pipe / base / pipeline_graph / _nodes.py: 84%
31 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-17 08:59 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-17 08:59 +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 software is dual licensed under the GNU General Public License and also
10# under a 3-clause BSD license. Recipients may choose which of these licenses
11# to use; please see the files gpl-3.0.txt and/or bsd_license.txt,
12# respectively. If you choose the GPL option then the following text applies
13# (but note that there is still no warranty even if you opt for BSD instead):
14#
15# This program is free software: you can redistribute it and/or modify
16# it under the terms of the GNU General Public License as published by
17# the Free Software Foundation, either version 3 of the License, or
18# (at your option) any later version.
19#
20# This program is distributed in the hope that it will be useful,
21# but WITHOUT ANY WARRANTY; without even the implied warranty of
22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23# GNU General Public License for more details.
24#
25# You should have received a copy of the GNU General Public License
26# along with this program. If not, see <http://www.gnu.org/licenses/>.
27from __future__ import annotations
29__all__ = (
30 "NodeBipartite",
31 "NodeKey",
32 "NodeType",
33)
35import enum
36import sys
37from typing import Any, NamedTuple
40class NodeBipartite(enum.IntEnum):
41 """Constants for the 'bipartite' key in NetworkX graph views."""
43 DATASET_OR_TYPE = 0
44 """Value for nodes that represent dataset types (in pipeline graphs)
45 or datasets (in quantum graphs).
46 """
48 TASK_OR_QUANTUM = 1
49 """Value for nodes that represent tasks (in pipeline graphs) or quanta
50 (in quantum graphs).
51 """
53 if "sphinx" in sys.modules:
55 @classmethod
56 def from_bytes(cls, *args: Any, **kwargs: Any) -> Any: # pragma: no cover
57 """See `IntEnum.from_bytes`."""
58 return super().from_bytes(*args, **kwargs)
60 def to_bytes(self, *args: Any, **kwargs: Any) -> Any: # pragma: no cover
61 """See `IntEnum.to_bytes`."""
62 return super().to_bytes(self, *args, **kwargs)
65class NodeType(enum.Enum):
66 """Enumeration of the types of nodes in a PipelineGraph."""
68 DATASET_TYPE = 0
69 TASK_INIT = 1
70 TASK = 2
72 @property
73 def bipartite(self) -> NodeBipartite:
74 """The integer used as the "bipartite" key in networkx exports of a
75 `PipelineGraph`.
77 This key is used by the `networkx.algorithms.bipartite` module.
78 """
79 return NodeBipartite(self is not NodeType.DATASET_TYPE)
81 def __lt__(self, other: NodeType) -> bool:
82 # We define __lt__ only to be able to provide deterministic tiebreaking
83 # on top of topological ordering of `PipelineGraph`` and views thereof.
84 return self.value < other.value
87class NodeKey(NamedTuple):
88 """A special key type for nodes in networkx graphs.
90 Notes
91 -----
92 Using a tuple for the key allows tasks labels and dataset type names with
93 the same string value to coexist in the graph. These only rarely appear in
94 `PipelineGraph` public interfaces; when the node type is implicit, bare
95 `str` task labels or dataset type names are used instead.
97 NodeKey objects stringify to just their name, which is used both as a way
98 to convert to the `str` objects used in the main public interface and as an
99 easy way to usefully stringify containers returned directly by networkx
100 algorithms (especially in error messages). Note that this requires `repr`,
101 not just `str`, because Python builtin containers always use `repr` on
102 their items, even in their implementations for `str`.
103 """
105 node_type: NodeType
106 """Node type enum for this key."""
108 name: str
109 """Task label or dataset type name.
111 This is always the parent dataset type name for component dataset types.
112 """
114 def __repr__(self) -> str:
115 return self.name
117 def __str__(self) -> str:
118 return self.name
120 @property
121 def node_id(self) -> str:
122 return f"{self.name}:{self.node_type.value}"