Skip to content

Commit 3c685e2

Browse files
committed
Adding reliability analysis for ABM
1 parent 9827e03 commit 3c685e2

File tree

5 files changed

+451
-10
lines changed

5 files changed

+451
-10
lines changed
134 Bytes
Binary file not shown.

workshop/4_mesa_agent_based_models/1_geosir_network_reliability.ipynb

Lines changed: 333 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "71fa9406-b967-4303-baec-9e4eaac19622",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": []
10+
}
11+
],
12+
"metadata": {
13+
"kernelspec": {
14+
"display_name": "Python 3 (ipykernel)",
15+
"language": "python",
16+
"name": "python3"
17+
},
18+
"language_info": {
19+
"codemirror_mode": {
20+
"name": "ipython",
21+
"version": 3
22+
},
23+
"file_extension": ".py",
24+
"mimetype": "text/x-python",
25+
"name": "python",
26+
"nbconvert_exporter": "python",
27+
"pygments_lexer": "ipython3",
28+
"version": "3.10.17"
29+
}
30+
},
31+
"nbformat": 4,
32+
"nbformat_minor": 5
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "a14b59ed-5ef6-46fc-b61f-e5714c2d90d0",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": []
10+
}
11+
],
12+
"metadata": {
13+
"kernelspec": {
14+
"display_name": "Python 3 (ipykernel)",
15+
"language": "python",
16+
"name": "python3"
17+
},
18+
"language_info": {
19+
"codemirror_mode": {
20+
"name": "ipython",
21+
"version": 3
22+
},
23+
"file_extension": ".py",
24+
"mimetype": "text/x-python",
25+
"name": "python",
26+
"nbconvert_exporter": "python",
27+
"pygments_lexer": "ipython3",
28+
"version": "3.10.17"
29+
}
30+
},
31+
"nbformat": 4,
32+
"nbformat_minor": 5
33+
}

workshop/4_mesa_agent_based_models/geosir_network.py

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import mesa
88
import mesa_geo as mg
99
from shapely.geometry import Point
10+
import numpy as np
1011

1112
class PersonAgent(mg.GeoAgent):
1213
"""Person Agent."""
@@ -239,6 +240,7 @@ def __init__(
239240
self.space = mg.GeoSpace(warn_crs_conversion=False)
240241
self.networks = []
241242
self.states = []
243+
self.r_0_ts = []
242244
self.network_grid = None
243245
network_grid_types=[
244246
"person",
@@ -263,6 +265,9 @@ def __init__(
263265
"susceptible": get_susceptible_count,
264266
"recovered": get_recovered_count,
265267
"dead": get_dead_count,
268+
"contact_r_e": get_contact_r_e,
269+
"contact_r_0t": get_contact_r_0t,
270+
"proxy_contact_r0": get_proxy_contact_r0
266271
}
267272
)
268273

@@ -359,9 +364,49 @@ def run(self, n_steps) -> None:
359364
for _ in range(n_steps):
360365
self.step()
361366

367+
def calculate_r_e(self) -> float:
368+
induced_infections = [
369+
a.induced_infections_at_t for a in self.agents_by_type[PersonAgent]
370+
if a.infected_others_at_t == True
371+
]
372+
n_infections = np.array(induced_infections)
373+
if len(n_infections) == 0:
374+
effective_r = 0
375+
else:
376+
effective_r = np.average(n_infections)
377+
378+
return effective_r
379+
380+
def calculate_r_0t(self, r_e: int) -> float:
381+
S = get_susceptible_count(self)
382+
if S == 0:
383+
S = 1e-8
384+
385+
N = self.pop_size
386+
initial_outbreak_size = 0
387+
r_0_t = r_e * (N - initial_outbreak_size - 0) / S
388+
389+
self.r_0_ts.append(r_0_t)
390+
return r_0_t
391+
392+
def calculate_proxy_contact_r0(self) -> float:
393+
return np.array(self.r_0_ts).mean()
394+
395+
def resistant_susceptible_ratio(self):
396+
S = self.counts["SUSCEPTIBLE"]
397+
if S == 0:
398+
S = 1e-8
399+
400+
return self.counts["RESISTANT"] / S
401+
362402
def get_state(self) -> dict:
363403
state = deepcopy(self.counts)
364404
state["t"] = self.steps
405+
406+
state["contact_r_e"] = self.calculate_r_e()
407+
state["contact_r_0t"] = self.calculate_r_0t(state["contact_r_e"])
408+
state["proxy_contact_r0"] = self.calculate_proxy_contact_r0()
409+
365410
return state
366411

367412
def get_states(self):
@@ -419,7 +464,6 @@ def get_edge_df(self):
419464
df = pd.DataFrame(rows)
420465
return df
421466

422-
# Functions needed for datacollector
423467
def get_infected_count(model):
424468
return model.counts["infected"]
425469

@@ -431,17 +475,15 @@ def get_susceptible_count(model):
431475
def get_recovered_count(model):
432476
return model.counts["recovered"]
433477

434-
435478
def get_dead_count(model):
436479
return model.counts["dead"]
437480

481+
def get_contact_r_e(model):
482+
return model.calculate_r_e()
438483

439-
model = GeoSir()
440-
441-
for t in range(10):
442-
model.run(1)
484+
def get_contact_r_0t(model):
485+
r_e = model.calculate_r_e()
486+
return model.calculate_r_0t(r_e)
443487

444-
node_df = model.get_node_df()
445-
edge_df = model.get_edge_df()
446-
print(node_df)
447-
print(edge_df)
488+
def get_proxy_contact_r0(model):
489+
return model.calculate_proxy_contact_r0()

0 commit comments

Comments
 (0)