Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,7 +8,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"])
|
12 |
|
13 |
# Helper function to draw and display graph
|
14 |
def draw_graph(G, pos=None, title="Graph Visualization"):
|
@@ -149,8 +149,82 @@ def display_cluster_layout():
|
|
149 |
|
150 |
plt.tight_layout()
|
151 |
st.pyplot(plt)
|
152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
153 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
|
155 |
# Display Basic: Properties if selected
|
156 |
if sidebar_option == "Basic: Properties":
|
@@ -260,3 +334,7 @@ elif sidebar_option == "Drawing: Custom Node Position":
|
|
260 |
# Display Drawing: Cluster Layout if selected
|
261 |
elif sidebar_option == "Drawing: Cluster Layout":
|
262 |
display_cluster_layout()
|
|
|
|
|
|
|
|
|
|
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"):
|
|
|
149 |
|
150 |
plt.tight_layout()
|
151 |
st.pyplot(plt)
|
152 |
+
|
153 |
+
# Function to display Degree Analysis for Drawing: Degree Analysis
|
154 |
+
def display_degree_analysis():
|
155 |
+
st.title("Drawing: Degree Analysis")
|
156 |
+
|
157 |
+
option = st.radio("Choose a graph type:", ("Default Example", "Create your own"))
|
158 |
+
|
159 |
+
if option == "Default Example":
|
160 |
+
G = nx.gnp_random_graph(100, 0.02, seed=10374196)
|
161 |
|
162 |
+
degree_sequence = sorted((d for n, d in G.degree()), reverse=True)
|
163 |
+
dmax = max(degree_sequence)
|
164 |
+
|
165 |
+
fig = plt.figure("Degree of a random graph", figsize=(8, 8))
|
166 |
+
# Create a gridspec for adding subplots of different sizes
|
167 |
+
axgrid = fig.add_gridspec(5, 4)
|
168 |
+
|
169 |
+
ax0 = fig.add_subplot(axgrid[0:3, :])
|
170 |
+
Gcc = G.subgraph(sorted(nx.connected_components(G), key=len, reverse=True)[0])
|
171 |
+
pos = nx.spring_layout(Gcc, seed=10396953)
|
172 |
+
nx.draw_networkx_nodes(Gcc, pos, ax=ax0, node_size=20)
|
173 |
+
nx.draw_networkx_edges(Gcc, pos, ax=ax0, alpha=0.4)
|
174 |
+
ax0.set_title("Connected components of G")
|
175 |
+
ax0.set_axis_off()
|
176 |
+
|
177 |
+
ax1 = fig.add_subplot(axgrid[3:, :2])
|
178 |
+
ax1.plot(degree_sequence, "b-", marker="o")
|
179 |
+
ax1.set_title("Degree Rank Plot")
|
180 |
+
ax1.set_ylabel("Degree")
|
181 |
+
ax1.set_xlabel("Rank")
|
182 |
+
|
183 |
+
ax2 = fig.add_subplot(axgrid[3:, 2:])
|
184 |
+
ax2.bar(*np.unique(degree_sequence, return_counts=True))
|
185 |
+
ax2.set_title("Degree histogram")
|
186 |
+
ax2.set_xlabel("Degree")
|
187 |
+
ax2.set_ylabel("# of Nodes")
|
188 |
+
|
189 |
+
fig.tight_layout()
|
190 |
+
st.pyplot(fig)
|
191 |
+
|
192 |
+
elif option == "Create your own":
|
193 |
+
n_nodes = st.number_input("Number of nodes:", min_value=2, max_value=500, value=100)
|
194 |
+
p_edge = st.slider("Edge probability:", min_value=0.0, max_value=1.0, value=0.02)
|
195 |
+
|
196 |
+
if st.button("Generate"):
|
197 |
+
if n_nodes >= 2:
|
198 |
+
G_custom = nx.gnp_random_graph(n_nodes, p_edge, seed=10374196)
|
199 |
+
degree_sequence = sorted((d for n, d in G_custom.degree()), reverse=True)
|
200 |
+
dmax = max(degree_sequence)
|
201 |
+
|
202 |
+
fig = plt.figure("Degree of a random graph", figsize=(8, 8))
|
203 |
+
# Create a gridspec for adding subplots of different sizes
|
204 |
+
axgrid = fig.add_gridspec(5, 4)
|
205 |
+
|
206 |
+
ax0 = fig.add_subplot(axgrid[0:3, :])
|
207 |
+
Gcc = G_custom.subgraph(sorted(nx.connected_components(G_custom), key=len, reverse=True)[0])
|
208 |
+
pos = nx.spring_layout(Gcc, seed=10396953)
|
209 |
+
nx.draw_networkx_nodes(Gcc, pos, ax=ax0, node_size=20)
|
210 |
+
nx.draw_networkx_edges(Gcc, pos, ax=ax0, alpha=0.4)
|
211 |
+
ax0.set_title("Connected components of G")
|
212 |
+
ax0.set_axis_off()
|
213 |
+
|
214 |
+
ax1 = fig.add_subplot(axgrid[3:, :2])
|
215 |
+
ax1.plot(degree_sequence, "b-", marker="o")
|
216 |
+
ax1.set_title("Degree Rank Plot")
|
217 |
+
ax1.set_ylabel("Degree")
|
218 |
+
ax1.set_xlabel("Rank")
|
219 |
+
|
220 |
+
ax2 = fig.add_subplot(axgrid[3:, 2:])
|
221 |
+
ax2.bar(*np.unique(degree_sequence, return_counts=True))
|
222 |
+
ax2.set_title("Degree histogram")
|
223 |
+
ax2.set_xlabel("Degree")
|
224 |
+
ax2.set_ylabel("# of Nodes")
|
225 |
+
|
226 |
+
fig.tight_layout()
|
227 |
+
st.pyplot(fig)
|
228 |
|
229 |
# Display Basic: Properties if selected
|
230 |
if sidebar_option == "Basic: Properties":
|
|
|
334 |
# Display Drawing: Cluster Layout if selected
|
335 |
elif sidebar_option == "Drawing: Cluster Layout":
|
336 |
display_cluster_layout()
|
337 |
+
|
338 |
+
# Display Drawing: Degree Analysis if selected
|
339 |
+
elif sidebar_option == "Drawing: Degree Analysis":
|
340 |
+
display_degree_analysis()
|