harishB97 commited on
Commit
171b613
·
verified ·
1 Parent(s): a332c13

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +158 -8
app.py CHANGED
@@ -7,7 +7,10 @@ import igraph as ig
7
 
8
  # print(os.pwd())
9
 
10
- species_to_imgpath = {'bird': './descendent_specific_topk_heatmap_withbb_ep=last_024+051'}
 
 
 
11
 
12
  # this has to be there for each species
13
  imgname_to_filepath = {} # this ignores the extension such as .png
@@ -184,10 +187,111 @@ def plot_tree_using_igraph():
184
 
185
  return fig
186
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
187
 
188
  def get_protoIDs(nodename):
189
  return gr.Dropdown(choices=nodename_to_protoIDs[nodename], interactive=True)
190
 
 
 
191
 
192
  def get_image(nodename, protoID):
193
  imgname = '-'.join([nodename, protoID]) + '.png'
@@ -202,26 +306,72 @@ with gr.Blocks() as demo:
202
  ROOT = get_tree(imgpath)
203
  print(ROOT.name)
204
  gr.Markdown("## Interactive Tree and Image Display")
205
- # with gr.Row():
206
- # dropdown_species = gr.Dropdown(label="Select a species", choices=list(species_to_imgpath.keys()))
207
 
208
- with gr.Row():
209
- tree_output = gr.Plot(plot_tree_using_igraph) # Connect the function directly
210
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
211
  with gr.Row():
212
  with gr.Column():
213
- dropdown_1_nodename = gr.Dropdown(label="Select a node name", choices=list(nodename_to_protoIDs.keys()))
214
  dropdown_1_protos = gr.Dropdown(label="Select a prototype ID", choices=[], allow_custom_value=True)
215
  image_output_1 = gr.Image()
216
  with gr.Column():
217
- dropdown_2_nodename = gr.Dropdown(label="Select a node name", choices=list(nodename_to_protoIDs.keys()))
218
  dropdown_2_protos = gr.Dropdown(label="Select a prototype ID", choices=[], allow_custom_value=True)
219
  image_output_2 = gr.Image()
220
-
 
 
 
221
  dropdown_1_nodename.change(get_protoIDs, dropdown_1_nodename, dropdown_1_protos)
222
  dropdown_1_protos.change(get_image, [dropdown_1_nodename, dropdown_1_protos], image_output_1)
223
  dropdown_2_nodename.change(get_protoIDs, dropdown_2_nodename, dropdown_2_protos)
224
  dropdown_2_protos.change(get_image, [dropdown_2_nodename, dropdown_2_protos], image_output_2)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
225
 
226
 
227
  # Initialize with placeholder images
 
7
 
8
  # print(os.pwd())
9
 
10
+ species_to_imgpath = {'bird': './descendent_specific_topk_heatmap_withbb_ep=last_024+051',
11
+ 'fish': './descendent_specific_topk_heatmap_withbb_ep=last_024+051',
12
+ 'butterfly': './descendent_specific_topk_heatmap_withbb_ep=last_024+051',
13
+ }
14
 
15
  # this has to be there for each species
16
  imgname_to_filepath = {} # this ignores the extension such as .png
 
187
 
188
  return fig
189
 
