Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -217,93 +217,86 @@ if sidebar_option == "Drawing: Traveling Salesman Problem":
|
|
217 |
display_tsp()
|
218 |
|
219 |
# Function to display Drawing: Spectral Embedding
|
220 |
-
import matplotlib.pyplot as plt
|
221 |
-
import networkx as nx
|
222 |
-
import streamlit as st
|
223 |
-
import numpy as np
|
224 |
-
|
225 |
def display_spectral_embedding():
|
226 |
st.title("Drawing: Spectral Embedding")
|
227 |
|
228 |
option = st.radio("Choose a graph type:", ("Default Example", "Create your own"))
|
229 |
|
230 |
-
def draw_graph_with_arrows(G, ax, pos, labels, options):
|
231 |
-
"""
|
232 |
-
Draws the graph with arrows pointing to nodes and avoids label overlap.
|
233 |
-
"""
|
234 |
-
# Draw nodes and edges
|
235 |
-
nx.draw_networkx_nodes(G, pos, ax=ax, **options)
|
236 |
-
nx.draw_networkx_edges(G, pos, ax=ax)
|
237 |
-
|
238 |
-
# Add labels with arrows
|
239 |
-
for node, (x, y) in pos.items():
|
240 |
-
label = labels[node]
|
241 |
-
ax.annotate(
|
242 |
-
label,
|
243 |
-
xy=(x, y), # Node position
|
244 |
-
xytext=(x + 0.05, y + 0.05), # Offset for arrow
|
245 |
-
arrowprops=dict(arrowstyle="->", color="gray", lw=1),
|
246 |
-
fontsize=8,
|
247 |
-
bbox=dict(boxstyle="round,pad=0.3", edgecolor="gray", facecolor="white", alpha=0.8)
|
248 |
-
)
|
249 |
-
|
250 |
if option == "Default Example":
|
251 |
-
|
|
|
252 |
G = nx.grid_2d_graph(6, 6)
|
253 |
-
pos = nx.spectral_layout(G)
|
254 |
-
labels = {node: str(node) for node in G.nodes()}
|
255 |
|
256 |
fig, axs = plt.subplots(3, 3, figsize=(12, 12))
|
257 |
axs = axs.flatten()
|
258 |
|
259 |
-
for i in range(7):
|
260 |
-
if i
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
268 |
for j in range(7, 9):
|
269 |
-
fig.delaxes(axs[j]) # Delete
|
270 |
|
271 |
st.pyplot(fig)
|
272 |
|
273 |
elif option == "Create your own":
|
|
|
274 |
grid_size = st.slider("Choose grid size (n x n):", min_value=3, max_value=10, value=6)
|
275 |
G_custom = nx.grid_2d_graph(grid_size, grid_size)
|
276 |
-
|
277 |
-
|
278 |
all_edges = list(G_custom.edges())
|
279 |
|
|
|
280 |
selected_edges_per_graph = []
|
281 |
-
for i in range(7):
|
282 |
-
selected_edges = st.multiselect(
|
283 |
-
|
284 |
-
options=[str(edge) for edge in all_edges]
|
285 |
-
)
|
286 |
selected_edges_per_graph.append(selected_edges)
|
287 |
|
|
|
288 |
generate_button = st.button("Generate Graph")
|
289 |
|
290 |
if generate_button:
|
291 |
fig, axs = plt.subplots(3, 3, figsize=(12, 12))
|
292 |
axs = axs.flatten()
|
293 |
|
294 |
-
|
|
|
295 |
edges_to_remove = [tuple(eval(edge)) for edge in selected_edges_per_graph[i]]
|
|
|
|
|
296 |
G_custom_copy = G_custom.copy()
|
297 |
G_custom_copy.remove_edges_from(edges_to_remove)
|
298 |
|
299 |
-
|
|
|
300 |
|
|
|
301 |
for j in range(7, 9):
|
302 |
-
fig.delaxes(axs[j]) # Delete
|
303 |
|
304 |
st.pyplot(fig)
|
305 |
|
306 |
-
|
307 |
# Display Drawing: Spectral Embedding if selected
|
308 |
if sidebar_option == "Drawing: Spectral Embedding":
|
309 |
display_spectral_embedding()
|
|
|
217 |
display_tsp()
|
218 |
|
219 |
# Function to display Drawing: Spectral Embedding
|
|
|
|
|
|
|
|
|
|
|
220 |
def display_spectral_embedding():
|
221 |
st.title("Drawing: Spectral Embedding")
|
222 |
|
223 |
option = st.radio("Choose a graph type:", ("Default Example", "Create your own"))
|
224 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
225 |
if option == "Default Example":
|
226 |
+
# Default example of spectral embedding with a grid graph
|
227 |
+
options = {"node_color": "C0", "node_size": 100} # No labels
|
228 |
G = nx.grid_2d_graph(6, 6)
|
|
|
|
|
229 |
|
230 |
fig, axs = plt.subplots(3, 3, figsize=(12, 12))
|
231 |
axs = axs.flatten()
|
232 |
|
233 |
+
for i in range(7): # Looping over 7 images
|
234 |
+
if i == 0:
|
235 |
+
nx.draw_spectral(G, **options, ax=axs[i])
|
236 |
+
elif i == 1:
|
237 |
+
G.remove_edge((2, 2), (2, 3))
|
238 |
+
nx.draw_spectral(G, **options, ax=axs[i])
|
239 |
+
elif i == 2:
|
240 |
+
G.remove_edge((3, 2), (3, 3))
|
241 |
+
nx.draw_spectral(G, **options, ax=axs[i])
|
242 |
+
elif i == 3:
|
243 |
+
G.remove_edge((2, 2), (3, 2))
|
244 |
+
nx.draw_spectral(G, **options, ax=axs[i])
|
245 |
+
elif i == 4:
|
246 |
+
G.remove_edge((2, 3), (3, 3))
|
247 |
+
nx.draw_spectral(G, **options, ax=axs[i])
|
248 |
+
elif i == 5:
|
249 |
+
G.remove_edge((1, 2), (1, 3))
|
250 |
+
nx.draw_spectral(G, **options, ax=axs[i])
|
251 |
+
elif i == 6:
|
252 |
+
G.remove_edge((4, 2), (4, 3))
|
253 |
+
nx.draw_spectral(G, **options, ax=axs[i])
|
254 |
+
|
255 |
+
# Hide the last two subplots (8th and 9th)
|
256 |
for j in range(7, 9):
|
257 |
+
fig.delaxes(axs[j]) # Delete the extra axes
|
258 |
|
259 |
st.pyplot(fig)
|
260 |
|
261 |
elif option == "Create your own":
|
262 |
+
# User can interactively modify the grid and see the results
|
263 |
grid_size = st.slider("Choose grid size (n x n):", min_value=3, max_value=10, value=6)
|
264 |
G_custom = nx.grid_2d_graph(grid_size, grid_size)
|
265 |
+
|
266 |
+
# List all edges to allow removal
|
267 |
all_edges = list(G_custom.edges())
|
268 |
|
269 |
+
# Collect user input for edges to remove (before showing the "Generate" button)
|
270 |
selected_edges_per_graph = []
|
271 |
+
for i in range(7): # Loop over 7 graphs
|
272 |
+
selected_edges = st.multiselect(f"Select edges to remove for graph {i+1}:",
|
273 |
+
options=[str(edge) for edge in all_edges])
|
|
|
|
|
274 |
selected_edges_per_graph.append(selected_edges)
|
275 |
|
276 |
+
# Add "Generate" button after edge selection
|
277 |
generate_button = st.button("Generate Graph")
|
278 |
|
279 |
if generate_button:
|
280 |
fig, axs = plt.subplots(3, 3, figsize=(12, 12))
|
281 |
axs = axs.flatten()
|
282 |
|
283 |
+
# Loop through each subplot and allow edge removal individually
|
284 |
+
for i in range(7): # Loop over 7 graphs
|
285 |
edges_to_remove = [tuple(eval(edge)) for edge in selected_edges_per_graph[i]]
|
286 |
+
|
287 |
+
# Remove the selected edges
|
288 |
G_custom_copy = G_custom.copy()
|
289 |
G_custom_copy.remove_edges_from(edges_to_remove)
|
290 |
|
291 |
+
# Draw the graph with removed edges
|
292 |
+
nx.draw_spectral(G_custom_copy, **{"node_color": "C0", "node_size": 100}, ax=axs[i])
|
293 |
|
294 |
+
# Hide the last two subplots (8th and 9th)
|
295 |
for j in range(7, 9):
|
296 |
+
fig.delaxes(axs[j]) # Delete the extra axes
|
297 |
|
298 |
st.pyplot(fig)
|
299 |
|
|
|
300 |
# Display Drawing: Spectral Embedding if selected
|
301 |
if sidebar_option == "Drawing: Spectral Embedding":
|
302 |
display_spectral_embedding()
|