comparison env/lib/python3.9/site-packages/networkx/readwrite/json_graph/adjacency.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__ = ["adjacency_data", "adjacency_graph"]
5
6 _attrs = dict(id="id", key="key")
7
8
9 def adjacency_data(G, attrs=_attrs):
10 """Returns data in adjacency format that is suitable for JSON serialization
11 and use in Javascript documents.
12
13 Parameters
14 ----------
15 G : NetworkX graph
16
17 attrs : dict
18 A dictionary that contains two keys 'id' and 'key'. The corresponding
19 values provide the attribute names for storing NetworkX-internal graph
20 data. The values should be unique. Default value:
21 :samp:`dict(id='id', key='key')`.
22
23 If some user-defined graph data use these attribute names as data keys,
24 they may be silently dropped.
25
26 Returns
27 -------
28 data : dict
29 A dictionary with adjacency formatted data.
30
31 Raises
32 ------
33 NetworkXError
34 If values in attrs are not unique.
35
36 Examples
37 --------
38 >>> from networkx.readwrite import json_graph
39 >>> G = nx.Graph([(1, 2)])
40 >>> data = json_graph.adjacency_data(G)
41
42 To serialize with json
43
44 >>> import json
45 >>> s = json.dumps(data)
46
47 Notes
48 -----
49 Graph, node, and link attributes will be written when using this format
50 but attribute keys must be strings if you want to serialize the resulting
51 data with JSON.
52
53 The default value of attrs will be changed in a future release of NetworkX.
54
55 See Also
56 --------
57 adjacency_graph, node_link_data, tree_data
58 """
59 multigraph = G.is_multigraph()
60 id_ = attrs["id"]
61 # Allow 'key' to be omitted from attrs if the graph is not a multigraph.
62 key = None if not multigraph else attrs["key"]
63 if id_ == key:
64 raise nx.NetworkXError("Attribute names are not unique.")
65 data = {}
66 data["directed"] = G.is_directed()
67 data["multigraph"] = multigraph
68 data["graph"] = list(G.graph.items())
69 data["nodes"] = []
70 data["adjacency"] = []
71 for n, nbrdict in G.adjacency():
72 data["nodes"].append(dict(chain(G.nodes[n].items(), [(id_, n)])))
73 adj = []
74 if multigraph:
75 for nbr, keys in nbrdict.items():
76 for k, d in keys.items():
77 adj.append(dict(chain(d.items(), [(id_, nbr), (key, k)])))
78 else:
79 for nbr, d in nbrdict.items():
80 adj.append(dict(chain(d.items(), [(id_, nbr)])))
81 data["adjacency"].append(adj)
82 return data
83
84
85 def adjacency_graph(data, directed=False, multigraph=True, attrs=_attrs):
86 """Returns graph from adjacency data format.
87
88 Parameters
89 ----------
90 data : dict
91 Adjacency list formatted graph data
92
93 Returns
94 -------
95 G : NetworkX graph
96 A NetworkX graph object
97
98 directed : bool
99 If True, and direction not specified in data, return a directed graph.
100
101 multigraph : bool
102 If True, and multigraph not specified in data, return a multigraph.
103
104 attrs : dict
105 A dictionary that contains two keys 'id' and 'key'. The corresponding
106 values provide the attribute names for storing NetworkX-internal graph
107 data. The values should be unique. Default value:
108 :samp:`dict(id='id', key='key')`.
109
110 Examples
111 --------
112 >>> from networkx.readwrite import json_graph
113 >>> G = nx.Graph([(1, 2)])
114 >>> data = json_graph.adjacency_data(G)
115 >>> H = json_graph.adjacency_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 adjacency_graph, node_link_data, tree_data
124 """
125 multigraph = data.get("multigraph", multigraph)
126 directed = data.get("directed", directed)
127 if multigraph:
128 graph = nx.MultiGraph()
129 else:
130 graph = nx.Graph()
131 if directed:
132 graph = graph.to_directed()
133 id_ = attrs["id"]
134 # Allow 'key' to be omitted from attrs if the graph is not a multigraph.
135 key = None if not multigraph else attrs["key"]
136 graph.graph = dict(data.get("graph", []))
137 mapping = []
138 for d in data["nodes"]:
139 node_data = d.copy()
140 node = node_data.pop(id_)
141 mapping.append(node)
142 graph.add_node(node)
143 graph.nodes[node].update(node_data)
144 for i, d in enumerate(data["adjacency"]):
145 source = mapping[i]
146 for tdata in d:
147 target_data = tdata.copy()
148 target = target_data.pop(id_)
149 if not multigraph:
150 graph.add_edge(source, target)
151 graph[source][target].update(tdata)
152 else:
153 ky = target_data.pop(key, None)
154 graph.add_edge(source, target, key=ky)
155 graph[source][target][ky].update(tdata)
156 return graph