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
epymorph/attribute.py ADDED
@@ -0,0 +1,301 @@
1
+ from dataclasses import dataclass, field
2
+ from typing import Generic, Self, TypeVar
3
+
4
+ from epymorph.data_shape import DataShape
5
+ from epymorph.data_type import AttributeType, AttributeValue, dtype_as_np, dtype_check
6
+ from epymorph.util import acceptable_name
7
+
8
+ ########################
9
+ # Names and Namespaces #
10
+ ########################
11
+
12
+
13
+ def _validate_pattern_segments(*names: str) -> None:
14
+ for n in names:
15
+ if len(n) == 0:
16
+ raise ValueError("Invalid pattern: cannot use empty strings.")
17
+ if "::" in n:
18
+ raise ValueError("Invalid pattern: cannot contain '::'.")
19
+
20
+
21
+ def _validate_name_segments(*names: str) -> None:
22
+ for n in names:
23
+ if len(n) == 0:
24
+ raise ValueError("Invalid name: cannot use empty strings.")
25
+ if n == "*":
26
+ raise ValueError("Invalid name: cannot use wildcards (*).")
27
+ if "::" in n:
28
+ raise ValueError("Invalid name: cannot contain '::'.")
29
+
30
+
31
+ @dataclass(frozen=True)
32
+ class ModuleNamespace:
33
+ """A namespace with a specified strata and module."""
34
+
35
+ strata: str
36
+ module: str
37
+
38
+ def __post_init__(self):
39
+ _validate_name_segments(self.strata, self.module)
40
+
41
+ @classmethod
42
+ def parse(cls, name: str) -> Self:
43
+ """Parse a module name from a ::-delimited string."""
44
+ parts = name.split("::")
45
+ if len(parts) != 2:
46
+ raise ValueError("Invalid number of parts for namespace.")
47
+ return cls(*parts)
48
+
49
+ def __str__(self) -> str:
50
+ return f"{self.strata}::{self.module}"
51
+
52
+ def to_absolute(self, attrib_id: str) -> "AbsoluteName":
53
+ """Creates an absolute name by providing the attribute ID."""
54
+ return AbsoluteName(self.strata, self.module, attrib_id)
55
+
56
+
57
+ @dataclass(frozen=True)
58
+ class AbsoluteName:
59
+ """A fully-specified name (strata, module, and attribute ID)."""
60
+
61
+ strata: str
62
+ module: str
63
+ id: str
64
+
65
+ def __post_init__(self):
66
+ _validate_name_segments(self.strata, self.module, self.id)
67
+
68
+ @classmethod
69
+ def parse(cls, name: str) -> Self:
70
+ """Parse a module name from a ::-delimited string."""
71
+ parts = name.split("::")
72
+ if len(parts) != 3:
73
+ raise ValueError("Invalid number of parts for absolute name.")
74
+ return cls(*parts)
75
+
76
+ def __str__(self) -> str:
77
+ return f"{self.strata}::{self.module}::{self.id}"
78
+
79
+ def in_strata(self, new_strata: str) -> "AbsoluteName":
80
+ """
81
+ Creates a new AbsoluteName that is a copy of this name
82
+ but with the given strata.
83
+ """
84
+ return AbsoluteName(new_strata, self.module, self.id)
85
+
86
+ def with_id(self, new_id: str) -> "AbsoluteName":
87
+ """
88
+ Creates a new AbsoluteName that is a copy of this name
89
+ but with the given ID.
90
+ """
91
+ return AbsoluteName(self.strata, self.module, new_id)
92
+
93
+ def to_namespace(self) -> ModuleNamespace:
94
+ """Extracts the module namespace of this name."""
95
+ return ModuleNamespace(self.strata, self.module)
96
+
97
+ def to_pattern(self) -> "NamePattern":
98
+ """Converts this name to a pattern that is an exact match for this name."""
99
+ return NamePattern(self.strata, self.module, self.id)
100
+
101
+
102
+ STRATA_PLACEHOLDER = "(unspecified)"
103
+ MODULE_PLACEHOLDER = "(unspecified)"
104
+ ID_PLACEHOLDER = "(unspecified)"
105
+ NAMESPACE_PLACEHOLDER = ModuleNamespace(STRATA_PLACEHOLDER, MODULE_PLACEHOLDER)
106
+ """A namespace to use when we don't need to be specific."""
107
+ NAME_PLACEHOLDER = NAMESPACE_PLACEHOLDER.to_absolute(ID_PLACEHOLDER)
108
+ """An absolute name to use when we don't need to be specific."""
109
+
110
+
111
+ @dataclass(frozen=True)
112
+ class ModuleName:
113
+ """A partially-specified name with module and attribute ID."""
114
+
115
+ module: str
116
+ id: str
117
+
118
+ def __post_init__(self):
119
+ _validate_name_segments(self.module, self.id)
120
+
121
+ @classmethod
122
+ def parse(cls, name: str) -> Self:
123
+ """Parse a module name from a ::-delimited string."""
124
+ parts = name.split("::")
125
+ if len(parts) != 2:
126
+ raise ValueError("Invalid number of parts for module name.")
127
+ return cls(*parts)
128
+
129
+ def __str__(self) -> str:
130
+ return f"{self.module}::{self.id}"
131
+
132
+ def to_absolute(self, strata: str) -> AbsoluteName:
133
+ """Creates an absolute name by providing the strata."""
134
+ return AbsoluteName(strata, self.module, self.id)
135
+
136
+
137
+ @dataclass(frozen=True)
138
+ class AttributeName:
139
+ """A partially-specified name with just an attribute ID."""
140
+
141
+ id: str
142
+
143
+ def __post_init__(self):
144
+ _validate_name_segments(self.id)
145
+
146
+ def __str__(self) -> str:
147
+ return self.id
148
+
149
+
150
+ @dataclass(frozen=True)
151
+ class NamePattern:
152
+ """
153
+ A name with a strata, module, and attribute ID that allows wildcards (*) so it can
154
+ act as a pattern to match against AbsoluteNames.
155
+ """
156
+
157
+ strata: str
158
+ module: str
159
+ id: str
160
+
161
+ def __post_init__(self):
162
+ _validate_pattern_segments(self.strata, self.module, self.id)
163
+
164
+ @classmethod
165
+ def parse(cls, name: str) -> Self:
166
+ """
167
+ Parse a pattern from a ::-delimited string. As a shorthand, you can omit
168
+ preceding wildcard segments and they will be automatically filled in,
169
+ e.g., "a" will become "*::*::a" and "a::b" will become "*::a::b".
170
+ """
171
+ parts = name.split("::")
172
+ match len(parts):
173
+ case 1:
174
+ return cls("*", "*", *parts)
175
+ case 2:
176
+ return cls("*", *parts)
177
+ case 3:
178
+ return cls(*parts)
179
+ case _:
180
+ raise ValueError("Invalid number of parts for name pattern.")
181
+
182
+ @staticmethod
183
+ def of(name: "str | NamePattern") -> "NamePattern":
184
+ """Coerce the given value to a NamePattern.
185
+ If it's already a NamePattern, return it; if it's a string, parse it."""
186
+ return name if isinstance(name, NamePattern) else NamePattern.parse(name)
187
+
188
+ def match(self, name: "AbsoluteName | NamePattern") -> bool:
189
+ """
190
+ Test this pattern to see if it matches the given AbsoluteName or NamePattern.
191
+ The ability to match against NamePatterns is useful to see if two patterns
192
+ conflict with each other and would create ambiguity.
193
+ """
194
+ match name:
195
+ case AbsoluteName(s, m, i):
196
+ if self.strata != "*" and self.strata != s:
197
+ return False
198
+ if self.module != "*" and self.module != m:
199
+ return False
200
+ if self.id != "*" and self.id != i:
201
+ return False
202
+ return True
203
+ case NamePattern(s, m, i):
204
+ if self.strata != "*" and s != "*" and self.strata != s:
205
+ return False
206
+ if self.module != "*" and m != "*" and self.module != m:
207
+ return False
208
+ if self.id != "*" and i != "*" and self.id != i:
209
+ return False
210
+ return True
211
+ case _:
212
+ raise ValueError(f"Unsupported match: {type(name)}")
213
+
214
+ def __str__(self) -> str:
215
+ return f"{self.strata}::{self.module}::{self.id}"
216
+
217
+
218
+ @dataclass(frozen=True)
219
+ class ModuleNamePattern:
220
+ """
221
+ A name with a module and attribute ID that allows wildcards (*).
222
+ Mostly this is useful to provide parameters to GPMs, which don't have
223
+ a concept of which strata they belong to. A ModuleNamePattern can be
224
+ transformed into a full NamePattern by adding the strata.
225
+ """
226
+
227
+ module: str
228
+ id: str
229
+
230
+ def __post_init__(self):
231
+ _validate_pattern_segments(self.module, self.id)
232
+
233
+ @classmethod
234
+ def parse(cls, name: str) -> Self:
235
+ """
236
+ Parse a pattern from a ::-delimited string. As a shorthand, you can omit
237
+ a preceding wildcard segment and it will be automatically filled in,
238
+ e.g.,"a" will become "*::a".
239
+ """
240
+ if len(name) == 0:
241
+ raise ValueError("Empty string is not a valid name.")
242
+ parts = name.split("::")
243
+ match len(parts):
244
+ case 1:
245
+ return cls("*", *parts)
246
+ case 2:
247
+ return cls(*parts)
248
+ case _:
249
+ raise ValueError("Invalid number of parts for module name pattern.")
250
+
251
+ def to_absolute(self, strata: str) -> NamePattern:
252
+ """Creates a full name pattern by providing the strata."""
253
+ return NamePattern(strata, self.module, self.id)
254
+
255
+ def __str__(self) -> str:
256
+ return f"{self.module}::{self.id}"
257
+
258
+
259
+ ##############
260
+ # Attributes #
261
+ ##############
262
+
263
+
264
+ AttributeT = TypeVar("AttributeT", bound=AttributeType)
265
+ """The data type of an attribute; maps to the numpy type of the attribute array."""
266
+
267
+
268
+ @dataclass(frozen=True)
269
+ class AttributeDef(Generic[AttributeT]):
270
+ """
271
+ Definition of a simulation attribute;
272
+ the identity plus optional default value and comment.
273
+ """
274
+
275
+ name: str
276
+ type: AttributeT
277
+ shape: DataShape
278
+ default_value: AttributeValue | None = field(default=None, compare=False)
279
+ comment: str | None = field(default=None, compare=False)
280
+
281
+ def __post_init__(self):
282
+ if acceptable_name.match(self.name) is None:
283
+ raise ValueError(f"Invalid attribute name: {self.name}")
284
+ try:
285
+ dtype_as_np(self.type)
286
+ except Exception as e:
287
+ msg = (
288
+ f"AttributeDef's type is not correctly specified: {self.type}\n"
289
+ "See documentation for appropriate type designations."
290
+ )
291
+ raise ValueError(msg) from e
292
+
293
+ if (
294
+ self.default_value is not None #
295
+ and not dtype_check(self.type, self.default_value)
296
+ ):
297
+ msg = (
298
+ "AttributeDef's default value does not align with its dtype "
299
+ f"('{self.name}')."
300
+ )
301
+ raise ValueError(msg)