shukdevdatta123 commited on
Commit
8ae1a9a
·
verified ·
1 Parent(s): ec08d04

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py CHANGED
@@ -157,6 +157,53 @@ def display_cluster_layout():
157
  st.write("For custom graph creation, you can specify the number of nodes and edges.")
158
  # Here you can add functionality for custom graph creation if required
159
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
160
  # Display Basic: Properties if selected
161
  if sidebar_option == "Basic: Properties":
162
  st.title("Basic: Properties")
 
157
  st.write("For custom graph creation, you can specify the number of nodes and edges.")
158
  # Here you can add functionality for custom graph creation if required
159
 
160
+ # Input for the number of nodes
161
+ num_nodes = st.number_input("Number of nodes:", min_value=2, max_value=100, value=5)
162
+
163
+ # Input for edges: users can input edges manually
164
+ edge_input = st.text_area("Enter edges (format: u,v):", value="0,1\n1,2\n2,3")
165
+
166
+ # Button to generate the graph
167
+ if st.button("Generate Graph"):
168
+ edges = []
169
+ if edge_input:
170
+ # Parse the input string to create the edges
171
+ edge_list = edge_input.split("\n")
172
+ for edge in edge_list:
173
+ try:
174
+ u, v = map(int, edge.split(","))
175
+ edges.append((u, v))
176
+ except ValueError:
177
+ st.error("Invalid edge format. Please use the format 'u,v'.")
178
+
179
+ # Create a graph and add the edges
180
+ G_custom = nx.Graph()
181
+ G_custom.add_edges_from(edges)
182
+
183
+ if len(G_custom.nodes) == 0:
184
+ st.error("No nodes found. Please ensure you have specified edges correctly.")
185
+ else:
186
+ # Compute clusters (communities) and layout
187
+ communities = list(nx.community.greedy_modularity_communities(G_custom))
188
+
189
+ # Compute positions for the node clusters as if they were themselves nodes in a supergraph using a larger scale factor
190
+ supergraph = nx.cycle_graph(len(communities))
191
+ superpos = nx.spring_layout(G_custom, scale=50, seed=429)
192
+
193
+ # Use the "supernode" positions as the center of each node cluster
194
+ centers = list(superpos.values())
195
+ pos = {}
196
+ for center, comm in zip(centers, communities):
197
+ pos.update(nx.spring_layout(nx.subgraph(G_custom, comm), center=center, seed=1430))
198
+
199
+ # Nodes colored by cluster
200
+ for nodes, clr in zip(communities, ("tab:blue", "tab:orange", "tab:green")):
201
+ nx.draw_networkx_nodes(G_custom, pos=pos, nodelist=nodes, node_color=clr, node_size=100)
202
+ nx.draw_networkx_edges(G_custom, pos=pos)
203
+
204
+ plt.tight_layout()
205
+ st.pyplot(plt)
206
+
207
  # Display Basic: Properties if selected
208
  if sidebar_option == "Basic: Properties":
209
  st.title("Basic: Properties")