AxisPattern API¶
Rates that change along the axis, and schedules per subgroup. Guide: Axis Patterns.
AxisPattern ¶
Bases: BaseModel
An expression that changes along the axis, usable as (or within) a transition rate.
Instances are created exclusively via the factory classmethods (pulse,
periodic, window, etc.). Each instance may carry stratification
conditions that restrict it to a specific sub-group via for_group.
Multi-group schedules are built by calling AxisPattern.add_group(...)
directly on the class. The first call returns a new schedule; chain
further add_group and set_default calls on the returned instance.
A AxisPattern is accepted directly by
:meth:commol.api.model_builder.ModelBuilder.add_transition via the
rate argument; no auxiliary conversion is required.
Attributes:
| Name | Type | Description |
|---|---|---|
conditions |
list[ConditionDict] | None
|
The stratification conditions of the sub-group the pattern applies
to, set by |
source_compartment |
str | None
|
A compartment the rate is multiplied by, which makes the flow
absolute, set by |
Examples:
>>> from commol import AxisPattern
>>> # Single pattern
>>> rate = AxisPattern.pulse(at=10, value=0.05)
>>> str(rate)
'(if(step == 10, 0.05, 0))'
>>> # Multi-group schedule
>>> rate = AxisPattern.add_group(
... conditions=[{"stratification": "group", "category": "cat1"}],
... schedule=AxisPattern.pulse(at=10, value=0.05),
... ).add_group(
... conditions=[{"stratification": "group", "category": "cat2"}],
... schedule=AxisPattern.pulse(at=10, value=0.02),
... )
Source code in commol/api/patterns.py
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 | |
Attributes¶
formula
abstractmethod
property
¶
The mathematical expression for this pattern (always parenthesised).
Methods:¶
__str__ ¶
for_group ¶
Return a copy of this pattern restricted to a stratification sub-group.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
conditions
|
list[ConditionDict]
|
The stratification conditions of the sub-group, as in a stratified rate. |
required |
source_compartment
|
str | None
|
A compartment to multiply the rate by, which makes the flow absolute. If None, the flow is proportional to its source. |
None
|
Returns:
| Type | Description |
|---|---|
AxisPattern
|
The copy, with |
Source code in commol/api/patterns.py
to_stratified_rate ¶
Convert this pattern to a stratified-rate dict.
If source_compartment is set, the rate is multiplied by that
compartment variable (absolute flow). Otherwise the rate is the
formula alone (proportional flow — the engine multiplies by the source
compartment automatically).
Source code in commol/api/patterns.py
set_default ¶
set_default(pattern: AxisPattern) -> AxisPattern
Register the condition-free fallback pattern (schedules only).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
AxisPattern
|
The pattern of every compartment that no group matches, with no
conditions and no |
required |
Returns:
| Type | Description |
|---|---|
AxisPattern
|
The schedule. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If this pattern is not a schedule, one that
|
ValueError
|
If the schedule has a default already, or |
Source code in commol/api/patterns.py
pulse
classmethod
¶
pulse(at: int, value: float | str) -> AxisPattern
Single-step pulse: rate = value at step at, else 0.
pulses
classmethod
¶
pulses(at: Iterable[int], value: float | str | Iterable[float | str]) -> AxisPattern
Multi-step discrete pulses. Steps must be unique and non-negative.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
at
|
Iterable[int]
|
Step indices at which the pulse fires. Must be unique and non-negative. |
required |
value
|
float | str | Iterable[float | str]
|
Pulse magnitude. Either a single value applied to every step, or an
iterable of values with the same length as |
required |
Source code in commol/api/patterns.py
periodic
classmethod
¶
periodic(period: int, value: float | str, offset: int = 0) -> AxisPattern
Periodic pulse every period steps starting at offset.
Source code in commol/api/patterns.py
interval
classmethod
¶
interval(start: float, end: float, value: float | str) -> AxisPattern
Constant rate value while t is in [start, end), else 0.
In a DifferenceEquations model t is the step, so the rate acts on
the steps start <= step < end. In a DifferentialEquations model the
interval opens and closes at any position, and the engine restarts its
integration at both ends.
Source code in commol/api/patterns.py
sinusoid
classmethod
¶
sinusoid(amplitude: float | str, period: float | str, phase: float | str = 0, baseline: float | str = 0) -> AxisPattern
baseline + amplitude * sin(2*pi*(t - phase) / period).
Source code in commol/api/patterns.py
gaussian_pulse
classmethod
¶
gaussian_pulse(center: float, width: float, peak: float | str) -> AxisPattern
Gaussian bell pulse: peak * exp(-(t - center)^2 / (2 * width^2)).
Source code in commol/api/patterns.py
linear_ramp
classmethod
¶
linear_ramp(start: float, end: float, start_value: float, end_value: float) -> AxisPattern
Linear in t from start_value to end_value on [start, end).
The rate is 0 outside the interval. In a DifferenceEquations model
t is the step, so the ramp is read at each step; in a
DifferentialEquations model it is continuous.
Source code in commol/api/patterns.py
combine
classmethod
¶
combine(*patterns: AxisPattern, mode: CombineMode = SUM, config: SecurityConfig | None = None) -> AxisPattern
Combine patterns into a single composed pattern.
combine(p)is the identity (returnspunchanged).- All input patterns must share the same
conditionsandsource_compartment(or have none); the result preserves them. modeis aCombineModeor its value ("sum","max","min").- Raises
ValueErrorif no patterns are supplied,modeis not a mode, the inputs have inconsistent group bindings, or the combined formula exceeds the security length cap.
Source code in commol/api/patterns.py
from_formula
classmethod
¶
from_formula(expr: str, config: SecurityConfig | None = None) -> AxisPattern
Create a pattern from an arbitrary expression string.
The string is wrapped in (...) so it composes safely, then run
through the full expression security validator.