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.
Files changed (104) 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 +1378 -0
  5. epymorph/adrio/adrio.py +953 -0
  6. epymorph/adrio/cdc.py +1826 -0
  7. epymorph/adrio/commuting_flows.py +283 -0
  8. epymorph/adrio/csv.py +499 -0
  9. epymorph/adrio/humidity.py +206 -0
  10. epymorph/adrio/lodes.py +509 -0
  11. epymorph/adrio/numpy.py +160 -0
  12. epymorph/adrio/prism.py +345 -0
  13. epymorph/adrio/processing.py +1221 -0
  14. epymorph/adrio/soda.py +602 -0
  15. epymorph/adrio/us_tiger.py +289 -0
  16. epymorph/adrio/validation.py +382 -0
  17. epymorph/attribute.py +614 -0
  18. epymorph/cache.py +680 -0
  19. epymorph/cli/__init__.py +0 -0
  20. epymorph/cli/cache.py +78 -0
  21. epymorph/cli/main.py +29 -0
  22. epymorph/compartment_model.py +1873 -0
  23. epymorph/data/__init__.py +1 -0
  24. epymorph/data/ipm/__init__.py +22 -0
  25. epymorph/data/ipm/no.py +14 -0
  26. epymorph/data/ipm/pei.py +40 -0
  27. epymorph/data/ipm/seirs.py +50 -0
  28. epymorph/data/ipm/sirh.py +55 -0
  29. epymorph/data/ipm/sirs.py +48 -0
  30. epymorph/data/ipm/sparsemod.py +96 -0
  31. epymorph/data/mm/__init__.py +22 -0
  32. epymorph/data/mm/centroids.py +76 -0
  33. epymorph/data/mm/flat.py +61 -0
  34. epymorph/data/mm/icecube.py +50 -0
  35. epymorph/data/mm/no.py +29 -0
  36. epymorph/data/mm/pei.py +105 -0
  37. epymorph/data/mm/sparsemod.py +73 -0
  38. epymorph/data/pei.py +424 -0
  39. epymorph/data_shape.py +593 -0
  40. epymorph/data_type.py +185 -0
  41. epymorph/data_usage.py +258 -0
  42. epymorph/database.py +1301 -0
  43. epymorph/error.py +195 -0
  44. epymorph/event.py +196 -0
  45. epymorph/geography/__init__.py +0 -0
  46. epymorph/geography/custom.py +93 -0
  47. epymorph/geography/scope.py +457 -0
  48. epymorph/geography/us_census.py +1056 -0
  49. epymorph/geography/us_geography.py +502 -0
  50. epymorph/geography/us_tiger.py +1083 -0
  51. epymorph/initializer.py +786 -0
  52. epymorph/kit.py +107 -0
  53. epymorph/log/__init__.py +0 -0
  54. epymorph/log/file.py +128 -0
  55. epymorph/log/messaging.py +139 -0
  56. epymorph/log/movement.py +228 -0
  57. epymorph/movement_model.py +347 -0
  58. epymorph/parameter_fitting/__init__.py +0 -0
  59. epymorph/parameter_fitting/distribution.py +68 -0
  60. epymorph/parameter_fitting/dynamics.py +19 -0
  61. epymorph/parameter_fitting/filter/__init__.py +0 -0
  62. epymorph/parameter_fitting/filter/base_filter.py +50 -0
  63. epymorph/parameter_fitting/filter/particle.py +32 -0
  64. epymorph/parameter_fitting/filter/particle_filter.py +296 -0
  65. epymorph/parameter_fitting/likelihood.py +54 -0
  66. epymorph/parameter_fitting/output.py +73 -0
  67. epymorph/parameter_fitting/particlefilter_simulation.py +175 -0
  68. epymorph/parameter_fitting/perturbation.py +19 -0
  69. epymorph/parameter_fitting/utils/__init__.py +0 -0
  70. epymorph/parameter_fitting/utils/data_loader.py +97 -0
  71. epymorph/parameter_fitting/utils/epymorph_simulation.py +119 -0
  72. epymorph/parameter_fitting/utils/observations.py +63 -0
  73. epymorph/parameter_fitting/utils/parameter_estimation.py +49 -0
  74. epymorph/parameter_fitting/utils/params_perturb.py +43 -0
  75. epymorph/parameter_fitting/utils/particle_initializer.py +95 -0
  76. epymorph/parameter_fitting/utils/resampler.py +258 -0
  77. epymorph/parameter_fitting/utils/utils.py +101 -0
  78. epymorph/params.py +438 -0
  79. epymorph/py.typed +0 -0
  80. epymorph/rume.py +871 -0
  81. epymorph/simulation.py +991 -0
  82. epymorph/simulator/__init__.py +0 -0
  83. epymorph/simulator/basic/__init__.py +0 -0
  84. epymorph/simulator/basic/basic_simulator.py +171 -0
  85. epymorph/simulator/basic/ipm_exec.py +437 -0
  86. epymorph/simulator/basic/mm_exec.py +244 -0
  87. epymorph/simulator/basic/output.py +213 -0
  88. epymorph/simulator/world.py +149 -0
  89. epymorph/simulator/world_list.py +274 -0
  90. epymorph/strata.py +24 -0
  91. epymorph/sympy_shim.py +57 -0
  92. epymorph/time.py +1197 -0
  93. epymorph/tools/__init__.py +0 -0
  94. epymorph/tools/data.py +297 -0
  95. epymorph/tools/ipm_diagram.py +208 -0
  96. epymorph/tools/out_map.py +551 -0
  97. epymorph/tools/out_plot.py +423 -0
  98. epymorph/tools/out_table.py +582 -0
  99. epymorph/util.py +1036 -0
  100. epymorph-1.0.0.dist-info/METADATA +57 -0
  101. epymorph-1.0.0.dist-info/RECORD +104 -0
  102. epymorph-1.0.0.dist-info/WHEEL +5 -0
  103. epymorph-1.0.0.dist-info/entry_points.txt +2 -0
  104. epymorph-1.0.0.dist-info/top_level.txt +1 -0
