Skip to content

Commit 59ab01f

Browse files
rhtCorvince
authored andcommitted
solara: Add virus_on_network network example
1 parent 031f3df commit 59ab01f

File tree

1 file changed

+135
-0
lines changed
  • examples/virus_on_network

1 file changed

+135
-0
lines changed

examples/virus_on_network/app.py

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
import math
2+
3+
import solara
4+
from matplotlib.figure import Figure
5+
from matplotlib.ticker import MaxNLocator
6+
from mesa.experimental import JupyterViz, make_text
7+
from virus_on_network.model import State, VirusOnNetwork, number_infected
8+
9+
10+
def agent_portrayal(graph):
11+
def get_agent(node):
12+
return graph.nodes[node]["agent"][0]
13+
14+
edge_width = []
15+
edge_color = []
16+
for u, v in graph.edges():
17+
agent1 = get_agent(u)
18+
agent2 = get_agent(v)
19+
w = 2
20+
ec = "#e8e8e8"
21+
if State.RESISTANT in (agent1.state, agent2.state):
22+
w = 3
23+
ec = "black"
24+
edge_width.append(w)
25+
edge_color.append(ec)
26+
node_color_dict = {
27+
State.INFECTED: "tab:red",
28+
State.SUSCEPTIBLE: "tab:green",
29+
State.RESISTANT: "tab:gray",
30+
}
31+
node_color = [node_color_dict[get_agent(node).state] for node in graph.nodes()]
32+
return {
33+
"width": edge_width,
34+
"edge_color": edge_color,
35+
"node_color": node_color,
36+
}
37+
38+
39+
def get_resistant_susceptible_ratio(model):
40+
ratio = model.resistant_susceptible_ratio()
41+
ratio_text = r"$\infty$" if ratio is math.inf else f"{ratio:.2f}"
42+
infected_text = str(number_infected(model))
43+
44+
return "Resistant/Susceptible Ratio: {}<br>Infected Remaining: {}".format(
45+
ratio_text, infected_text
46+
)
47+
48+
49+
def make_plot(model):
50+
# This is for the case when we want to plot multiple measures in 1 figure.
51+
# We could incorporate this into core Mesa.
52+
fig = Figure()
53+
ax = fig.subplots()
54+
measures = ["Infected", "Susceptible", "Resistant"]
55+
colors = ["tab:red", "tab:green", "tab:gray"]
56+
for i, m in enumerate(measures):
57+
color = colors[i]
58+
df = model.datacollector.get_model_vars_dataframe()
59+
ax.plot(df.loc[:, m], label=m, color=color)
60+
fig.legend()
61+
# Set integer x axis
62+
ax.xaxis.set_major_locator(MaxNLocator(integer=True))
63+
solara.FigureMatplotlib(fig)
64+
65+
66+
model_params = {
67+
"num_nodes": {
68+
"type": "SliderInt",
69+
"value": 10,
70+
"label": "Number of agents",
71+
"min": 10,
72+
"max": 100,
73+
"step": 1,
74+
},
75+
"avg_node_degree": {
76+
"type": "SliderInt",
77+
"value": 3,
78+
"label": "Avg Node Degree",
79+
"min": 3,
80+
"max": 8,
81+
"step": 1,
82+
},
83+
"initial_outbreak_size": {
84+
"type": "SliderInt",
85+
"value": 1,
86+
"label": "Initial Outbreak Size",
87+
"min": 1,
88+
"max": 10,
89+
"step": 1,
90+
},
91+
"virus_spread_chance": {
92+
"type": "SliderFloat",
93+
"value": 0.4,
94+
"label": "Virus Spread Chance",
95+
"min": 0.0,
96+
"max": 1.0,
97+
"step": 0.1,
98+
},
99+
"virus_check_frequency": {
100+
"type": "SliderFloat",
101+
"value": 0.4,
102+
"label": "Virus Check Frequency",
103+
"min": 0.0,
104+
"max": 1.0,
105+
"step": 0.1,
106+
},
107+
"recovery_chance": {
108+
"type": "SliderFloat",
109+
"value": 0.3,
110+
"label": "Recovery Chance",
111+
"min": 0.0,
112+
"max": 1.0,
113+
"step": 0.1,
114+
},
115+
"gain_resistance_chance": {
116+
"type": "SliderFloat",
117+
"value": 0.5,
118+
"label": "Gain Resistance Chance",
119+
"min": 0.0,
120+
"max": 1.0,
121+
"step": 0.1,
122+
},
123+
}
124+
125+
page = JupyterViz(
126+
VirusOnNetwork,
127+
model_params,
128+
measures=[
129+
make_plot,
130+
make_text(get_resistant_susceptible_ratio),
131+
],
132+
name="Virus Model",
133+
agent_portrayal=agent_portrayal,
134+
)
135+
page # noqa

0 commit comments

Comments
 (0)