Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -20,18 +20,30 @@ def draw_graph(G, pos=None, title="Graph Visualization"):
|
|
20 |
nx.draw(G, pos=pos, with_labels=True, node_color='lightblue', node_size=500, font_size=10, font_weight='bold')
|
21 |
st.pyplot(plt)
|
22 |
|
23 |
-
# Function
|
24 |
def multilayered_graph(*subset_sizes):
|
25 |
-
extents = nx.utils.pairwise(it.accumulate((0,) + subset_sizes))
|
26 |
-
layers = [range(start, end) for start, end in extents]
|
27 |
G = nx.Graph()
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
return G
|
33 |
|
34 |
-
# Function to display
|
35 |
def display_multipartite_layout():
|
36 |
st.title("Drawing: Multipartite Layout")
|
37 |
|
@@ -64,25 +76,27 @@ def display_multipartite_layout():
|
|
64 |
subset_colors_input = st.text_area("Enter subset colors (comma-separated, e.g., gold,violet,green):", value="gold,violet,violet,violet,violet,limegreen,limegreen,darkorange")
|
65 |
subset_colors = subset_colors_input.split(',')
|
66 |
|
67 |
-
#
|
68 |
-
if len(subset_sizes)
|
69 |
-
# Generate and plot multipartite graph
|
70 |
-
G = multilayered_graph(*subset_sizes)
|
71 |
-
color = [subset_colors[data["layer"]] for v, data in G.nodes(data=True)]
|
72 |
-
pos = nx.multipartite_layout(G, subset_key="layer")
|
73 |
-
|
74 |
-
plt.figure(figsize=(8, 8))
|
75 |
-
nx.draw(G, pos, node_color=color, with_labels=False)
|
76 |
-
plt.axis("equal")
|
77 |
-
st.pyplot(plt)
|
78 |
-
else:
|
79 |
st.error("The number of colors should match the number of subsets.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
|
81 |
# Display Drawing: Multipartite Layout if selected
|
82 |
if sidebar_option == "Drawing: Multipartite Layout":
|
83 |
display_multipartite_layout()
|
84 |
|
85 |
-
# Function to display
|
86 |
def display_labels_and_colors():
|
87 |
st.title("Drawing: Labels And Colors")
|
88 |
|
@@ -135,29 +149,31 @@ def display_labels_and_colors():
|
|
135 |
edges = st.text_area("Enter edges (format: node1-node2, comma-separated, e.g., a-b,b-c):", value="a-b,b-c,c-d")
|
136 |
edge_list = [tuple(edge.split('-')) for edge in edges.split(',')]
|
137 |
|
138 |
-
# Generate graph based on user input
|
139 |
-
G_custom = nx.Graph()
|
140 |
-
G_custom.add_nodes_from(node_labels)
|
141 |
-
G_custom.add_edges_from(edge_list)
|
142 |
-
|
143 |
# Let user choose colors for nodes and edges
|
144 |
node_color = st.color_picker("Pick a color for nodes:", "#FF6347")
|
145 |
edge_color = st.color_picker("Pick a color for edges:", "#4682B4")
|
146 |
-
|
147 |
-
# Generate layout for the nodes
|
148 |
-
pos_custom = nx.spring_layout(G_custom)
|
149 |
|
150 |
-
#
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
nx.draw_networkx_labels(G_custom, pos_custom, labels=custom_labels, font_size=22, font_color="whitesmoke")
|
157 |
|
158 |
-
|
159 |
-
|
160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
161 |
|
162 |
# Display Drawing: Labels And Colors if selected
|
163 |
if sidebar_option == "Drawing: Labels And Colors":
|
|
|
20 |
nx.draw(G, pos=pos, with_labels=True, node_color='lightblue', node_size=500, font_size=10, font_weight='bold')
|
21 |
st.pyplot(plt)
|
22 |
|
23 |
+
# Function to create a multipartite graph
|
24 |
def multilayered_graph(*subset_sizes):
|
|
|
|
|
25 |
G = nx.Graph()
|
26 |
+
layers = len(subset_sizes)
|
27 |
+
node_id = 0
|
28 |
+
|
29 |
+
# Create nodes for each subset and add edges between nodes in adjacent layers
|
30 |
+
for i, size in enumerate(subset_sizes):
|
31 |
+
for j in range(size):
|
32 |
+
G.add_node(node_id, layer=i) # Assign a layer attribute
|
33 |
+
node_id += 1
|
34 |
+
|
35 |
+
# Add edges between nodes in adjacent layers
|
36 |
+
node_ids = list(G.nodes())
|
37 |
+
for i in range(layers - 1):
|
38 |
+
layer_nodes = [node for node in node_ids if G.nodes[node]["layer"] == i]
|
39 |
+
next_layer_nodes = [node for node in node_ids if G.nodes[node]["layer"] == i + 1]
|
40 |
+
for node in layer_nodes:
|
41 |
+
for next_node in next_layer_nodes:
|
42 |
+
G.add_edge(node, next_node)
|
43 |
+
|
44 |
return G
|
45 |
|
46 |
+
# Function to display Multipartite Layout
|
47 |
def display_multipartite_layout():
|
48 |
st.title("Drawing: Multipartite Layout")
|
49 |
|
|
|
76 |
subset_colors_input = st.text_area("Enter subset colors (comma-separated, e.g., gold,violet,green):", value="gold,violet,violet,violet,violet,limegreen,limegreen,darkorange")
|
77 |
subset_colors = subset_colors_input.split(',')
|
78 |
|
79 |
+
# Check if the number of colors matches the number of subsets
|
80 |
+
if len(subset_sizes) != len(subset_colors):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
st.error("The number of colors should match the number of subsets.")
|
82 |
+
else:
|
83 |
+
# Add a button to generate the graph
|
84 |
+
if st.button("Generate Graph"):
|
85 |
+
# Generate and plot multipartite graph
|
86 |
+
G = multilayered_graph(*subset_sizes)
|
87 |
+
color = [subset_colors[data["layer"]] for v, data in G.nodes(data=True)]
|
88 |
+
pos = nx.multipartite_layout(G, subset_key="layer")
|
89 |
+
|
90 |
+
plt.figure(figsize=(8, 8))
|
91 |
+
nx.draw(G, pos, node_color=color, with_labels=False)
|
92 |
+
plt.axis("equal")
|
93 |
+
st.pyplot(plt)
|
94 |
|
95 |
# Display Drawing: Multipartite Layout if selected
|
96 |
if sidebar_option == "Drawing: Multipartite Layout":
|
97 |
display_multipartite_layout()
|
98 |
|
99 |
+
# Function to display Labels and Colors
|
100 |
def display_labels_and_colors():
|
101 |
st.title("Drawing: Labels And Colors")
|
102 |
|
|
|
149 |
edges = st.text_area("Enter edges (format: node1-node2, comma-separated, e.g., a-b,b-c):", value="a-b,b-c,c-d")
|
150 |
edge_list = [tuple(edge.split('-')) for edge in edges.split(',')]
|
151 |
|
|
|
|
|
|
|
|
|
|
|
152 |
# Let user choose colors for nodes and edges
|
153 |
node_color = st.color_picker("Pick a color for nodes:", "#FF6347")
|
154 |
edge_color = st.color_picker("Pick a color for edges:", "#4682B4")
|
|
|
|
|
|
|
155 |
|
156 |
+
# Add a button to generate the graph
|
157 |
+
if st.button("Generate Graph"):
|
158 |
+
# Generate graph based on user input
|
159 |
+
G_custom = nx.Graph()
|
160 |
+
G_custom.add_nodes_from(node_labels)
|
161 |
+
G_custom.add_edges_from(edge_list)
|
|
|
162 |
|
163 |
+
# Generate layout for the nodes
|
164 |
+
pos_custom = nx.spring_layout(G_custom)
|
165 |
+
|
166 |
+
# Draw the graph
|
167 |
+
nx.draw_networkx_nodes(G_custom, pos_custom, node_color=node_color, node_size=800, edgecolors="gray", alpha=0.9)
|
168 |
+
nx.draw_networkx_edges(G_custom, pos_custom, edge_color=edge_color, width=2, alpha=0.7)
|
169 |
+
|
170 |
+
# Create custom labels
|
171 |
+
custom_labels = {node: f"${node}$" for node in node_labels}
|
172 |
+
nx.draw_networkx_labels(G_custom, pos_custom, labels=custom_labels, font_size=22, font_color="whitesmoke")
|
173 |
+
|
174 |
+
plt.tight_layout()
|
175 |
+
plt.axis("off")
|
176 |
+
st.pyplot(plt)
|
177 |
|
178 |
# Display Drawing: Labels And Colors if selected
|
179 |
if sidebar_option == "Drawing: Labels And Colors":
|