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.
Files changed (102) hide show
  1. epymorph/__init__.py +7 -0
  2. epymorph/__main__.py +16 -0
  3. epymorph/adrio/__init__.py +0 -0
  4. epymorph/adrio/acs5.py +602 -0
  5. epymorph/adrio/adrio.py +215 -0
  6. epymorph/adrio/cdc.py +781 -0
  7. epymorph/adrio/commuting_flows.py +237 -0
  8. epymorph/adrio/csv.py +387 -0
  9. epymorph/adrio/humidity.py +84 -0
  10. epymorph/adrio/lodes.py +624 -0
  11. epymorph/adrio/numpy.py +119 -0
  12. epymorph/adrio/prism.py +543 -0
  13. epymorph/adrio/respiratory_cdc.py +489 -0
  14. epymorph/adrio/us_tiger.py +217 -0
  15. epymorph/attribute.py +301 -0
  16. epymorph/cache.py +494 -0
  17. epymorph/cli/__init__.py +0 -0
  18. epymorph/cli/cache.py +77 -0
  19. epymorph/cli/main.py +29 -0
  20. epymorph/compartment_model.py +1335 -0
  21. epymorph/data/__init__.py +1 -0
  22. epymorph/data/ipm/__init__.py +22 -0
  23. epymorph/data/ipm/no.py +14 -0
  24. epymorph/data/ipm/pei.py +40 -0
  25. epymorph/data/ipm/seirs.py +50 -0
  26. epymorph/data/ipm/sirh.py +55 -0
  27. epymorph/data/ipm/sirs.py +48 -0
  28. epymorph/data/ipm/sparsemod.py +96 -0
  29. epymorph/data/mm/__init__.py +22 -0
  30. epymorph/data/mm/centroids.py +76 -0
  31. epymorph/data/mm/flat.py +61 -0
  32. epymorph/data/mm/icecube.py +50 -0
  33. epymorph/data/mm/no.py +29 -0
  34. epymorph/data/mm/pei.py +105 -0
  35. epymorph/data/mm/sparsemod.py +73 -0
  36. epymorph/data/pei.py +424 -0
  37. epymorph/data_shape.py +475 -0
  38. epymorph/data_type.py +129 -0
  39. epymorph/data_usage.py +171 -0
  40. epymorph/database.py +1010 -0
  41. epymorph/error.py +183 -0
  42. epymorph/event.py +188 -0
  43. epymorph/geography/__init__.py +0 -0
  44. epymorph/geography/custom.py +61 -0
  45. epymorph/geography/scope.py +214 -0
  46. epymorph/geography/us_census.py +622 -0
  47. epymorph/geography/us_geography.py +278 -0
  48. epymorph/geography/us_tiger.py +755 -0
  49. epymorph/initializer.py +596 -0
  50. epymorph/kit.py +90 -0
  51. epymorph/log/__init__.py +0 -0
  52. epymorph/log/file.py +108 -0
  53. epymorph/log/messaging.py +129 -0
  54. epymorph/log/movement.py +151 -0
  55. epymorph/movement_model.py +274 -0
  56. epymorph/parameter_fitting/__init__.py +0 -0
  57. epymorph/parameter_fitting/distribution.py +68 -0
  58. epymorph/parameter_fitting/dynamics.py +19 -0
  59. epymorph/parameter_fitting/filter/__init__.py +0 -0
  60. epymorph/parameter_fitting/filter/base_filter.py +50 -0
  61. epymorph/parameter_fitting/filter/particle.py +32 -0
  62. epymorph/parameter_fitting/filter/particle_filter.py +296 -0
  63. epymorph/parameter_fitting/likelihood.py +54 -0
  64. epymorph/parameter_fitting/output.py +73 -0
  65. epymorph/parameter_fitting/particlefilter_simulation.py +175 -0
  66. epymorph/parameter_fitting/perturbation.py +19 -0
  67. epymorph/parameter_fitting/utils/__init__.py +0 -0
  68. epymorph/parameter_fitting/utils/data_loader.py +111 -0
  69. epymorph/parameter_fitting/utils/epymorph_simulation.py +119 -0
  70. epymorph/parameter_fitting/utils/observations.py +63 -0
  71. epymorph/parameter_fitting/utils/parameter_estimation.py +49 -0
  72. epymorph/parameter_fitting/utils/params_perturb.py +43 -0
  73. epymorph/parameter_fitting/utils/particle_initializer.py +95 -0
  74. epymorph/parameter_fitting/utils/resampler.py +249 -0
  75. epymorph/parameter_fitting/utils/utils.py +101 -0
  76. epymorph/params.py +248 -0
  77. epymorph/py.typed +0 -0
  78. epymorph/rume.py +700 -0
  79. epymorph/simulation.py +706 -0
  80. epymorph/simulator/__init__.py +0 -0
  81. epymorph/simulator/basic/__init__.py +0 -0
  82. epymorph/simulator/basic/basic_simulator.py +145 -0
  83. epymorph/simulator/basic/ipm_exec.py +406 -0
  84. epymorph/simulator/basic/mm_exec.py +197 -0
  85. epymorph/simulator/basic/output.py +181 -0
  86. epymorph/simulator/world.py +83 -0
  87. epymorph/simulator/world_list.py +196 -0
  88. epymorph/strata.py +12 -0
  89. epymorph/sympy_shim.py +55 -0
  90. epymorph/time.py +649 -0
  91. epymorph/tools/__init__.py +0 -0
  92. epymorph/tools/data.py +218 -0
  93. epymorph/tools/ipm_diagram.py +165 -0
  94. epymorph/tools/out_map.py +426 -0
  95. epymorph/tools/out_plot.py +412 -0
  96. epymorph/tools/out_table.py +575 -0
  97. epymorph/util.py +825 -0
  98. epymorph-1.0.0b0.dist-info/METADATA +57 -0
  99. epymorph-1.0.0b0.dist-info/RECORD +102 -0
  100. epymorph-1.0.0b0.dist-info/WHEEL +5 -0
  101. epymorph-1.0.0b0.dist-info/entry_points.txt +2 -0
  102. epymorph-1.0.0b0.dist-info/top_level.txt +1 -0
@@ -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)