Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# This file is part of ctrl_bps. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (https://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 <https://www.gnu.org/licenses/>. 

21 

22"""Class definitions for a Clustered QuantumGraph where a node in the graph is 

23a QuantumGraph. 

24""" 

25 

26__all__ = ["ClusteredQuantumGraph"] 

27 

28 

29import networkx 

30 

31 

32class ClusteredQuantumGraph(networkx.DiGraph): 

33 """Graph where the data for a node is a subgraph of the full 

34 QuantumGraph represented by a list of NodeIds. 

35 

36 Using lsst.pipe.base.NodeId instead of integer because the QuantumGraph 

37 API requires them. Chose skipping the repeated creation of objects to 

38 use API over totally minimized memory usage. 

39 """ 

40 

41 def add_cluster(self, name, node_ids, label=None, tags=None): 

42 """Add a cluster of quanta as a node in the graph. 

43 

44 Parameters 

45 ---------- 

46 name : `str` 

47 Node name which must be unique in the graph. 

48 node_ids : `list` [`lsst.pipe.base.NodeId`] 

49 NodeIds for QuantumGraph subset. 

50 label : `str`, optional 

51 Label for the cluster. Can be used in grouping clusters. 

52 tags : `dict` [`str`, `Any`], optional 

53 Arbitrary tags for the cluster. 

54 """ 

55 self.add_node(name, qgraph_node_ids=node_ids, label=label, tags=tags) 

56 

57 def add_node(self, node_for_adding, **attr): 

58 """Override add_node function to ensure that nodes are limited 

59 to QuantumGraphs. 

60 

61 Parameters 

62 ---------- 

63 node_for_adding : `str` or `list` [`lsst.pipe.base.NodeId`] 

64 Name of cluster or cluster data (list of NodeIds). 

65 attr : keyword arguments, optional 

66 Attributes to be saved with node in graph. 

67 """ 

68 if isinstance(node_for_adding, list): 

69 name = f"{len(self) + 1:06d}" # Create cluster name by counter. 

70 attr["qgraph_node_ids"] = node_for_adding 

71 elif "qgraph_node_ids" in attr: 

72 if not isinstance(attr["qgraph_node_ids"], list): 

73 raise RuntimeError(f"Invalid type {type(attr['qgraph_node_ids'])}" 

74 " for qgraph_node_ids attribute; should be list.") 

75 name = node_for_adding 

76 else: 

77 raise RuntimeError("Missing qgraph_node_ids attribute.") 

78 

79 if "label" not in attr: 

80 attr["label"] = None 

81 if "tags" not in attr: 

82 attr["tags"] = None 

83 super().add_node(name, **attr) 

84 

85 def add_nodes_from(self, nodes_for_adding, **attr): 

86 # Docstring inherited from networkx.Digraph 

87 raise TypeError("Multiple nodes should not have same attributes.")