190
+ def plot_tree_from_species(species_name):
191
+ # Define the edges in a tree structure
192
+ # edges = [(0, 1), (0, 2), (1, 3), (1, 4), (2, 5), (2, 6)]
193
+
194
+ imgpath = species_to_imgpath[species_name]
195
+ print(imgpath)
196
+ root = get_tree(imgpath)
197
+
198
+ # root = ROOT
199
+ edges = create_binary_tree_edges(root)
200
+ # edges = [(str(n1), str(n2)) for (n1, n2) in edges]
201
+
202
+ # print(edges)
203
+
204
+ # Create an igraph Graph from the edge list
205
+ g = ig.Graph(edges, directed=True)
206
+
207
+ # Validate the root index
208
+ if g.vcount() > 0: # Check if the graph has any vertices
209
+ root_vertex_id = 0 # This assumes that vertex '0' is the root
210
+ else:
211
+ print("The graph has no vertices.")
212
+ return None
213
+
214
+ # Use the Reingold-Tilford layout to position the nodes
215
+ try:
216
+ layout = g.layout_reingold_tilford(root=None) # Correct root specification
217
+ except Exception as e:
218
+ print(f"Error computing layout: {e}")
219
+ return None
220
+
221
+ # Edge traces
222
+ edge_x = []
223
+ edge_y = []
224
+ for edge in edges:
225
+ start_idx, end_idx = edge
226
+ x0, y0 = layout.coords[start_idx]
227
+ x1, y1 = layout.coords[end_idx]
228
+ edge_x.extend([x0, x1, None])
229
+ edge_y.extend([-y0, -y1, None]) # y values are inverted to make the tree top-down
230
+
231
+ edge_trace = go.Scatter(
232
+ x=edge_x, y=edge_y,
233
+ line=dict(width=0.5, color='#888'),
234
+ hoverinfo='none',
235
+ mode='lines')
236
+
237
+ # Node traces
238
+ node_x = [pos[0] for pos in layout.coords]
239
+ node_y = [-pos[1] for pos in layout.coords] # y values are inverted
240
+
241
+ node_trace = go.Scatter(
242
+ x=node_x, y=node_y,
243
+ text=[id_to_node[i].name for i in range(len(layout.coords))],
244
+ # text=["Node {}".format(i) for i in range(len(layout.coords))],
245
+ mode='markers+text',
246
+ hoverinfo='text',
247
+ marker=dict(
248
+ showscale=False,
249
+ size=10,
250
+ color='LightSkyBlue'
251
+ ),
252
+ textposition="bottom center"
253
+ )
254
+
255
+ # Create a Plotly figure
256
+ fig = go.Figure(data=[edge_trace, node_trace],
257
+ layout=go.Layout(
258
+ title='<b>Tree Layout with iGraph and Plotly</b>',
259
+ titlefont_size=16,
260
+ showlegend=False,
261
+ hovermode='closest',
262
+ margin=dict(b=0, l=0, r=0, t=50),
263
+ xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
264
+ yaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
265
+ # height=600,
266
+ # width=600,
267
+ annotations=[dict(
268
+ showarrow=False,
269
+ xref="paper", yref="paper",
270
+ x=0.005, y=-0.002 )]
271
+ ))
272
+
273
+ return gr.Plot(fig)
274
+
275
+ def set_nodename_to_protoIDs(species_name):
276
+ global nodename_to_protoIDs
277
+ imgpath = species_to_imgpath[species_name]
278
+ for foldername in os.listdir(imgpath):
279
+ if os.path.isdir(os.path.join(imgpath, foldername)):
280
+ folderpath = os.path.join(imgpath, foldername)
281
+ for filename in os.listdir(folderpath):
282
+ if filename.endswith('png') or filename.endswith('jpg'):
283
+ filepath = os.path.join(folderpath, filename)
284
+ imgname_to_filepath[filename] = filepath
285
+ nodename = filename.split('.')[0].split('-')[0]
286
+ protoID = filename.split('.')[0].split('-')[1]
287
+ nodename_to_protoIDs[nodename].append(protoID)
288
+
289
 
290
  def get_protoIDs(nodename):
291
  return gr.Dropdown(choices=nodename_to_protoIDs[nodename], interactive=True)
292
 
293
+ def get_nodenames(species_name):
294
+ return gr.Dropdown(choices=list(nodename_to_protoIDs.keys()), interactive=True)
295
 
296
  def get_image(nodename, protoID):
297
  imgname = '-'.join([nodename, protoID]) + '.png'
 
306
  ROOT = get_tree(imgpath)
307
  print(ROOT.name)
308
  gr.Markdown("## Interactive Tree and Image Display")
 
 
309
 
310
+ # with gr.Row():
311
+ # tree_output = gr.Plot(plot_tree_using_igraph) # Connect the function directly
312
 
