-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompartment_model.py
1332 lines (1121 loc) · 47.5 KB
/
compartment_model.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
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
The basis of the intra-population model (disease mechanics) system in epymorph.
This represents disease mechanics using a compartmental model for tracking
populations as groupings of integer-numbered individuals.
"""
import dataclasses
import re
from abc import ABC, ABCMeta, abstractmethod
from dataclasses import dataclass, field
from pathlib import Path
from typing import (
Any,
Callable,
Iterable,
Iterator,
Literal,
NamedTuple,
OrderedDict,
Self,
Sequence,
Type,
TypeVar,
final,
)
from warnings import warn
import numpy as np
from numpy.typing import NDArray
from sympy import Add, Expr, Float, Integer, Symbol
from typing_extensions import override
from epymorph.database import AbsoluteName, AttributeDef
from epymorph.error import IpmValidationException
from epymorph.simulation import DEFAULT_STRATA, META_STRATA, gpm_strata
from epymorph.sympy_shim import simplify, simplify_sum, substitute, to_symbol
from epymorph.tools.ipm_diagram import render_diagram
from epymorph.util import (
acceptable_name,
are_instances,
are_unique,
filter_unique,
)
######################
# Model Compartments #
######################
BIRTH = Symbol("birth_exogenous")
"""An IPM psuedo-compartment representing exogenous input of individuals."""
DEATH = Symbol("death_exogenous")
"""An IPM psuedo-compartment representing exogenous removal of individuals."""
exogenous_states = (BIRTH, DEATH)
"""A complete listing of epymorph-supported exogenous states."""
@dataclass(frozen=True)
class CompartmentName:
base: str
subscript: str | None
strata: str | None
full: str = field(init=False, hash=False, compare=False)
def __post_init__(self):
full = "_".join(
x for x in (self.base, self.subscript, self.strata) if x is not None
)
if acceptable_name.match(full) is None:
raise ValueError(f"Invalid compartment name: {full}")
object.__setattr__(self, "full", full)
def with_subscript(self, subscript: str | None) -> Self:
if self.subscript == "exogenous":
return self
return dataclasses.replace(self, subscript=subscript)
def with_strata(self, strata: str | None) -> Self:
if self.subscript == "exogenous":
return self
return dataclasses.replace(self, strata=strata)
def __str__(self) -> str:
return self.full
@classmethod
def parse(cls, name: str) -> Self:
if (i := name.find("_")) != -1:
return cls(name[0:i], name[i + 1 :], None)
return cls(name, None, None)
@dataclass(frozen=True)
class CompartmentDef:
"""Defines an IPM compartment."""
name: CompartmentName
tags: list[str]
description: str | None = field(default=None)
def with_strata(self, strata: str) -> Self:
return dataclasses.replace(self, name=self.name.with_strata(strata))
def compartment(
name: str,
tags: list[str] | None = None,
description: str | None = None,
) -> CompartmentDef:
"""Define an IPM compartment."""
return CompartmentDef(CompartmentName.parse(name), tags or [], description)
def quick_compartments(symbol_names: str) -> list[CompartmentDef]:
"""
Define a number of IPM compartments from a space-delimited string.
This is just short-hand syntax for the `compartment()` function.
"""
return [compartment(name) for name in symbol_names.split()]
#####################
# Model Transitions #
#####################
@dataclass(frozen=True)
class EdgeName:
compartment_from: CompartmentName
compartment_to: CompartmentName
full: str = field(init=False, hash=False, compare=False)
def __post_init__(self):
full = f"{self.compartment_from} → {self.compartment_to}"
object.__setattr__(self, "full", full)
if (
self.compartment_from.subscript != "exogenous"
and self.compartment_to.subscript != "exogenous"
and self.compartment_from.strata != self.compartment_to.strata
):
raise ValueError(f"Edges must be within a single strata ({full})")
def with_subscript(self, subscript: str | None) -> Self:
return dataclasses.replace(
self,
compartment_from=self.compartment_from.with_subscript(subscript),
compartment_to=self.compartment_to.with_subscript(subscript),
)
def with_strata(self, strata: str | None) -> Self:
return dataclasses.replace(
self,
compartment_from=self.compartment_from.with_strata(strata),
compartment_to=self.compartment_to.with_strata(strata),
)
def __str__(self) -> str:
return self.full
@dataclass(frozen=True)
class EdgeDef:
"""Defines a single edge transitions in a compartment model."""
name: EdgeName
rate: Expr
compartment_from: Symbol
compartment_to: Symbol
@property
def tuple(self) -> tuple[str, str]:
return str(self.compartment_from), str(self.compartment_to)
def edge(
compartment_from: Symbol,
compartment_to: Symbol,
rate: Expr | int | float,
) -> EdgeDef:
"""Define a transition edge from one compartment to another at the given rate."""
if isinstance(rate, int):
_rate = Integer(rate)
elif isinstance(rate, float):
_rate = Float(rate)
else:
_rate = rate
name = EdgeName(
CompartmentName.parse(str(compartment_from)),
CompartmentName.parse(str(compartment_to)),
)
return EdgeDef(name, _rate, compartment_from, compartment_to)
@dataclass(frozen=True)
class ForkDef:
"""Defines a fork-style transition in a compartment model."""
rate: Expr
edges: list[EdgeDef]
probs: list[Expr]
def __str__(self) -> str:
lhs = str(self.edges[0].compartment_from)
rhs = ",".join([str(edge.compartment_to) for edge in self.edges])
return f"{lhs} → ({rhs})"
def fork(*edges: EdgeDef) -> ForkDef:
"""
Define a forked transition: a set of edges that come from the same compartment
but go to different compartments. It is assumed the edges will share a
"base rate"-- a common sub-expression among all edge rates --
and that each edge in the fork is given a proportion on that base rate.
For example, consider two edges given rates:
1. `delta * EXPOSED * rho`
2. `delta * EXPOSED * (1 - rho)`
`delta * EXPOSED` is the base rate and `rho` describes the proportional split for
each edge.
"""
# First verify that the edges all come from the same state.
if len(set(e.compartment_from for e in edges)) > 1:
err = (
"In a Fork, all edges must share the same `state_from`.\n"
f" Problem in: {str(edges)}"
)
raise IpmValidationException(err)
# it is assumed the fork's edges are defined with complementary rate expressions
edge_rates = [e.rate for e in edges]
# the "base rate" -- how many individuals transition on any of these edges --
# is the sum of all the edge rates (this defines the lambda for the poisson draw)
rate = simplify_sum(edge_rates)
# the probability of following a particular edge is then the edge's rate divided by
# the base rate
# (this defines the probability split in the eventual multinomial draw)
probs = [simplify(r / rate) for r in edge_rates] # type: ignore
return ForkDef(rate, list(edges), probs)
TransitionDef = EdgeDef | ForkDef
"""All ways to define a compartment model transition: edges or forks."""
def _as_events(trxs: Iterable[TransitionDef]) -> Iterator[EdgeDef]:
"""
Iterator for all unique events defined in the transition model.
Each edge corresponds to a single event, even the edges that are part of a fork.
The events are returned in a stable order (definition order) so that they can be
indexed that way.
"""
for t in trxs:
match t:
case EdgeDef() as e:
yield e
case ForkDef(_, edges):
for e in edges:
yield e
def _remap_edge(
e: EdgeDef,
strata: str,
symbol_mapping: dict[Symbol, Symbol],
) -> EdgeDef:
return EdgeDef(
name=e.name.with_strata(strata),
rate=substitute(e.rate, symbol_mapping),
compartment_from=symbol_mapping[e.compartment_from],
compartment_to=symbol_mapping[e.compartment_to],
)
def _remap_fork(
f: ForkDef,
strata: str,
symbol_mapping: dict[Symbol, Symbol],
) -> ForkDef:
return ForkDef(
rate=substitute(f.rate, symbol_mapping),
edges=[_remap_edge(e, strata, symbol_mapping) for e in f.edges],
probs=[substitute(p, symbol_mapping) for p in f.probs],
)
def _remap_transition(
t: TransitionDef,
strata: str,
symbol_mapping: dict[Symbol, Symbol],
) -> TransitionDef:
"""
Replaces symbols used in the transition using substitution from `symbol_mapping`.
"""
match t:
case EdgeDef():
return _remap_edge(t, strata, symbol_mapping)
case ForkDef():
return _remap_fork(t, strata, symbol_mapping)
######################
# Compartment Models #
######################
class ModelSymbols:
"""IPM symbols needed in defining the model's transition rate expressions."""
all_compartments: Sequence[Symbol]
"""Compartment symbols in definition order."""
all_requirements: Sequence[Symbol]
"""Requirements symbols in definition order."""
_csymbols: dict[str, Symbol]
"""Mapping of compartment name to symbol."""
_rsymbols: dict[str, Symbol]
"""Mapping of requirement name to symbol."""
def __init__(
self,
compartments: Sequence[tuple[str, str]],
requirements: Sequence[tuple[str, str]],
):
# NOTE: the arguments here are tuples of name and symbolic name;
# this is redundant for single-strata models, but allows multistrata models
# to keep fine-grained control over symbol substitution while allowing
# the user to refer to the names they already know.
cs = [(n, to_symbol(s)) for n, s in compartments]
rs = [(n, to_symbol(s)) for n, s in requirements]
self.all_compartments = [s for _, s in cs]
self.all_requirements = [s for _, s in rs]
self._csymbols = dict(cs)
self._rsymbols = dict(rs)
def compartments(self, *names: str) -> Sequence[Symbol]:
"""Select compartment symbols by name."""
return [self._csymbols[n] for n in names]
def requirements(self, *names: str) -> Sequence[Symbol]:
"""Select requirement symbols by name."""
return [self._rsymbols[n] for n in names]
class BaseCompartmentModel(ABC):
"""Shared base-class for compartment models."""
compartments: Sequence[CompartmentDef] = ()
"""The compartments of the model."""
requirements: Sequence[AttributeDef] = ()
"""The attributes required by the model."""
# NOTE: these two (above) attributes are coded as such so that overriding
# this class is simpler for users. Normally I'd make them properties,
# -- since they really should not be modified after creation --
# but this would increase the implementation complexity.
# And to avoid requiring users to call the initializer, the rest
# of the attributes are cached_properties which initialize lazily.
# These private attributes will be computed during instance initialization
# by the metaclass. It's better to do eager evaluation of these things
# so that issues in model construction pop up sooner than later.
@property
def quantities(self) -> Iterator[CompartmentDef | EdgeDef]:
yield from self.compartments
yield from self.events
@property
@abstractmethod
def symbols(self) -> ModelSymbols:
"""The symbols which represent parts of this model."""
@property
@abstractmethod
def transitions(self) -> Sequence[TransitionDef]:
"""The transitions in the model."""
@property
@abstractmethod
def events(self) -> Sequence[EdgeDef]:
"""Iterate over all events in order."""
@property
@abstractmethod
def requirements_dict(self) -> OrderedDict[AbsoluteName, AttributeDef]:
"""The attributes required by this model."""
@property
def num_compartments(self) -> int:
"""The number of compartments in this model."""
return len(self.compartments)
@property
def num_events(self) -> int:
"""The number of distinct events (transitions) in this model."""
return len(self.events)
@property
@abstractmethod
def strata(self) -> Sequence[str]:
"""The names of the strata involved in this compartment model."""
@property
@abstractmethod
def is_multistrata(self) -> bool:
"""True if this compartment model is multistrata (False for single-strata)."""
@property
def select(self) -> "QuantitySelector":
return QuantitySelector(self)
def diagram(
self,
*,
file: str | Path | None = None,
figsize: tuple[float, float] | None = None,
) -> None:
"""Render a diagram of this IPM, either by showing it with matplotlib (default)
or by saving it to `file` as a png image.
Parameters
----------
file : str | Path, optional
Provide a file path to save a png image of the diagram to this path.
If `file` is None, we will instead use matplotlib to show the diagram.
figsize : tuple[float, float], optional
The matplotlib figure size to use when displaying the diagram.
Only used if `file` is not provided.
"""
render_diagram(ipm=self, file=file, figsize=figsize)
def validate_compartment_model(model: BaseCompartmentModel) -> None:
name = model.__class__.__name__
# transitions cannot have the source and destination both be exogenous;
# this would be madness.
if any(
edge.compartment_from in exogenous_states
and edge.compartment_to in exogenous_states
for edge in model.events
):
err = (
f"Invalid transitions in {name}: "
"transitions cannot use exogenous states (BIRTH/DEATH) "
"as both source and destination."
)
raise IpmValidationException(err)
# Extract the set of compartments used by transitions.
trx_comps = set(
compartment
for e in model.events
for compartment in [e.compartment_from, e.compartment_to]
# don't include exogenous states in the compartment set
if compartment not in exogenous_states
)
# Extract the set of requirements used by transition rate expressions
# by taking all used symbols and subtracting compartment symbols.
trx_reqs = set(
symbol
for e in model.events
for symbol in e.rate.free_symbols
if isinstance(symbol, Symbol)
).difference(trx_comps)
# transition compartments minus declared compartments should be empty
missing_comps = trx_comps.difference(model.symbols.all_compartments)
if len(missing_comps) > 0:
err = (
f"Invalid transitions in {name}: "
"transitions reference compartments which were not declared.\n"
f"Missing compartments: {', '.join(map(str, missing_comps))}"
)
raise IpmValidationException(err)
# declared compartments minus used compartments is ideally empty,
# otherwise raise a warning
extra_comps = set(model.symbols.all_compartments).difference(trx_comps)
if len(extra_comps) > 0:
msg = (
f"Possible issue in {name}: "
"not all declared compartments are being used in transitions.\n"
f"Extra compartments: {', '.join(map(str, extra_comps))}"
)
warn(msg)
# transition requirements minus declared requirements should be empty
missing_reqs = trx_reqs.difference(model.symbols.all_requirements)
if len(missing_reqs) > 0:
err = (
f"Invalid transitions in {name}: "
"transitions reference requirements which were not declared.\n"
f"Missing requirements: {', '.join(map(str, missing_reqs))}"
)
raise IpmValidationException(err)
# declared requirements minus used requirements is ideally empty,
# otherwise raise a warning
extra_reqs = set(model.symbols.all_requirements).difference(trx_reqs)
if len(extra_reqs) > 0:
msg = (
f"Possible issue in {name}: "
"not all declared requirements are being used in transitions.\n"
f"Extra requirements: {', '.join(map(str, extra_reqs))}"
)
warn(msg)
####################################
# Single-strata Compartment Models #
####################################
class CompartmentModelClass(ABCMeta):
"""
The metaclass for user-defined CompartmentModel classes.
Used to verify proper class implementation.
"""
def __new__(
mcs: Type["CompartmentModelClass"],
name: str,
bases: tuple[type, ...],
dct: dict[str, Any],
) -> "CompartmentModelClass":
# Skip these checks for abstract classes:
cls0 = super().__new__(mcs, name, bases, dct)
if getattr(cls0, "__abstractmethods__", False):
return cls0
# Check model compartments.
cmps = dct.get("compartments")
if cmps is None or not isinstance(cmps, (list, tuple)):
err = f"Invalid compartments in {name}: please specify as a list or tuple."
raise IpmValidationException(err)
if len(cmps) == 0:
err = (
f"Invalid compartments in {name}: "
"please specify at least one compartment."
)
raise IpmValidationException(err)
if not are_instances(cmps, CompartmentDef):
err = (
f"Invalid compartments in {name}: must be instances of CompartmentDef."
)
raise IpmValidationException(err)
if not are_unique(c.name for c in cmps):
err = f"Invalid compartments in {name}: compartment names must be unique."
raise IpmValidationException(err)
# Make compartments immutable.
dct["compartments"] = tuple(cmps)
# Check transitions... we have to instantiate the class.
cls = super().__new__(mcs, name, bases, dct)
instance = cls()
validate_compartment_model(instance)
return cls
def __call__(cls, *args, **kwargs):
# Perform our initialization on all newly created instances.
# This allows us to bypass writing an __init__ function, which
# end users would then have to remember to call when subclassing.
# This should make implementations easier to write.
instance = super().__call__(*args, **kwargs)
instance._construct_model()
return instance
class CompartmentModel(BaseCompartmentModel, ABC, metaclass=CompartmentModelClass):
"""
A compartment model definition and its corresponding metadata.
Effectively, a collection of compartments, transitions between compartments,
and the data parameters which are required to compute the transitions.
"""
@abstractmethod
def edges(self, symbols: ModelSymbols) -> Sequence[TransitionDef]:
"""
When implementing a CompartmentModel, override this method
to build the transition edges between compartments. You are
given a reference to this model's symbols library so you can
build expressions for the transition rates.
"""
_symbols: ModelSymbols
_transitions: Sequence[TransitionDef]
_events: Sequence[EdgeDef]
_requirements_dict: OrderedDict[AbsoluteName, AttributeDef]
@final
def _construct_model(self):
# epymorph's initialization logic, invoked by the metaclass
# (see metaclass __call__ for more info)
self._symbols = ModelSymbols(
[(c.name.full, c.name.full) for c in self.compartments],
[(r.name, r.name) for r in self.requirements],
)
self._transitions = self.edges(self.symbols)
self._events = list(_as_events(self._transitions))
self._requirements_dict = OrderedDict(
[
(AbsoluteName(gpm_strata(DEFAULT_STRATA), "ipm", r.name), r)
for r in self.requirements
]
)
@property
@override
def symbols(self) -> ModelSymbols:
"""The symbols which represent parts of this model."""
return self._symbols
@property
@override
def transitions(self) -> Sequence[TransitionDef]:
"""The transitions in the model."""
return self._transitions
@property
@override
def events(self) -> Sequence[EdgeDef]:
"""Iterate over all events in order."""
# return list(_as_events(self.transitions))
return self._events
@property
@override
def requirements_dict(self) -> OrderedDict[AbsoluteName, AttributeDef]:
"""The attributes required by this model."""
return self._requirements_dict
@property
@override
def strata(self) -> Sequence[str]:
return ["all"]
@property
@override
def is_multistrata(self) -> bool:
return False
###################################
# Multi-strata Compartment Models #
###################################
class MultistrataModelSymbols(ModelSymbols):
"""IPM symbols needed in defining the model's transition rate expressions."""
all_meta_requirements: Sequence[Symbol]
"""Meta-requirement symbols in definition order."""
_msymbols: dict[str, Symbol]
"""Mapping of meta requirements name to symbol."""
strata: Sequence[str]
"""The strata names used in this model."""
_strata_symbols: dict[str, ModelSymbols]
"""
Mapping of strata name to the symbols of that strata.
The symbols within use their original names.
"""
def __init__(
self,
strata: Sequence[tuple[str, CompartmentModel]],
meta_requirements: Sequence[AttributeDef],
):
# These are all tuples of:
# (original name, strata name, symbolic name)
# where the symbolic name is disambiguated by appending
# the strata it belongs to.
cs = [
(c.name.full, strata_name, f"{c.name}_{strata_name}")
for strata_name, ipm in strata
for c in ipm.compartments
]
rs = [
(r.name, strata_name, f"{r.name}_{strata_name}")
for strata_name, ipm in strata
for r in ipm.requirements
]
ms = [(r.name, "meta", f"{r.name}_meta") for r in meta_requirements]
super().__init__(
compartments=[(sym, sym) for _, _, sym in cs],
requirements=[
*((sym, sym) for _, _, sym in rs),
*((orig, sym) for orig, _, sym in ms),
],
)
self.strata = [strata_name for strata_name, _ in strata]
self._strata_symbols = {
strata_name: ModelSymbols(
compartments=[
(orig, sym) for orig, strt, sym in cs if strt == strata_name
],
requirements=[
(orig, sym) for orig, strt, sym in rs if strt == strata_name
],
)
for strata_name, _ in strata
}
self.all_meta_requirements = [to_symbol(sym) for _, _, sym in ms]
self._msymbols = {orig: to_symbol(sym) for orig, _, sym in ms}
def strata_compartments(self, strata: str, *names: str) -> Sequence[Symbol]:
"""
Select compartment symbols by name in a particular strata.
If `names` is non-empty, select those symbols by their original name.
If `names` is empty, return all symbols.
"""
sym = self._strata_symbols[strata]
return sym.all_compartments if len(names) == 0 else sym.compartments(*names)
def strata_requirements(self, strata: str, *names: str) -> Sequence[Symbol]:
"""
Select requirement symbols by name in a particular strata.
If `names` is non-empty, select those symbols by their original name.
If `names` is empty, return all symbols.
"""
sym = self._strata_symbols[strata]
return sym.all_requirements if len(names) == 0 else sym.requirements(*names)
MetaEdgeBuilder = Callable[[MultistrataModelSymbols], Sequence[TransitionDef]]
"""A function for creating meta edges in a multistrata RUME."""
class CombinedCompartmentModel(BaseCompartmentModel):
"""A CompartmentModel constructed by combining others."""
compartments: Sequence[CompartmentDef]
"""All compartments; renamed with strata."""
requirements: Sequence[AttributeDef]
"""All requirements, including meta-requirements."""
_strata: Sequence[tuple[str, CompartmentModel]]
_meta_requirements: Sequence[AttributeDef]
_meta_edges: MetaEdgeBuilder
_symbols: MultistrataModelSymbols
_transitions: Sequence[TransitionDef]
_events: Sequence[EdgeDef]
_requirements_dict: OrderedDict[AbsoluteName, AttributeDef]
def __init__(
self,
strata: Sequence[tuple[str, CompartmentModel]],
meta_requirements: Sequence[AttributeDef],
meta_edges: MetaEdgeBuilder,
):
self._strata = strata
self._meta_requirements = meta_requirements
self._meta_edges = meta_edges
self.compartments = [
comp.with_strata(strata_name)
for strata_name, ipm in strata
for comp in ipm.compartments
]
self.requirements = [
*(r for _, ipm in strata for r in ipm.requirements),
*self._meta_requirements,
]
symbols = MultistrataModelSymbols(
strata=self._strata, meta_requirements=self._meta_requirements
)
self._symbols = symbols
# Figure out the per-strata mapping from old symbol to new symbol
# by matching everything up in-order.
strata_mapping = list[dict[Symbol, Symbol]]()
# And a mapping from new (stratified) symbols back to their original form
# and which strata they belong to.
reverse_mapping = dict[Symbol, tuple[str | None, Symbol]]()
all_cs = iter(symbols.all_compartments)
all_rs = iter(symbols.all_requirements)
for strata_name, ipm in self._strata:
mapping = {x: x for x in exogenous_states}
old = ipm.symbols
for old_symbol in old.all_compartments:
new_symbol = next(all_cs)
mapping[old_symbol] = new_symbol
reverse_mapping[new_symbol] = (strata_name, old_symbol)
for old_symbol in old.all_requirements:
new_symbol = next(all_rs)
mapping[old_symbol] = new_symbol
reverse_mapping[new_symbol] = (strata_name, old_symbol)
strata_mapping.append(mapping)
# (exogenous states just map to themselves, no strata)
reverse_mapping |= {x: (None, x) for x in exogenous_states}
# The meta_edges function produces edges with invalid names:
# users `edge()` which just parses the symbol string, but this causes
# the strata to be mistaken as a subscript. This function fixes things.
def fix_edge_names(x: TransitionDef) -> TransitionDef:
match x:
case ForkDef():
edges = [fix_edge_names(e) for e in x.edges]
return dataclasses.replace(x, edges=edges)
case EdgeDef():
s_from, c_from = reverse_mapping[x.compartment_from]
s_to, c_to = reverse_mapping[x.compartment_to]
strata = next(s for s in (s_from, s_to) if s is not None)
name = EdgeName(
CompartmentName.parse(str(c_from)),
CompartmentName.parse(str(c_to)),
).with_strata(strata)
return dataclasses.replace(x, name=name)
self._transitions = [
*(
_remap_transition(trx, strata, mapping)
for (strata, ipm), mapping in zip(self._strata, strata_mapping)
for trx in ipm.transitions
),
*(fix_edge_names(x) for x in self._meta_edges(symbols)),
]
self._events = list(_as_events(self._transitions))
self._requirements_dict = OrderedDict(
[
*(
(AbsoluteName(gpm_strata(strata_name), "ipm", r.name), r)
for strata_name, ipm in self._strata
for r in ipm.requirements
),
*(
(AbsoluteName(META_STRATA, "ipm", r.name), r)
for r in self._meta_requirements
),
]
)
@property
@override
def symbols(self) -> MultistrataModelSymbols:
"""The symbols which represent parts of this model."""
return self._symbols
@property
@override
def transitions(self) -> Sequence[TransitionDef]:
"""The transitions in the model."""
return self._transitions
@property
@override
def events(self) -> Sequence[EdgeDef]:
"""Iterate over all events in order."""
# return list(_as_events(self.transitions))
return self._events
@property
@override
def requirements_dict(self) -> OrderedDict[AbsoluteName, AttributeDef]:
"""The attributes required by this model."""
return self._requirements_dict
@property
@override
def strata(self) -> Sequence[str]:
return [name for name, _ in self._strata]
@property
@override
def is_multistrata(self) -> bool:
return True
#####################################################
# Compartment Model quantity select/group/aggregate #
#####################################################
Quantity = CompartmentDef | EdgeDef
class QuantityGroupResult(NamedTuple):
"""The result of a quantity grouping operation."""
groups: tuple[Quantity, ...]
"""The quantities (or psuedo-quantities) representing each group."""
indices: tuple[tuple[int, ...], ...]
"""The IPM quantity indices included in each group."""
_N = TypeVar("_N", bound=CompartmentName | EdgeName)
class QuantityGrouping(NamedTuple):
"""Describes how to group simulation output quantities (events and compartments).
The default combines any quantity whose names match exactly. This is common in
multistrata models where events from several strata impact one transition.
You can also choose to group across strata and subscript differences.
Setting `strata` or `subscript` to True means those elements of quantity names
(if they exist) are ignored for the purposes of matching."""
strata: bool
"""True to combine quantities across strata."""
subscript: bool
"""True to combine quantities across subscript."""
def _strip(self, name: _N) -> _N:
if self.strata:
name = name.with_strata(None)
if self.subscript:
name = name.with_subscript(None)
return name
def map(self, quantities: Sequence[Quantity]) -> QuantityGroupResult:
# first simplify the names to account for `strata` and `subscript`
names = [self._strip(q.name) for q in quantities]
# the groups are now the unique names in the list (maintain ordering)
group_names = filter_unique(names)
# figure out which original quantities belong in each group (by index)
group_indices = tuple(
tuple(j for j, qty in enumerate(names) if group == qty)
for group in group_names
)
# we can create an artificial CompartmentDef or EdgeDef for each group
# if we assume compartments and events will never mix (which they shouldn't)
def _combine(
group_name: CompartmentName | EdgeName,
indices: tuple[int, ...],
) -> Quantity:
qs = [q for i, q in enumerate(quantities) if i in indices]
if isinstance(group_name, CompartmentName) and are_instances(
qs, CompartmentDef
):
return CompartmentDef(group_name, [], None)
elif isinstance(group_name, EdgeName) and are_instances(qs, EdgeDef):
return EdgeDef(
name=group_name,
rate=Add(*[q.rate for q in qs]),
compartment_from=to_symbol(group_name.compartment_from.full),
compartment_to=to_symbol(group_name.compartment_to.full),
)
# If we got here, it probably means compartments and groups wound
# up in the same group somehow. This should not be possible,
# so something went terribly wrong.
raise ValueError("Unable to compute quantity groups.")
groups = tuple(_combine(n, i) for n, i in zip(group_names, group_indices))
return QuantityGroupResult(groups, group_indices)
QuantityAggMethod = Literal["sum"]
@dataclass(frozen=True)
class QuantityStrategy:
"""A strategy for dealing with the quantity axis, e.g., in processing results.
Quantities here are an IPM's compartments and events.
Strategies can include selection of a subset, grouping, and aggregation."""
ipm: BaseCompartmentModel
"""The original IPM quantity information."""
selection: NDArray[np.bool_]
"""A boolean mask for selection of a subset of quantities."""
grouping: QuantityGrouping | None
"""A method for grouping IPM quantities."""
aggregation: QuantityAggMethod | None
"""A method for aggregating the quantity groups."""
@property
def selected(self) -> Sequence[Quantity]:
"""The quantities from the IPM which are selected, prior to any grouping."""
return [q for sel, q in zip(self.selection, self.ipm.quantities) if sel]
@property
@abstractmethod
def quantities(self) -> Sequence[Quantity]:
"""The quantities in the result. If the strategy performs grouping these
may be pseudo-quantities made by combining the quantities in the group."""
@property
@abstractmethod
def labels(self) -> Sequence[str]:
"""Labels for the quantities in the result, after any grouping."""
def disambiguate(self) -> OrderedDict[str, str]:
"""Creates a name mapping to disambiguate IPM quantities that have
the same name. This happens commonly in multistrata IPMs with
meta edges where multiple other strata influence a transmission rate
in a single strata. The returned mapping includes only the selected IPM
compartments and events, but definition order is maintained.
Keys are the unique name and values are the original names
(because the original names might contain duplicates);