Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,7 +5,9 @@ import networkx as nx
|
|
5 |
import numpy as np
|
6 |
from operator import itemgetter
|
7 |
import math # Import the math module
|
8 |
-
|
|
|
|
|
9 |
|
10 |
# Sidebar for selecting an option
|
11 |
sidebar_option = st.sidebar.radio("Select an option",
|
@@ -18,7 +20,7 @@ sidebar_option = st.sidebar.radio("Select an option",
|
|
18 |
"Drawing: Multipartite Layout", "Drawing: Node Colormap",
|
19 |
"Drawing: Rainbow Coloring", "Drawing: Random Geometric Graph","Drawing: Self-loops",
|
20 |
"Drawing: Simple Path", "Drawing: Spectral Embedding", "Drawing: Traveling Salesman Problem",
|
21 |
-
"Drawing: Weighted Graph"])
|
22 |
|
23 |
# Helper function to draw and display graph
|
24 |
def draw_graph(G, pos=None, title="Graph Visualization"):
|
@@ -26,6 +28,75 @@ def draw_graph(G, pos=None, title="Graph Visualization"):
|
|
26 |
nx.draw(G, pos=pos, with_labels=True, node_color='lightblue', node_size=500, font_size=10, font_weight='bold')
|
27 |
st.pyplot(plt)
|
28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
# Function to display Weighted Graph
|
30 |
def display_weighted_graph():
|
31 |
st.title("Drawing: Weighted Graph")
|
|
|
5 |
import numpy as np
|
6 |
from operator import itemgetter
|
7 |
import math # Import the math module
|
8 |
+
from mayavi import mlab
|
9 |
+
from mayavi.mlab import pipeline
|
10 |
+
from tempfile import TemporaryDirectory
|
11 |
|
12 |
# Sidebar for selecting an option
|
13 |
sidebar_option = st.sidebar.radio("Select an option",
|
|
|
20 |
"Drawing: Multipartite Layout", "Drawing: Node Colormap",
|
21 |
"Drawing: Rainbow Coloring", "Drawing: Random Geometric Graph","Drawing: Self-loops",
|
22 |
"Drawing: Simple Path", "Drawing: Spectral Embedding", "Drawing: Traveling Salesman Problem",
|
23 |
+
"Drawing: Weighted Graph", "3D Drawing: Mayavi2"])
|
24 |
|
25 |
# Helper function to draw and display graph
|
26 |
def draw_graph(G, pos=None, title="Graph Visualization"):
|
|
|
28 |
nx.draw(G, pos=pos, with_labels=True, node_color='lightblue', node_size=500, font_size=10, font_weight='bold')
|
29 |
st.pyplot(plt)
|
30 |
|
31 |
+
def create_3d_graph(graph_type="cycle_graph", num_nodes=20):
|
32 |
+
if graph_type == "cycle_graph":
|
33 |
+
G = nx.cycle_graph(num_nodes)
|
34 |
+
elif graph_type == "kite_graph":
|
35 |
+
G = nx.krackhardt_kite_graph()
|
36 |
+
elif graph_type == "grid_graph":
|
37 |
+
G = nx.grid_2d_graph(4, 5)
|
38 |
+
else:
|
39 |
+
G = nx.Graph()
|
40 |
+
G.add_edge('a', 'b')
|
41 |
+
G.add_edge('a', 'c')
|
42 |
+
G.add_edge('a', 'd')
|
43 |
+
|
44 |
+
# Reorder nodes from 0 to len(G) - 1
|
45 |
+
G = nx.convert_node_labels_to_integers(G)
|
46 |
+
|
47 |
+
# 3D spring layout
|
48 |
+
pos = nx.spring_layout(G, dim=3, seed=1001)
|
49 |
+
xyz = np.array([pos[v] for v in sorted(G)])
|
50 |
+
scalars = np.array(list(G.nodes())) + 5
|
51 |
+
|
52 |
+
mlab.figure(size=(800, 600), bgcolor=(1, 1, 1))
|
53 |
+
|
54 |
+
pts = mlab.points3d(
|
55 |
+
xyz[:, 0],
|
56 |
+
xyz[:, 1],
|
57 |
+
xyz[:, 2],
|
58 |
+
scalars,
|
59 |
+
scale_factor=0.1,
|
60 |
+
scale_mode="none",
|
61 |
+
colormap="Blues",
|
62 |
+
resolution=20,
|
63 |
+
)
|
64 |
+
|
65 |
+
pts.mlab_source.dataset.lines = np.array(list(G.edges()))
|
66 |
+
tube = pipeline.tube(pts, tube_radius=0.01)
|
67 |
+
pipeline.surface(tube, color=(0.8, 0.8, 0.8))
|
68 |
+
mlab.orientation_axes()
|
69 |
+
|
70 |
+
# Save to temporary file
|
71 |
+
with TemporaryDirectory() as temp_dir:
|
72 |
+
file_path = os.path.join(temp_dir, "graph.png")
|
73 |
+
mlab.savefig(file_path)
|
74 |
+
mlab.close()
|
75 |
+
return file_path
|
76 |
+
|
77 |
+
if sidebar_option == "3D Drawing: Mayavi2":
|
78 |
+
st.title("3D Drawing: Mayavi2")
|
79 |
+
|
80 |
+
st.sidebar.subheader("Graph Settings")
|
81 |
+
graph_type = st.sidebar.selectbox(
|
82 |
+
"Select Graph Type", ["cycle_graph", "kite_graph", "grid_graph", "custom"]
|
83 |
+
)
|
84 |
+
|
85 |
+
if graph_type == "cycle_graph":
|
86 |
+
num_nodes = st.sidebar.slider("Number of Nodes", min_value=3, max_value=50, value=20)
|
87 |
+
file_path = create_3d_graph(graph_type, num_nodes=num_nodes)
|
88 |
+
elif graph_type == "grid_graph":
|
89 |
+
st.sidebar.write("Grid graph is preconfigured as a 4x5 grid.")
|
90 |
+
file_path = create_3d_graph(graph_type)
|
91 |
+
elif graph_type == "kite_graph":
|
92 |
+
st.sidebar.write("Krackhardt kite graph is preconfigured.")
|
93 |
+
file_path = create_3d_graph(graph_type)
|
94 |
+
elif graph_type == "custom":
|
95 |
+
st.sidebar.write("Custom graph example with predefined edges.")
|
96 |
+
file_path = create_3d_graph(graph_type)
|
97 |
+
|
98 |
+
st.image(file_path, caption="3D Graph Visualization", use_container_width=True)
|
99 |
+
|
100 |
# Function to display Weighted Graph
|
101 |
def display_weighted_graph():
|
102 |
st.title("Drawing: Weighted Graph")
|