1
1
"""Contains stuff for visualization"""
2
+
2
3
from pyvis .network import Network
3
- from stormvogel .model import Model , EmptyAction
4
+ from stormvogel .model import Model , EmptyAction , Number
4
5
from ipywidgets import interact
5
6
from IPython .display import display
6
7
from fractions import Fraction
7
- ######
8
- sejfi sief #Syntax error
8
+
9
+
9
10
class Visualization :
10
11
"""Handles visualization of a Model using a pyvis Network."""
11
- def __init__ (self , model : Model , name : str , notebook : bool = True , cdn_resources : str = "remote" ) -> None :
12
+
13
+ def __init__ (
14
+ self ,
15
+ model : Model ,
16
+ name : str ,
17
+ notebook : bool = True ,
18
+ cdn_resources : str = "remote" ,
19
+ ) -> None :
12
20
"""Create visualization of a Model using a pyvis Network
13
21
14
22
Args:
@@ -17,14 +25,16 @@ def __init__(self, model: Model, name: str, notebook: bool=True, cdn_resources:
17
25
notebook (bool, optional): Leave to true if you are using in a notebook. Defaults to True.
18
26
"""
19
27
self .model = model
20
- if name [- 5 :] != ".html" : # We do not require the user to explicitly type .html in their names.
28
+ if (
29
+ name [- 5 :] != ".html"
30
+ ): # We do not require the user to explicitly type .html in their names.
21
31
name += ".html"
22
32
self .name = name
23
33
self .g = Network (notebook = notebook , directed = True , cdn_resources = cdn_resources )
24
34
self .__add_nodes ()
25
35
self .__add_transitions ()
26
36
self .__set_layout ()
27
-
37
+
28
38
def __set_layout (self ):
29
39
self .g .set_options ("""
30
40
var options = {
@@ -38,35 +48,48 @@ def __set_layout(self):
38
48
"color": "blue"
39
49
}
40
50
}""" )
41
-
51
+
42
52
def __add_nodes (self ):
43
53
"""For each state in the model, add a node to the graph."""
44
54
for state in self .model .states .values ():
45
55
borderWidth = 1
46
56
if state == self .model .get_initial_state ():
47
57
borderWidth = 3
48
- self .g .add_node (state .id , label = "," .join (state .labels ), color = None , borderWidth = borderWidth )
49
-
50
- def __formatted_prob (self , prob : float ) -> str :
58
+ self .g .add_node (
59
+ state .id ,
60
+ label = "," .join (state .labels ),
61
+ color = None , # type: ignore
62
+ borderWidth = borderWidth ,
63
+ )
64
+
65
+ def __formatted_prob (self , prob : Number ) -> str :
51
66
"""Take a probability value and format it nicely"""
52
67
return str (Fraction (prob ).limit_denominator (20 ))
53
-
68
+
54
69
def __add_transitions (self ):
55
70
"""For each transition in the model, add a transition in the graph."""
56
71
for state_id , transition in self .model .transitions .items ():
57
72
for action , branch in transition .transition .items ():
58
73
if action == EmptyAction :
59
74
# Only draw probabilities
60
75
for prob , target in branch .branch :
61
- self .g .add_edge (state_id , target .id , color = "red" , label = self .__formatted_prob (prob ))
76
+ self .g .add_edge (
77
+ state_id ,
78
+ target .id ,
79
+ color = "red" ,
80
+ label = self .__formatted_prob (prob ),
81
+ )
62
82
else :
63
- raise NotImplementedError ("Non-empty actions are not supported yet." )
83
+ raise NotImplementedError (
84
+ "Non-empty actions are not supported yet."
85
+ )
64
86
65
87
def show (self ):
66
88
"""Show the constructed model"""
67
89
display (self .g .show (name = self .name ))
68
90
69
- def show (model : Model , name : str , notebook : bool = True ):
91
+
92
+ def show (model : Model , name : str , notebook : bool = True ):
70
93
"""Create visualization of a Model using a pyvis Network
71
94
72
95
Args:
@@ -77,5 +100,6 @@ def show(model: Model, name: str, notebook: bool=True):
77
100
vis = Visualization (model , name , notebook )
78
101
vis .show ()
79
102
103
+
80
104
def make_slider ():
81
105
return interact (lambda x : x , x = 10 )
0 commit comments