313
+ # with gr.Row():
314
+ # with gr.Column():
315
+ # dropdown_1_nodename = gr.Dropdown(label="Select a node name", choices=list(nodename_to_protoIDs.keys()))
316
+ # dropdown_1_protos = gr.Dropdown(label="Select a prototype ID", choices=[], allow_custom_value=True)
317
+ # image_output_1 = gr.Image()
318
+ # with gr.Column():
319
+ # dropdown_2_nodename = gr.Dropdown(label="Select a node name", choices=list(nodename_to_protoIDs.keys()))
320
+ # dropdown_2_protos = gr.Dropdown(label="Select a prototype ID", choices=[], allow_custom_value=True)
321
+ # image_output_2 = gr.Image()
322
+
323
+ # dropdown_1_nodename.change(get_protoIDs, dropdown_1_nodename, dropdown_1_protos)
324
+ # dropdown_1_protos.change(get_image, [dropdown_1_nodename, dropdown_1_protos], image_output_1)
325
+ # dropdown_2_nodename.change(get_protoIDs, dropdown_2_nodename, dropdown_2_protos)
326
+ # dropdown_2_protos.change(get_image, [dropdown_2_nodename, dropdown_2_protos], image_output_2)
327
+
328
+ with gr.Row():
329
+ dropdown_species = gr.Dropdown(label="Select a species", choices=list(species_to_imgpath.keys()))
330
+ with gr.Row():
331
+ tree_output = gr.Plot() # Connect the function directly
332
  with gr.Row():
333
  with gr.Column():
334
+ dropdown_1_nodename = gr.Dropdown(label="Select a node name", choices=[])
335
  dropdown_1_protos = gr.Dropdown(label="Select a prototype ID", choices=[], allow_custom_value=True)
336
  image_output_1 = gr.Image()
337
  with gr.Column():
338
+ dropdown_2_nodename = gr.Dropdown(label="Select a node name", choices=[])
339
  dropdown_2_protos = gr.Dropdown(label="Select a prototype ID", choices=[], allow_custom_value=True)
340
  image_output_2 = gr.Image()
341
+ dropdown_species.change(plot_tree_from_species, dropdown_species, tree_output)
342
+ dropdown_species.change(set_nodename_to_protoIDs)
343
+ dropdown_species.change(get_nodenames, dropdown_species, dropdown_1_nodename)
344
+ dropdown_species.change(get_nodenames, dropdown_species, dropdown_2_nodename)
345
  dropdown_1_nodename.change(get_protoIDs, dropdown_1_nodename, dropdown_1_protos)
346
  dropdown_1_protos.change(get_image, [dropdown_1_nodename, dropdown_1_protos], image_output_1)
347
  dropdown_2_nodename.change(get_protoIDs, dropdown_2_nodename, dropdown_2_protos)
348
  dropdown_2_protos.change(get_image, [dropdown_2_nodename, dropdown_2_protos], image_output_2)
349
+
350
+ # imgpath = species_to_imgpath['bird']
351
+ # print(imgpath)
352
+ # ROOT = get_tree(imgpath)
353
+ # print(ROOT.name)
354
+ # gr.Markdown("## Interactive Tree and Image Display")
355
+
356
+ # with gr.Row():
357
+ # tree_output = gr.Plot(plot_tree_using_igraph) # Connect the function directly
358
+
359
+ # with gr.Row():
360
+ # with gr.Column():
361
+ # dropdown_3_nodename = gr.Dropdown(label="Select a node name", choices=list(nodename_to_protoIDs.keys()))
362
+ # dropdown_3_protos = gr.Dropdown(label="Select a prototype ID", choices=[], allow_custom_value=True)
363
+ # image_output_3 = gr.Image()
364
+ # with gr.Column():
365
+ # dropdown_4_nodename = gr.Dropdown(label="Select a node name", choices=list(nodename_to_protoIDs.keys()))
366
+ # dropdown_4_protos = gr.Dropdown(label="Select a prototype ID", choices=[], allow_custom_value=True)
367
+ # image_output_4 = gr.Image()
368
+
369
+ # dropdown_3_nodename.change(get_protoIDs, dropdown_3_nodename, dropdown_3_protos)
370
+ # dropdown_3_protos.change(get_image, [dropdown_3_nodename, dropdown_3_protos], image_output_3)
371
+ # dropdown_4_nodename.change(get_protoIDs, dropdown_4_nodename, dropdown_4_protos)
372
+ # dropdown_4_protos.change(get_image, [dropdown_4_nodename, dropdown_4_protos], image_output_4)
373
+
374
+
375
 
376
 
377
  # Initialize with placeholder images