-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbasic_usage.py
More file actions
57 lines (45 loc) · 1.69 KB
/
basic_usage.py
File metadata and controls
57 lines (45 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python3
"""Standalone FrankenNetworkX usage example."""
from __future__ import annotations
import json
import tempfile
from pathlib import Path
import franken_networkx as fnx
def main() -> int:
graph = fnx.Graph()
graph.add_edge("a", "b", weight=1.0)
graph.add_edge("b", "c", weight=2.5)
graph.add_edge("a", "c", weight=10.0)
path = fnx.shortest_path(graph, "a", "c", weight="weight")
pagerank = fnx.pagerank(graph)
components = [sorted(component) for component in fnx.connected_components(graph)]
payload = fnx.node_link_data(graph)
restored = fnx.node_link_graph(payload)
with tempfile.TemporaryDirectory() as tmpdir:
edge_path = Path(tmpdir) / "graph.edgelist"
fnx.write_edgelist(graph, edge_path)
reloaded = fnx.read_edgelist(edge_path)
reloaded_edges = reloaded.number_of_edges()
if path != ["a", "b", "c"]:
raise RuntimeError(f"unexpected shortest path: {path}")
if len(components) != 1:
raise RuntimeError(f"unexpected component count: {components}")
if restored.number_of_edges() != graph.number_of_edges():
raise RuntimeError("node-link roundtrip changed edge count")
if reloaded_edges != graph.number_of_edges():
raise RuntimeError("edgelist roundtrip changed edge count")
print(
json.dumps(
{
"path": path,
"components": components,
"pagerank_sum": round(sum(pagerank.values()), 6),
"roundtrip_edges": restored.number_of_edges(),
},
indent=2,
sort_keys=True,
)
)
return 0
if __name__ == "__main__":
raise SystemExit(main())