Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,157 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import igraph as ig
|
2 |
+
import plotly.graph_objects as go
|
3 |
+
|
4 |
+
# Let's create a list of edges with a binary tree structure up to depth 6
|
5 |
+
# A binary tree has a structure where each node has two children, often referred to as left and right child.
|
6 |
+
# We will assume that the topmost node (root) has an index 0.
|
7 |
+
|
8 |
+
class binNode():
|
9 |
+
def __init__(self, id) -> None:
|
10 |
+
self.id = id
|
11 |
+
self.child1 = None
|
12 |
+
self.child2 = None
|
13 |
+
|
14 |
+
def create_binary_tree_edges(depth):
|
15 |
+
edges = []
|
16 |
+
|
17 |
+
# build binary tree
|
18 |
+
id = 0
|
19 |
+
root = binNode(id)
|
20 |
+
prev = [root]
|
21 |
+
for _ in range(depth):
|
22 |
+
new_prev = []
|
23 |
+
for node in prev:
|
24 |
+
id += 1
|
25 |
+
node.child1 = binNode(id)
|
26 |
+
edges.append((node.id, node.child1.id))
|
27 |
+
id += 1
|
28 |
+
node.child2 = binNode(id)
|
29 |
+
edges.append((node.id, node.child2.id))
|
30 |
+
new_prev += [node.child1, node.child2]
|
31 |
+
prev = new_prev
|
32 |
+
|
33 |
+
return edges
|
34 |
+
|
35 |
+
|
36 |
+
# def create_binary_tree_edges(depth):
|
37 |
+
# edges = []
|
38 |
+
# current_index = 0 # Start with the root node
|
39 |
+
|
40 |
+
# # For each level of depth until the desired depth
|
41 |
+
# for level in range(depth):
|
42 |
+
# # Calculate the number of nodes at this level
|
43 |
+
# level_nodes = 2 ** level
|
44 |
+
|
45 |
+
# # For each node at this level, create edges to its two children
|
46 |
+
# for i in range(level_nodes):
|
47 |
+
# left_child_index = current_index + level_nodes
|
48 |
+
# right_child_index = current_index + level_nodes + 1
|
49 |
+
# edges.append((current_index, left_child_index))
|
50 |
+
# edges.append((current_index, right_child_index))
|
51 |
+
# current_index += 1
|
52 |
+
|
53 |
+
# # The current index is now at the next level, so increase by the number of nodes at this level
|
54 |
+
# current_index += level_nodes
|
55 |
+
|
56 |
+
# return edges
|
57 |
+
|
58 |
+
# # Create edges for a binary tree with depth of 6
|
59 |
+
# binary_tree_edges = create_binary_tree_edges(6)
|
60 |
+
# binary_tree_edges
|
61 |
+
|
62 |
+
|
63 |
+
def plot_tree_using_igraph():
|
64 |
+
# Define the edges in a tree structure
|
65 |
+
# edges = [(0, 1), (0, 2), (1, 3), (1, 4), (2, 5), (2, 6)]
|
66 |
+
edges = create_binary_tree_edges(3)
|
67 |
+
# edges = [(str(n1), str(n2)) for (n1, n2) in edges]
|
68 |
+
|
69 |
+
print(edges)
|
70 |
+
|
71 |
+
# Create an igraph Graph from the edge list
|
72 |
+
g = ig.Graph(edges, directed=True)
|
73 |
+
|
74 |
+
# Validate the root index
|
75 |
+
if g.vcount() > 0: # Check if the graph has any vertices
|
76 |
+
root_vertex_id = 0 # This assumes that vertex '0' is the root
|
77 |
+
else:
|
78 |
+
print("The graph has no vertices.")
|
79 |
+
return None
|
80 |
+
|
81 |
+
# Use the Reingold-Tilford layout to position the nodes
|
82 |
+
try:
|
83 |
+
layout = g.layout_reingold_tilford(root=None) # Correct root specification
|
84 |
+
except Exception as e:
|
85 |
+
print(f"Error computing layout: {e}")
|
86 |
+
return None
|
87 |
+
|
88 |
+
# Edge traces
|
89 |
+
edge_x = []
|
90 |
+
edge_y = []
|
91 |
+
for edge in edges:
|
92 |
+
start_idx, end_idx = edge
|
93 |
+
x0, y0 = layout.coords[start_idx]
|
94 |
+
x1, y1 = layout.coords[end_idx]
|
95 |
+
edge_x.extend([x0, x1, None])
|
96 |
+
edge_y.extend([-y0, -y1, None]) # y values are inverted to make the tree top-down
|
97 |
+
|
98 |
+
edge_trace = go.Scatter(
|
99 |
+
x=edge_x, y=edge_y,
|
100 |
+
line=dict(width=0.5, color='#888'),
|
101 |
+
hoverinfo='none',
|
102 |
+
mode='lines')
|
103 |
+
|
104 |
+
# Node traces
|
105 |
+
node_x = [pos[0] for pos in layout.coords]
|
106 |
+
node_y = [-pos[1] for pos in layout.coords] # y values are inverted
|
107 |
+
|
108 |
+
node_trace = go.Scatter(
|
109 |
+
x=node_x, y=node_y,
|
110 |
+
text=["Node {}".format(i) for i in range(len(layout.coords))],
|
111 |
+
mode='markers+text',
|
112 |
+
hoverinfo='text',
|
113 |
+
marker=dict(
|
114 |
+
showscale=False,
|
115 |
+
size=10,
|
116 |
+
color='LightSkyBlue'
|
117 |
+
),
|
118 |
+
textposition="bottom center"
|
119 |
+
)
|
120 |
+
|
121 |
+
# Create a Plotly figure
|
122 |
+
fig = go.Figure(data=[edge_trace, node_trace],
|
123 |
+
layout=go.Layout(
|
124 |
+
title='<b>Tree Layout with iGraph and Plotly</b>',
|
125 |
+
titlefont_size=16,
|
126 |
+
showlegend=False,
|
127 |
+
hovermode='closest',
|
128 |
+
margin=dict(b=0, l=0, r=0, t=50),
|
129 |
+
xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
|
130 |
+
yaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
|
131 |
+
height=600,
|
132 |
+
width=600,
|
133 |
+
annotations=[dict(
|
134 |
+
showarrow=False,
|
135 |
+
xref="paper", yref="paper",
|
136 |
+
x=0.005, y=-0.002 )]
|
137 |
+
))
|
138 |
+
|
139 |
+
return fig
|
140 |
+
|
141 |
+
# Try generating the figure
|
142 |
+
# fig = plot_tree_using_igraph()
|
143 |
+
# print(fig)
|
144 |
+
# # if fig is not None:
|
145 |
+
# # fig.show()
|
146 |
+
|
147 |
+
# from plotly.offline import plot
|
148 |
+
# plot(fig)
|
149 |
+
|
150 |
+
with gr.Blocks() as demo:
|
151 |
+
|
152 |
+
gr.Markdown("## Interactive Tree and Image Display")
|
153 |
+
with gr.Row():
|
154 |
+
tree_output = gr.Plot(plot_tree_using_igraph) # Connect the function directly
|
155 |
+
|
156 |
+
|
157 |
+
demo.launch()
|