-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulation.py
706 lines (600 loc) · 21.8 KB
/
simulation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
"""General simulation requisites and utility functions."""
import functools
from abc import ABC, ABCMeta, abstractmethod
from copy import deepcopy
from datetime import date, timedelta
from typing import (
Any,
Callable,
Generic,
Iterable,
Literal,
Mapping,
NamedTuple,
Self,
Sequence,
Type,
TypeGuard,
TypeVar,
Union,
final,
)
import numpy as np
from jsonpickle.util import is_picklable
from numpy.random import SeedSequence
from numpy.typing import NDArray
from sympy import Expr
from typing_extensions import override
from epymorph.attribute import (
NAME_PLACEHOLDER,
AbsoluteName,
AttributeDef,
AttributeName,
NamePattern,
)
from epymorph.compartment_model import BaseCompartmentModel
from epymorph.data_shape import Dimensions
from epymorph.data_type import (
AttributeArray,
ScalarDType,
ScalarValue,
StructDType,
StructValue,
)
from epymorph.database import (
Database,
DataResolver,
RecursiveValue,
ReqTree,
is_recursive_value,
)
from epymorph.geography.scope import GeoScope
from epymorph.time import TimeFrame
from epymorph.util import are_instances, are_unique
def default_rng(
seed: int | SeedSequence | None = None,
) -> Callable[[], np.random.Generator]:
"""
Convenience constructor to create a factory function for a simulation's
random number generator, optionally with a given seed.
"""
return lambda: np.random.default_rng(seed)
###################
# Simulation time #
###################
class Tick(NamedTuple):
"""
A Tick bundles related time-step information.
For instance, each time step corresponds to a calendar day,
a numeric day (i.e., relative to the start of the simulation),
which tau step this corresponds to, and so on.
"""
sim_index: int
"""Which simulation step are we on? (0,1,2,3,...)"""
day: int
"""Which day increment are we on? Same for each tau step: (0,0,1,1,2,2,...)"""
date: date
"""The calendar date corresponding to `day`"""
step: int
"""Which tau step are we on? (0,1,0,1,0,1,...)"""
tau: float
"""What's the tau length of the current step? (0.666,0.333,0.666,0.333,...)"""
class TickIndex(NamedTuple):
"""A zero-based index of the simulation tau steps."""
step: int # which tau step within that day (zero-indexed)
class TickDelta(NamedTuple):
"""
An offset relative to a Tick expressed as a number of days which should elapse,
and the step on which to end up. In applying this delta, it does not matter which
step we start on. We need the Clock configuration to apply a TickDelta, so see
Clock for the relevant method.
"""
days: int # number of whole days
step: int # which tau step within that day (zero-indexed)
NEVER = TickDelta(-1, -1)
"""
A special TickDelta value which expresses an event that should never happen.
Any Tick plus Never returns Never.
"""
def resolve_tick_delta(tau_steps_per_day: int, tick: Tick, delta: TickDelta) -> int:
"""Add a delta to a tick to get the index of the resulting tick."""
return (
-1
if delta.days == -1
else tick.sim_index - tick.step + (tau_steps_per_day * delta.days) + delta.step
)
def simulation_clock(
time_frame: TimeFrame,
tau_step_lengths: list[float],
) -> Iterable[Tick]:
"""Generator for the sequence of ticks which makes up the simulation clock."""
one_day = timedelta(days=1)
tau_steps = list(enumerate(tau_step_lengths))
curr_index = 0
curr_date = time_frame.start_date
for day in range(time_frame.days):
for step, tau in tau_steps:
yield Tick(curr_index, day, curr_date, step, tau)
curr_index += 1
curr_date += one_day
##############################
# Simulation parameter types #
##############################
ListValue = Sequence[Union[ScalarValue, StructValue, "ListValue"]]
ParamValue = Union[
ScalarValue,
StructValue,
ListValue,
"SimulationFunction",
Expr,
NDArray[ScalarDType | StructDType],
]
"""All acceptable input forms for parameter values."""
########################
# Simulation functions #
########################
class _Context(ABC):
"""
The evaluation context of a SimulationFunction. We want SimulationFunction
instances to be able to access properties of the simulation by using
various methods on `self`. But we also want to instantiate SimulationFunctions
before the simulation context exists! Hence this object starts out "empty"
and will be swapped for a "full" context when the function is evaluated in
a simulation context object. Partial contexts exist to allow easy one-off
evaluation of SimulationFunctions without a full RUME.
"""
def _invalid_context(
self,
component: Literal["data", "scope", "time_frame", "ipm", "rng"],
) -> TypeError:
err = (
"Missing function context during evaluation.\n"
"Simulation function tried to access "
f"'{component}' but this has not been provided. "
"Call `with_context()` first, providing all context that is required "
"by this function. Then call `evaluate()` on the returned object "
"to compute the value."
)
return TypeError(err)
@property
@abstractmethod
def name(self) -> AbsoluteName:
"""The name under which this attribute is being evaluated."""
@abstractmethod
def data(self, attribute: AttributeDef) -> AttributeArray:
"""Retrieve the value of an attribute."""
@property
@abstractmethod
def scope(self) -> GeoScope:
"""The simulation GeoScope."""
@property
@abstractmethod
def time_frame(self) -> TimeFrame:
"""The simulation time frame."""
@property
@abstractmethod
def ipm(self) -> BaseCompartmentModel:
"""The simulation's IPM."""
@property
@abstractmethod
def rng(self) -> np.random.Generator:
"""The simulation's random number generator."""
@property
@abstractmethod
def dim(self) -> Dimensions:
"""Simulation dimensions."""
@staticmethod
def of(
name: AbsoluteName,
data: DataResolver | None,
scope: GeoScope | None,
time_frame: TimeFrame | None,
ipm: BaseCompartmentModel | None,
rng: np.random.Generator | None,
) -> "_PartialContext | _FullContext":
if (
name is None
or data is None
or scope is None
or time_frame is None
or ipm is None
or rng is None
):
return _PartialContext(name, data, scope, time_frame, ipm, rng)
else:
return _FullContext(name, data, scope, time_frame, ipm, rng)
class _PartialContext(_Context):
_name: AbsoluteName
_data: DataResolver | None
_scope: GeoScope | None
_time_frame: TimeFrame | None
_ipm: BaseCompartmentModel | None
_rng: np.random.Generator | None
_dim: Dimensions
def __init__(
self,
name: AbsoluteName,
data: DataResolver | None,
scope: GeoScope | None,
time_frame: TimeFrame | None,
ipm: BaseCompartmentModel | None,
rng: np.random.Generator | None,
):
self._name = name
self._data = data
self._scope = scope
self._time_frame = time_frame
self._ipm = ipm
self._rng = rng
self._dim = Dimensions.of(
T=time_frame.duration_days if time_frame is not None else None,
N=scope.nodes if scope is not None else None,
C=ipm.num_compartments if ipm is not None else None,
E=ipm.num_events if ipm is not None else None,
)
@property
def name(self) -> AbsoluteName:
return self._name
@override
def data(self, attribute: AttributeDef) -> NDArray:
if self._data is None:
raise self._invalid_context("data")
name = self._name.to_namespace().to_absolute(attribute.name)
return self._data.resolve(name, attribute)
@property
@override
def time_frame(self) -> TimeFrame:
if self._time_frame is None:
raise self._invalid_context("time_frame")
return self._time_frame
@property
@override
def ipm(self) -> BaseCompartmentModel:
if self._ipm is None:
raise self._invalid_context("ipm")
return self._ipm
@property
@override
def scope(self) -> GeoScope:
if self._scope is None:
raise self._invalid_context("scope")
return self._scope
@property
@override
def rng(self) -> np.random.Generator:
if self._rng is None:
raise self._invalid_context("rng")
return self._rng
@property
@override
def dim(self) -> Dimensions:
return self._dim
_EMPTY_CONTEXT = _PartialContext(NAME_PLACEHOLDER, None, None, None, None, None)
class _FullContext(_Context):
_name: AbsoluteName
_data: DataResolver
_scope: GeoScope
_time_frame: TimeFrame
_ipm: BaseCompartmentModel
_rng: np.random.Generator
_dim: Dimensions
def __init__(
self,
name: AbsoluteName,
data: DataResolver,
scope: GeoScope,
time_frame: TimeFrame,
ipm: BaseCompartmentModel,
rng: np.random.Generator,
):
self._name = name
self._data = data
self._scope = scope
self._time_frame = time_frame
self._ipm = ipm
self._rng = rng
self._dim = Dimensions.of(
T=time_frame.duration_days,
N=scope.nodes,
C=ipm.num_compartments,
E=ipm.num_events,
)
@property
def name(self) -> AbsoluteName:
return self._name
@override
def data(self, attribute: AttributeDef) -> NDArray:
name = self._name.to_namespace().to_absolute(attribute.name)
return self._data.resolve(name, attribute)
@property
@override
def scope(self) -> GeoScope:
return self._scope
@property
@override
def time_frame(self) -> TimeFrame:
return self._time_frame
@property
@override
def ipm(self) -> BaseCompartmentModel:
return self._ipm
@property
@override
def rng(self) -> np.random.Generator:
return self._rng
@property
@override
def dim(self) -> Dimensions:
return self._dim
_TypeT = TypeVar("_TypeT")
class SimulationFunctionClass(ABCMeta):
"""
The metaclass for SimulationFunctions.
Used to verify proper class implementation.
"""
def __new__(
mcs: Type[_TypeT],
name: str,
bases: tuple[type, ...],
dct: dict[str, Any],
) -> _TypeT:
# Check requirements if this class overrides it.
# (Otherwise class will inherit from parent.)
if (reqs := dct.get("requirements")) is not None:
# The user may specify requirements as a property, in which case we
# can't validate much about the implementation.
if not isinstance(reqs, property):
# But if it's a static value, check types:
if not isinstance(reqs, (list, tuple)):
raise TypeError(
f"Invalid requirements in {name}: "
"please specify as a list or tuple."
)
if not are_instances(reqs, AttributeDef):
raise TypeError(
f"Invalid requirements in {name}: "
"must be instances of AttributeDef."
)
if not are_unique(r.name for r in reqs):
raise TypeError(
f"Invalid requirements in {name}: "
"requirement names must be unique."
)
# Make requirements list immutable
dct["requirements"] = tuple(reqs)
# Check serializable
if not is_picklable(name, mcs):
raise TypeError(
f"Invalid simulation function {name}: "
"classes must be serializable (using jsonpickle)."
)
# NOTE: is_picklable() is misleading here; it does not guarantee that instances
# of a class are picklable, nor (if you called it against an instance) that all
# of the instance's attributes are picklable. jsonpickle simply ignores
# unpicklable fields, decoding objects into attribute swiss cheese.
# It will be more effective to check that all of the attributes of an object
# are picklable before we try to serialize it...
# Thus I don't think we can guarantee picklability at class definition time.
# Something like:
# [(n, is_picklable(n, x)) for n, x in obj.__dict__.items()]
# Why worry? Lambda functions are probably the most likely problem;
# they're not picklable by default.
# But a simple workaround is to use a def function and,
# if needed, partial function application.
if (orig_evaluate := dct.get("evaluate")) is not None:
@functools.wraps(orig_evaluate)
def evaluate(self, *args, **kwargs):
result = orig_evaluate(self, *args, **kwargs)
self.validate(result)
return result
dct["evaluate"] = evaluate
return super().__new__(mcs, name, bases, dct)
ResultT = TypeVar("ResultT")
"""The result type of a SimulationFunction."""
_DeferResultT = TypeVar("_DeferResultT")
"""The result type of a SimulationFunction during deference."""
_DeferFunctionT = TypeVar("_DeferFunctionT", bound="BaseSimulationFunction")
"""The type of a SimulationFunction during deference."""
class BaseSimulationFunction(ABC, Generic[ResultT], metaclass=SimulationFunctionClass):
"""
A function which runs in the context of a simulation to produce a value
(as a numpy array). This base class exists to share functionality without
limiting the function signature of evaluate().
"""
requirements: Sequence[AttributeDef] | property = ()
"""The attribute definitions describing the data requirements for this function.
For advanced use-cases, you may specify requirements as a property if you need it
to be dynamically computed.
"""
randomized: bool = False
"""Should this function be re-evaluated every time it's referenced in a RUME?
(Mostly useful for randomized results.) If False, even a function that utilizes
the context RNG will only be computed once, resulting in a single random value
that is shared by all references during evaluation."""
_ctx: _FullContext | _PartialContext = _EMPTY_CONTEXT
@property
def class_name(self) -> str:
"""The class name of the SimulationFunction."""
return f"{self.__class__.__module__}.{self.__class__.__qualname__}"
def validate(self, result: ResultT) -> None:
"""Override this method to validate the evaluation result.
Implementations should raise an appropriate error if results
are not valid."""
@final
def with_context(
self,
name: AbsoluteName = NAME_PLACEHOLDER,
params: Mapping[str, ParamValue] | None = None,
scope: GeoScope | None = None,
time_frame: TimeFrame | None = None,
ipm: BaseCompartmentModel | None = None,
rng: np.random.Generator | None = None,
) -> Self:
"""Constructs a clone of this instance which has access to the given context."""
# This version allows users to specify data using strings for names.
# epymorph should use `with_context_internal()` whenever possible.
if params is None:
params = {}
try:
for p in params:
AttributeName(p)
except ValueError:
err = (
"When evaluating a sim function this way, namespaced params "
"are not allowed (names using '::') because those values would "
"not be able to contribute to the evaluation. "
"Specify param names as simple strings instead."
)
raise ValueError(err)
reqs = ReqTree.of(
{name.with_id(req.name): req for req in self.requirements},
Database({NamePattern.parse(k): v for k, v in params.items()}),
)
data = reqs.evaluate(scope, time_frame, ipm, rng)
return self.with_context_internal(name, data, scope, time_frame, ipm, rng)
def with_context_internal(
self,
name: AbsoluteName = NAME_PLACEHOLDER,
data: DataResolver | None = None,
scope: GeoScope | None = None,
time_frame: TimeFrame | None = None,
ipm: BaseCompartmentModel | None = None,
rng: np.random.Generator | None = None,
) -> Self:
"""Constructs a clone of this instance which has access to the given context."""
# clone this instance, then run evaluate on that; accomplishes two things:
# 1. don't have to worry about cleaning up _ctx
# 2. instances can use @cached_property without surprising results
clone = deepcopy(self)
setattr(clone, "_ctx", _Context.of(name, data, scope, time_frame, ipm, rng))
return clone
@final
def defer_context(
self,
other: _DeferFunctionT,
scope: GeoScope | None = None,
time_frame: TimeFrame | None = None,
) -> _DeferFunctionT:
"""Defer processing to another instance of a SimulationFunction."""
return other.with_context_internal(
name=self._ctx._name,
data=self._ctx._data,
scope=scope or self._ctx._scope,
time_frame=time_frame or self._ctx._time_frame,
ipm=self._ctx._ipm,
rng=self._ctx._rng,
)
@final
@property
def name(self) -> AbsoluteName:
"""The name under which this attribute is being evaluated."""
return self._ctx.name
@final
def data(self, attribute: AttributeDef | str) -> NDArray:
"""Retrieve the value of a specific attribute."""
if isinstance(attribute, str):
name = attribute
req = next((r for r in self.requirements if r.name == attribute), None)
else:
name = attribute.name
req = attribute
if req is None or req not in self.requirements:
raise ValueError(
f"Simulation function {self.__class__.__name__} "
f"accessed an attribute ({name}) "
"which you did not declare as a requirement."
)
return self._ctx.data(req)
@final
@property
def scope(self) -> GeoScope:
"""The simulation GeoScope."""
return self._ctx.scope
@final
@property
def time_frame(self) -> TimeFrame:
"""The simulation TimeFrame."""
return self._ctx.time_frame
@final
@property
def ipm(self) -> BaseCompartmentModel:
"""The simulation IPM."""
return self._ctx.ipm
@final
@property
def rng(self) -> np.random.Generator:
"""The simulation's random number generator."""
return self._ctx.rng
@final
@property
def dim(self) -> Dimensions:
return self._ctx.dim
@is_recursive_value.register
def _(value: BaseSimulationFunction) -> TypeGuard[RecursiveValue]:
return True
class SimulationFunction(BaseSimulationFunction[ResultT]):
"""
A function which runs in the context of a simulation to produce a value
(as a numpy array).
Implement a SimulationFunction by extending this class and overriding the
`evaluate()` method.
"""
@abstractmethod
def evaluate(self) -> ResultT:
"""
Implement this method to provide logic for the function.
Use self methods and properties to access the simulation context or defer
processing to another function.
"""
@final
def defer(
self,
other: "SimulationFunction[_DeferResultT]",
scope: GeoScope | None = None,
time_frame: TimeFrame | None = None,
) -> _DeferResultT:
"""Defer processing to another instance of a SimulationFunction, returning
the result of evaluation.
Parameters
----------
other : SimulationFunction
the other function to defer to
scope : GeoScope, optional
override the geo scope for evaluation; if None, use the same scope
time_frame : TimeFrame, optional
override the time frame for evaluation; if None, use the same time frame
"""
return self.defer_context(other, scope, time_frame).evaluate()
class SimulationTickFunction(BaseSimulationFunction[ResultT]):
"""
A function which runs in the context of a simulation to produce a sim-time-specific
value (as a numpy array). Implement a SimulationTickFunction by extending this class
and overriding the `evaluate()` method.
"""
@abstractmethod
def evaluate(self, tick: Tick) -> ResultT:
"""
Implement this method to provide logic for the function.
Use self methods and properties to access the simulation context or defer
processing to another function.
"""
@final
def defer(
self,
other: "SimulationTickFunction[_DeferResultT]",
tick: Tick,
scope: GeoScope | None = None,
time_frame: TimeFrame | None = None,
) -> _DeferResultT:
"""Defer processing to another instance of a SimulationTickFunction, returning
the result of evaluation.
Parameters
----------
other : SimulationFunction
the other function to defer to
scope : GeoScope, optional
override the geo scope for evaluation; if None, use the same scope
time_frame : TimeFrame, optional
override the time frame for evaluation; if None, use the same time frame
"""
return self.defer_context(other, scope, time_frame).evaluate(tick)