Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,13 +2,15 @@ import streamlit as st
|
|
2 |
import matplotlib.pyplot as plt
|
3 |
import networkx as nx
|
4 |
import numpy as np
|
|
|
5 |
|
6 |
# Sidebar for selecting an option
|
7 |
sidebar_option = st.sidebar.radio("Select an option",
|
8 |
["Select an option", "Basic: Properties",
|
9 |
"Basic: Read and write graphs", "Basic: Simple graph",
|
10 |
"Basic: Simple graph Directed", "Drawing: Custom Node Position",
|
11 |
-
"Drawing: Cluster Layout", "Drawing: Degree Analysis"
|
|
|
12 |
|
13 |
# Helper function to draw and display graph
|
14 |
def draw_graph(G, pos=None, title="Graph Visualization"):
|
@@ -226,8 +228,67 @@ def display_degree_analysis():
|
|
226 |
fig.tight_layout()
|
227 |
st.pyplot(fig)
|
228 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
229 |
# Display Basic: Properties if selected
|
230 |
-
|
231 |
st.title("Basic: Properties")
|
232 |
option = st.radio("Choose a graph type:", ("Default Example", "Create your own"))
|
233 |
|
|
|
2 |
import matplotlib.pyplot as plt
|
3 |
import networkx as nx
|
4 |
import numpy as np
|
5 |
+
from operator import itemgetter
|
6 |
|
7 |
# Sidebar for selecting an option
|
8 |
sidebar_option = st.sidebar.radio("Select an option",
|
9 |
["Select an option", "Basic: Properties",
|
10 |
"Basic: Read and write graphs", "Basic: Simple graph",
|
11 |
"Basic: Simple graph Directed", "Drawing: Custom Node Position",
|
12 |
+
"Drawing: Cluster Layout", "Drawing: Degree Analysis",
|
13 |
+
"Drawing: Ego Graph"])
|
14 |
|
15 |
# Helper function to draw and display graph
|
16 |
def draw_graph(G, pos=None, title="Graph Visualization"):
|
|
|
228 |
fig.tight_layout()
|
229 |
st.pyplot(fig)
|
230 |
|
231 |
+
# Function to display Ego Graph for Drawing: Ego Graph
|
232 |
+
def display_ego_graph():
|
233 |
+
st.title("Drawing: Ego Graph")
|
234 |
+
|
235 |
+
option = st.radio("Choose a graph type:", ("Default Example", "Create your own"))
|
236 |
+
|
237 |
+
if option == "Default Example":
|
238 |
+
# Create a BA model graph - use seed for reproducibility
|
239 |
+
n = 1000
|
240 |
+
m = 2
|
241 |
+
seed = 20532
|
242 |
+
G = nx.barabasi_albert_graph(n, m, seed=seed)
|
243 |
+
|
244 |
+
# Find node with largest degree
|
245 |
+
node_and_degree = G.degree()
|
246 |
+
(largest_hub, degree) = sorted(node_and_degree, key=itemgetter(1))[-1]
|
247 |
+
|
248 |
+
# Create ego graph of main hub
|
249 |
+
hub_ego = nx.ego_graph(G, largest_hub)
|
250 |
+
|
251 |
+
# Draw graph
|
252 |
+
pos = nx.spring_layout(hub_ego, seed=seed) # Seed layout for reproducibility
|
253 |
+
nx.draw(hub_ego, pos, node_color="b", node_size=50, with_labels=False)
|
254 |
+
|
255 |
+
# Draw ego as large and red
|
256 |
+
options = {"node_size": 300, "node_color": "r"}
|
257 |
+
nx.draw_networkx_nodes(hub_ego, pos, nodelist=[largest_hub], **options)
|
258 |
+
plt.tight_layout()
|
259 |
+
st.pyplot(plt)
|
260 |
+
|
261 |
+
elif option == "Create your own":
|
262 |
+
n_nodes = st.number_input("Number of nodes:", min_value=2, max_value=1000, value=100)
|
263 |
+
m_edges = st.number_input("Edges per node:", min_value=1, max_value=10, value=2)
|
264 |
+
|
265 |
+
if st.button("Generate"):
|
266 |
+
if n_nodes >= 2:
|
267 |
+
G_custom = nx.barabasi_albert_graph(n_nodes, m_edges, seed=20532)
|
268 |
+
|
269 |
+
# Find node with largest degree
|
270 |
+
node_and_degree = G_custom.degree()
|
271 |
+
(largest_hub, degree) = sorted(node_and_degree, key=itemgetter(1))[-1]
|
272 |
+
|
273 |
+
# Create ego graph of main hub
|
274 |
+
hub_ego = nx.ego_graph(G_custom, largest_hub)
|
275 |
+
|
276 |
+
# Draw graph
|
277 |
+
pos = nx.spring_layout(hub_ego, seed=20532) # Seed layout for reproducibility
|
278 |
+
nx.draw(hub_ego, pos, node_color="b", node_size=50, with_labels=False)
|
279 |
+
|
280 |
+
# Draw ego as large and red
|
281 |
+
options = {"node_size": 300, "node_color": "r"}
|
282 |
+
nx.draw_networkx_nodes(hub_ego, pos, nodelist=[largest_hub], **options)
|
283 |
+
plt.tight_layout()
|
284 |
+
st.pyplot(plt)
|
285 |
+
|
286 |
+
# Display Drawing: Ego Graph if selected
|
287 |
+
if sidebar_option == "Drawing: Ego Graph":
|
288 |
+
display_ego_graph()
|
289 |
+
|
290 |
# Display Basic: Properties if selected
|
291 |
+
elif sidebar_option == "Basic: Properties":
|
292 |
st.title("Basic: Properties")
|
293 |
option = st.radio("Choose a graph type:", ("Default Example", "Create your own"))
|
294 |
|