Repository navigation
Expand file tree
/
Copy pathvisualize.py
More file actions
94 lines (71 loc) · 2.47 KB
/
Copy pathvisualize.py
File metadata and controls
94 lines (71 loc) · 2.47 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# visualize.py
import os
import networkx as nx
from datetime import datetime
from pyvis.network import Network
OUTPUT_DIR = "output"
NODE_STYLE = {
"Connection Point": {"shape": "dot","size": 18,"color": "#ff0000","font": {"size": 14}},
"Busbar in Cable Cabinet": {"shape": "box","size": 24,"color": "#FFFF00","font": {"size": 14}},
"DEFAULT": {"shape": "dot","size": 12,"color": "#ffffff","font": {"size": 10}},
}
EDGE_STYLE = {
"Service Cable (Underground)": {"color": "#41FF24","width": 2},
"Feeder Cable (Underground)": {"color": "#006605","width": 4},
"DEFAULT": {"color": "#000000","width": 1}
}
NET_OPTIONS = """
{
"layout": {"hierarchical": {"enabled": true,"direction": "UD","nodeSpacing": 120,"levelSeparation": 150,"treeSpacing": 200}},
"physics": {"enabled": false}
}
"""
def visualize_graph(graph, name, start_oid):
net = Network(
height="90vh",
width="100%",
bgcolor="#ffffff",
font_color="black",
directed=False,
)
net.set_options(NET_OPTIONS)
levels = nx.single_source_shortest_path_length(graph, start_oid)
# ---- nodes ----
for node, data in graph.nodes(data=True):
node_type = data.get("type")
style = NODE_STYLE.get(node_type, NODE_STYLE["DEFAULT"])
node_title = "\n".join(f"{k}: {v}" for k, v in data.items())
net.add_node(
node,
label=node_type,
title=f"OID: {node}\n{node_title}",
shape=style["shape"],
size=style["size"],
color=style["color"],
font=style["font"],
level=levels.get(node, 0),
)
# ---- edges (cables) ----
for u, v, data in graph.edges(data=True):
cable_type = data.get("type", "UNKNOWN")
style = EDGE_STYLE.get(cable_type, EDGE_STYLE["DEFAULT"])
edge_title = (
"\n".join(f"{k}: {val}" for k, val in data.items()) +
"\n" +
f"source_id: {u}\n" +
f"target_id: {v}\n"
)
net.add_edge(
u,
v,
title=edge_title,
color=style["color"],
width=style["width"],
dashes=style.get("dashes", False),
)
os.makedirs(OUTPUT_DIR, exist_ok=True)
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
output_path = os.path.join(OUTPUT_DIR, f"{name}_{timestamp}.html")
# net.show(output_path, notebook=False)
net.write_html(output_path)
return output_path