shukdevdatta123 commited on
Commit
0de8ad2
·
verified ·
1 Parent(s): f414108

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -1
app.py CHANGED
@@ -14,7 +14,8 @@ sidebar_option = st.sidebar.radio("Select an option",
14
  "Drawing: Ego Graph", "Drawing: Eigenvalues", "Drawing: Four Grids",
15
  "Drawing: House With Colors", "Drawing: Labels And Colors",
16
  "Drawing: Multipartite Layout", "Drawing: Node Colormap",
17
- "Drawing: Rainbow Coloring"])
 
18
 
19
 
20
  # Helper function to draw and display graph
@@ -23,6 +24,89 @@ def draw_graph(G, pos=None, title="Graph Visualization"):
23
  nx.draw(G, pos=pos, with_labels=True, node_color='lightblue', node_size=500, font_size=10, font_weight='bold')
24
  st.pyplot(plt)
25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  # Function to display Drawing: Rainbow Coloring
27
  def display_rainbow_coloring():
28
  st.title("Drawing: Rainbow Coloring")
 
14
  "Drawing: Ego Graph", "Drawing: Eigenvalues", "Drawing: Four Grids",
15
  "Drawing: House With Colors", "Drawing: Labels And Colors",
16
  "Drawing: Multipartite Layout", "Drawing: Node Colormap",
17
+ "Drawing: Rainbow Coloring", "Drawing: Random Geometric Graph"])
18
+
19
 
20
 
21
  # Helper function to draw and display graph
 
24
  nx.draw(G, pos=pos, with_labels=True, node_color='lightblue', node_size=500, font_size=10, font_weight='bold')
25
  st.pyplot(plt)
26
 
27
+ # Function to display Drawing: Random Geometric Graph
28
+ def display_random_geometric_graph():
29
+ st.title("Drawing: Random Geometric Graph")
30
+
31
+ option = st.radio("Choose a graph type:", ("Default Example", "Create your own"))
32
+
33
+ if option == "Default Example":
34
+ # Default random geometric graph example
35
+ G = nx.random_geometric_graph(200, 0.125, seed=896803)
36
+ pos = nx.get_node_attributes(G, "pos")
37
+
38
+ # Find node near the center (0.5, 0.5)
39
+ dmin = 1
40
+ ncenter = 0
41
+ for n in pos:
42
+ x, y = pos[n]
43
+ d = (x - 0.5) ** 2 + (y - 0.5) ** 2
44
+ if d < dmin:
45
+ ncenter = n
46
+ dmin = d
47
+
48
+ # Color by path length from node near center
49
+ p = dict(nx.single_source_shortest_path_length(G, ncenter))
50
+
51
+ plt.figure(figsize=(8, 8))
52
+ nx.draw_networkx_edges(G, pos, alpha=0.4)
53
+ nx.draw_networkx_nodes(
54
+ G,
55
+ pos,
56
+ nodelist=list(p.keys()),
57
+ node_size=80,
58
+ node_color=list(p.values()),
59
+ cmap=plt.cm.Reds_r,
60
+ )
61
+
62
+ plt.xlim(-0.05, 1.05)
63
+ plt.ylim(-0.05, 1.05)
64
+ plt.axis("off")
65
+ st.pyplot(plt)
66
+
67
+ elif option == "Create your own":
68
+ # User can create their own random geometric graph
69
+ num_nodes = st.number_input("Number of nodes:", min_value=2, max_value=500, value=200)
70
+ distance = st.slider("Edge distance threshold (between 0 and 1):", 0.01, 1.0, 0.125)
71
+
72
+ if st.button("Generate Graph"):
73
+ # Generate the graph with user input
74
+ G = nx.random_geometric_graph(num_nodes, distance, seed=896803)
75
+ pos = nx.get_node_attributes(G, "pos")
76
+
77
+ # Find node near the center (0.5, 0.5)
78
+ dmin = 1
79
+ ncenter = 0
80
+ for n in pos:
81
+ x, y = pos[n]
82
+ d = (x - 0.5) ** 2 + (y - 0.5) ** 2
83
+ if d < dmin:
84
+ ncenter = n
85
+ dmin = d
86
+
87
+ # Color by path length from node near center
88
+ p = dict(nx.single_source_shortest_path_length(G, ncenter))
89
+
90
+ plt.figure(figsize=(8, 8))
91
+ nx.draw_networkx_edges(G, pos, alpha=0.4)
92
+ nx.draw_networkx_nodes(
93
+ G,
94
+ pos,
95
+ nodelist=list(p.keys()),
96
+ node_size=80,
97
+ node_color=list(p.values()),
98
+ cmap=plt.cm.Reds_r,
99
+ )
100
+
101
+ plt.xlim(-0.05, 1.05)
102
+ plt.ylim(-0.05, 1.05)
103
+ plt.axis("off")
104
+ st.pyplot(plt)
105
+
106
+ # Display Drawing: Random Geometric Graph if selected
107
+ if sidebar_option == "Drawing: Random Geometric Graph":
108
+ display_random_geometric_graph()
109
+
110
  # Function to display Drawing: Rainbow Coloring
111
  def display_rainbow_coloring():
112
  st.title("Drawing: Rainbow Coloring")