Skip to content

Commit f746155

Browse files
committed
Experiment with pyvis
1 parent 3fcafd4 commit f746155

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

semantic_matcher/visualization.py

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
11
import matplotlib.pyplot as plt # type: ignore
22
import networkx as nx
3+
import pyvis.network
34

45
from semantic_matcher.algorithm import SemanticMatchGraph
56

6-
# Todo: This is WIP
7+
8+
def _to_pyvis_network(g: SemanticMatchGraph) -> pyvis.network.Network:
9+
network = pyvis.network.Network(notebook=True, directed=True, height="600px", width="100%")
10+
11+
# Manually add nodes and edges with labels for weights
12+
for node in g.nodes():
13+
network.add_node(node, label=node) # Todo: Do something smart with labels, e.g. source
14+
15+
for source, target, data in g.edges(data=True):
16+
# This breaks, if weight is missing, but that is expected behaviour, since we need a semantic similarity score
17+
weight = data["weight"]
18+
network.add_edge(source, target, label=str(weight), title=f"Weight: {weight}")
19+
20+
# Enable physics for animation and gravity effects
21+
# network.force_atlas_2based()
22+
network.toggle_physics(True)
23+
network.show_buttons(filter_=['physics'])
24+
return network
725

826

927
def save_graph_as_figure(g: SemanticMatchGraph, filename: str) -> None:
@@ -24,10 +42,12 @@ def save_graph_as_figure(g: SemanticMatchGraph, filename: str) -> None:
2442

2543
if __name__ == "__main__":
2644
graph_complex = SemanticMatchGraph()
27-
graph_complex.add_edge("A", "B", weight=0.9, source="dataset1")
28-
graph_complex.add_edge("A", "C", weight=0.8, source="dataset2")
29-
graph_complex.add_edge("B", "D", weight=0.7, source="dataset3")
30-
graph_complex.add_edge("C", "D", weight=0.6, source="dataset4")
31-
graph_complex.add_edge("D", "E", weight=0.5, source="dataset5")
45+
graph_complex.add_edge("A", "B", weight=0.9)
46+
graph_complex.add_edge("A", "C", weight=0.8)
47+
graph_complex.add_edge("B", "D", weight=0.7)
48+
graph_complex.add_edge("C", "D", weight=0.6)
49+
graph_complex.add_edge("D", "E", weight=0.5)
50+
51+
net = _to_pyvis_network(graph_complex)
3252

33-
save_graph_as_figure(graph_complex, "temp.png")
53+
net.save_graph("graph.html")

0 commit comments

Comments
 (0)