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
epymorph/attribute.py
ADDED
|
@@ -0,0 +1,614 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Data attributes are how epymorph keeps track of input data for simulations.
|
|
3
|
+
An IPM's requirements are expressed as attributes, and when supplying data
|
|
4
|
+
values to a RUME you use names to match values to the requirement(s) they fulfill.
|
|
5
|
+
|
|
6
|
+
This module provides a foundation for that, encoding systems for how
|
|
7
|
+
names work and defining the `DataAttribute` object itself.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from dataclasses import dataclass, field
|
|
11
|
+
from typing import Generic, Self, TypeVar
|
|
12
|
+
|
|
13
|
+
from epymorph.data_shape import DataShape
|
|
14
|
+
from epymorph.data_type import AttributeType, AttributeValue, dtype_as_np, dtype_check
|
|
15
|
+
from epymorph.util import acceptable_name
|
|
16
|
+
|
|
17
|
+
########################
|
|
18
|
+
# Names and Namespaces #
|
|
19
|
+
########################
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _validate_pattern_segments(*names: str) -> None:
|
|
23
|
+
for n in names:
|
|
24
|
+
if len(n) == 0:
|
|
25
|
+
raise ValueError("Invalid pattern: cannot use empty strings.")
|
|
26
|
+
if "::" in n:
|
|
27
|
+
raise ValueError("Invalid pattern: cannot contain '::'.")
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _validate_name_segments(*names: str) -> None:
|
|
31
|
+
for n in names:
|
|
32
|
+
if len(n) == 0:
|
|
33
|
+
raise ValueError("Invalid name: cannot use empty strings.")
|
|
34
|
+
if n == "*":
|
|
35
|
+
raise ValueError("Invalid name: cannot use wildcards (*).")
|
|
36
|
+
if "::" in n:
|
|
37
|
+
raise ValueError("Invalid name: cannot contain '::'.")
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
@dataclass(frozen=True)
|
|
41
|
+
class ModuleNamespace:
|
|
42
|
+
"""
|
|
43
|
+
A namespace which specifies strata and module.
|
|
44
|
+
|
|
45
|
+
Parameters
|
|
46
|
+
----------
|
|
47
|
+
strata :
|
|
48
|
+
The strata name.
|
|
49
|
+
module :
|
|
50
|
+
The module name.
|
|
51
|
+
"""
|
|
52
|
+
|
|
53
|
+
strata: str
|
|
54
|
+
"""The strata name."""
|
|
55
|
+
module: str
|
|
56
|
+
"""The module name."""
|
|
57
|
+
|
|
58
|
+
def __post_init__(self):
|
|
59
|
+
_validate_name_segments(self.strata, self.module)
|
|
60
|
+
|
|
61
|
+
@classmethod
|
|
62
|
+
def parse(cls, name: str) -> Self:
|
|
63
|
+
"""
|
|
64
|
+
Parse a module namespace from a ::-delimited string.
|
|
65
|
+
|
|
66
|
+
Parameters
|
|
67
|
+
----------
|
|
68
|
+
name :
|
|
69
|
+
The string to parse.
|
|
70
|
+
|
|
71
|
+
Returns
|
|
72
|
+
-------
|
|
73
|
+
:
|
|
74
|
+
The new namespace instance.
|
|
75
|
+
|
|
76
|
+
Raises
|
|
77
|
+
------
|
|
78
|
+
ValueError
|
|
79
|
+
If the given string cannot be parsed.
|
|
80
|
+
|
|
81
|
+
Examples
|
|
82
|
+
--------
|
|
83
|
+
>>> ModuleNamespace.parse("gpm:human::ipm")
|
|
84
|
+
ModuleNamespace(strata="gpm:human", module="ipm")
|
|
85
|
+
"""
|
|
86
|
+
parts = name.split("::")
|
|
87
|
+
if len(parts) != 2:
|
|
88
|
+
raise ValueError("Invalid number of parts for namespace.")
|
|
89
|
+
return cls(*parts)
|
|
90
|
+
|
|
91
|
+
def __str__(self) -> str:
|
|
92
|
+
return f"{self.strata}::{self.module}"
|
|
93
|
+
|
|
94
|
+
def to_absolute(self, attrib_id: str) -> "AbsoluteName":
|
|
95
|
+
"""
|
|
96
|
+
Create an absolute name by providing the attribute ID.
|
|
97
|
+
|
|
98
|
+
Parameters
|
|
99
|
+
----------
|
|
100
|
+
attrib_id :
|
|
101
|
+
The attribute ID to append to the namespace.
|
|
102
|
+
|
|
103
|
+
Returns
|
|
104
|
+
-------
|
|
105
|
+
:
|
|
106
|
+
The new absolute name instance.
|
|
107
|
+
"""
|
|
108
|
+
return AbsoluteName(self.strata, self.module, attrib_id)
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
@dataclass(frozen=True)
|
|
112
|
+
class AbsoluteName:
|
|
113
|
+
"""
|
|
114
|
+
A fully-specified name: strata, module, and attribute ID.
|
|
115
|
+
|
|
116
|
+
Parameters
|
|
117
|
+
----------
|
|
118
|
+
strata :
|
|
119
|
+
The strata name.
|
|
120
|
+
module :
|
|
121
|
+
The module name.
|
|
122
|
+
id :
|
|
123
|
+
The attribute ID.
|
|
124
|
+
"""
|
|
125
|
+
|
|
126
|
+
strata: str
|
|
127
|
+
"""The strata name."""
|
|
128
|
+
module: str
|
|
129
|
+
"""The module name."""
|
|
130
|
+
id: str
|
|
131
|
+
"""The attribute ID."""
|
|
132
|
+
|
|
133
|
+
def __post_init__(self):
|
|
134
|
+
_validate_name_segments(self.strata, self.module, self.id)
|
|
135
|
+
|
|
136
|
+
@classmethod
|
|
137
|
+
def parse(cls, name: str) -> Self:
|
|
138
|
+
"""
|
|
139
|
+
Parse a module name from a ::-delimited string.
|
|
140
|
+
|
|
141
|
+
Parameters
|
|
142
|
+
----------
|
|
143
|
+
name :
|
|
144
|
+
The string to parse.
|
|
145
|
+
|
|
146
|
+
Returns
|
|
147
|
+
-------
|
|
148
|
+
:
|
|
149
|
+
The new name instance.
|
|
150
|
+
|
|
151
|
+
Raises
|
|
152
|
+
------
|
|
153
|
+
ValueError
|
|
154
|
+
If the given string cannot be parsed.
|
|
155
|
+
|
|
156
|
+
Examples
|
|
157
|
+
--------
|
|
158
|
+
>>> AbsoluteName.parse("gpm:human::ipm::beta")
|
|
159
|
+
AbsoluteName(strata="gpm:human", module="ipm", id="beta")
|
|
160
|
+
"""
|
|
161
|
+
parts = name.split("::")
|
|
162
|
+
if len(parts) != 3:
|
|
163
|
+
raise ValueError("Invalid number of parts for absolute name.")
|
|
164
|
+
return cls(*parts)
|
|
165
|
+
|
|
166
|
+
def __str__(self) -> str:
|
|
167
|
+
return f"{self.strata}::{self.module}::{self.id}"
|
|
168
|
+
|
|
169
|
+
def in_strata(self, new_strata: str) -> "AbsoluteName":
|
|
170
|
+
"""
|
|
171
|
+
Create a new `AbsoluteName` that is a copy of this name
|
|
172
|
+
but with the given strata.
|
|
173
|
+
|
|
174
|
+
Parameters
|
|
175
|
+
----------
|
|
176
|
+
new_strata :
|
|
177
|
+
The strata to use to replace this name's strata.
|
|
178
|
+
|
|
179
|
+
Returns
|
|
180
|
+
-------
|
|
181
|
+
:
|
|
182
|
+
The new name instance.
|
|
183
|
+
"""
|
|
184
|
+
return AbsoluteName(new_strata, self.module, self.id)
|
|
185
|
+
|
|
186
|
+
def with_id(self, new_id: str) -> "AbsoluteName":
|
|
187
|
+
"""
|
|
188
|
+
Create a new `AbsoluteName` that is a copy of this name
|
|
189
|
+
but with the given ID.
|
|
190
|
+
|
|
191
|
+
Parameters
|
|
192
|
+
----------
|
|
193
|
+
new_id :
|
|
194
|
+
The attribute ID to use to replace this name's ID.
|
|
195
|
+
|
|
196
|
+
Returns
|
|
197
|
+
-------
|
|
198
|
+
:
|
|
199
|
+
The new name instance.
|
|
200
|
+
"""
|
|
201
|
+
return AbsoluteName(self.strata, self.module, new_id)
|
|
202
|
+
|
|
203
|
+
def to_namespace(self) -> ModuleNamespace:
|
|
204
|
+
"""
|
|
205
|
+
Extract the module namespace part of this name.
|
|
206
|
+
|
|
207
|
+
Returns
|
|
208
|
+
-------
|
|
209
|
+
:
|
|
210
|
+
The new module namespace instance.
|
|
211
|
+
"""
|
|
212
|
+
return ModuleNamespace(self.strata, self.module)
|
|
213
|
+
|
|
214
|
+
def to_pattern(self) -> "NamePattern":
|
|
215
|
+
"""
|
|
216
|
+
Convert this name to a pattern that is an exact match for this name.
|
|
217
|
+
|
|
218
|
+
Returns
|
|
219
|
+
-------
|
|
220
|
+
:
|
|
221
|
+
The new name pattern instance.
|
|
222
|
+
"""
|
|
223
|
+
return NamePattern(self.strata, self.module, self.id)
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
STRATA_PLACEHOLDER = "(unspecified)"
|
|
227
|
+
"""The strata name to use when one has not been specified."""
|
|
228
|
+
MODULE_PLACEHOLDER = "(unspecified)"
|
|
229
|
+
"""The module name to use when one has not been specified."""
|
|
230
|
+
ID_PLACEHOLDER = "(unspecified)"
|
|
231
|
+
"""The attribute ID to use when one has not been specified."""
|
|
232
|
+
NAMESPACE_PLACEHOLDER = ModuleNamespace(STRATA_PLACEHOLDER, MODULE_PLACEHOLDER)
|
|
233
|
+
"""A namespace to use when we don't need to be specific."""
|
|
234
|
+
NAME_PLACEHOLDER = NAMESPACE_PLACEHOLDER.to_absolute(ID_PLACEHOLDER)
|
|
235
|
+
"""An absolute name to use when we don't need to be specific."""
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
@dataclass(frozen=True)
|
|
239
|
+
class ModuleName:
|
|
240
|
+
"""
|
|
241
|
+
A partially-specified name with module and attribute ID.
|
|
242
|
+
|
|
243
|
+
Parameters
|
|
244
|
+
----------
|
|
245
|
+
module :
|
|
246
|
+
The module name.
|
|
247
|
+
id :
|
|
248
|
+
The attribute ID.
|
|
249
|
+
"""
|
|
250
|
+
|
|
251
|
+
module: str
|
|
252
|
+
"""The module name."""
|
|
253
|
+
id: str
|
|
254
|
+
"""The attribute ID."""
|
|
255
|
+
|
|
256
|
+
def __post_init__(self):
|
|
257
|
+
_validate_name_segments(self.module, self.id)
|
|
258
|
+
|
|
259
|
+
@classmethod
|
|
260
|
+
def parse(cls, name: str) -> Self:
|
|
261
|
+
"""
|
|
262
|
+
Parse a module name from a ::-delimited string.
|
|
263
|
+
|
|
264
|
+
Parameters
|
|
265
|
+
----------
|
|
266
|
+
name :
|
|
267
|
+
The string to parse.
|
|
268
|
+
|
|
269
|
+
Returns
|
|
270
|
+
-------
|
|
271
|
+
:
|
|
272
|
+
The new name instance.
|
|
273
|
+
|
|
274
|
+
Raises
|
|
275
|
+
------
|
|
276
|
+
ValueError
|
|
277
|
+
If the given string cannot be parsed.
|
|
278
|
+
|
|
279
|
+
Examples
|
|
280
|
+
--------
|
|
281
|
+
>>> ModuleName.parse("ipm::beta")
|
|
282
|
+
ModuleName(module="ipm", id="beta")
|
|
283
|
+
"""
|
|
284
|
+
parts = name.split("::")
|
|
285
|
+
if len(parts) != 2:
|
|
286
|
+
raise ValueError("Invalid number of parts for module name.")
|
|
287
|
+
return cls(*parts)
|
|
288
|
+
|
|
289
|
+
def __str__(self) -> str:
|
|
290
|
+
return f"{self.module}::{self.id}"
|
|
291
|
+
|
|
292
|
+
def to_absolute(self, strata: str) -> AbsoluteName:
|
|
293
|
+
"""
|
|
294
|
+
Create an absolute name by providing the strata.
|
|
295
|
+
|
|
296
|
+
Parameters
|
|
297
|
+
----------
|
|
298
|
+
strata :
|
|
299
|
+
The strata name to use.
|
|
300
|
+
|
|
301
|
+
Returns
|
|
302
|
+
-------
|
|
303
|
+
:
|
|
304
|
+
The new absolute name instance.
|
|
305
|
+
"""
|
|
306
|
+
return AbsoluteName(strata, self.module, self.id)
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
@dataclass(frozen=True)
|
|
310
|
+
class AttributeName:
|
|
311
|
+
"""
|
|
312
|
+
A partially-specified name with just an attribute ID.
|
|
313
|
+
|
|
314
|
+
Parameters
|
|
315
|
+
----------
|
|
316
|
+
id :
|
|
317
|
+
The attribute ID.
|
|
318
|
+
"""
|
|
319
|
+
|
|
320
|
+
id: str
|
|
321
|
+
"""The attribute ID."""
|
|
322
|
+
|
|
323
|
+
def __post_init__(self):
|
|
324
|
+
_validate_name_segments(self.id)
|
|
325
|
+
|
|
326
|
+
def __str__(self) -> str:
|
|
327
|
+
return self.id
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
@dataclass(frozen=True)
|
|
331
|
+
class NamePattern:
|
|
332
|
+
"""
|
|
333
|
+
A name with a strata, module, and attribute ID that allows wildcards (*) so it can
|
|
334
|
+
act as a pattern to match against `AbsoluteNames`.
|
|
335
|
+
|
|
336
|
+
Parameters
|
|
337
|
+
----------
|
|
338
|
+
strata :
|
|
339
|
+
The strata name or wildcard.
|
|
340
|
+
module :
|
|
341
|
+
The module name or wildcard.
|
|
342
|
+
id :
|
|
343
|
+
The attribute ID or wildcard.
|
|
344
|
+
"""
|
|
345
|
+
|
|
346
|
+
strata: str
|
|
347
|
+
"""The strata name or wildcard."""
|
|
348
|
+
module: str
|
|
349
|
+
"""The module name or wildcard."""
|
|
350
|
+
id: str
|
|
351
|
+
"""The attribute ID or wildcard."""
|
|
352
|
+
|
|
353
|
+
def __post_init__(self):
|
|
354
|
+
_validate_pattern_segments(self.strata, self.module, self.id)
|
|
355
|
+
|
|
356
|
+
@classmethod
|
|
357
|
+
def parse(cls, name: str) -> Self:
|
|
358
|
+
"""
|
|
359
|
+
Parse a pattern from a ::-delimited string. As a shorthand, you can omit
|
|
360
|
+
preceding wildcard segments and they will be automatically filled in,
|
|
361
|
+
e.g., "a" will become "*::*::a" and "a::b" will become "*::a::b".
|
|
362
|
+
|
|
363
|
+
Parameters
|
|
364
|
+
----------
|
|
365
|
+
name :
|
|
366
|
+
The string to parse.
|
|
367
|
+
|
|
368
|
+
Returns
|
|
369
|
+
-------
|
|
370
|
+
:
|
|
371
|
+
The new name pattern instance.
|
|
372
|
+
|
|
373
|
+
Raises
|
|
374
|
+
------
|
|
375
|
+
ValueError
|
|
376
|
+
If the given string cannot be parsed.
|
|
377
|
+
|
|
378
|
+
Examples
|
|
379
|
+
--------
|
|
380
|
+
>>> NamePattern.parse("gpm:human::ipm::beta")
|
|
381
|
+
NamePattern(strata="gpm:human", module="ipm", id="beta")
|
|
382
|
+
|
|
383
|
+
>>> NamePattern.parse("*::*::beta")
|
|
384
|
+
NamePattern(strata="*", module="*", id="beta")
|
|
385
|
+
|
|
386
|
+
>>> NamePattern.parse("beta")
|
|
387
|
+
NamePattern(strata="*", module="*", id="beta")
|
|
388
|
+
"""
|
|
389
|
+
parts = name.split("::")
|
|
390
|
+
match len(parts):
|
|
391
|
+
case 1:
|
|
392
|
+
return cls("*", "*", *parts)
|
|
393
|
+
case 2:
|
|
394
|
+
return cls("*", *parts)
|
|
395
|
+
case 3:
|
|
396
|
+
return cls(*parts)
|
|
397
|
+
case _:
|
|
398
|
+
raise ValueError("Invalid number of parts for name pattern.")
|
|
399
|
+
|
|
400
|
+
@staticmethod
|
|
401
|
+
def of(name: "str | NamePattern") -> "NamePattern":
|
|
402
|
+
"""
|
|
403
|
+
Coerce the given value to a `NamePattern`.
|
|
404
|
+
|
|
405
|
+
Parameters
|
|
406
|
+
----------
|
|
407
|
+
name :
|
|
408
|
+
The name to coerce. This will be parsed if given as a string or
|
|
409
|
+
returned as-is if it's already a `NamePattern` instance.
|
|
410
|
+
|
|
411
|
+
Returns
|
|
412
|
+
-------
|
|
413
|
+
:
|
|
414
|
+
The name pattern instance.
|
|
415
|
+
|
|
416
|
+
Raises
|
|
417
|
+
------
|
|
418
|
+
ValueError
|
|
419
|
+
If `name` is given as a string but cannot be parsed.
|
|
420
|
+
"""
|
|
421
|
+
return name if isinstance(name, NamePattern) else NamePattern.parse(name)
|
|
422
|
+
|
|
423
|
+
def match(self, name: "AbsoluteName | NamePattern") -> bool:
|
|
424
|
+
"""
|
|
425
|
+
Test this pattern to see if it matches the given `AbsoluteName` or
|
|
426
|
+
`NamePattern`. The ability to match against `NamePatterns` is useful to see if
|
|
427
|
+
two patterns conflict with each other and would create ambiguity.
|
|
428
|
+
|
|
429
|
+
Parameters
|
|
430
|
+
----------
|
|
431
|
+
name :
|
|
432
|
+
The name to check against this pattern.
|
|
433
|
+
|
|
434
|
+
Returns
|
|
435
|
+
-------
|
|
436
|
+
:
|
|
437
|
+
True if there is a match, false otherwise.
|
|
438
|
+
"""
|
|
439
|
+
match name:
|
|
440
|
+
case AbsoluteName(s, m, i):
|
|
441
|
+
if self.strata != "*" and self.strata != s:
|
|
442
|
+
return False
|
|
443
|
+
if self.module != "*" and self.module != m:
|
|
444
|
+
return False
|
|
445
|
+
if self.id != "*" and self.id != i:
|
|
446
|
+
return False
|
|
447
|
+
return True
|
|
448
|
+
case NamePattern(s, m, i):
|
|
449
|
+
if self.strata != "*" and s != "*" and self.strata != s:
|
|
450
|
+
return False
|
|
451
|
+
if self.module != "*" and m != "*" and self.module != m:
|
|
452
|
+
return False
|
|
453
|
+
if self.id != "*" and i != "*" and self.id != i:
|
|
454
|
+
return False
|
|
455
|
+
return True
|
|
456
|
+
case _:
|
|
457
|
+
raise ValueError(f"Unsupported match: {type(name)}")
|
|
458
|
+
|
|
459
|
+
def __str__(self) -> str:
|
|
460
|
+
return f"{self.strata}::{self.module}::{self.id}"
|
|
461
|
+
|
|
462
|
+
|
|
463
|
+
@dataclass(frozen=True)
|
|
464
|
+
class ModuleNamePattern:
|
|
465
|
+
"""
|
|
466
|
+
A name with a module and attribute ID that allows wildcards (*).
|
|
467
|
+
Mostly this is useful to provide parameters to GPMs, which don't have
|
|
468
|
+
a concept of which strata they belong to. A `ModuleNamePattern` can be
|
|
469
|
+
transformed into a full `NamePattern` by adding the strata.
|
|
470
|
+
|
|
471
|
+
Parameters
|
|
472
|
+
----------
|
|
473
|
+
module :
|
|
474
|
+
The module name or wildcard.
|
|
475
|
+
id :
|
|
476
|
+
The attribute ID or wildcard.
|
|
477
|
+
"""
|
|
478
|
+
|
|
479
|
+
module: str
|
|
480
|
+
"""The module name or wildcard."""
|
|
481
|
+
id: str
|
|
482
|
+
"""The attribute ID or wildcard."""
|
|
483
|
+
|
|
484
|
+
def __post_init__(self):
|
|
485
|
+
_validate_pattern_segments(self.module, self.id)
|
|
486
|
+
|
|
487
|
+
@classmethod
|
|
488
|
+
def parse(cls, name: str) -> Self:
|
|
489
|
+
"""
|
|
490
|
+
Parse a pattern from a ::-delimited string. As a shorthand, you can omit
|
|
491
|
+
a preceding wildcard segment and it will be automatically filled in,
|
|
492
|
+
e.g.,"a" will become "*::a".
|
|
493
|
+
|
|
494
|
+
Parameters
|
|
495
|
+
----------
|
|
496
|
+
name :
|
|
497
|
+
The string to parse.
|
|
498
|
+
|
|
499
|
+
Returns
|
|
500
|
+
-------
|
|
501
|
+
:
|
|
502
|
+
The new name pattern instance.
|
|
503
|
+
|
|
504
|
+
Raises
|
|
505
|
+
------
|
|
506
|
+
ValueError
|
|
507
|
+
If the given string cannot be parsed.
|
|
508
|
+
|
|
509
|
+
Examples
|
|
510
|
+
--------
|
|
511
|
+
>>> ModuleNamePattern.parse("ipm::beta")
|
|
512
|
+
ModuleNamePattern(module="ipm", id="beta")
|
|
513
|
+
|
|
514
|
+
>>> ModuleNamePattern.parse("*::beta")
|
|
515
|
+
ModuleNamePattern(module="*", id="beta")
|
|
516
|
+
|
|
517
|
+
>>> ModuleNamePattern.parse("beta")
|
|
518
|
+
ModuleNamePattern(module="*", id="beta")
|
|
519
|
+
"""
|
|
520
|
+
if len(name) == 0:
|
|
521
|
+
raise ValueError("Empty string is not a valid name.")
|
|
522
|
+
parts = name.split("::")
|
|
523
|
+
match len(parts):
|
|
524
|
+
case 1:
|
|
525
|
+
return cls("*", *parts)
|
|
526
|
+
case 2:
|
|
527
|
+
return cls(*parts)
|
|
528
|
+
case _:
|
|
529
|
+
raise ValueError("Invalid number of parts for module name pattern.")
|
|
530
|
+
|
|
531
|
+
def to_absolute(self, strata: str) -> NamePattern:
|
|
532
|
+
"""
|
|
533
|
+
Create a full name pattern by providing the strata.
|
|
534
|
+
|
|
535
|
+
Parameters
|
|
536
|
+
----------
|
|
537
|
+
strata :
|
|
538
|
+
The strata to use.
|
|
539
|
+
|
|
540
|
+
Returns
|
|
541
|
+
-------
|
|
542
|
+
:
|
|
543
|
+
The new name pattern instance.
|
|
544
|
+
"""
|
|
545
|
+
return NamePattern(strata, self.module, self.id)
|
|
546
|
+
|
|
547
|
+
def __str__(self) -> str:
|
|
548
|
+
return f"{self.module}::{self.id}"
|
|
549
|
+
|
|
550
|
+
|
|
551
|
+
##############
|
|
552
|
+
# Attributes #
|
|
553
|
+
##############
|
|
554
|
+
|
|
555
|
+
|
|
556
|
+
AttributeT = TypeVar("AttributeT", bound=AttributeType)
|
|
557
|
+
"""The data type of an attribute; maps to the numpy type of the attribute array."""
|
|
558
|
+
|
|
559
|
+
|
|
560
|
+
@dataclass(frozen=True)
|
|
561
|
+
class AttributeDef(Generic[AttributeT]):
|
|
562
|
+
"""
|
|
563
|
+
The definition of a data attribute. Attributes are frequently used to define the
|
|
564
|
+
data requirements of things like IPMs and parameter functions.
|
|
565
|
+
|
|
566
|
+
`AttributeDef` is generic on the `AttributeType` which describes the type
|
|
567
|
+
of the data (`AttributeT`).
|
|
568
|
+
|
|
569
|
+
Parameters
|
|
570
|
+
----------
|
|
571
|
+
name :
|
|
572
|
+
The name used to identify the attribute.
|
|
573
|
+
type :
|
|
574
|
+
The type of the data.
|
|
575
|
+
shape :
|
|
576
|
+
The expected array shape of the data.
|
|
577
|
+
default_value :
|
|
578
|
+
An optional default value.
|
|
579
|
+
comment :
|
|
580
|
+
An optional description of the attribute.
|
|
581
|
+
"""
|
|
582
|
+
|
|
583
|
+
name: str
|
|
584
|
+
"""The name used to identify the attribute."""
|
|
585
|
+
type: AttributeT
|
|
586
|
+
"""The type of the data."""
|
|
587
|
+
shape: DataShape
|
|
588
|
+
"""The expected array shape of the data."""
|
|
589
|
+
default_value: AttributeValue | None = field(default=None, compare=False)
|
|
590
|
+
"""An optional default value."""
|
|
591
|
+
comment: str | None = field(default=None, compare=False)
|
|
592
|
+
"""An optional description of the attribute."""
|
|
593
|
+
|
|
594
|
+
def __post_init__(self):
|
|
595
|
+
if acceptable_name.match(self.name) is None:
|
|
596
|
+
raise ValueError(f"Invalid attribute name: {self.name}")
|
|
597
|
+
try:
|
|
598
|
+
dtype_as_np(self.type)
|
|
599
|
+
except Exception as e:
|
|
600
|
+
msg = (
|
|
601
|
+
f"AttributeDef's type is not correctly specified: {self.type}\n"
|
|
602
|
+
"See documentation for appropriate type designations."
|
|
603
|
+
)
|
|
604
|
+
raise ValueError(msg) from e
|
|
605
|
+
|
|
606
|
+
if (
|
|
607
|
+
self.default_value is not None #
|
|
608
|
+
and not dtype_check(self.type, self.default_value)
|
|
609
|
+
):
|
|
610
|
+
msg = (
|
|
611
|
+
"AttributeDef's default value does not align with its dtype "
|
|
612
|
+
f"('{self.name}')."
|
|
613
|
+
)
|
|
614
|
+
raise ValueError(msg)
|