Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -21,7 +21,8 @@ sidebar_option = st.sidebar.radio("Select an option",
|
|
21 |
"Drawing: Rainbow Coloring", "Drawing: Random Geometric Graph","Drawing: Self-loops",
|
22 |
"Drawing: Simple Path", "Drawing: Spectral Embedding", "Drawing: Traveling Salesman Problem",
|
23 |
"Drawing: Weighted Graph", "3D Drawing: Animations of 3D Rotation", "3D Drawing: Basic Matplotlib",
|
24 |
-
"Graph: DAG - Topological Layout", "Graph: Erdos Renyi", "Graph: Karate Club", "Graph: Minimum Spanning Tree"
|
|
|
25 |
|
26 |
# Helper function to draw and display graph
|
27 |
def draw_graph(G, pos=None, title="Graph Visualization"):
|
@@ -29,6 +30,117 @@ def draw_graph(G, pos=None, title="Graph Visualization"):
|
|
29 |
nx.draw(G, pos=pos, with_labels=True, node_color='lightblue', node_size=500, font_size=10, font_weight='bold')
|
30 |
st.pyplot(plt)
|
31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
def minimum_spanning_tree_graph():
|
33 |
st.title("Graph: Minimum Spanning Tree")
|
34 |
|
|
|
21 |
"Drawing: Rainbow Coloring", "Drawing: Random Geometric Graph","Drawing: Self-loops",
|
22 |
"Drawing: Simple Path", "Drawing: Spectral Embedding", "Drawing: Traveling Salesman Problem",
|
23 |
"Drawing: Weighted Graph", "3D Drawing: Animations of 3D Rotation", "3D Drawing: Basic Matplotlib",
|
24 |
+
"Graph: DAG - Topological Layout", "Graph: Erdos Renyi", "Graph: Karate Club", "Graph: Minimum Spanning Tree",
|
25 |
+
"Graph: Triads"])
|
26 |
|
27 |
# Helper function to draw and display graph
|
28 |
def draw_graph(G, pos=None, title="Graph Visualization"):
|
|
|
30 |
nx.draw(G, pos=pos, with_labels=True, node_color='lightblue', node_size=500, font_size=10, font_weight='bold')
|
31 |
st.pyplot(plt)
|
32 |
|
33 |
+
def triads_graph():
|
34 |
+
st.title("Graph: Triads")
|
35 |
+
|
36 |
+
# Sidebar selection for Default Example or Custom Triads
|
37 |
+
graph_mode = st.radio(
|
38 |
+
"Choose a Mode:",
|
39 |
+
("Default Example", "Create Your Own"),
|
40 |
+
help="Default example shows predefined triads, or you can create your own triads."
|
41 |
+
)
|
42 |
+
|
43 |
+
if graph_mode == "Default Example":
|
44 |
+
# Define the triads
|
45 |
+
triads = {
|
46 |
+
"003": [],
|
47 |
+
"012": [(1, 2)],
|
48 |
+
"102": [(1, 2), (2, 1)],
|
49 |
+
"021D": [(3, 1), (3, 2)],
|
50 |
+
"021U": [(1, 3), (2, 3)],
|
51 |
+
"021C": [(1, 3), (3, 2)],
|
52 |
+
"111D": [(1, 2), (2, 1), (3, 1)],
|
53 |
+
"111U": [(1, 2), (2, 1), (1, 3)],
|
54 |
+
"030T": [(1, 2), (3, 2), (1, 3)],
|
55 |
+
"030C": [(1, 3), (3, 2), (2, 1)],
|
56 |
+
"201": [(1, 2), (2, 1), (3, 1), (1, 3)],
|
57 |
+
"120D": [(1, 2), (2, 1), (3, 1), (3, 2)],
|
58 |
+
"120U": [(1, 2), (2, 1), (1, 3), (2, 3)],
|
59 |
+
"120C": [(1, 2), (2, 1), (1, 3), (3, 2)],
|
60 |
+
"210": [(1, 2), (2, 1), (1, 3), (3, 2), (2, 3)],
|
61 |
+
"300": [(1, 2), (2, 1), (2, 3), (3, 2), (1, 3), (3, 1)],
|
62 |
+
}
|
63 |
+
|
64 |
+
fig, axes = plt.subplots(4, 4, figsize=(10, 10))
|
65 |
+
|
66 |
+
for (title, triad), ax in zip(triads.items(), axes.flatten()):
|
67 |
+
G = nx.DiGraph()
|
68 |
+
G.add_nodes_from([1, 2, 3])
|
69 |
+
G.add_edges_from(triad)
|
70 |
+
nx.draw_networkx(
|
71 |
+
G,
|
72 |
+
ax=ax,
|
73 |
+
with_labels=False,
|
74 |
+
node_color=["green"],
|
75 |
+
node_size=200,
|
76 |
+
arrowsize=20,
|
77 |
+
width=2,
|
78 |
+
pos=nx.planar_layout(G),
|
79 |
+
)
|
80 |
+
ax.set_xlim(val * 1.2 for val in ax.get_xlim())
|
81 |
+
ax.set_ylim(val * 1.2 for val in ax.get_ylim())
|
82 |
+
ax.text(
|
83 |
+
0,
|
84 |
+
0,
|
85 |
+
title,
|
86 |
+
fontsize=15,
|
87 |
+
fontweight="extra bold",
|
88 |
+
horizontalalignment="center",
|
89 |
+
bbox={"boxstyle": "square,pad=0.3", "fc": "none"},
|
90 |
+
)
|
91 |
+
fig.tight_layout()
|
92 |
+
st.pyplot(fig)
|
93 |
+
|
94 |
+
elif graph_mode == "Create Your Own":
|
95 |
+
st.write("### Create Your Own Triads")
|
96 |
+
|
97 |
+
# Let user define custom triads
|
98 |
+
triad_input = st.text_area(
|
99 |
+
"Enter the edges for your triad (e.g., [(1, 2), (2, 1), (3, 1)])",
|
100 |
+
value="[(1, 2), (2, 1), (3, 1)]"
|
101 |
+
)
|
102 |
+
|
103 |
+
# Try to evaluate the input as a list of edges
|
104 |
+
try:
|
105 |
+
custom_triad = eval(triad_input)
|
106 |
+
if isinstance(custom_triad, list) and all(isinstance(edge, tuple) and len(edge) == 2 for edge in custom_triad):
|
107 |
+
G = nx.DiGraph()
|
108 |
+
G.add_nodes_from([1, 2, 3])
|
109 |
+
G.add_edges_from(custom_triad)
|
110 |
+
|
111 |
+
# Plot the graph
|
112 |
+
fig, ax = plt.subplots(figsize=(5, 5))
|
113 |
+
nx.draw_networkx(
|
114 |
+
G,
|
115 |
+
ax=ax,
|
116 |
+
with_labels=False,
|
117 |
+
node_color=["green"],
|
118 |
+
node_size=200,
|
119 |
+
arrowsize=20,
|
120 |
+
width=2,
|
121 |
+
pos=nx.planar_layout(G),
|
122 |
+
)
|
123 |
+
ax.set_xlim(val * 1.2 for val in ax.get_xlim())
|
124 |
+
ax.set_ylim(val * 1.2 for val in ax.get_ylim())
|
125 |
+
ax.text(
|
126 |
+
0,
|
127 |
+
0,
|
128 |
+
"Custom Triad",
|
129 |
+
fontsize=15,
|
130 |
+
fontweight="extra bold",
|
131 |
+
horizontalalignment="center",
|
132 |
+
bbox={"boxstyle": "square,pad=0.3", "fc": "none"},
|
133 |
+
)
|
134 |
+
st.pyplot(fig)
|
135 |
+
else:
|
136 |
+
st.error("Invalid format. Please enter a list of edges in the format [(1, 2), (2, 1), ...].")
|
137 |
+
except Exception as e:
|
138 |
+
st.error(f"Error parsing input: {e}")
|
139 |
+
|
140 |
+
# Display the corresponding page based on sidebar option
|
141 |
+
if sidebar_option == "Graph: Triads":
|
142 |
+
triads_graph()
|
143 |
+
|
144 |
def minimum_spanning_tree_graph():
|
145 |
st.title("Graph: Minimum Spanning Tree")
|
146 |
|