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 

27from lsst.pipe.base import QuantumGraph 

28 

29 

30class ClusteredQuantumGraph(networkx.DiGraph): 

31 """Graph where node in the graph is a QuantumGraph 

32 """ 

33 

34 def add_cluster(self, name, qgraph, label=None): 

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

36 

37 Parameters 

38 ---------- 

39 name : `str` 

40 Node name which must be unique in the graph. 

41 qgraph : `~lsst.pipe.base.QuantumGraph` 

42 QuantumGraph containing the quanta in the cluster. 

43 label : `str`, optional 

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

45 """ 

46 self.add_node(name, qgraph=qgraph, label=label) 

47 

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

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

50 to QuantumGraphs 

51 

52 Parameters 

53 ---------- 

54 node_for_adding : `str` or `~lsst.pipe.base.QuantumGraph` 

55 Name of cluster or QuantumGraph to add where name will 

56 be a padded node counter. 

57 attr : 

58 Attributes to be saved with node in graph 

59 """ 

60 if isinstance(node_for_adding, QuantumGraph): 

61 name = f"{len(self) + 1:06d}" # create cluster name by counter 

62 attr['qgraph'] = node_for_adding 

63 elif 'qgraph' in attr: 

64 if not isinstance(attr['qgraph'], QuantumGraph): 

65 raise RuntimeError("Invalid type for qgraph attribute.") 

66 name = node_for_adding 

67 else: 

68 raise RuntimeError("Missing qgraph attribute.") 

69 

70 if 'label' not in attr: 

71 attr['label'] = None 

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

73 

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

75 # Docstring inherited from networkx.Digraph 

76 raise TypeError("Multiple nodes should not have same qgraph attribute.")