Skip to content

Commit 546647f

Browse files
Add visualization and fix linter
1 parent 2bd92f6 commit 546647f

File tree

1 file changed

+38
-14
lines changed

1 file changed

+38
-14
lines changed

stormvogel/visualization.py

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
"""Contains stuff for visualization"""
2+
23
from pyvis.network import Network
3-
from stormvogel.model import Model, EmptyAction
4+
from stormvogel.model import Model, EmptyAction, Number
45
from ipywidgets import interact
56
from IPython.display import display
67
from fractions import Fraction
7-
######
8-
sejfi sief #Syntax error
8+
9+
910
class Visualization:
1011
"""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:
1220
"""Create visualization of a Model using a pyvis Network
1321
1422
Args:
@@ -17,14 +25,16 @@ def __init__(self, model: Model, name: str, notebook: bool=True, cdn_resources:
1725
notebook (bool, optional): Leave to true if you are using in a notebook. Defaults to True.
1826
"""
1927
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.
2131
name += ".html"
2232
self.name = name
2333
self.g = Network(notebook=notebook, directed=True, cdn_resources=cdn_resources)
2434
self.__add_nodes()
2535
self.__add_transitions()
2636
self.__set_layout()
27-
37+
2838
def __set_layout(self):
2939
self.g.set_options("""
3040
var options = {
@@ -38,35 +48,48 @@ def __set_layout(self):
3848
"color": "blue"
3949
}
4050
}""")
41-
51+
4252
def __add_nodes(self):
4353
"""For each state in the model, add a node to the graph."""
4454
for state in self.model.states.values():
4555
borderWidth = 1
4656
if state == self.model.get_initial_state():
4757
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:
5166
"""Take a probability value and format it nicely"""
5267
return str(Fraction(prob).limit_denominator(20))
53-
68+
5469
def __add_transitions(self):
5570
"""For each transition in the model, add a transition in the graph."""
5671
for state_id, transition in self.model.transitions.items():
5772
for action, branch in transition.transition.items():
5873
if action == EmptyAction:
5974
# Only draw probabilities
6075
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+
)
6282
else:
63-
raise NotImplementedError("Non-empty actions are not supported yet.")
83+
raise NotImplementedError(
84+
"Non-empty actions are not supported yet."
85+
)
6486

6587
def show(self):
6688
"""Show the constructed model"""
6789
display(self.g.show(name=self.name))
6890

69-
def show(model: Model, name: str, notebook: bool=True):
91+
92+
def show(model: Model, name: str, notebook: bool = True):
7093
"""Create visualization of a Model using a pyvis Network
7194
7295
Args:
@@ -77,5 +100,6 @@ def show(model: Model, name: str, notebook: bool=True):
77100
vis = Visualization(model, name, notebook)
78101
vis.show()
79102

103+
80104
def make_slider():
81105
return interact(lambda x: x, x=10)

0 commit comments

Comments
 (0)