Specs¶
specs
¶
op_system.specs.
Public façade for RHS specification models and normalization.
The implementation lives in op_system._normalize. This module re-exports
the public types and functions under the stable op_system.specs namespace
for backward compatibility.
Supported RHS kinds¶
1) kind: "expr" - explicit d(state)/dt equations per state variable. 2) kind: "transitions" - diagram-style per-capita hazard transitions.
See op_system._normalize for the full implementation.
AxisDeclaration
¶
Bases: NamedTuple
Physical interpretation of a named axis.
Attributes:
| Name | Type | Description |
|---|---|---|
units |
str | None
|
Coordinate units, independent of measure units. |
measure |
AxisMeasure | None
|
Explicit measure, or None when undeclared. |
AxisMeasure
¶
Bases: NamedTuple
Explicit physical measure in the axis coordinate order.
Attributes:
| Name | Type | Description |
|---|---|---|
kind |
_MeasureKind
|
Counting, cell widths/volumes, or point quadrature weights. |
weights |
tuple[float, ...]
|
Finite strictly positive weights, one per coordinate. |
units |
str | None
|
Optional measure units, without dimensional verification. |
from_mapping(value, *, size)
classmethod
¶
Validate a measure without inferring geometry from coordinates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
object
|
Mapping with kind, optional units, and weights for cell/quadrature. |
required |
size
|
int
|
Number of axis coordinates. |
required |
Returns:
| Type | Description |
|---|---|
AxisMeasure
|
Immutable measure in coordinate order. |
Raises:
| Type | Description |
|---|---|
InvalidRhsSpecError
|
If the kind, units, weights or length are invalid. |
Source code in src/op_system/_scientific_meta.py
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 | |
ExprRhs(state_names, equations, aliases, param_names, all_symbols, meta, state_templates=(), shaped_params=(), time_varying_params=(), aliases_ir=dict(), equations_ir=(), aliases_ir_reduce=dict(), equations_ir_reduce=(), alias_templates=())
dataclass
¶
Bases: _RhsBase
Normalized RHS for kind="expr" specs (explicit d(state)/dt equations).
Produced by :func:normalize_expr_rhs. Use :data:NormalizedRhs as the
union type when you need to accept both kinds.
ExpressionString(source)
dataclass
¶
Validated expression wrapper with cached AST and symbol names.
as_ir()
¶
Return the typed IR representation for this expression.
Returns:
| Type | Description |
|---|---|
Expr
|
Parsed typed IR tree. |
Source code in src/op_system/_symbols.py
43 44 45 46 47 48 49 | |
as_lowered_ir()
¶
Return helper-lowered typed IR for this expression.
Returns:
| Type | Description |
|---|---|
Expr
|
Typed IR tree with helper calls lowered to |
Source code in src/op_system/_symbols.py
51 52 53 54 55 56 57 | |
ObservableDefinition
¶
Bases: NamedTuple
Backend-neutral definition of one instantaneous latent observable.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Unique output name within a compiled model. |
expression |
str
|
Restricted pointwise state/parameter/time expression. |
axes |
tuple[str, ...]
|
Output axes, after any explicit reduction. |
reduction |
ObservableReduction | None
|
Optional unweighted sum or physical integral. |
units |
str | None
|
Optional declared output units, without dimensional verification. |
description |
str | None
|
Optional human-readable interpretation. |
from_mapping(name, value)
classmethod
¶
Validate the bounded observable schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Output identifier. |
required |
value
|
object
|
Mapping with expr, optional axes/reduction/units/description. |
required |
Returns:
| Type | Description |
|---|---|
ObservableDefinition
|
Detached immutable definition. |
Raises:
| Type | Description |
|---|---|
InvalidRhsSpecError
|
If identifiers, fields or reductions are invalid. |
Source code in src/op_system/_observables.py
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 | |
ObservableReduction
¶
Bases: NamedTuple
Explicit reduction independent of legacy equation quadrature.
Attributes:
| Name | Type | Description |
|---|---|---|
kind |
Literal['sum', 'integral']
|
Unweighted sum or integral with declared physical measures. |
axes |
tuple[str, ...]
|
Named axes to remove, in the declared reduction order. |
ParameterDeclaration
¶
Bases: NamedTuple
Optional scientific metadata for an inferred runtime parameter.
Attributes:
| Name | Type | Description |
|---|---|---|
support |
ParameterSupport | None
|
Declared domain, or None when it was not specified. |
units |
str | None
|
Unit annotation, without dimensional-analysis verification. |
description |
str | None
|
Optional human-readable parameter description. |
fixed |
bool
|
Whether inference should treat the parameter as fixed. This does not bind a value or remove a runtime parameter. |
from_mapping(value)
classmethod
¶
Validate and copy one parameter declaration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
object
|
Mapping with optional support, units, description and fixed. |
required |
Returns:
| Type | Description |
|---|---|
ParameterDeclaration
|
Immutable parameter declaration. |
Raises:
| Type | Description |
|---|---|
InvalidRhsSpecError
|
If an annotation is malformed or unknown. |
Source code in src/op_system/_parameter_meta.py
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 | |
ParameterSupport
¶
Bases: NamedTuple
Declared domain; bounds are inclusive and positivity is strict.
Attributes:
| Name | Type | Description |
|---|---|---|
kind |
_SupportKind
|
Domain identifier; unit_interval includes zero and one. |
lower |
float | None
|
Finite lower bound for bounded support, otherwise None. |
upper |
float | None
|
Finite upper bound for bounded support, otherwise None. |
from_mapping(value)
classmethod
¶
Parse a domain name or bounded-domain mapping.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
object
|
Domain string or mapping with kind, lower and upper. |
required |
Returns:
| Type | Description |
|---|---|
ParameterSupport
|
Immutable support declaration. |
Raises:
| Type | Description |
|---|---|
InvalidRhsSpecError
|
If the domain or bounds are invalid. |
Source code in src/op_system/_parameter_meta.py
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 | |
PinnedToken(axis, coord)
dataclass
¶
Axis token pinned to a specific coordinate.
StateDeclaration
¶
Bases: NamedTuple
Optional representation and physical reduction requirements of a state.
Attributes:
| Name | Type | Description |
|---|---|---|
representation |
_Representation | None
|
Extensive counts/mass/counter or intensive density/ concentration; None when unknown. Cumulative means an extensive accumulated counter, without asserting monotonicity of its equations. |
units |
str | None
|
State value units, without dimensional verification. |
description |
str | None
|
Human-readable annotation. |
measure_axes |
tuple[str, ...] | None
|
Axes whose measures are required for an intensive integral. None means undeclared; an empty tuple explicitly needs none. |
scale |
Literal['identity', 'log']
|
Requested identity/log coordinate for an explicit coordinate_view. Primary compiled equations and reactions retain natural coordinates. |
from_mapping(value)
classmethod
¶
Validate a state annotation without changing its equations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
object
|
Mapping of representation, units, description and measure_axes. |
required |
Returns:
| Type | Description |
|---|---|
StateDeclaration
|
Immutable declaration; omitted information remains unknown. |
Raises:
| Type | Description |
|---|---|
InvalidRhsSpecError
|
If fields or their values are invalid. |
Source code in src/op_system/_scientific_meta.py
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 | |
StateTemplate(base, axes, shape, expanded_names, coord_assignments, offset)
dataclass
¶
Structural record for a state template prior to scalar expansion.
A spec like state: ["S[age, vax]"] over axes age (size 4) and
vax (size 2) produces a single StateTemplate with base="S",
axes=("age", "vax"), shape=(4, 2), and expanded_names listing the
eight scalar state names in cartesian-product order (age outer,
vax inner — matching itertools.product and the order used to expand
the flat state vector).
Non-templated entries (e.g. a bare "D" in state) are also reported
as StateTemplate records with axes=(), shape=(), and a single
expanded_names = ("D",) so consumers can iterate templates uniformly.
Attributes:
| Name | Type | Description |
|---|---|---|
base |
str
|
Compartment name without selector brackets (e.g. |
axes |
tuple[str, ...]
|
Wildcard axes in declaration order. Empty for scalar templates. |
shape |
tuple[int, ...]
|
Per-axis sizes in |
expanded_names |
tuple[str, ...]
|
Flat scalar state names, in cartesian-product order
over |
coord_assignments |
tuple[Mapping[str, str], ...]
|
For each entry in |
offset |
int
|
Index of |
TransitionsRhs(state_names, equations, aliases, param_names, all_symbols, meta, state_templates=(), shaped_params=(), time_varying_params=(), aliases_ir=dict(), equations_ir=(), aliases_ir_reduce=dict(), equations_ir_reduce=(), alias_templates=(), reactions_ir=(), reaction_gaps_ir=())
dataclass
¶
Bases: _RhsBase
Normalized RHS for kind="transitions" specs (per-capita hazard diagram).
Produced by :func:normalize_transitions_rhs. Use :data:NormalizedRhs
as the union type when you need to accept both kinds.
WildcardToken(axis)
dataclass
¶
Axis token that expands over all coordinates of that axis.
normalize_expr_rhs(spec)
¶
Normalize an expression-based RHS specification.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
Mapping[str, Any]
|
Raw RHS specification mapping. |
required |
Returns:
| Type | Description |
|---|---|
ExprRhs
|
Backend-facing normalized RHS representation. |
Raises:
| Type | Description |
|---|---|
InvalidRhsSpecError
|
If validation fails. |
Source code in src/op_system/_normalize.py
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 | |
normalize_rhs(spec)
¶
Normalize a RHS specification dict into a backend-facing representation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
Mapping[str, Any] | None
|
Raw RHS specification mapping. |
required |
Returns:
| Type | Description |
|---|---|
NormalizedRhs
|
Backend-facing normalized RHS representation. |
Raises:
| Type | Description |
|---|---|
InvalidRhsSpecError
|
If validation fails. |
UnsupportedFeatureError
|
If validation fails. |
Source code in src/op_system/_normalize.py
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 | |
normalize_transitions_rhs(spec)
¶
Normalize a transition-based RHS specification (diagram/hazard semantics).
Returns:
| Type | Description |
|---|---|
TransitionsRhs
|
Backend-facing normalized RHS representation for the transitions kind. |
Raises:
| Type | Description |
|---|---|
InvalidRhsSpecError
|
If validation fails. |
Source code in src/op_system/_normalize.py
1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 | |