Coverage for python/lsst/pipe/base/pipeline_graph/_nodes.py: 83%

22 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-11-30 12:09 +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 

28 

29__all__ = ( 

30 "NodeKey", 

31 "NodeType", 

32) 

33 

34import enum 

35from typing import NamedTuple 

36 

37 

38class NodeType(enum.Enum): 

39 """Enumeration of the types of nodes in a PipelineGraph.""" 

40 

41 DATASET_TYPE = 0 

42 TASK_INIT = 1 

43 TASK = 2 

44 

45 @property 

46 def bipartite(self) -> int: 

47 """The integer used as the "bipartite" key in networkx exports of a 

48 `PipelineGraph`. 

49 

50 This key is used by the `networkx.algorithms.bipartite` module. 

51 """ 

52 return int(self is not NodeType.DATASET_TYPE) 

53 

54 def __lt__(self, other: NodeType) -> bool: 

55 # We define __lt__ only to be able to provide deterministic tiebreaking 

56 # on top of topological ordering of `PipelineGraph`` and views thereof. 

57 return self.value < other.value 

58 

59 

60class NodeKey(NamedTuple): 

61 """A special key type for nodes in networkx graphs. 

62 

63 Notes 

64 ----- 

65 Using a tuple for the key allows tasks labels and dataset type names with 

66 the same string value to coexist in the graph. These only rarely appear in 

67 `PipelineGraph` public interfaces; when the node type is implicit, bare 

68 `str` task labels or dataset type names are used instead. 

69 

70 NodeKey objects stringify to just their name, which is used both as a way 

71 to convert to the `str` objects used in the main public interface and as an 

72 easy way to usefully stringify containers returned directly by networkx 

73 algorithms (especially in error messages). Note that this requires `repr`, 

74 not just `str`, because Python builtin containers always use `repr` on 

75 their items, even in their implementations for `str`. 

76 """ 

77 

78 node_type: NodeType 

79 """Node type enum for this key.""" 

80 

81 name: str 

82 """Task label or dataset type name. 

83 

84 This is always the parent dataset type name for component dataset types. 

85 """ 

86 

87 def __repr__(self) -> str: 

88 return self.name 

89 

90 def __str__(self) -> str: 

91 return self.name