Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -20,8 +20,7 @@ sidebar_option = st.sidebar.radio("Select an option",
|
|
20 |
"Drawing: Multipartite Layout", "Drawing: Node Colormap",
|
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 |
-
"Graphviz Layout: Decomposition"])
|
25 |
|
26 |
# Helper function to draw and display graph
|
27 |
def draw_graph(G, pos=None, title="Graph Visualization"):
|
@@ -29,100 +28,6 @@ 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 |
-
# Default example code
|
33 |
-
def default_example_decomposition():
|
34 |
-
# Create a Directed Graph (Bayesian Network)
|
35 |
-
B = nx.DiGraph()
|
36 |
-
B.add_nodes_from(["A", "B", "C", "D", "E", "F"])
|
37 |
-
B.add_edges_from(
|
38 |
-
[("A", "B"), ("A", "C"), ("B", "D"), ("B", "F"), ("C", "E"), ("E", "F")]
|
39 |
-
)
|
40 |
-
|
41 |
-
options = {"with_labels": True, "node_color": "white", "edgecolors": "blue"}
|
42 |
-
|
43 |
-
fig = plt.figure(figsize=(6, 9))
|
44 |
-
axgrid = fig.add_gridspec(3, 2)
|
45 |
-
|
46 |
-
ax1 = fig.add_subplot(axgrid[0, 0])
|
47 |
-
ax1.set_title("Bayesian Network")
|
48 |
-
pos = nx.nx_pydot.pydot_layout(B, prog="neato")
|
49 |
-
nx.draw_networkx(B, pos=pos, **options)
|
50 |
-
|
51 |
-
mg = nx.moral_graph(B)
|
52 |
-
ax2 = fig.add_subplot(axgrid[0, 1], sharex=ax1, sharey=ax1)
|
53 |
-
ax2.set_title("Moralized Graph")
|
54 |
-
nx.draw_networkx(mg, pos=pos, **options)
|
55 |
-
|
56 |
-
jt = nx.junction_tree(B)
|
57 |
-
ax3 = fig.add_subplot(axgrid[1:, :])
|
58 |
-
ax3.set_title("Junction Tree")
|
59 |
-
ax3.margins(0.15, 0.25)
|
60 |
-
nsize = [2000 * len(n) for n in list(jt.nodes())]
|
61 |
-
pos = nx.nx_pydot.pydot_layout(jt, prog="neato")
|
62 |
-
nx.draw_networkx(jt, pos=pos, node_size=nsize, **options)
|
63 |
-
|
64 |
-
plt.tight_layout()
|
65 |
-
st.pyplot(fig)
|
66 |
-
|
67 |
-
# Create your own graph option
|
68 |
-
def create_own_graph_decomposition():
|
69 |
-
# Input fields to customize the graph
|
70 |
-
nodes = st.text_area("Enter nodes (comma separated)", "A, B, C, D, E, F")
|
71 |
-
edges = st.text_area("Enter edges (comma separated)", "A,B;A,C;B,D;B,F;C,E;E,F")
|
72 |
-
|
73 |
-
# Add a button to generate the graph
|
74 |
-
generate_button = st.button("Generate Graph")
|
75 |
-
|
76 |
-
if generate_button:
|
77 |
-
# Generate graph and layout
|
78 |
-
G = nx.DiGraph()
|
79 |
-
node_list = nodes.split(",")
|
80 |
-
edge_list = edges.split(";")
|
81 |
-
G.add_nodes_from(node_list)
|
82 |
-
for edge in edge_list:
|
83 |
-
G.add_edge(*edge.split(","))
|
84 |
-
|
85 |
-
options = {"with_labels": True, "node_color": "white", "edgecolors": "blue"}
|
86 |
-
fig = plt.figure(figsize=(6, 9))
|
87 |
-
axgrid = fig.add_gridspec(3, 2)
|
88 |
-
|
89 |
-
ax1 = fig.add_subplot(axgrid[0, 0])
|
90 |
-
ax1.set_title("Bayesian Network")
|
91 |
-
pos = nx.nx_pydot.pydot_layout(G, prog="neato")
|
92 |
-
nx.draw_networkx(G, pos=pos, **options)
|
93 |
-
|
94 |
-
mg = nx.moral_graph(G)
|
95 |
-
ax2 = fig.add_subplot(axgrid[0, 1], sharex=ax1, sharey=ax1)
|
96 |
-
ax2.set_title("Moralized Graph")
|
97 |
-
nx.draw_networkx(mg, pos=pos, **options)
|
98 |
-
|
99 |
-
jt = nx.junction_tree(G)
|
100 |
-
ax3 = fig.add_subplot(axgrid[1:, :])
|
101 |
-
ax3.set_title("Junction Tree")
|
102 |
-
ax3.margins(0.15, 0.25)
|
103 |
-
nsize = [2000 * len(n) for n in list(jt.nodes())]
|
104 |
-
pos = nx.nx_pydot.pydot_layout(jt, prog="neato")
|
105 |
-
nx.draw_networkx(jt, pos=pos, node_size=nsize, **options)
|
106 |
-
|
107 |
-
plt.tight_layout()
|
108 |
-
st.pyplot(fig)
|
109 |
-
|
110 |
-
if sidebar_option == "Graphviz Layout: Decomposition":
|
111 |
-
st.title("Graphviz Layout: Decomposition")
|
112 |
-
|
113 |
-
# Provide options for Default Example or Custom Graph
|
114 |
-
graph_mode = st.radio(
|
115 |
-
"Choose a Mode:",
|
116 |
-
("Default Example", "Create Your Own"),
|
117 |
-
help="Default example shows a decomposition of a Bayesian network, or you can create your own custom graph."
|
118 |
-
)
|
119 |
-
|
120 |
-
# Display the chosen option
|
121 |
-
if graph_mode == "Default Example":
|
122 |
-
default_example_decomposition()
|
123 |
-
elif graph_mode == "Create Your Own":
|
124 |
-
create_own_graph_decomposition()
|
125 |
-
|
126 |
if sidebar_option == "3D Drawing: Animations of 3D Rotation":
|
127 |
st.title("3D Drawing: Animations of 3D Rotation")
|
128 |
|
|
|
20 |
"Drawing: Multipartite Layout", "Drawing: Node Colormap",
|
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 |
|
25 |
# Helper function to draw and display graph
|
26 |
def draw_graph(G, pos=None, title="Graph Visualization"):
|
|
|
28 |
nx.draw(G, pos=pos, with_labels=True, node_color='lightblue', node_size=500, font_size=10, font_weight='bold')
|
29 |
st.pyplot(plt)
|
30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
if sidebar_option == "3D Drawing: Animations of 3D Rotation":
|
32 |
st.title("3D Drawing: Animations of 3D Rotation")
|
33 |
|