Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/networkx/readwrite/json_graph/jit.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 """ | |
2 Read and write NetworkX graphs as JavaScript InfoVis Toolkit (JIT) format JSON. | |
3 | |
4 See the `JIT documentation`_ for more examples. | |
5 | |
6 Format | |
7 ------ | |
8 var json = [ | |
9 { | |
10 "id": "aUniqueIdentifier", | |
11 "name": "usually a nodes name", | |
12 "data": { | |
13 "some key": "some value", | |
14 "some other key": "some other value" | |
15 }, | |
16 "adjacencies": [ | |
17 { | |
18 nodeTo:"aNodeId", | |
19 data: {} //put whatever you want here | |
20 }, | |
21 'other adjacencies go here...' | |
22 }, | |
23 | |
24 'other nodes go here...' | |
25 ]; | |
26 .. _JIT documentation: http://thejit.org | |
27 """ | |
28 | |
29 import json | |
30 import networkx as nx | |
31 from networkx.utils.decorators import not_implemented_for | |
32 | |
33 __all__ = ["jit_graph", "jit_data"] | |
34 | |
35 | |
36 def jit_graph(data, create_using=None): | |
37 """Read a graph from JIT JSON. | |
38 | |
39 Parameters | |
40 ---------- | |
41 data : JSON Graph Object | |
42 | |
43 create_using : Networkx Graph, optional (default: Graph()) | |
44 Return graph of this type. The provided instance will be cleared. | |
45 | |
46 Returns | |
47 ------- | |
48 G : NetworkX Graph built from create_using if provided. | |
49 """ | |
50 if create_using is None: | |
51 G = nx.Graph() | |
52 else: | |
53 G = create_using | |
54 G.clear() | |
55 | |
56 if isinstance(data, str): | |
57 data = json.loads(data) | |
58 | |
59 for node in data: | |
60 G.add_node(node["id"], **node["data"]) | |
61 if node.get("adjacencies") is not None: | |
62 for adj in node["adjacencies"]: | |
63 G.add_edge(node["id"], adj["nodeTo"], **adj["data"]) | |
64 return G | |
65 | |
66 | |
67 @not_implemented_for("multigraph") | |
68 def jit_data(G, indent=None, default=None): | |
69 """Returns data in JIT JSON format. | |
70 | |
71 Parameters | |
72 ---------- | |
73 G : NetworkX Graph | |
74 | |
75 indent: optional, default=None | |
76 If indent is a non-negative integer, then JSON array elements and | |
77 object members will be pretty-printed with that indent level. | |
78 An indent level of 0, or negative, will only insert newlines. | |
79 None (the default) selects the most compact representation. | |
80 | |
81 default: optional, default=None | |
82 It will pass the value to the json.dumps function in order to | |
83 be able to serialize custom objects used as nodes. | |
84 | |
85 Returns | |
86 ------- | |
87 data: JIT JSON string | |
88 """ | |
89 json_graph = [] | |
90 for node in G.nodes(): | |
91 json_node = {"id": node, "name": node} | |
92 # node data | |
93 json_node["data"] = G.nodes[node] | |
94 # adjacencies | |
95 if G[node]: | |
96 json_node["adjacencies"] = [] | |
97 for neighbour in G[node]: | |
98 adjacency = {"nodeTo": neighbour} | |
99 # adjacency data | |
100 adjacency["data"] = G.edges[node, neighbour] | |
101 json_node["adjacencies"].append(adjacency) | |
102 json_graph.append(json_node) | |
103 return json.dumps(json_graph, indent=indent, default=default) |