Coverage for python/lsst/pipe/base/pipeline_graph/_nodes.py: 83%
22 statements
« prev ^ index » next coverage.py v7.3.0, created at 2023-08-31 09:39 +0000
« prev ^ index » next coverage.py v7.3.0, created at 2023-08-31 09:39 +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__ = (
24 "NodeKey",
25 "NodeType",
26)
28import enum
29from typing import NamedTuple
32class NodeType(enum.Enum):
33 """Enumeration of the types of nodes in a PipelineGraph."""
35 DATASET_TYPE = 0
36 TASK_INIT = 1
37 TASK = 2
39 @property
40 def bipartite(self) -> int:
41 """The integer used as the "bipartite" key in networkx exports of a
42 `PipelineGraph`.
44 This key is used by the `networkx.algorithms.bipartite` module.
45 """
46 return int(self is not NodeType.DATASET_TYPE)
48 def __lt__(self, other: NodeType) -> bool:
49 # We define __lt__ only to be able to provide deterministic tiebreaking
50 # on top of topological ordering of `PipelineGraph`` and views thereof.
51 return self.value < other.value
54class NodeKey(NamedTuple):
55 """A special key type for nodes in networkx graphs.
57 Notes
58 -----
59 Using a tuple for the key allows tasks labels and dataset type names with
60 the same string value to coexist in the graph. These only rarely appear in
61 `PipelineGraph` public interfaces; when the node type is implicit, bare
62 `str` task labels or dataset type names are used instead.
64 NodeKey objects stringify to just their name, which is used both as a way
65 to convert to the `str` objects used in the main public interface and as an
66 easy way to usefully stringify containers returned directly by networkx
67 algorithms (especially in error messages). Note that this requires `repr`,
68 not just `str`, because Python builtin containers always use `repr` on
69 their items, even in their implementations for `str`.
70 """
72 node_type: NodeType
73 """Node type enum for this key."""
75 name: str
76 """Task label or dataset type name.
78 This is always the parent dataset type name for component dataset types.
79 """
81 def __repr__(self) -> str:
82 return self.name
84 def __str__(self) -> str:
85 return self.name