epymorph 1.0.0b0__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 +602 -0
- epymorph/adrio/adrio.py +215 -0
- epymorph/adrio/cdc.py +781 -0
- epymorph/adrio/commuting_flows.py +237 -0
- epymorph/adrio/csv.py +387 -0
- epymorph/adrio/humidity.py +84 -0
- epymorph/adrio/lodes.py +624 -0
- epymorph/adrio/numpy.py +119 -0
- epymorph/adrio/prism.py +543 -0
- epymorph/adrio/respiratory_cdc.py +489 -0
- epymorph/adrio/us_tiger.py +217 -0
- epymorph/attribute.py +301 -0
- epymorph/cache.py +494 -0
- epymorph/cli/__init__.py +0 -0
- epymorph/cli/cache.py +77 -0
- epymorph/cli/main.py +29 -0
- epymorph/compartment_model.py +1335 -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 +475 -0
- epymorph/data_type.py +129 -0
- epymorph/data_usage.py +171 -0
- epymorph/database.py +1010 -0
- epymorph/error.py +183 -0
- epymorph/event.py +188 -0
- epymorph/geography/__init__.py +0 -0
- epymorph/geography/custom.py +61 -0
- epymorph/geography/scope.py +214 -0
- epymorph/geography/us_census.py +622 -0
- epymorph/geography/us_geography.py +278 -0
- epymorph/geography/us_tiger.py +755 -0
- epymorph/initializer.py +596 -0
- epymorph/kit.py +90 -0
- epymorph/log/__init__.py +0 -0
- epymorph/log/file.py +108 -0
- epymorph/log/messaging.py +129 -0
- epymorph/log/movement.py +151 -0
- epymorph/movement_model.py +274 -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 +111 -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 +249 -0
- epymorph/parameter_fitting/utils/utils.py +101 -0
- epymorph/params.py +248 -0
- epymorph/py.typed +0 -0
- epymorph/rume.py +700 -0
- epymorph/simulation.py +706 -0
- epymorph/simulator/__init__.py +0 -0
- epymorph/simulator/basic/__init__.py +0 -0
- epymorph/simulator/basic/basic_simulator.py +145 -0
- epymorph/simulator/basic/ipm_exec.py +406 -0
- epymorph/simulator/basic/mm_exec.py +197 -0
- epymorph/simulator/basic/output.py +181 -0
- epymorph/simulator/world.py +83 -0
- epymorph/simulator/world_list.py +196 -0
- epymorph/strata.py +12 -0
- epymorph/sympy_shim.py +55 -0
- epymorph/time.py +649 -0
- epymorph/tools/__init__.py +0 -0
- epymorph/tools/data.py +218 -0
- epymorph/tools/ipm_diagram.py +165 -0
- epymorph/tools/out_map.py +426 -0
- epymorph/tools/out_plot.py +412 -0
- epymorph/tools/out_table.py +575 -0
- epymorph/util.py +825 -0
- epymorph-1.0.0b0.dist-info/METADATA +57 -0
- epymorph-1.0.0b0.dist-info/RECORD +102 -0
- epymorph-1.0.0b0.dist-info/WHEEL +5 -0
- epymorph-1.0.0b0.dist-info/entry_points.txt +2 -0
- epymorph-1.0.0b0.dist-info/top_level.txt +1 -0
epymorph/adrio/adrio.py
ADDED
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Implements the base class for all ADRIOs, as well as some general-purpose
|
|
3
|
+
ADRIO implementations.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
import functools
|
|
7
|
+
from abc import abstractmethod
|
|
8
|
+
from time import perf_counter
|
|
9
|
+
from typing import Callable, TypeVar, final
|
|
10
|
+
|
|
11
|
+
import numpy as np
|
|
12
|
+
from numpy.typing import NDArray
|
|
13
|
+
from typing_extensions import override
|
|
14
|
+
|
|
15
|
+
from epymorph.attribute import NAME_PLACEHOLDER, AbsoluteName, AttributeDef
|
|
16
|
+
from epymorph.compartment_model import BaseCompartmentModel
|
|
17
|
+
from epymorph.data_shape import Shapes
|
|
18
|
+
from epymorph.data_type import AttributeArray
|
|
19
|
+
from epymorph.data_usage import DataEstimate, EmptyDataEstimate
|
|
20
|
+
from epymorph.database import (
|
|
21
|
+
DataResolver,
|
|
22
|
+
evaluate_param,
|
|
23
|
+
)
|
|
24
|
+
from epymorph.event import ADRIOProgress, DownloadActivity, EventBus
|
|
25
|
+
from epymorph.geography.scope import GeoScope
|
|
26
|
+
from epymorph.simulation import SimulationFunction
|
|
27
|
+
from epymorph.time import TimeFrame
|
|
28
|
+
|
|
29
|
+
ResultDType = TypeVar("ResultDType", bound=np.generic)
|
|
30
|
+
"""The result type of an Adrio."""
|
|
31
|
+
|
|
32
|
+
ProgressCallback = Callable[[float, DownloadActivity | None], None]
|
|
33
|
+
|
|
34
|
+
_events = EventBus()
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class ADRIO(SimulationFunction[NDArray[ResultDType]]):
|
|
38
|
+
"""
|
|
39
|
+
ADRIO (or Abstract Data Resource Interface Object) are functions which are intended
|
|
40
|
+
to load data from external sources for epymorph simulations. This may be from
|
|
41
|
+
web APIs, local files or database, or anything imaginable.
|
|
42
|
+
"""
|
|
43
|
+
|
|
44
|
+
def estimate_data(self) -> DataEstimate:
|
|
45
|
+
"""Estimate the data usage for this ADRIO in a RUME.
|
|
46
|
+
If a reasonable estimate cannot be made, return EmptyDataEstimate."""
|
|
47
|
+
return EmptyDataEstimate(self.class_name)
|
|
48
|
+
|
|
49
|
+
@abstractmethod
|
|
50
|
+
def evaluate_adrio(self) -> NDArray[ResultDType]:
|
|
51
|
+
"""Implement this method to provide logic for the function.
|
|
52
|
+
Use self methods and properties to access the simulation context or defer
|
|
53
|
+
processing to another function.
|
|
54
|
+
"""
|
|
55
|
+
|
|
56
|
+
@override
|
|
57
|
+
@final
|
|
58
|
+
def evaluate(self) -> NDArray[ResultDType]:
|
|
59
|
+
"""The ADRIO parent class overrides this to provide ADRIO-specific
|
|
60
|
+
functionality. ADRIO implementations should override `evaluate_adrio`."""
|
|
61
|
+
_events.on_adrio_progress.publish(
|
|
62
|
+
ADRIOProgress(
|
|
63
|
+
adrio_name=self.class_name,
|
|
64
|
+
attribute=self.name,
|
|
65
|
+
final=False,
|
|
66
|
+
ratio_complete=0,
|
|
67
|
+
download=None,
|
|
68
|
+
duration=None,
|
|
69
|
+
)
|
|
70
|
+
)
|
|
71
|
+
t0 = perf_counter()
|
|
72
|
+
result = self.evaluate_adrio()
|
|
73
|
+
t1 = perf_counter()
|
|
74
|
+
_events.on_adrio_progress.publish(
|
|
75
|
+
ADRIOProgress(
|
|
76
|
+
adrio_name=self.class_name,
|
|
77
|
+
attribute=self.name,
|
|
78
|
+
final=True,
|
|
79
|
+
ratio_complete=1,
|
|
80
|
+
download=None,
|
|
81
|
+
duration=t1 - t0,
|
|
82
|
+
)
|
|
83
|
+
)
|
|
84
|
+
return result
|
|
85
|
+
|
|
86
|
+
@final
|
|
87
|
+
def progress(
|
|
88
|
+
self,
|
|
89
|
+
ratio_complete: float,
|
|
90
|
+
download: DownloadActivity | None = None,
|
|
91
|
+
) -> None:
|
|
92
|
+
"""Emit a progress event."""
|
|
93
|
+
_events.on_adrio_progress.publish(
|
|
94
|
+
ADRIOProgress(
|
|
95
|
+
adrio_name=self.class_name,
|
|
96
|
+
attribute=self.name,
|
|
97
|
+
final=False,
|
|
98
|
+
ratio_complete=ratio_complete,
|
|
99
|
+
download=download,
|
|
100
|
+
duration=None,
|
|
101
|
+
)
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
@evaluate_param.register
|
|
106
|
+
def _(
|
|
107
|
+
value: ADRIO,
|
|
108
|
+
name: AbsoluteName,
|
|
109
|
+
data: DataResolver,
|
|
110
|
+
scope: GeoScope | None,
|
|
111
|
+
time_frame: TimeFrame | None,
|
|
112
|
+
ipm: BaseCompartmentModel | None,
|
|
113
|
+
rng: np.random.Generator | None,
|
|
114
|
+
) -> AttributeArray:
|
|
115
|
+
# depth-first evaluation guarantees `data` has our dependencies.
|
|
116
|
+
sim_func = value.with_context_internal(name, data, scope, time_frame, ipm, rng)
|
|
117
|
+
return np.asarray(sim_func.evaluate())
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
AdrioClassT = TypeVar("AdrioClassT", bound=type[ADRIO])
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def adrio_cache(cls: AdrioClassT) -> AdrioClassT:
|
|
124
|
+
"""Adrio class decorator to add result-caching behavior."""
|
|
125
|
+
|
|
126
|
+
orig_with_context = cls.with_context_internal
|
|
127
|
+
cached_instance: AdrioClassT | None = None
|
|
128
|
+
cached_hash: int | None = None
|
|
129
|
+
|
|
130
|
+
@functools.wraps(orig_with_context)
|
|
131
|
+
def with_context_internal(
|
|
132
|
+
self,
|
|
133
|
+
name: AbsoluteName = NAME_PLACEHOLDER,
|
|
134
|
+
data: DataResolver | None = None,
|
|
135
|
+
scope: GeoScope | None = None,
|
|
136
|
+
time_frame: TimeFrame | None = None,
|
|
137
|
+
ipm: BaseCompartmentModel | None = None,
|
|
138
|
+
rng: np.random.Generator | None = None,
|
|
139
|
+
):
|
|
140
|
+
if data is None:
|
|
141
|
+
req_hashes = ()
|
|
142
|
+
else:
|
|
143
|
+
req_hashes = (
|
|
144
|
+
data.resolve(name.with_id(r.name), r).tobytes()
|
|
145
|
+
for r in self.requirements
|
|
146
|
+
)
|
|
147
|
+
ipm_hash = None
|
|
148
|
+
if ipm is not None:
|
|
149
|
+
C = ipm.num_compartments
|
|
150
|
+
E = ipm.num_events
|
|
151
|
+
ipm_hash = (ipm.__class__.__name__, C, E)
|
|
152
|
+
curr_hash = hash(tuple([str(name), scope, time_frame, ipm_hash, *req_hashes]))
|
|
153
|
+
nonlocal cached_instance, cached_hash
|
|
154
|
+
if cached_instance is None or cached_hash != curr_hash:
|
|
155
|
+
cached_instance = orig_with_context(
|
|
156
|
+
self, name, data, scope, time_frame, ipm, rng
|
|
157
|
+
)
|
|
158
|
+
cached_hash = curr_hash
|
|
159
|
+
return cached_instance
|
|
160
|
+
|
|
161
|
+
cls.with_context_internal = with_context_internal
|
|
162
|
+
return cls
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
class NodeID(ADRIO[np.str_]):
|
|
166
|
+
"""An ADRIO that provides the node IDs as they exist in the geo scope."""
|
|
167
|
+
|
|
168
|
+
@override
|
|
169
|
+
def evaluate_adrio(self) -> NDArray:
|
|
170
|
+
return self.scope.node_ids
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
class Scale(ADRIO[np.float64]):
|
|
174
|
+
"""Scales the result of another ADRIO by multiplying values by the given factor."""
|
|
175
|
+
|
|
176
|
+
_parent: ADRIO[np.float64]
|
|
177
|
+
_factor: float
|
|
178
|
+
|
|
179
|
+
def __init__(self, parent: ADRIO[np.float64], factor: float):
|
|
180
|
+
"""
|
|
181
|
+
Initializes scaling with the ADRIO to be scaled and with the factor to multiply
|
|
182
|
+
those resulting ADRIO values by.
|
|
183
|
+
|
|
184
|
+
Parameters
|
|
185
|
+
----------
|
|
186
|
+
parent : Adrio[np.int64 | np.float64]
|
|
187
|
+
The ADRIO to scale all values for.
|
|
188
|
+
factor : float
|
|
189
|
+
The factor to multiply all resulting ADRIO values by.
|
|
190
|
+
"""
|
|
191
|
+
self._parent = parent
|
|
192
|
+
self._factor = factor
|
|
193
|
+
|
|
194
|
+
@override
|
|
195
|
+
def evaluate_adrio(self) -> NDArray[np.float64]:
|
|
196
|
+
return self.defer(self._parent).astype(dtype=np.float64) * self._factor
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
class PopulationPerKM2(ADRIO[np.float64]):
|
|
200
|
+
"""
|
|
201
|
+
Calculates population density by combining the values from attributes named
|
|
202
|
+
`population` and `land_area_km2`. You must provide those attributes
|
|
203
|
+
separately.
|
|
204
|
+
"""
|
|
205
|
+
|
|
206
|
+
POPULATION = AttributeDef("population", int, Shapes.N)
|
|
207
|
+
LAND_AREA_KM2 = AttributeDef("land_area_km2", float, Shapes.N)
|
|
208
|
+
|
|
209
|
+
requirements = [POPULATION, LAND_AREA_KM2]
|
|
210
|
+
|
|
211
|
+
@override
|
|
212
|
+
def evaluate_adrio(self) -> NDArray[np.float64]:
|
|
213
|
+
pop = self.data(self.POPULATION)
|
|
214
|
+
area = self.data(self.LAND_AREA_KM2)
|
|
215
|
+
return (pop / area).astype(dtype=np.float64)
|