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 

23in the graph is a QuantumGraph. 

24""" 

25 

26import networkx 

27 

28 

29class ClusteredQuantumGraph(networkx.DiGraph): 

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

31 QuantumGraph represented by a list of NodeIds. 

32 

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

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

35 use API over totally minimized memory usage. 

36 """ 

37 

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

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

40 

41 Parameters 

42 ---------- 

43 name : `str` 

44 Node name which must be unique in the graph. 

45 node_ids : `list` of `~lsst.pipe.base.NodeId` 

46 NodeIds for QuantumGraph subset. 

47 label : `str`, optional 

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

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

50 Arbitrary tags for the cluster. 

51 """ 

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

53 

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

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

56 to QuantumGraphs 

57 

58 Parameters 

59 ---------- 

60 node_for_adding : `str` or `list` of `~lsst.pipe.base.NodeId` 

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

62 attr : keyword arguments, optional 

63 Attributes to be saved with node in graph. 

64 """ 

65 if isinstance(node_for_adding, list): 

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

67 attr["qgraph_node_ids"] = node_for_adding 

68 elif "qgraph_node_ids" in attr: 

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

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

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

72 name = node_for_adding 

73 else: 

74 raise RuntimeError("Missing qgraph_node_ids attribute.") 

75 

76 if "label" not in attr: 

77 attr["label"] = None 

78 if "tags" not in attr: 

79 attr["tags"] = None 

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

81 

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

83 # Docstring inherited from networkx.Digraph 

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