epymorph 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- epymorph/__init__.py +7 -0
- epymorph/__main__.py +16 -0
- epymorph/adrio/__init__.py +0 -0
- epymorph/adrio/acs5.py +1378 -0
- epymorph/adrio/adrio.py +953 -0
- epymorph/adrio/cdc.py +1826 -0
- epymorph/adrio/commuting_flows.py +283 -0
- epymorph/adrio/csv.py +499 -0
- epymorph/adrio/humidity.py +206 -0
- epymorph/adrio/lodes.py +509 -0
- epymorph/adrio/numpy.py +160 -0
- epymorph/adrio/prism.py +345 -0
- epymorph/adrio/processing.py +1221 -0
- epymorph/adrio/soda.py +602 -0
- epymorph/adrio/us_tiger.py +289 -0
- epymorph/adrio/validation.py +382 -0
- epymorph/attribute.py +614 -0
- epymorph/cache.py +680 -0
- epymorph/cli/__init__.py +0 -0
- epymorph/cli/cache.py +78 -0
- epymorph/cli/main.py +29 -0
- epymorph/compartment_model.py +1873 -0
- epymorph/data/__init__.py +1 -0
- epymorph/data/ipm/__init__.py +22 -0
- epymorph/data/ipm/no.py +14 -0
- epymorph/data/ipm/pei.py +40 -0
- epymorph/data/ipm/seirs.py +50 -0
- epymorph/data/ipm/sirh.py +55 -0
- epymorph/data/ipm/sirs.py +48 -0
- epymorph/data/ipm/sparsemod.py +96 -0
- epymorph/data/mm/__init__.py +22 -0
- epymorph/data/mm/centroids.py +76 -0
- epymorph/data/mm/flat.py +61 -0
- epymorph/data/mm/icecube.py +50 -0
- epymorph/data/mm/no.py +29 -0
- epymorph/data/mm/pei.py +105 -0
- epymorph/data/mm/sparsemod.py +73 -0
- epymorph/data/pei.py +424 -0
- epymorph/data_shape.py +593 -0
- epymorph/data_type.py +185 -0
- epymorph/data_usage.py +258 -0
- epymorph/database.py +1301 -0
- epymorph/error.py +195 -0
- epymorph/event.py +196 -0
- epymorph/geography/__init__.py +0 -0
- epymorph/geography/custom.py +93 -0
- epymorph/geography/scope.py +457 -0
- epymorph/geography/us_census.py +1056 -0
- epymorph/geography/us_geography.py +502 -0
- epymorph/geography/us_tiger.py +1083 -0
- epymorph/initializer.py +786 -0
- epymorph/kit.py +107 -0
- epymorph/log/__init__.py +0 -0
- epymorph/log/file.py +128 -0
- epymorph/log/messaging.py +139 -0
- epymorph/log/movement.py +228 -0
- epymorph/movement_model.py +347 -0
- epymorph/parameter_fitting/__init__.py +0 -0
- epymorph/parameter_fitting/distribution.py +68 -0
- epymorph/parameter_fitting/dynamics.py +19 -0
- epymorph/parameter_fitting/filter/__init__.py +0 -0
- epymorph/parameter_fitting/filter/base_filter.py +50 -0
- epymorph/parameter_fitting/filter/particle.py +32 -0
- epymorph/parameter_fitting/filter/particle_filter.py +296 -0
- epymorph/parameter_fitting/likelihood.py +54 -0
- epymorph/parameter_fitting/output.py +73 -0
- epymorph/parameter_fitting/particlefilter_simulation.py +175 -0
- epymorph/parameter_fitting/perturbation.py +19 -0
- epymorph/parameter_fitting/utils/__init__.py +0 -0
- epymorph/parameter_fitting/utils/data_loader.py +97 -0
- epymorph/parameter_fitting/utils/epymorph_simulation.py +119 -0
- epymorph/parameter_fitting/utils/observations.py +63 -0
- epymorph/parameter_fitting/utils/parameter_estimation.py +49 -0
- epymorph/parameter_fitting/utils/params_perturb.py +43 -0
- epymorph/parameter_fitting/utils/particle_initializer.py +95 -0
- epymorph/parameter_fitting/utils/resampler.py +258 -0
- epymorph/parameter_fitting/utils/utils.py +101 -0
- epymorph/params.py +438 -0
- epymorph/py.typed +0 -0
- epymorph/rume.py +871 -0
- epymorph/simulation.py +991 -0
- epymorph/simulator/__init__.py +0 -0
- epymorph/simulator/basic/__init__.py +0 -0
- epymorph/simulator/basic/basic_simulator.py +171 -0
- epymorph/simulator/basic/ipm_exec.py +437 -0
- epymorph/simulator/basic/mm_exec.py +244 -0
- epymorph/simulator/basic/output.py +213 -0
- epymorph/simulator/world.py +149 -0
- epymorph/simulator/world_list.py +274 -0
- epymorph/strata.py +24 -0
- epymorph/sympy_shim.py +57 -0
- epymorph/time.py +1197 -0
- epymorph/tools/__init__.py +0 -0
- epymorph/tools/data.py +297 -0
- epymorph/tools/ipm_diagram.py +208 -0
- epymorph/tools/out_map.py +551 -0
- epymorph/tools/out_plot.py +423 -0
- epymorph/tools/out_table.py +582 -0
- epymorph/util.py +1036 -0
- epymorph-1.0.0.dist-info/METADATA +57 -0
- epymorph-1.0.0.dist-info/RECORD +104 -0
- epymorph-1.0.0.dist-info/WHEEL +5 -0
- epymorph-1.0.0.dist-info/entry_points.txt +2 -0
- epymorph-1.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""The package for epymorph's built-in IPMs, MMs, and GEOs."""
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"""epymorph's collection of built-in compartment models."""
|
|
2
|
+
|
|
3
|
+
from epymorph.data.ipm.no import No
|
|
4
|
+
from epymorph.data.ipm.pei import Pei
|
|
5
|
+
from epymorph.data.ipm.seirs import SEIRS
|
|
6
|
+
from epymorph.data.ipm.sirh import SIRH
|
|
7
|
+
from epymorph.data.ipm.sirs import SIRS
|
|
8
|
+
from epymorph.data.ipm.sparsemod import SparseMod
|
|
9
|
+
|
|
10
|
+
### WARNING ##########################################
|
|
11
|
+
# This file is generated by the data_exports script. #
|
|
12
|
+
# Manual changes to this file will be lost. #
|
|
13
|
+
######################################################
|
|
14
|
+
|
|
15
|
+
__all__ = [
|
|
16
|
+
"No",
|
|
17
|
+
"Pei",
|
|
18
|
+
"SEIRS",
|
|
19
|
+
"SIRH",
|
|
20
|
+
"SIRS",
|
|
21
|
+
"SparseMod",
|
|
22
|
+
]
|
epymorph/data/ipm/no.py
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"""Defines a compartmental IPM with one compartment and no transitions."""
|
|
2
|
+
|
|
3
|
+
from epymorph.compartment_model import CompartmentModel, compartment
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class No(CompartmentModel):
|
|
7
|
+
"""The 'no' IPM: a single compartment with no transitions."""
|
|
8
|
+
|
|
9
|
+
_suppress_ipm_validation_warnings = True
|
|
10
|
+
|
|
11
|
+
compartments = (compartment("P"),)
|
|
12
|
+
|
|
13
|
+
def edges(self, symbols):
|
|
14
|
+
return []
|
epymorph/data/ipm/pei.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"""Defines a compartmental IPM mirroring the Pei paper's beta treatment."""
|
|
2
|
+
|
|
3
|
+
from sympy import Max, exp, log
|
|
4
|
+
|
|
5
|
+
from epymorph.attribute import AttributeDef
|
|
6
|
+
from epymorph.compartment_model import CompartmentModel, compartment, edge
|
|
7
|
+
from epymorph.data_shape import Shapes
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class Pei(CompartmentModel):
|
|
11
|
+
"""The 'pei' IPM: an SIRS model driven by humidity."""
|
|
12
|
+
|
|
13
|
+
compartments = [
|
|
14
|
+
compartment("S"),
|
|
15
|
+
compartment("I"),
|
|
16
|
+
compartment("R"),
|
|
17
|
+
]
|
|
18
|
+
|
|
19
|
+
requirements = [
|
|
20
|
+
AttributeDef("infection_duration", float, Shapes.TxN),
|
|
21
|
+
AttributeDef("immunity_duration", float, Shapes.TxN),
|
|
22
|
+
AttributeDef("humidity", float, Shapes.TxN),
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
def edges(self, symbols):
|
|
26
|
+
[S, I, R] = symbols.all_compartments
|
|
27
|
+
[D, L, H] = symbols.all_requirements
|
|
28
|
+
|
|
29
|
+
beta = (exp(-180 * H + log(2.0 - 1.3)) + 1.3) / D
|
|
30
|
+
|
|
31
|
+
# formulate N so as to avoid dividing by zero;
|
|
32
|
+
# this is safe in this instance because if the denominator is zero,
|
|
33
|
+
# the numerator must also be zero
|
|
34
|
+
N = Max(1, S + I + R)
|
|
35
|
+
|
|
36
|
+
return [
|
|
37
|
+
edge(S, I, rate=beta * S * I / N),
|
|
38
|
+
edge(I, R, rate=I / D),
|
|
39
|
+
edge(R, S, rate=R / L),
|
|
40
|
+
]
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
from sympy import Max
|
|
2
|
+
|
|
3
|
+
from epymorph.attribute import AttributeDef
|
|
4
|
+
from epymorph.compartment_model import CompartmentModel, compartment, edge
|
|
5
|
+
from epymorph.data_shape import Shapes
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class SEIRS(CompartmentModel):
|
|
9
|
+
"""A basic SEIRS model."""
|
|
10
|
+
|
|
11
|
+
compartments = [
|
|
12
|
+
compartment("S"),
|
|
13
|
+
compartment("E"),
|
|
14
|
+
compartment("I"),
|
|
15
|
+
compartment("R"),
|
|
16
|
+
]
|
|
17
|
+
requirements = [
|
|
18
|
+
AttributeDef("beta", type=float, shape=Shapes.TxN, comment="infectivity"),
|
|
19
|
+
AttributeDef(
|
|
20
|
+
"sigma",
|
|
21
|
+
type=float,
|
|
22
|
+
shape=Shapes.TxN,
|
|
23
|
+
comment="progression from exposed to infected",
|
|
24
|
+
),
|
|
25
|
+
AttributeDef(
|
|
26
|
+
"gamma",
|
|
27
|
+
type=float,
|
|
28
|
+
shape=Shapes.TxN,
|
|
29
|
+
comment="progression from infected to recovered",
|
|
30
|
+
),
|
|
31
|
+
AttributeDef(
|
|
32
|
+
"xi",
|
|
33
|
+
type=float,
|
|
34
|
+
shape=Shapes.TxN,
|
|
35
|
+
comment="progression from recovered to susceptible",
|
|
36
|
+
),
|
|
37
|
+
]
|
|
38
|
+
|
|
39
|
+
def edges(self, symbols):
|
|
40
|
+
[S, E, I, R] = symbols.all_compartments
|
|
41
|
+
[β, σ, γ, ξ] = symbols.all_requirements
|
|
42
|
+
|
|
43
|
+
N = Max(1, S + E + I + R)
|
|
44
|
+
|
|
45
|
+
return [
|
|
46
|
+
edge(S, E, rate=β * S * I / N),
|
|
47
|
+
edge(E, I, rate=σ * E),
|
|
48
|
+
edge(I, R, rate=γ * I),
|
|
49
|
+
edge(R, S, rate=ξ * R),
|
|
50
|
+
]
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"""Defines a compartmental IPM for a generic SIRH model."""
|
|
2
|
+
|
|
3
|
+
from sympy import Max
|
|
4
|
+
|
|
5
|
+
from epymorph.attribute import AttributeDef
|
|
6
|
+
from epymorph.compartment_model import CompartmentModel, compartment, edge, fork
|
|
7
|
+
from epymorph.data_shape import Shapes
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class SIRH(CompartmentModel):
|
|
11
|
+
"""A basic SIRH model."""
|
|
12
|
+
|
|
13
|
+
compartments = [
|
|
14
|
+
compartment("S"),
|
|
15
|
+
compartment("I"),
|
|
16
|
+
compartment("R"),
|
|
17
|
+
compartment("H", tags=["immobile"]),
|
|
18
|
+
]
|
|
19
|
+
|
|
20
|
+
requirements = [
|
|
21
|
+
AttributeDef("beta", type=float, shape=Shapes.TxN, comment="infectivity"),
|
|
22
|
+
AttributeDef("gamma", type=float, shape=Shapes.TxN, comment="recovery rate"),
|
|
23
|
+
AttributeDef("xi", type=float, shape=Shapes.TxN, comment="immune waning rate"),
|
|
24
|
+
AttributeDef(
|
|
25
|
+
"hospitalization_prob",
|
|
26
|
+
type=float,
|
|
27
|
+
shape=Shapes.TxN,
|
|
28
|
+
comment="a ratio of cases which are expected to require hospitalization",
|
|
29
|
+
),
|
|
30
|
+
AttributeDef(
|
|
31
|
+
"hospitalization_duration",
|
|
32
|
+
type=float,
|
|
33
|
+
shape=Shapes.TxN,
|
|
34
|
+
comment="the mean duration of hospitalization, in days",
|
|
35
|
+
),
|
|
36
|
+
]
|
|
37
|
+
|
|
38
|
+
def edges(self, symbols):
|
|
39
|
+
[S, I, R, H] = symbols.all_compartments
|
|
40
|
+
[β, γ, ξ, h_prob, h_dur] = symbols.all_requirements
|
|
41
|
+
|
|
42
|
+
# formulate N so as to avoid dividing by zero;
|
|
43
|
+
# this is safe in this instance because if the denominator is zero,
|
|
44
|
+
# the numerator must also be zero
|
|
45
|
+
N = Max(1, S + I + R + H)
|
|
46
|
+
|
|
47
|
+
return [
|
|
48
|
+
edge(S, I, rate=β * S * I / N),
|
|
49
|
+
fork(
|
|
50
|
+
edge(I, H, rate=γ * I * h_prob),
|
|
51
|
+
edge(I, R, rate=γ * I * (1 - h_prob)),
|
|
52
|
+
),
|
|
53
|
+
edge(H, R, rate=H / h_dur),
|
|
54
|
+
edge(R, S, rate=ξ * R),
|
|
55
|
+
]
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"""Defines a compartmental IPM for a generic SIRS model."""
|
|
2
|
+
|
|
3
|
+
from sympy import Max
|
|
4
|
+
|
|
5
|
+
from epymorph.attribute import AttributeDef
|
|
6
|
+
from epymorph.compartment_model import CompartmentModel, compartment, edge
|
|
7
|
+
from epymorph.data_shape import Shapes
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class SIRS(CompartmentModel):
|
|
11
|
+
"""A basic SIRS model."""
|
|
12
|
+
|
|
13
|
+
compartments = [
|
|
14
|
+
compartment("S"),
|
|
15
|
+
compartment("I"),
|
|
16
|
+
compartment("R"),
|
|
17
|
+
]
|
|
18
|
+
|
|
19
|
+
requirements = [
|
|
20
|
+
AttributeDef("beta", type=float, shape=Shapes.TxN, comment="infectivity"),
|
|
21
|
+
AttributeDef(
|
|
22
|
+
"gamma",
|
|
23
|
+
type=float,
|
|
24
|
+
shape=Shapes.TxN,
|
|
25
|
+
comment="progression from infected to recovered",
|
|
26
|
+
),
|
|
27
|
+
AttributeDef(
|
|
28
|
+
"xi",
|
|
29
|
+
type=float,
|
|
30
|
+
shape=Shapes.TxN,
|
|
31
|
+
comment="progression from recovered to susceptible",
|
|
32
|
+
),
|
|
33
|
+
]
|
|
34
|
+
|
|
35
|
+
def edges(self, symbols):
|
|
36
|
+
[S, I, R] = symbols.all_compartments
|
|
37
|
+
[β, γ, ξ] = symbols.all_requirements
|
|
38
|
+
|
|
39
|
+
# formulate N so as to avoid dividing by zero;
|
|
40
|
+
# this is safe in this instance because if the denominator is zero,
|
|
41
|
+
# the numerator must also be zero
|
|
42
|
+
N = Max(1, S + I + R)
|
|
43
|
+
|
|
44
|
+
return [
|
|
45
|
+
edge(S, I, rate=β * S * I / N),
|
|
46
|
+
edge(I, R, rate=γ * I),
|
|
47
|
+
edge(R, S, rate=ξ * R),
|
|
48
|
+
]
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"""Defines a copmartmental IPM mirroring the SPARSEMOD COVID model."""
|
|
2
|
+
|
|
3
|
+
from sympy import Max
|
|
4
|
+
|
|
5
|
+
from epymorph.attribute import AttributeDef
|
|
6
|
+
from epymorph.compartment_model import CompartmentModel, compartment, edge, fork
|
|
7
|
+
from epymorph.data_shape import Shapes
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class SparseMod(CompartmentModel):
|
|
11
|
+
"""A model similar to one used in sparsemod."""
|
|
12
|
+
|
|
13
|
+
compartments = [
|
|
14
|
+
compartment("S", description="susceptible"),
|
|
15
|
+
compartment("E", description="exposed"),
|
|
16
|
+
compartment("I_a", description="infected asymptomatic"),
|
|
17
|
+
compartment("I_p", description="infected presymptomatic"),
|
|
18
|
+
compartment("I_s", description="infected symptomatic"),
|
|
19
|
+
compartment("I_b", description="infected bed-rest"),
|
|
20
|
+
compartment("I_h", description="infected hospitalized"),
|
|
21
|
+
compartment("I_c1", description="infected in ICU"),
|
|
22
|
+
compartment("I_c2", description="infected in ICU Step-Down"),
|
|
23
|
+
compartment("D", description="deceased"),
|
|
24
|
+
compartment("R", description="recovered"),
|
|
25
|
+
]
|
|
26
|
+
|
|
27
|
+
requirements = [
|
|
28
|
+
AttributeDef("beta", type=float, shape=Shapes.TxN),
|
|
29
|
+
AttributeDef("omega_1", type=float, shape=Shapes.TxN),
|
|
30
|
+
AttributeDef("omega_2", type=float, shape=Shapes.TxN),
|
|
31
|
+
AttributeDef("delta_1", type=float, shape=Shapes.TxN),
|
|
32
|
+
AttributeDef("delta_2", type=float, shape=Shapes.TxN),
|
|
33
|
+
AttributeDef("delta_3", type=float, shape=Shapes.TxN),
|
|
34
|
+
AttributeDef("delta_4", type=float, shape=Shapes.TxN),
|
|
35
|
+
AttributeDef("delta_5", type=float, shape=Shapes.TxN),
|
|
36
|
+
AttributeDef("gamma_a", type=float, shape=Shapes.TxN),
|
|
37
|
+
AttributeDef("gamma_b", type=float, shape=Shapes.TxN),
|
|
38
|
+
AttributeDef("gamma_c", type=float, shape=Shapes.TxN),
|
|
39
|
+
AttributeDef("rho_1", type=float, shape=Shapes.TxN),
|
|
40
|
+
AttributeDef("rho_2", type=float, shape=Shapes.TxN),
|
|
41
|
+
AttributeDef("rho_3", type=float, shape=Shapes.TxN),
|
|
42
|
+
AttributeDef("rho_4", type=float, shape=Shapes.TxN),
|
|
43
|
+
AttributeDef("rho_5", type=float, shape=Shapes.TxN),
|
|
44
|
+
]
|
|
45
|
+
|
|
46
|
+
def edges(self, symbols):
|
|
47
|
+
[S, E, I_a, I_p, I_s, I_b, I_h, I_c1, I_c2, D, R] = symbols.all_compartments
|
|
48
|
+
[
|
|
49
|
+
beta,
|
|
50
|
+
omega_1,
|
|
51
|
+
omega_2,
|
|
52
|
+
delta_1,
|
|
53
|
+
delta_2,
|
|
54
|
+
delta_3,
|
|
55
|
+
delta_4,
|
|
56
|
+
delta_5,
|
|
57
|
+
gamma_a,
|
|
58
|
+
gamma_b,
|
|
59
|
+
gamma_c,
|
|
60
|
+
rho_1,
|
|
61
|
+
rho_2,
|
|
62
|
+
rho_3,
|
|
63
|
+
rho_4,
|
|
64
|
+
rho_5,
|
|
65
|
+
] = symbols.all_requirements
|
|
66
|
+
|
|
67
|
+
# formulate the divisor so as to avoid dividing by zero;
|
|
68
|
+
# this is safe in this instance becase if the denominator is zero,
|
|
69
|
+
# the numerator must also be zero
|
|
70
|
+
N = Max(1, S + E + I_a + I_p + I_s + I_b + I_h + I_c1 + I_c2 + R)
|
|
71
|
+
lambda_1 = (omega_1 * I_a + I_p + I_s + I_b + omega_2 * (I_h + I_c1 + I_c2)) / N
|
|
72
|
+
|
|
73
|
+
return [
|
|
74
|
+
edge(S, E, rate=beta * lambda_1 * S),
|
|
75
|
+
fork(
|
|
76
|
+
edge(E, I_a, rate=E * delta_1 * rho_1),
|
|
77
|
+
edge(E, I_p, rate=E * delta_1 * (1 - rho_1)),
|
|
78
|
+
),
|
|
79
|
+
edge(I_p, I_s, rate=I_p * delta_2),
|
|
80
|
+
fork(
|
|
81
|
+
edge(I_s, I_h, rate=I_s * delta_3 * rho_2),
|
|
82
|
+
edge(I_s, I_c1, rate=I_s * delta_3 * rho_3),
|
|
83
|
+
edge(I_s, I_b, rate=I_s * delta_3 * (1 - rho_2 - rho_3)),
|
|
84
|
+
),
|
|
85
|
+
fork(
|
|
86
|
+
edge(I_h, I_c1, rate=I_h * delta_4 * rho_4),
|
|
87
|
+
edge(I_h, R, rate=I_h * delta_4 * (1 - rho_4)),
|
|
88
|
+
),
|
|
89
|
+
fork(
|
|
90
|
+
edge(I_c1, D, rate=I_c1 * delta_5 * rho_5),
|
|
91
|
+
edge(I_c1, I_c2, rate=I_c1 * delta_5 * (1 - rho_5)),
|
|
92
|
+
),
|
|
93
|
+
edge(I_a, R, rate=I_a * gamma_a),
|
|
94
|
+
edge(I_b, R, rate=I_b * gamma_b),
|
|
95
|
+
edge(I_c2, R, rate=I_c2 * gamma_c),
|
|
96
|
+
]
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"""epymorph's collection of built-in movement models."""
|
|
2
|
+
|
|
3
|
+
from epymorph.data.mm.centroids import Centroids
|
|
4
|
+
from epymorph.data.mm.flat import Flat
|
|
5
|
+
from epymorph.data.mm.icecube import Icecube
|
|
6
|
+
from epymorph.data.mm.no import No
|
|
7
|
+
from epymorph.data.mm.pei import Pei
|
|
8
|
+
from epymorph.data.mm.sparsemod import Sparsemod
|
|
9
|
+
|
|
10
|
+
### WARNING ##########################################
|
|
11
|
+
# This file is generated by the data_exports script. #
|
|
12
|
+
# Manual changes to this file will be lost. #
|
|
13
|
+
######################################################
|
|
14
|
+
|
|
15
|
+
__all__ = [
|
|
16
|
+
"Centroids",
|
|
17
|
+
"Flat",
|
|
18
|
+
"Icecube",
|
|
19
|
+
"No",
|
|
20
|
+
"Pei",
|
|
21
|
+
"Sparsemod",
|
|
22
|
+
]
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
from functools import cached_property
|
|
2
|
+
|
|
3
|
+
import numpy as np
|
|
4
|
+
from numpy.typing import NDArray
|
|
5
|
+
|
|
6
|
+
from epymorph.attribute import AttributeDef
|
|
7
|
+
from epymorph.data_shape import Shapes
|
|
8
|
+
from epymorph.data_type import CentroidType, SimDType
|
|
9
|
+
from epymorph.movement_model import EveryDay, MovementClause, MovementModel
|
|
10
|
+
from epymorph.simulation import Tick, TickDelta, TickIndex
|
|
11
|
+
from epymorph.util import pairwise_haversine, row_normalize
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class CentroidsClause(MovementClause):
|
|
15
|
+
"""The clause of the centroids model."""
|
|
16
|
+
|
|
17
|
+
requirements = (
|
|
18
|
+
AttributeDef(
|
|
19
|
+
"population", int, Shapes.N, comment="The total population at each node."
|
|
20
|
+
),
|
|
21
|
+
AttributeDef(
|
|
22
|
+
"centroid",
|
|
23
|
+
CentroidType,
|
|
24
|
+
Shapes.N,
|
|
25
|
+
comment="The centroids for each node as (longitude, latitude) tuples.",
|
|
26
|
+
),
|
|
27
|
+
AttributeDef(
|
|
28
|
+
"phi",
|
|
29
|
+
float,
|
|
30
|
+
Shapes.Scalar,
|
|
31
|
+
default_value=40.0,
|
|
32
|
+
comment="Influences the distance that movers tend to travel.",
|
|
33
|
+
),
|
|
34
|
+
AttributeDef(
|
|
35
|
+
"commuter_proportion",
|
|
36
|
+
float,
|
|
37
|
+
Shapes.Scalar,
|
|
38
|
+
default_value=0.1,
|
|
39
|
+
comment="The proportion of the total population which commutes.",
|
|
40
|
+
),
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
predicate = EveryDay()
|
|
44
|
+
leaves = TickIndex(step=0)
|
|
45
|
+
returns = TickDelta(step=1, days=0)
|
|
46
|
+
|
|
47
|
+
@cached_property
|
|
48
|
+
def dispersal_kernel(self) -> NDArray[np.float64]:
|
|
49
|
+
"""
|
|
50
|
+
The NxN matrix or dispersal kernel describing the tendency for movers to move
|
|
51
|
+
to a particular location. In this model, the kernel is:
|
|
52
|
+
1 / e ^ (distance / phi)
|
|
53
|
+
which is then row-normalized.
|
|
54
|
+
"""
|
|
55
|
+
centroid = self.data("centroid")
|
|
56
|
+
phi = self.data("phi")
|
|
57
|
+
distance = pairwise_haversine(centroid)
|
|
58
|
+
return row_normalize(1 / np.exp(distance / phi))
|
|
59
|
+
|
|
60
|
+
def evaluate(self, tick: Tick) -> NDArray[np.int64]:
|
|
61
|
+
pop = self.data("population")
|
|
62
|
+
comm_prop = self.data("commuter_proportion")
|
|
63
|
+
n_commuters = np.floor(pop * comm_prop).astype(SimDType)
|
|
64
|
+
return self.rng.multinomial(n_commuters, self.dispersal_kernel)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
class Centroids(MovementModel):
|
|
68
|
+
"""
|
|
69
|
+
The centroids MM describes a basic commuter movement where a fixed proportion
|
|
70
|
+
of the population commutes every day, travels to another location for 1/3 of a day
|
|
71
|
+
(with a location likelihood that decreases with distance), and then returns home for
|
|
72
|
+
the remaining 2/3 of the day.
|
|
73
|
+
"""
|
|
74
|
+
|
|
75
|
+
steps = (1 / 3, 2 / 3)
|
|
76
|
+
clauses = (CentroidsClause(),)
|
epymorph/data/mm/flat.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
from functools import cached_property
|
|
2
|
+
|
|
3
|
+
import numpy as np
|
|
4
|
+
from numpy.typing import NDArray
|
|
5
|
+
|
|
6
|
+
from epymorph.attribute import AttributeDef
|
|
7
|
+
from epymorph.data_shape import Shapes
|
|
8
|
+
from epymorph.data_type import SimDType
|
|
9
|
+
from epymorph.movement_model import EveryDay, MovementClause, MovementModel
|
|
10
|
+
from epymorph.simulation import Tick, TickDelta, TickIndex
|
|
11
|
+
from epymorph.util import row_normalize
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class FlatClause(MovementClause):
|
|
15
|
+
"""The clause of the flat model."""
|
|
16
|
+
|
|
17
|
+
requirements = (
|
|
18
|
+
AttributeDef(
|
|
19
|
+
"population", int, Shapes.N, comment="The total population at each node."
|
|
20
|
+
),
|
|
21
|
+
AttributeDef(
|
|
22
|
+
"commuter_proportion",
|
|
23
|
+
float,
|
|
24
|
+
Shapes.Scalar,
|
|
25
|
+
default_value=0.1,
|
|
26
|
+
comment="The proportion of the total population which commutes.",
|
|
27
|
+
),
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
predicate = EveryDay()
|
|
31
|
+
leaves = TickIndex(step=0)
|
|
32
|
+
returns = TickDelta(step=1, days=0)
|
|
33
|
+
|
|
34
|
+
@cached_property
|
|
35
|
+
def dispersal_kernel(self) -> NDArray[np.float64]:
|
|
36
|
+
"""
|
|
37
|
+
The NxN matrix or dispersal kernel describing the tendency for movers to move
|
|
38
|
+
to a particular location. In this model, the kernel is full of 1s
|
|
39
|
+
except for 0s on the diagonal, which is then row-normalized.
|
|
40
|
+
Effectively: every destination is equally likely.
|
|
41
|
+
"""
|
|
42
|
+
ones = np.ones((self.scope.nodes, self.scope.nodes))
|
|
43
|
+
np.fill_diagonal(ones, 0)
|
|
44
|
+
return row_normalize(ones)
|
|
45
|
+
|
|
46
|
+
def evaluate(self, tick: Tick) -> NDArray[SimDType]:
|
|
47
|
+
pop = self.data("population")
|
|
48
|
+
comm_prop = self.data("commuter_proportion")
|
|
49
|
+
n_commuters = np.floor(pop * comm_prop).astype(SimDType)
|
|
50
|
+
return self.rng.multinomial(n_commuters, self.dispersal_kernel)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class Flat(MovementModel):
|
|
54
|
+
"""
|
|
55
|
+
This model evenly weights the probability of movement to all other nodes.
|
|
56
|
+
It uses parameter 'commuter_proportion' to determine how many people should
|
|
57
|
+
be moving, based on the total normal population of each node.
|
|
58
|
+
"""
|
|
59
|
+
|
|
60
|
+
steps = (1 / 3, 2 / 3)
|
|
61
|
+
clauses = (FlatClause(),)
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
from numpy.typing import NDArray
|
|
3
|
+
|
|
4
|
+
from epymorph.attribute import AttributeDef
|
|
5
|
+
from epymorph.data_shape import Shapes
|
|
6
|
+
from epymorph.data_type import SimDType
|
|
7
|
+
from epymorph.movement_model import EveryDay, MovementClause, MovementModel
|
|
8
|
+
from epymorph.simulation import Tick, TickDelta, TickIndex
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class IcecubeClause(MovementClause):
|
|
12
|
+
"""The clause of the icecube model."""
|
|
13
|
+
|
|
14
|
+
requirements = (
|
|
15
|
+
AttributeDef(
|
|
16
|
+
"population", int, Shapes.N, comment="The total population at each node."
|
|
17
|
+
),
|
|
18
|
+
AttributeDef(
|
|
19
|
+
"commuter_proportion",
|
|
20
|
+
float,
|
|
21
|
+
Shapes.Scalar,
|
|
22
|
+
default_value=0.1,
|
|
23
|
+
comment="The proportion of the total population which commutes.",
|
|
24
|
+
),
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
predicate = EveryDay()
|
|
28
|
+
leaves = TickIndex(step=0)
|
|
29
|
+
returns = TickDelta(step=1, days=0)
|
|
30
|
+
|
|
31
|
+
def evaluate(self, tick: Tick) -> NDArray[np.int64]:
|
|
32
|
+
N = self.scope.nodes
|
|
33
|
+
pop = self.data("population")
|
|
34
|
+
comm_prop = self.data("commuter_proportion")
|
|
35
|
+
commuters = np.zeros((N, N), dtype=SimDType)
|
|
36
|
+
for src in range(N):
|
|
37
|
+
if (src + 1) < N:
|
|
38
|
+
commuters[src, src + 1] = pop[src] * comm_prop
|
|
39
|
+
return commuters
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class Icecube(MovementModel):
|
|
43
|
+
"""
|
|
44
|
+
A toy example: ice cube tray movement movement model
|
|
45
|
+
Each state sends a fixed number of commuters to the next
|
|
46
|
+
state in the line (without wraparound).
|
|
47
|
+
"""
|
|
48
|
+
|
|
49
|
+
steps = (1 / 2, 1 / 2)
|
|
50
|
+
clauses = (IcecubeClause(),)
|
epymorph/data/mm/no.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
from numpy.typing import NDArray
|
|
3
|
+
|
|
4
|
+
from epymorph.data_type import SimDType
|
|
5
|
+
from epymorph.movement_model import EveryDay, MovementClause, MovementModel
|
|
6
|
+
from epymorph.simulation import Tick, TickDelta, TickIndex
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class NoClause(MovementClause):
|
|
10
|
+
"""The clause of the "no" model."""
|
|
11
|
+
|
|
12
|
+
requirements = ()
|
|
13
|
+
predicate = EveryDay()
|
|
14
|
+
leaves = TickIndex(step=0)
|
|
15
|
+
returns = TickDelta(step=0, days=0)
|
|
16
|
+
|
|
17
|
+
def evaluate(self, tick: Tick) -> NDArray[np.int64]:
|
|
18
|
+
N = self.scope.nodes
|
|
19
|
+
return np.zeros((N, N), dtype=SimDType)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class No(MovementModel):
|
|
23
|
+
"""
|
|
24
|
+
No movement at all. This is handy for cases when you want to disable movement
|
|
25
|
+
in an experiment, or for testing.
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
steps = (1.0,)
|
|
29
|
+
clauses = (NoClause(),)
|
epymorph/data/mm/pei.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
from functools import cached_property
|
|
2
|
+
|
|
3
|
+
import numpy as np
|
|
4
|
+
from numpy.typing import NDArray
|
|
5
|
+
|
|
6
|
+
from epymorph.attribute import AttributeDef
|
|
7
|
+
from epymorph.data_shape import Shapes
|
|
8
|
+
from epymorph.data_type import SimDType
|
|
9
|
+
from epymorph.movement_model import EveryDay, MovementClause, MovementModel
|
|
10
|
+
from epymorph.simulation import Tick, TickDelta, TickIndex
|
|
11
|
+
from epymorph.util import row_normalize
|
|
12
|
+
|
|
13
|
+
_COMMUTERS_ATTRIB = AttributeDef(
|
|
14
|
+
"commuters", int, Shapes.NxN, comment="A node-to-node commuters marix."
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class Commuters(MovementClause):
|
|
19
|
+
"""The commuter clause of the pei model."""
|
|
20
|
+
|
|
21
|
+
requirements = (
|
|
22
|
+
_COMMUTERS_ATTRIB,
|
|
23
|
+
AttributeDef(
|
|
24
|
+
"move_control",
|
|
25
|
+
float,
|
|
26
|
+
Shapes.Scalar,
|
|
27
|
+
default_value=0.9,
|
|
28
|
+
comment=(
|
|
29
|
+
"A factor which modulates the number of commuters "
|
|
30
|
+
"by conducting a binomial draw with this probability "
|
|
31
|
+
"and the expected commuters from the commuters matrix."
|
|
32
|
+
),
|
|
33
|
+
),
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
predicate = EveryDay()
|
|
37
|
+
leaves = TickIndex(step=0)
|
|
38
|
+
returns = TickDelta(step=1, days=0)
|
|
39
|
+
|
|
40
|
+
@cached_property
|
|
41
|
+
def commuters_by_node(self) -> NDArray[SimDType]:
|
|
42
|
+
"""Total commuters living in each state."""
|
|
43
|
+
commuters = self.data("commuters")
|
|
44
|
+
return np.sum(commuters, axis=1)
|
|
45
|
+
|
|
46
|
+
@cached_property
|
|
47
|
+
def commuting_probability(self) -> NDArray[np.float64]:
|
|
48
|
+
"""
|
|
49
|
+
Commuters as a ratio to the total commuters living in that state.
|
|
50
|
+
For cases where there are no commuters, avoid div-by-0 errors
|
|
51
|
+
"""
|
|
52
|
+
commuters = self.data("commuters")
|
|
53
|
+
return row_normalize(commuters)
|
|
54
|
+
|
|
55
|
+
def evaluate(self, tick: Tick) -> NDArray[np.int64]:
|
|
56
|
+
move_control = self.data("move_control")
|
|
57
|
+
actual = self.rng.binomial(self.commuters_by_node, move_control)
|
|
58
|
+
return self.rng.multinomial(actual, self.commuting_probability)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
class Dispersers(MovementClause):
|
|
62
|
+
"""The dispersers clause of the pei model."""
|
|
63
|
+
|
|
64
|
+
requirements = (
|
|
65
|
+
_COMMUTERS_ATTRIB,
|
|
66
|
+
AttributeDef(
|
|
67
|
+
"theta",
|
|
68
|
+
float,
|
|
69
|
+
Shapes.Scalar,
|
|
70
|
+
default_value=0.1,
|
|
71
|
+
comment=(
|
|
72
|
+
"A factor which allows for randomized movement "
|
|
73
|
+
"by conducting a poisson draw with this factor "
|
|
74
|
+
"times the average number of commuters between two nodes "
|
|
75
|
+
"from the commuters matrix."
|
|
76
|
+
),
|
|
77
|
+
),
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
predicate = EveryDay()
|
|
81
|
+
leaves = TickIndex(step=0)
|
|
82
|
+
returns = TickDelta(step=1, days=0)
|
|
83
|
+
|
|
84
|
+
@cached_property
|
|
85
|
+
def commuters_average(self) -> NDArray[SimDType]:
|
|
86
|
+
"""Average commuters between locations."""
|
|
87
|
+
commuters = self.data("commuters")
|
|
88
|
+
return (commuters + commuters.T) // 2
|
|
89
|
+
|
|
90
|
+
def evaluate(self, tick: Tick) -> NDArray[SimDType]:
|
|
91
|
+
theta = self.data("theta")
|
|
92
|
+
return self.rng.poisson(theta * self.commuters_average)
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
class Pei(MovementModel):
|
|
96
|
+
"""
|
|
97
|
+
Modeled after the Pei influenza paper, this model simulates
|
|
98
|
+
two types of movers -- regular commuters and more-randomized dispersers.
|
|
99
|
+
Each is somewhat stochastic but adhere to the general shape dictated
|
|
100
|
+
by the commuters array. Both kinds of movement can be "tuned" through
|
|
101
|
+
their respective parameters: move_control and theta.
|
|
102
|
+
"""
|
|
103
|
+
|
|
104
|
+
steps = (1 / 3, 2 / 3)
|
|
105
|
+
clauses = (Commuters(), Dispersers())
|