naos-ku commited on
Commit
1fdf3d0
·
1 Parent(s): a8d3286

implemented graph style selection from PyVis and NetworkX

Browse files
Files changed (1) hide show
  1. viewer.py +55 -4
viewer.py CHANGED
@@ -3,10 +3,12 @@ from __future__ import annotations
3
  import json
4
  from pathlib import Path
5
 
 
 
6
  import streamlit as st
7
  import torch
8
  from torch_geometric.data import Data
9
- from torch_geometric.utils import is_undirected
10
 
11
  DATA_DIR = Path("data")
12
  DEFAULT_SUBSETS = ("ba_shapes", "tree_cycle", "tree_grid", "ba_two_motifs")
@@ -137,6 +139,47 @@ def _graph_html(graph: Data, layout_seed: int) -> str:
137
  return net.generate_html()
138
 
139
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140
  def main() -> None:
141
  st.set_page_config(page_title="MotifQA Graph Viewer", layout="wide")
142
  st.title("MotifQA Graph Viewer")
@@ -174,14 +217,22 @@ def main() -> None:
174
  value=0,
175
  step=1,
176
  )
177
- layout_seed = st.sidebar.number_input("Layout seed", min_value=0, value=42, step=1)
 
 
178
  example = examples[sample_idx]
179
  graph = _build_graph(example)
180
 
181
  left, right = st.columns([2, 1])
182
  with left:
183
- html = _graph_html(graph, layout_seed=layout_seed)
184
- st.components.v1.html(html, height=700, scrolling=True)
 
 
 
 
 
 
185
 
186
  with right:
187
  st.subheader("Sample details")
 
3
  import json
4
  from pathlib import Path
5
 
6
+ import matplotlib.pyplot as plt
7
+ import networkx as nx
8
  import streamlit as st
9
  import torch
10
  from torch_geometric.data import Data
11
+ from torch_geometric.utils import is_undirected, to_networkx
12
 
13
  DATA_DIR = Path("data")
14
  DEFAULT_SUBSETS = ("ba_shapes", "tree_cycle", "tree_grid", "ba_two_motifs")
 
139
  return net.generate_html()
140
 
141
 
142
+ def _plot_graph(graph: Data, layout_seed: int) -> plt.Figure:
143
+ if not is_undirected(graph.edge_index):
144
+ raise ValueError("The generated graph is not undirected.")
145
+
146
+ G = to_networkx(graph, to_undirected=True)
147
+ pos = nx.spring_layout(G, seed=layout_seed)
148
+
149
+ node_mask = getattr(graph, "node_mask", None)
150
+ motif_nodes = node_mask.nonzero(as_tuple=True)[0].tolist()
151
+ normal_nodes = [n for n in G.nodes if n not in motif_nodes]
152
+
153
+ fig, ax = plt.subplots(figsize=(6, 6))
154
+ nx.draw_networkx_nodes(G, pos, nodelist=normal_nodes, node_color="skyblue", node_size=120, ax=ax)
155
+ nx.draw_networkx_nodes(G, pos, nodelist=motif_nodes, node_color="orange", node_size=120, ax=ax)
156
+ nx.draw_networkx_edges(G, pos, alpha=0.3, ax=ax)
157
+
158
+ labels = {n: str(n) for n in G.nodes()}
159
+ if normal_nodes:
160
+ nx.draw_networkx_labels(
161
+ G,
162
+ pos,
163
+ labels={n: labels[n] for n in normal_nodes},
164
+ font_size=10,
165
+ font_color="black",
166
+ ax=ax,
167
+ )
168
+ if motif_nodes:
169
+ nx.draw_networkx_labels(
170
+ G,
171
+ pos,
172
+ labels={n: labels[n] for n in motif_nodes},
173
+ font_size=10,
174
+ font_color="black",
175
+ ax=ax,
176
+ )
177
+
178
+ ax.axis("off")
179
+ fig.tight_layout()
180
+ return fig
181
+
182
+
183
  def main() -> None:
184
  st.set_page_config(page_title="MotifQA Graph Viewer", layout="wide")
185
  st.title("MotifQA Graph Viewer")
 
217
  value=0,
218
  step=1,
219
  )
220
+ st.sidebar.divider()
221
+ st.sidebar.markdown("### Visualization Settings")
222
+ viz_method = st.sidebar.radio("Visualization method", ["Interactive (PyVis)", "Static (NetworkX)"], index=0)
223
  example = examples[sample_idx]
224
  graph = _build_graph(example)
225
 
226
  left, right = st.columns([2, 1])
227
  with left:
228
+ if viz_method == "Static (NetworkX)":
229
+ layout_seed = st.sidebar.number_input("Layout seed", min_value=0, value=42, step=1)
230
+ fig = _plot_graph(graph, layout_seed=layout_seed)
231
+ st.pyplot(fig, clear_figure=True)
232
+ plt.close(fig)
233
+ else:
234
+ html = _graph_html(graph, layout_seed=42)
235
+ st.components.v1.html(html, height=700, scrolling=True)
236
 
237
  with right:
238
  st.subheader("Sample details")