1
1
import matplotlib .pyplot as plt # type: ignore
2
2
import networkx as nx
3
+ import pyvis .network
3
4
4
5
from semantic_matcher .algorithm import SemanticMatchGraph
5
6
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
7
25
8
26
9
27
def save_graph_as_figure (g : SemanticMatchGraph , filename : str ) -> None :
@@ -24,10 +42,12 @@ def save_graph_as_figure(g: SemanticMatchGraph, filename: str) -> None:
24
42
25
43
if __name__ == "__main__" :
26
44
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 )
32
52
33
- save_graph_as_figure ( graph_complex , "temp.png " )
53
+ net . save_graph ( "graph.html " )
0 commit comments