Skip to content

Commit ed88e90

Browse files
committed
models
1 parent 85eea90 commit ed88e90

File tree

10 files changed

+172
-11
lines changed

10 files changed

+172
-11
lines changed

models/SEIIR.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Epidemic Model with 5 compartments and 6 transitions:
2+
Name: SEIIR
3+
4+
Parameters:
5+
rate : 0.166667
6+
beta : 0.222222
7+
epsilon_a : 0.160000
8+
epsilon_s : 0.240000
9+
mu : 0.100000
10+
11+
12+
Transitions:
13+
- S + Ia = E rate
14+
- S + Is = E beta
15+
- E -> Ia epsilon_a
16+
- E -> Is epsilon_s
17+
- Ia -> R mu
18+
- Is -> R mu
19+
20+
# R0=2.00

models/SEIIRD.yaml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Epidemic Model with 6 compartments and 7 transitions:
2+
Name: SEIIRD
3+
4+
Parameters:
5+
rbeta : 0.166667
6+
beta : 0.222222
7+
epsilon_a : 0.160000
8+
epsilon_s : 0.240000
9+
mu : 0.100000
10+
mu_nd : 0.090000
11+
mu_d : 0.010000
12+
13+
14+
Transitions:
15+
- S + Ia = E rbeta
16+
- S + Is = E beta
17+
- E -> Ia epsilon_a
18+
- E -> Is epsilon_s
19+
- Ia -> R mu
20+
- Is -> R mu_nd
21+
- Is -> D mu_d
22+
23+
# R0=2.00

models/SEIR.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Epidemic Model with 4 compartments and 3 transitions:
2+
Name: SEIR
3+
4+
Parameters:
5+
beta : 0.200000
6+
epsilon : 0.400000
7+
mu : 0.100000
8+
9+
10+
Transitions:
11+
- S + I = E beta
12+
- E -> I epsilon
13+
- I -> R mu
14+
15+
# R0=2.00

models/SEIRS.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Epidemic Model with 4 compartments and 4 transitions:
2+
Name: SEIRS
3+
4+
Parameters:
5+
beta : 0.200000
6+
epsilon : 0.400000
7+
mu : 0.100000
8+
rho : 0.300000
9+
10+
11+
Transitions:
12+
- S + I = E beta
13+
- E -> I epsilon
14+
- I -> R mu
15+
- R -> S rho

models/SI.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Epidemic Model with 2 compartments and 1 transitions:
2+
3+
Parameters:
4+
beta : 0.200000
5+
6+
7+
Transitions:
8+
- S + I = I beta

models/model_list.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
SEIIR.yaml
2+
SEIIRD.yaml
3+
SEIR.yaml
4+
SEIRS.yaml
5+
SI.yaml
6+
SIR.yaml

poetry.lock

Lines changed: 24 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ dependencies = [
2121
"scipy>=1.7",
2222
"tqdm>=4",
2323
"pyyaml (>=6.0.2,<7.0.0)",
24+
"seaborn (>=0.13.2,<0.14.0)",
2425
]
2526
[project.urls]
2627
Homepage = "https://github.com/DataForScience/epidemik"

src/epidemik/EpiModel.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import time
1010
import os
1111
import re
12+
from urllib.parse import urlparse
13+
from urllib.request import urlretrieve
1214

1315
import networkx as nx
1416
import numpy as np
@@ -21,7 +23,7 @@
2123

2224
from typing import Union
2325

24-
from .utils import *
26+
from . import utils
2527

2628

2729
class EpiModel(object):
@@ -325,7 +327,7 @@ def plot(
325327
ax = plt.gca()
326328

327329
for comp in self.values_.columns:
328-
(self.values_[comp] / N).plot(c=epi_colors[comp[0]], **kwargs)
330+
(self.values_[comp] / N).plot(c=utils.EPI_COLORS[comp[0]], **kwargs)
329331

330332
ax.legend(self.values_.columns)
331333
ax.set_xlabel("Time")
@@ -340,7 +342,7 @@ def plot(
340342
return ax
341343
except Exception as e:
342344
print(e)
343-
raise NotInitialized("You must call integrate() or simulate() first")
345+
raise utils.NotInitialized("You must call integrate() or simulate() first")
344346

345347
def __getattr__(self, name: str) -> pd.Series:
346348
"""
@@ -670,7 +672,7 @@ def draw_model(self, ax: Union[plt.Axes, None] = None, show: bool = True) -> Non
670672
orig_pos = pos[node[3:]]
671673
pos[node] = [orig_pos[0], orig_pos[1] - 1]
672674
else:
673-
node_colors.append(epi_colors[node[0]])
675+
node_colors.append(utils.EPI_COLORS[node[0]])
674676

675677
edge_labels = {}
676678

@@ -821,3 +823,27 @@ def load_model(filename: str) -> None:
821823
model.name = data[key]
822824

823825
return model
826+
827+
828+
def download_model(filename: str, repo: Union[str, None] = None) -> None:
829+
"""
830+
Download model from offical repository
831+
"""
832+
if repo is None:
833+
repo = utils.OFFICIAL_REPO
834+
835+
parsed_repo = urlparse(repo)
836+
837+
if parsed_repo.netloc == 'github.com':
838+
repo = repo.replace('github.com', 'raw.githubusercontent.com')
839+
remote_path = repo + os.path.join('refs/heads/models/models/', filename)
840+
841+
if not os.path.exists(utils.LOCAL_DIRECTORY):
842+
os.makedirs(utils.LOCAL_DIRECTORY)
843+
844+
local_path = os.path.join(utils.LOCAL_DIRECTORY, filename)
845+
846+
if not os.path.exists(local_path):
847+
urlretrieve(remote_path, local_path)
848+
849+
return EpiModel.load_model(local_path)

src/epidemik/utils.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,37 @@
11
from collections import defaultdict
2+
import os
3+
import sys
24

5+
__all__ = [
6+
"OFFICIAL_REPO",
7+
"EPI_COLORS",
8+
"NotInitialized",
9+
"get_cache_directory",
10+
]
311

412
class NotInitialized(Exception):
513
pass
614

15+
APPNAME = os.path.basename(os.path.realpath('.'))
16+
OFFICIAL_REPO = "https://github.com/DataForScience/epidemik/"
717

8-
epi_colors = defaultdict(lambda: "#f39019")
9-
epi_colors["S"] = "#51a7f9"
10-
epi_colors["E"] = "#f9e351"
11-
epi_colors["I"] = "#cf51f9"
12-
epi_colors["R"] = "#70bf41"
13-
epi_colors["D"] = "#8b8b8b"
18+
EPI_COLORS = defaultdict(lambda: "#f39019")
19+
EPI_COLORS["S"] = "#51a7f9"
20+
EPI_COLORS["E"] = "#f9e351"
21+
EPI_COLORS["I"] = "#cf51f9"
22+
EPI_COLORS["R"] = "#70bf41"
23+
EPI_COLORS["D"] = "#8b8b8b"
24+
25+
def get_cache_directory():
26+
"""
27+
Return the location of the cache directory for the current platform.
28+
"""
29+
system = sys.platform
30+
31+
if system == 'darwin':
32+
path = os.path.join(os.path.expanduser('~/Library/Caches'), APPNAME)
33+
else:
34+
path = os.getenv('XDG_CACHE_HOME', os.path.expanduser('~/.cache'))
35+
path = os.path.join(path, APPNAME)
36+
37+
return path

0 commit comments

Comments
 (0)