comparison env/lib/python3.9/site-packages/networkx/readwrite/json_graph/tree.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 from itertools import chain
2 import networkx as nx
3
4 __all__ = ["tree_data", "tree_graph"]
5
6 _attrs = dict(id="id", children="children")
7
8
9 def tree_data(G, root, attrs=_attrs):
10 """Returns data in tree format that is suitable for JSON serialization
11 and use in Javascript documents.
12
13 Parameters
14 ----------
15 G : NetworkX graph
16 G must be an oriented tree
17
18 root : node
19 The root of the tree
20
21 attrs : dict
22 A dictionary that contains two keys 'id' and 'children'. The
23 corresponding values provide the attribute names for storing
24 NetworkX-internal graph data. The values should be unique. Default
25 value: :samp:`dict(id='id', children='children')`.
26
27 If some user-defined graph data use these attribute names as data keys,
28 they may be silently dropped.
29
30 Returns
31 -------
32 data : dict
33 A dictionary with node-link formatted data.
34
35 Raises
36 ------
37 NetworkXError
38 If values in attrs are not unique.
39
40 Examples
41 --------
42 >>> from networkx.readwrite import json_graph
43 >>> G = nx.DiGraph([(1, 2)])
44 >>> data = json_graph.tree_data(G, root=1)
45
46 To serialize with json
47
48 >>> import json
49 >>> s = json.dumps(data)
50
51 Notes
52 -----
53 Node attributes are stored in this format but keys
54 for attributes must be strings if you want to serialize with JSON.
55
56 Graph and edge attributes are not stored.
57
58 The default value of attrs will be changed in a future release of NetworkX.
59
60 See Also
61 --------
62 tree_graph, node_link_data, node_link_data
63 """
64 if G.number_of_nodes() != G.number_of_edges() + 1:
65 raise TypeError("G is not a tree.")
66 if not G.is_directed():
67 raise TypeError("G is not directed.")
68
69 id_ = attrs["id"]
70 children = attrs["children"]
71 if id_ == children:
72 raise nx.NetworkXError("Attribute names are not unique.")
73
74 def add_children(n, G):
75 nbrs = G[n]
76 if len(nbrs) == 0:
77 return []
78 children_ = []
79 for child in nbrs:
80 d = dict(chain(G.nodes[child].items(), [(id_, child)]))
81 c = add_children(child, G)
82 if c:
83 d[children] = c
84 children_.append(d)
85 return children_
86
87 data = dict(chain(G.nodes[root].items(), [(id_, root)]))
88 data[children] = add_children(root, G)
89 return data
90
91
92 def tree_graph(data, attrs=_attrs):
93 """Returns graph from tree data format.
94
95 Parameters
96 ----------
97 data : dict
98 Tree formatted graph data
99
100 Returns
101 -------
102 G : NetworkX DiGraph
103
104 attrs : dict
105 A dictionary that contains two keys 'id' and 'children'. The
106 corresponding values provide the attribute names for storing
107 NetworkX-internal graph data. The values should be unique. Default
108 value: :samp:`dict(id='id', children='children')`.
109
110 Examples
111 --------
112 >>> from networkx.readwrite import json_graph
113 >>> G = nx.DiGraph([(1, 2)])
114 >>> data = json_graph.tree_data(G, root=1)
115 >>> H = json_graph.tree_graph(data)
116
117 Notes
118 -----
119 The default value of attrs will be changed in a future release of NetworkX.
120
121 See Also
122 --------
123 tree_graph, node_link_data, adjacency_data
124 """
125 graph = nx.DiGraph()
126 id_ = attrs["id"]
127 children = attrs["children"]
128
129 def add_children(parent, children_):
130 for data in children_:
131 child = data[id_]
132 graph.add_edge(parent, child)
133 grandchildren = data.get(children, [])
134 if grandchildren:
135 add_children(child, grandchildren)
136 nodedata = {
137 str(k): v for k, v in data.items() if k != id_ and k != children
138 }
139 graph.add_node(child, **nodedata)
140
141 root = data[id_]
142 children_ = data.get(children, [])
143 nodedata = {str(k): v for k, v in data.items() if k != id_ and k != children}
144 graph.add_node(root, **nodedata)
145 add_children(root, children_)
146 return graph