Spaces:
Sleeping
Sleeping
Commit ·
f1fbedf
1
Parent(s): bca3cba
updated commit 006
Browse files- utils/visualization.py +58 -9
utils/visualization.py
CHANGED
|
@@ -1,13 +1,62 @@
|
|
|
|
|
| 1 |
|
| 2 |
-
import
|
| 3 |
import networkx as nx
|
|
|
|
| 4 |
|
| 5 |
def plot_network(nodes, path):
|
| 6 |
-
G = nx.
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# utils/visualization.py
|
| 2 |
|
| 3 |
+
import plotly.graph_objs as go
|
| 4 |
import networkx as nx
|
| 5 |
+
import random
|
| 6 |
|
| 7 |
def plot_network(nodes, path):
|
| 8 |
+
G = nx.Graph()
|
| 9 |
+
|
| 10 |
+
for node in nodes:
|
| 11 |
+
G.add_node(node)
|
| 12 |
+
|
| 13 |
+
for i in range(len(path) - 1):
|
| 14 |
+
G.add_edge(path[i], path[i + 1])
|
| 15 |
+
|
| 16 |
+
# Random 3D coordinates for visual effect
|
| 17 |
+
pos = {node: (random.uniform(-10, 10), random.uniform(-10, 10), random.uniform(-10, 10)) for node in G.nodes}
|
| 18 |
+
|
| 19 |
+
edge_trace = go.Scatter3d(
|
| 20 |
+
x=[],
|
| 21 |
+
y=[],
|
| 22 |
+
z=[],
|
| 23 |
+
line=dict(width=4, color='gray'),
|
| 24 |
+
hoverinfo='none',
|
| 25 |
+
mode='lines'
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
for edge in G.edges():
|
| 29 |
+
x0, y0, z0 = pos[edge[0]]
|
| 30 |
+
x1, y1, z1 = pos[edge[1]]
|
| 31 |
+
edge_trace['x'] += (x0, x1, None)
|
| 32 |
+
edge_trace['y'] += (y0, y1, None)
|
| 33 |
+
edge_trace['z'] += (z0, z1, None)
|
| 34 |
+
|
| 35 |
+
node_trace = go.Scatter3d(
|
| 36 |
+
x=[pos[node][0] for node in G.nodes],
|
| 37 |
+
y=[pos[node][1] for node in G.nodes],
|
| 38 |
+
z=[pos[node][2] for node in G.nodes],
|
| 39 |
+
mode='markers+text',
|
| 40 |
+
text=list(G.nodes),
|
| 41 |
+
textposition="top center",
|
| 42 |
+
marker=dict(
|
| 43 |
+
size=10,
|
| 44 |
+
color='skyblue',
|
| 45 |
+
line=dict(width=1)
|
| 46 |
+
),
|
| 47 |
+
hoverinfo='text'
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
fig = go.Figure(data=[edge_trace, node_trace])
|
| 51 |
+
fig.update_layout(
|
| 52 |
+
title="3D Network Path Visualization",
|
| 53 |
+
showlegend=False,
|
| 54 |
+
margin=dict(l=0, r=0, b=0, t=40),
|
| 55 |
+
scene=dict(
|
| 56 |
+
xaxis=dict(showbackground=False),
|
| 57 |
+
yaxis=dict(showbackground=False),
|
| 58 |
+
zaxis=dict(showbackground=False)
|
| 59 |
+
)
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
return fig
|