shukdevdatta123 commited on
Commit
589643e
·
verified ·
1 Parent(s): 267a5b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -43
app.py CHANGED
@@ -217,87 +217,93 @@ if sidebar_option == "Drawing: Traveling Salesman Problem":
217
  display_tsp()
218
 
219
  # Function to display Drawing: Spectral Embedding
220
- # Function to display Drawing: Spectral Embedding
 
 
 
 
221
  def display_spectral_embedding():
222
  st.title("Drawing: Spectral Embedding")
223
 
224
  option = st.radio("Choose a graph type:", ("Default Example", "Create your own"))
225
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
226
  if option == "Default Example":
227
- # Default example of spectral embedding with a grid graph
228
- options = {"node_color": "C0", "node_size": 100, "with_labels": True}
229
  G = nx.grid_2d_graph(6, 6)
 
 
230
 
231
  fig, axs = plt.subplots(3, 3, figsize=(12, 12))
232
  axs = axs.flatten()
233
 
234
- for i in range(7): # Looping over 7 images
235
- if i == 0:
236
- nx.draw_spectral(G, **options, ax=axs[i])
237
- elif i == 1:
238
- G.remove_edge((2, 2), (2, 3))
239
- nx.draw_spectral(G, **options, ax=axs[i])
240
- elif i == 2:
241
- G.remove_edge((3, 2), (3, 3))
242
- nx.draw_spectral(G, **options, ax=axs[i])
243
- elif i == 3:
244
- G.remove_edge((2, 2), (3, 2))
245
- nx.draw_spectral(G, **options, ax=axs[i])
246
- elif i == 4:
247
- G.remove_edge((2, 3), (3, 3))
248
- nx.draw_spectral(G, **options, ax=axs[i])
249
- elif i == 5:
250
- G.remove_edge((1, 2), (1, 3))
251
- nx.draw_spectral(G, **options, ax=axs[i])
252
- elif i == 6:
253
- G.remove_edge((4, 2), (4, 3))
254
- nx.draw_spectral(G, **options, ax=axs[i])
255
-
256
- # Hide the last two subplots (8th and 9th)
257
  for j in range(7, 9):
258
- fig.delaxes(axs[j]) # Delete the extra axes
259
 
260
  st.pyplot(fig)
261
 
262
  elif option == "Create your own":
263
- # User can interactively modify the grid and see the results
264
  grid_size = st.slider("Choose grid size (n x n):", min_value=3, max_value=10, value=6)
265
  G_custom = nx.grid_2d_graph(grid_size, grid_size)
266
-
267
- # List all edges to allow removal
268
  all_edges = list(G_custom.edges())
269
 
270
- # Collect user input for edges to remove (before showing the "Generate" button)
271
  selected_edges_per_graph = []
272
- for i in range(7): # Loop over 7 graphs
273
- selected_edges = st.multiselect(f"Select edges to remove for graph {i+1}:",
274
- options=[str(edge) for edge in all_edges])
 
 
275
  selected_edges_per_graph.append(selected_edges)
276
 
277
- # Add "Generate" button after edge selection
278
  generate_button = st.button("Generate Graph")
279
 
280
  if generate_button:
281
  fig, axs = plt.subplots(3, 3, figsize=(12, 12))
282
  axs = axs.flatten()
283
 
284
- # Loop through each subplot and allow edge removal individually
285
- for i in range(7): # Loop over 7 graphs
286
  edges_to_remove = [tuple(eval(edge)) for edge in selected_edges_per_graph[i]]
287
-
288
- # Remove the selected edges
289
  G_custom_copy = G_custom.copy()
290
  G_custom_copy.remove_edges_from(edges_to_remove)
291
 
292
- # Draw the graph with removed edges and labels
293
- nx.draw_spectral(G_custom_copy, **{"node_color": "C0", "node_size": 100, "with_labels": True}, ax=axs[i])
294
 
295
- # Hide the last two subplots (8th and 9th)
296
  for j in range(7, 9):
297
- fig.delaxes(axs[j]) # Delete the extra axes
298
 
299
  st.pyplot(fig)
300
 
 
301
  # Display Drawing: Spectral Embedding if selected
302
  if sidebar_option == "Drawing: Spectral Embedding":
303
  display_spectral_embedding()
 
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
+ options = {"node_color": "C0", "node_size": 100}
 
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 > 0:
261
+ # Remove edges in sequence to modify the graph
262
+ edges_to_remove = [((2, 2), (2, 3)), ((3, 2), (3, 3)), ((2, 2), (3, 2)),
263
+ ((2, 3), (3, 3)), ((1, 2), (1, 3)), ((4, 2), (4, 3))]
264
+ if i <= len(edges_to_remove):
265
+ G.remove_edge(*edges_to_remove[i - 1])
266
+ draw_graph_with_arrows(G, axs[i], pos, labels, options)
267
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
268
  for j in range(7, 9):
269
+ fig.delaxes(axs[j]) # Delete unused subplots
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
+ pos = nx.spectral_layout(G_custom)
277
+ labels = {node: str(node) for node in G_custom.nodes()}
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
+ f"Select edges to remove for graph {i + 1}:",
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
+ for i in range(7):
 
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
+ draw_graph_with_arrows(G_custom_copy, axs[i], pos, labels, options)
 
300
 
 
301
  for j in range(7, 9):
302
+ fig.delaxes(axs[j]) # Delete unused subplots
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()