epymorph/params.py ADDED
@@ -0,0 +1,438 @@
1
+ """
2
+ Users may wish to express simulation parameters as functions of simulation dimensions
3
+ and/or of other simulation attributes. These classes provide a way to do that.
4
+ This module also defines the types of acceptable input values for simulation parameters.
5
+ """
6
+
7
+ from abc import ABC, abstractmethod
8
+ from typing import Literal, TypeVar, final
9
+
10
+ import numpy as np
11
+ from numpy.typing import NDArray
12
+ from sympy import Expr, Symbol
13
+ from typing_extensions import override
14
+
15
+ from epymorph.attribute import AbsoluteName
16
+ from epymorph.compartment_model import BaseCompartmentModel
17
+ from epymorph.data_type import (
18
+ AttributeArray,
19
+ AttributeValue,
20
+ )
21
+ from epymorph.database import DataResolver, evaluate_param
22
+ from epymorph.error import DataAttributeError
23
+ from epymorph.geography.scope import GeoScope
24
+ from epymorph.simulation import Context, SimulationFunction
25
+ from epymorph.sympy_shim import lambdify, to_symbol
26
+ from epymorph.time import TimeFrame
27
+
28
+ ResultDType = TypeVar("ResultDType", bound=np.generic)
29
+ """The result type of a `ParamFunction`."""
30
+
31
+
32
+ class ParamFunction(SimulationFunction[NDArray[ResultDType]], ABC):
33
+ """
34
+ Base class for parameter functions.
35
+
36
+ `ParamFunction`s are generic on the dtype of the numpy array
37
+ that they produce (`ResultDType`).
38
+ """
39
+
40
+
41
+ @evaluate_param.register
42
+ def _(
43
+ value: ParamFunction,
44
+ name: AbsoluteName,
45
+ data: DataResolver,
46
+ scope: GeoScope | None,
47
+ time_frame: TimeFrame | None,
48
+ ipm: BaseCompartmentModel | None,
49
+ rng: np.random.Generator | None,
50
+ ) -> AttributeArray:
51
+ # depth-first evaluation guarantees `data` has our dependencies.
52
+ ctx = Context.of(name, data, scope, time_frame, ipm, rng)
53
+ sim_func = value.with_context_internal(ctx)
54
+ return sim_func.evaluate()
55
+
56
+
57
+ class ParamFunctionNumpy(ParamFunction[ResultDType]):
58
+ """
59
+ Base class for param functions whose evaluate method produces
60
+ the whole data series as a numpy array.
61
+ """
62
+
63
+ @override
64
+ @abstractmethod
65
+ def evaluate(self) -> NDArray[ResultDType]:
66
+ """
67
+ Produce a numpy array containing all of this parameter's values.
68
+ This method must assure the values are the appropriate shape and data type.
69
+
70
+ Returns
71
+ -------
72
+ :
73
+ The data array.
74
+ """
75
+
76
+
77
+ class ParamFunction1(ParamFunction[ResultDType], ABC):
78
+ """Base class for parameter functions which calculate results one at a time."""
79
+
80
+ dtype: type[ResultDType] | None = None
81
+ """
82
+ The result type of this function. If specified, results will be coerced accordingly.
83
+ """
84
+
85
+
86
+ class ParamFunctionScalar(ParamFunction1[ResultDType]):
87
+ """
88
+ Base class for param functions whose evaluate method produces
89
+ a scalar value (which is the full data series).
90
+
91
+ Implement by overriding `evaluate1`.
92
+ """
93
+
94
+ @final
95
+ @override
96
+ def evaluate(self) -> NDArray[ResultDType]:
97
+ """
98
+ Fully evaluate the parameter function in the current simulation context.
99
+
100
+ Returns
101
+ -------
102
+ :
103
+ The data array.
104
+ """
105
+ return np.array(self.evaluate1(), dtype=self.dtype)
106
+
107
+ @abstractmethod
108
+ def evaluate1(self) -> AttributeValue:
109
+ """
110
+ Produce a scalar value for this parameter in the given simulation context.
111
+
112
+ Returns
113
+ -------
114
+ :
115
+ The data value.
116
+ """
117
+
118
+
119
+ class ParamFunctionTime(ParamFunction1[ResultDType]):
120
+ """
121
+ Base class for param functions whose evaluate method produces
122
+ a time-series of data, one value at a time.
123
+
124
+ Implement by overriding `evaluate1`.
125
+ """
126
+
127
+ @final
128
+ @override
129
+ def evaluate(self) -> NDArray[ResultDType]:
130
+ """
131
+ Fully evaluate the parameter function in the current simulation context.
132
+
133
+ Returns
134
+ -------
135
+ :
136
+ The data array.
137
+ """
138
+ result = [self.evaluate1(day) for day in range(self.time_frame.days)]
139
+ return np.array(result, dtype=self.dtype)
140
+
141
+ @abstractmethod
142
+ def evaluate1(self, day: int) -> AttributeValue:
143
+ """
144
+ Produce the daily value for this parameter.
145
+
146
+ Parameters
147
+ ----------
148
+ day :
149
+ The simulation day (0-indexed from the start of the simulation).
150
+
151
+ Returns
152
+ -------
153
+ :
154
+ The data value.
155
+ """
156
+
157
+
158
+ class ParamFunctionNode(ParamFunction1[ResultDType]):
159
+ """
160
+ Base class for param functions whose evaluate method produces
161
+ a node-series of data, one value at a time.
162
+
163
+ Implement by overriding `evaluate1`.
164
+ """
165
+
166
+ @final
167
+ @override
168
+ def evaluate(self) -> NDArray[ResultDType]:
169
+ """
170
+ Fully evaluate the parameter function in the current simulation context.
171
+
172
+ Returns
173
+ -------
174
+ :
175
+ The data array.
176
+ """
177
+ result = [self.evaluate1(n) for n in range(self.scope.nodes)]
178
+ return np.array(result, dtype=self.dtype)
179
+
180
+ @abstractmethod
181
+ def evaluate1(self, node_index: int) -> AttributeValue:
182
+ """
183
+ Produce the per-node value for this parameter.
184
+
185
+ Parameters
186
+ ----------
187
+ node_index :
188
+ The node index for which to compute the value.
189
+
190
+ Returns
191
+ -------
192
+ :
193
+ The data value.
194
+ """
195
+
196
+
197
+ class ParamFunctionNodeAndNode(ParamFunction1[ResultDType]):
198
+ """
199
+ Base class for param functions whose evaluate method produces
200
+ a node-by-node matrix of data, one value at a time.
201
+
202
+ Often, this kind of data is posed as having an originating node
203
+ and a destination node, for example: data describing the normal
204
+ number of commuters between locations. For some data this relationship
205
+ may be more figurative than literal.
206
+
207
+ Implement by overriding `evaluate1`.
208
+ """
209
+
210
+ @final
211
+ @override
212
+ def evaluate(self) -> NDArray[ResultDType]:
213
+ """
214
+ Fully evaluate the parameter function in the current simulation context.
215
+
216
+ Returns
217
+ -------
218
+ :
219
+ The data array.
220
+ """
221
+ result = [
222
+ [self.evaluate1(n1, n2) for n2 in range(self.scope.nodes)]
223
+ for n1 in range(self.scope.nodes)
224
+ ]
225
+ return np.array(result, dtype=self.dtype)
226
+
227
+ @abstractmethod
228
+ def evaluate1(self, node_from: int, node_to: int) -> AttributeValue:
229
+ """
230
+ Produce the per-node-pair value for this parameter.
231
+
232
+ Parameters
233
+ ----------
234
+ node_from :
235
+ The origin node.
236
+ node_to :
237
+ The destination node.
238
+
239
+ Returns
240
+ -------
241
+ :
242
+ The data value.
243
+ """
244
+
245
+
246
+ class ParamFunctionNodeAndCompartment(ParamFunction1[ResultDType]):
247
+ """
248
+ Base class for param functions whose evaluate method produces
249
+ a node-by-disease-compartment matrix of data, one value at a time.
250
+
251
+ Implement by overriding 'evaluate1`.
252
+ """
253
+
254
+ @final
255
+ @override
256
+ def evaluate(self) -> NDArray[ResultDType]:
257
+ """
258
+ Fully evaluate the parameter function in the current simulation context.
259
+
260
+ Returns
261
+ -------
262
+ :
263
+ The data array.
264
+ """
265
+ result = [
266
+ [self.evaluate1(n, c) for c in range(self.ipm.num_compartments)]
267
+ for n in range(self.scope.nodes)
268
+ ]
269
+ return np.array(result, dtype=self.dtype)
270
+
271
+ @abstractmethod
272
+ def evaluate1(self, node_index: int, compartment_index: int) -> AttributeValue:
273
+ """
274
+ Produce the per-node and per-disease-compartment value for this parameter.
275
+
276
+ Parameters
277
+ ----------
278
+ node_index :
279
+ The node index for which to compute the value.
280
+ compartment_index :
281
+ The disease compartment index for which to compute the value.
282
+ This is taken from the IPM compartment definition order, indexed from 0.
283
+
284
+ Returns
285
+ -------
286
+ :
287
+ The data value.
288
+ """
289
+
290
+
291
+ class ParamFunctionTimeAndNode(ParamFunction1[ResultDType]):
292
+ """
293
+ Base class for param functions whose evaluate method produces
294
+ a time-by-node matrix of data, one value at a time.
295
+
296
+ Implement by overriding `evaluate1`.
297
+ """
298
+
299
+ @final
300
+ @override
301
+ def evaluate(self) -> NDArray[ResultDType]:
302
+ """
303
+ Fully evaluate the parameter function in the current simulation context.
304
+
305
+ Returns
306
+ -------
307
+ :
308
+ The data array.
309
+ """
310
+ result = [
311
+ [self.evaluate1(day, n) for n in range(self.scope.nodes)]
312
+ for day in range(self.time_frame.days)
313
+ ]
314
+ return np.array(result, dtype=self.dtype)
315
+
316
+ @abstractmethod
317
+ def evaluate1(self, day: int, node_index: int) -> AttributeValue:
318
+ """
319
+ Produce the per-day and per-node value for this parameter.
320
+
321
+ Parameters
322
+ ----------
323
+ day :
324
+ The simulation day (0-indexed from the start of the simulation).
325
+ node_index :
326
+ The node index for which to compute the value.
327
+
328
+ Returns
329
+ -------
330
+ :
331
+ The data value.
332
+ """
333
+
334
+
335
+ _ALL_PARAMS = ("day", "node_index", "duration_days", "nodes")
336
+ _ALL_PARAM_SYMBOLS = [to_symbol(x) for x in _ALL_PARAMS]
337
+ _PARAMS_MAP = dict(zip(_ALL_PARAMS, _ALL_PARAM_SYMBOLS))
338
+
339
+ ParamSymbol = Literal["day", "node_index", "duration_days", "nodes"]
340
+ """
341
+ The names of common symbols used in epymorph simulation expressions.
342
+ For example, building a `ParamExpressionTimeAndNode` may make use of these symbols.
343
+
344
+ See Also
345
+ --------
346
+ [epymorph.rume.RUME.symbols][] and [epymorph.params.simulation_symbols][]
347
+ which are methods for using these names to obtain symbol references.
348
+ """
349
+
350
+
351
+ def simulation_symbols(*symbols: ParamSymbol) -> tuple[Symbol, ...]:
352
+ """
353
+ Retrieve the symbols used to represent simulation quantities.
354
+
355
+ Parameters
356
+ ----------
357
+ *symbols :
358
+ The names of symbols to retrieve, as var-args.
359
+
360
+ Returns
361
+ -------
362
+ :
363
+ A tuple containing the symbols named, in the order requested.
364
+ """
365
+ return tuple(_PARAMS_MAP[x] for x in symbols if x in _PARAMS_MAP)
366
+
367
+
368
+ class ParamExpressionTimeAndNode(ParamFunction[np.float64]):
369
+ """
370
+ A param function based on a sympy expression for a time-by-node matrix of data.
371
+
372
+ Parameters
373
+ ----------
374
+ expression :
375
+ The sympy expression to use.
376
+
377
+ See Also
378
+ --------
379
+ [`epymorph.params.simulation_symbols`][] which enables you to obtain
380
+ symbols standing for simulation properties like "day" and "node_index";
381
+ you will likely make use of the symbols in writing the expression.
382
+ """
383
+
384
+ requirements = ()
385
+ """
386
+ Param expressions do not support data requirements, so this list must be empty.
387
+ """
388
+ # TODO (ISSUE #250): support data requirements
389
+
390
+ _expr: Expr
391
+
392
+ def __init__(self, expression: Expr):
393
+ bad_symbols = [
394
+ x for x in expression.free_symbols if x not in _ALL_PARAM_SYMBOLS
395
+ ]
396
+ if len(bad_symbols) > 0:
397
+ bs = ", ".join(map(str, bad_symbols))
398
+ raise ValueError(f"expression uses unsupported symbols: {bs}")
399
+ self._expr = expression
400
+
401
+ @final
402
+ @override
403
+ def evaluate(self) -> NDArray[np.float64]:
404
+ """
405
+ Fully evaluate the parameter function in the current simulation context.
406
+
407
+ Returns
408
+ -------
409
+ :
410
+ The data array.
411
+ """
412
+ T = self.time_frame.days
413
+ N = self.scope.nodes
414
+ ds = np.broadcast_to(np.arange(T).reshape((T, 1)), (T, N))
415
+ ns = np.broadcast_to(np.arange(N).reshape((1, N)), (T, N))
416
+ fn = lambdify(_ALL_PARAM_SYMBOLS, self._expr)
417
+ result = fn([ds, ns, T, N])
418
+ if isinstance(result, np.ndarray):
419
+ return result.astype(dtype=np.float64, copy=False)
420
+ return np.broadcast_to(np.array(result, dtype=np.float64), (T, N))
421
+
422
+
423
+ @evaluate_param.register
424
+ def _(
425
+ value: Expr,
426
+ name: AbsoluteName,
427
+ data: DataResolver,
428
+ scope: GeoScope | None,
429
+ time_frame: TimeFrame | None,
430
+ ipm: BaseCompartmentModel | None,
431
+ rng: np.random.Generator | None,
432
+ ) -> AttributeArray:
433
+ # Automatically convert sympy expressions into a ParamFunction instance.
434
+ try:
435
+ expr_func = ParamExpressionTimeAndNode(value)
436
+ except ValueError as e:
437
+ raise DataAttributeError(str(e)) from None
438
+ return evaluate_param(expr_func, name, data, scope, time_frame, ipm, rng)
epymorph/py.typed ADDED
File without changes