viva-basic-processes 0.4.1__tar.gz
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.
- viva_basic_processes-0.4.1/LICENSE +21 -0
- viva_basic_processes-0.4.1/PKG-INFO +91 -0
- viva_basic_processes-0.4.1/README.md +72 -0
- viva_basic_processes-0.4.1/pbg_basic_processes/__init__.py +87 -0
- viva_basic_processes-0.4.1/pyproject.toml +26 -0
- viva_basic_processes-0.4.1/setup.cfg +4 -0
- viva_basic_processes-0.4.1/tests/test_clock.py +61 -0
- viva_basic_processes-0.4.1/tests/test_intervention.py +88 -0
- viva_basic_processes-0.4.1/tests/test_math_expression.py +87 -0
- viva_basic_processes-0.4.1/tests/test_registration.py +16 -0
- viva_basic_processes-0.4.1/tests/test_tick_and_plotting.py +73 -0
- viva_basic_processes-0.4.1/tests/test_time_series.py +82 -0
- viva_basic_processes-0.4.1/viva_basic_processes/__init__.py +48 -0
- viva_basic_processes-0.4.1/viva_basic_processes/clock.py +75 -0
- viva_basic_processes-0.4.1/viva_basic_processes/intervention.py +163 -0
- viva_basic_processes-0.4.1/viva_basic_processes/math_expression.py +201 -0
- viva_basic_processes-0.4.1/viva_basic_processes/plotting.py +127 -0
- viva_basic_processes-0.4.1/viva_basic_processes/tick.py +77 -0
- viva_basic_processes-0.4.1/viva_basic_processes/time_series.py +61 -0
- viva_basic_processes-0.4.1/viva_basic_processes.egg-info/PKG-INFO +91 -0
- viva_basic_processes-0.4.1/viva_basic_processes.egg-info/SOURCES.txt +22 -0
- viva_basic_processes-0.4.1/viva_basic_processes.egg-info/dependency_links.txt +1 -0
- viva_basic_processes-0.4.1/viva_basic_processes.egg-info/requires.txt +11 -0
- viva_basic_processes-0.4.1/viva_basic_processes.egg-info/top_level.txt +2 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 The Vivarium Collective
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: viva-basic-processes
|
|
3
|
+
Version: 0.4.1
|
|
4
|
+
Summary: Reusable, composite-agnostic process-bigraph processes — Clock, Tick, Intervention, MathExpression, and beyond (was pbg-basic-processes)
|
|
5
|
+
License: MIT
|
|
6
|
+
Requires-Python: >=3.11
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Requires-Dist: process-bigraph>=1.4.12
|
|
10
|
+
Requires-Dist: bigraph-schema
|
|
11
|
+
Requires-Dist: sympy
|
|
12
|
+
Requires-Dist: numpy
|
|
13
|
+
Provides-Extra: plotting
|
|
14
|
+
Requires-Dist: matplotlib; extra == "plotting"
|
|
15
|
+
Provides-Extra: dev
|
|
16
|
+
Requires-Dist: pytest; extra == "dev"
|
|
17
|
+
Requires-Dist: matplotlib; extra == "dev"
|
|
18
|
+
Dynamic: license-file
|
|
19
|
+
|
|
20
|
+
# pbg-basic-processes
|
|
21
|
+
|
|
22
|
+
Reusable, composite-agnostic [process-bigraph](https://github.com/vivarium-collective/process-bigraph) processes — the small, generic building blocks that show up in almost every workspace.
|
|
23
|
+
|
|
24
|
+
| Process | What it does |
|
|
25
|
+
|---|---|
|
|
26
|
+
| `Clock` | Emits an integer `tick` step counter each step — the one temporal quantity the engine does *not* expose as a store. (Absolute simulation time, `global_time`, is already maintained by the engine in every composite; wire any input to `['global_time']` to read it.) |
|
|
27
|
+
| `Tick` | Accumulates continuous simulation time into a float `t` store (`t -> t + interval` each update) — a wireable time signal for processes/steps that are functions of `t` (e.g. a time-driven `MathExpressionStep`). |
|
|
28
|
+
| `Intervention` | Applies a specific perturbation to one target store — `set` / `knockout` / `scale` / `add` / `decouple` / `invert`, optionally within a time window. The building block for negative controls and counterfactuals. |
|
|
29
|
+
| `MathExpressionStep` | Evaluates a list of named symbolic (SymPy) expressions as one Step, inferring input ports from free symbols and resolving inter-expression dependencies in topological order. |
|
|
30
|
+
| `TimeSeriesStep` | Holds one or more named series sampled at the same increasing `times` and, given an input `time`, writes each one's value at the latest sample at or before it (a zero-order hold, within a small `tolerance`). Drives a model with a signal computed elsewhere, exactly at its sampled instants. |
|
|
31
|
+
|
|
32
|
+
## Auto-registration
|
|
33
|
+
|
|
34
|
+
Like [`pbg-emitters`](https://github.com/vivarium-collective/pbg-emitters), these processes **auto-register** into any workspace `core`. `allocate_core()` walks every installed distribution that depends on `bigraph-schema` and registers its `Process`/`Step` subclasses — no entry points, no `register_*` boilerplate:
|
|
35
|
+
|
|
36
|
+
```python
|
|
37
|
+
from bigraph_schema import allocate_core
|
|
38
|
+
|
|
39
|
+
core = allocate_core()
|
|
40
|
+
assert 'Clock' in core.link_registry # just by being installed
|
|
41
|
+
assert 'Intervention' in core.link_registry
|
|
42
|
+
assert 'MathExpressionStep' in core.link_registry
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Add `pbg-basic-processes` as a dependency of your workspace (it is a base dependency of `pbg-superpowers`, so every pbg workspace gets it) and reference the processes by `local:Clock`, `local:Tick`, `local:Intervention`, `local:MathExpressionStep`.
|
|
46
|
+
|
|
47
|
+
## Plotting helpers (optional)
|
|
48
|
+
|
|
49
|
+
`viva_basic_processes.plotting` provides two notebook/tutorial convenience helpers — `plot_timeseries` (line plots of emitted records over a time column) and `plot_single_eval` (a bar chart of one step's inputs/outputs). They require matplotlib, which is an **optional** extra so the core processes stay lightweight:
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
pip install "viva-basic-processes[plotting]"
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
```python
|
|
56
|
+
from viva_basic_processes import plot_timeseries, plot_single_eval
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Install
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
pip install pbg-basic-processes # from PyPI (when published)
|
|
63
|
+
pip install -e . # from a checkout
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Usage
|
|
67
|
+
|
|
68
|
+
Node-builder helpers make wiring into a composite easy:
|
|
69
|
+
|
|
70
|
+
```python
|
|
71
|
+
from pbg_basic_processes import clock_node, intervention_node
|
|
72
|
+
|
|
73
|
+
state = {
|
|
74
|
+
'tick': 0,
|
|
75
|
+
'nutrient': 5.0,
|
|
76
|
+
'clock': clock_node(interval=1.0), # writes a step counter to ['tick']
|
|
77
|
+
# hold nutrient at 0 only between t=10 and t=20 of absolute sim time.
|
|
78
|
+
# use_global_time wires the window to the engine's reserved global_time store.
|
|
79
|
+
'starve': intervention_node(['nutrient'], mode='set', value=0.0,
|
|
80
|
+
window=[10, 20], use_global_time=True),
|
|
81
|
+
}
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## Develop
|
|
85
|
+
|
|
86
|
+
```bash
|
|
87
|
+
pip install -e '.[dev]'
|
|
88
|
+
pytest -q
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
MIT-licensed. Part of the [vivarium-collective](https://github.com/vivarium-collective) process-bigraph ecosystem.
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# pbg-basic-processes
|
|
2
|
+
|
|
3
|
+
Reusable, composite-agnostic [process-bigraph](https://github.com/vivarium-collective/process-bigraph) processes — the small, generic building blocks that show up in almost every workspace.
|
|
4
|
+
|
|
5
|
+
| Process | What it does |
|
|
6
|
+
|---|---|
|
|
7
|
+
| `Clock` | Emits an integer `tick` step counter each step — the one temporal quantity the engine does *not* expose as a store. (Absolute simulation time, `global_time`, is already maintained by the engine in every composite; wire any input to `['global_time']` to read it.) |
|
|
8
|
+
| `Tick` | Accumulates continuous simulation time into a float `t` store (`t -> t + interval` each update) — a wireable time signal for processes/steps that are functions of `t` (e.g. a time-driven `MathExpressionStep`). |
|
|
9
|
+
| `Intervention` | Applies a specific perturbation to one target store — `set` / `knockout` / `scale` / `add` / `decouple` / `invert`, optionally within a time window. The building block for negative controls and counterfactuals. |
|
|
10
|
+
| `MathExpressionStep` | Evaluates a list of named symbolic (SymPy) expressions as one Step, inferring input ports from free symbols and resolving inter-expression dependencies in topological order. |
|
|
11
|
+
| `TimeSeriesStep` | Holds one or more named series sampled at the same increasing `times` and, given an input `time`, writes each one's value at the latest sample at or before it (a zero-order hold, within a small `tolerance`). Drives a model with a signal computed elsewhere, exactly at its sampled instants. |
|
|
12
|
+
|
|
13
|
+
## Auto-registration
|
|
14
|
+
|
|
15
|
+
Like [`pbg-emitters`](https://github.com/vivarium-collective/pbg-emitters), these processes **auto-register** into any workspace `core`. `allocate_core()` walks every installed distribution that depends on `bigraph-schema` and registers its `Process`/`Step` subclasses — no entry points, no `register_*` boilerplate:
|
|
16
|
+
|
|
17
|
+
```python
|
|
18
|
+
from bigraph_schema import allocate_core
|
|
19
|
+
|
|
20
|
+
core = allocate_core()
|
|
21
|
+
assert 'Clock' in core.link_registry # just by being installed
|
|
22
|
+
assert 'Intervention' in core.link_registry
|
|
23
|
+
assert 'MathExpressionStep' in core.link_registry
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Add `pbg-basic-processes` as a dependency of your workspace (it is a base dependency of `pbg-superpowers`, so every pbg workspace gets it) and reference the processes by `local:Clock`, `local:Tick`, `local:Intervention`, `local:MathExpressionStep`.
|
|
27
|
+
|
|
28
|
+
## Plotting helpers (optional)
|
|
29
|
+
|
|
30
|
+
`viva_basic_processes.plotting` provides two notebook/tutorial convenience helpers — `plot_timeseries` (line plots of emitted records over a time column) and `plot_single_eval` (a bar chart of one step's inputs/outputs). They require matplotlib, which is an **optional** extra so the core processes stay lightweight:
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
pip install "viva-basic-processes[plotting]"
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
```python
|
|
37
|
+
from viva_basic_processes import plot_timeseries, plot_single_eval
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Install
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
pip install pbg-basic-processes # from PyPI (when published)
|
|
44
|
+
pip install -e . # from a checkout
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Usage
|
|
48
|
+
|
|
49
|
+
Node-builder helpers make wiring into a composite easy:
|
|
50
|
+
|
|
51
|
+
```python
|
|
52
|
+
from pbg_basic_processes import clock_node, intervention_node
|
|
53
|
+
|
|
54
|
+
state = {
|
|
55
|
+
'tick': 0,
|
|
56
|
+
'nutrient': 5.0,
|
|
57
|
+
'clock': clock_node(interval=1.0), # writes a step counter to ['tick']
|
|
58
|
+
# hold nutrient at 0 only between t=10 and t=20 of absolute sim time.
|
|
59
|
+
# use_global_time wires the window to the engine's reserved global_time store.
|
|
60
|
+
'starve': intervention_node(['nutrient'], mode='set', value=0.0,
|
|
61
|
+
window=[10, 20], use_global_time=True),
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Develop
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
pip install -e '.[dev]'
|
|
69
|
+
pytest -q
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
MIT-licensed. Part of the [vivarium-collective](https://github.com/vivarium-collective) process-bigraph ecosystem.
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"""Back-compat shim: ``pbg_basic_processes`` was renamed to ``viva_basic_processes``.
|
|
2
|
+
|
|
3
|
+
The import package was renamed as part of the pbg -> viva rebrand. This shim keeps
|
|
4
|
+
every existing consumer working during the deprecation window (Phase 1 of the
|
|
5
|
+
rename):
|
|
6
|
+
|
|
7
|
+
* ``import pbg_basic_processes`` / ``from pbg_basic_processes import X`` works
|
|
8
|
+
(re-exports the new package's top-level ``__all__``);
|
|
9
|
+
* ``import pbg_basic_processes.<sub>`` transparently resolves to
|
|
10
|
+
``viva_basic_processes.<sub>`` via a meta-path finder; and
|
|
11
|
+
* ``python -m pbg_basic_processes.<sub>`` still executes (``get_code`` forwards
|
|
12
|
+
the real module's code object to ``runpy``).
|
|
13
|
+
|
|
14
|
+
Importing anything under this package emits a one-time
|
|
15
|
+
:class:`DeprecationWarning`. Update imports to ``viva_basic_processes``; this shim
|
|
16
|
+
is removed in a future major release. (A ``pbg-basic-processes`` distribution shim
|
|
17
|
+
also ships under ``shim/`` so existing dependency strings keep resolving.)
|
|
18
|
+
"""
|
|
19
|
+
from __future__ import annotations
|
|
20
|
+
|
|
21
|
+
import importlib
|
|
22
|
+
import importlib.abc
|
|
23
|
+
import importlib.util
|
|
24
|
+
import sys
|
|
25
|
+
import warnings
|
|
26
|
+
|
|
27
|
+
warnings.warn(
|
|
28
|
+
"pbg_basic_processes is renamed to viva_basic_processes; update your imports "
|
|
29
|
+
"(the pbg_basic_processes alias is removed in a future major release).",
|
|
30
|
+
DeprecationWarning,
|
|
31
|
+
stacklevel=2,
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
_OLD = "pbg_basic_processes."
|
|
35
|
+
_NEW = "viva_basic_processes."
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class _Redirect(importlib.abc.MetaPathFinder, importlib.abc.Loader):
|
|
39
|
+
"""Forward ``pbg_basic_processes.<sub>`` imports to ``viva_basic_processes.<sub>``.
|
|
40
|
+
|
|
41
|
+
``create_module``/``exec_module`` handle ordinary ``import`` (the imported
|
|
42
|
+
submodule object is aliased into ``sys.modules`` under both names), while
|
|
43
|
+
``get_code`` lets ``python -m pbg_basic_processes.<sub>`` execute the real
|
|
44
|
+
module's code object as ``__main__``.
|
|
45
|
+
"""
|
|
46
|
+
|
|
47
|
+
def _target(self, name: str) -> str:
|
|
48
|
+
return _NEW + name[len(_OLD):]
|
|
49
|
+
|
|
50
|
+
def find_spec(self, name, path=None, target=None):
|
|
51
|
+
if not name.startswith(_OLD):
|
|
52
|
+
return None
|
|
53
|
+
real = importlib.util.find_spec(self._target(name))
|
|
54
|
+
if real is None:
|
|
55
|
+
return None
|
|
56
|
+
spec = importlib.util.spec_from_loader(
|
|
57
|
+
name,
|
|
58
|
+
self,
|
|
59
|
+
origin=real.origin,
|
|
60
|
+
is_package=real.submodule_search_locations is not None,
|
|
61
|
+
)
|
|
62
|
+
if real.submodule_search_locations is not None:
|
|
63
|
+
spec.submodule_search_locations = list(real.submodule_search_locations)
|
|
64
|
+
return spec
|
|
65
|
+
|
|
66
|
+
def create_module(self, spec):
|
|
67
|
+
# Alias the fully-initialized new-package module under BOTH names so
|
|
68
|
+
# `import a.b` and identity checks against either name agree.
|
|
69
|
+
mod = importlib.import_module(self._target(spec.name))
|
|
70
|
+
sys.modules[spec.name] = mod
|
|
71
|
+
return mod
|
|
72
|
+
|
|
73
|
+
def exec_module(self, module): # already executed by import_module
|
|
74
|
+
pass
|
|
75
|
+
|
|
76
|
+
def get_code(self, name):
|
|
77
|
+
# Support `python -m pbg_basic_processes.<sub>`: runpy needs a code object.
|
|
78
|
+
target = self._target(name)
|
|
79
|
+
return importlib.util.find_spec(target).loader.get_code(target)
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
sys.meta_path.insert(0, _Redirect())
|
|
83
|
+
|
|
84
|
+
_viva = importlib.import_module("viva_basic_processes")
|
|
85
|
+
__version__ = getattr(_viva, "__version__", "0.1.0")
|
|
86
|
+
# Re-export the new package's public surface.
|
|
87
|
+
globals().update({k: getattr(_viva, k) for k in getattr(_viva, "__all__", [])})
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68.0", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "viva-basic-processes"
|
|
7
|
+
version = "0.4.1"
|
|
8
|
+
description = "Reusable, composite-agnostic process-bigraph processes — Clock, Tick, Intervention, MathExpression, and beyond (was pbg-basic-processes)"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = { text = "MIT" }
|
|
11
|
+
requires-python = ">=3.11"
|
|
12
|
+
dependencies = [
|
|
13
|
+
"process-bigraph>=1.4.12",
|
|
14
|
+
"bigraph-schema",
|
|
15
|
+
"sympy",
|
|
16
|
+
"numpy",
|
|
17
|
+
]
|
|
18
|
+
|
|
19
|
+
[project.optional-dependencies]
|
|
20
|
+
plotting = ["matplotlib"]
|
|
21
|
+
dev = ["pytest", "matplotlib"]
|
|
22
|
+
|
|
23
|
+
# Ship the real package (viva_basic_processes) plus the pbg_basic_processes
|
|
24
|
+
# import shim (a meta-path redirect to viva_basic_processes) for back-compat.
|
|
25
|
+
[tool.setuptools.packages.find]
|
|
26
|
+
include = ["viva_basic_processes*", "pbg_basic_processes*"]
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"""Tests for the Clock step-counter process."""
|
|
2
|
+
import pytest
|
|
3
|
+
|
|
4
|
+
from bigraph_schema import allocate_core
|
|
5
|
+
from process_bigraph import Composite
|
|
6
|
+
|
|
7
|
+
from pbg_basic_processes.clock import Clock, register_clock, clock_node
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def _run(node, run_for):
|
|
11
|
+
core = allocate_core()
|
|
12
|
+
register_clock(core)
|
|
13
|
+
comp = Composite({'state': {'tick': 0, 'clk': node}}, core=core)
|
|
14
|
+
comp.run(run_for)
|
|
15
|
+
return comp.state
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def test_register_is_idempotent_and_safe():
|
|
19
|
+
core = allocate_core()
|
|
20
|
+
register_clock(core)
|
|
21
|
+
assert register_clock(core) is False
|
|
22
|
+
assert 'Clock' in (getattr(core, 'link_registry', {}) or {})
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def test_tick_counts_updates_at_unit_interval():
|
|
26
|
+
# run for 5 time units at interval 1.0 -> 5 updates
|
|
27
|
+
end = _run(clock_node(interval=1.0), run_for=5)
|
|
28
|
+
assert end['tick'] == 5
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def test_tick_counts_finer_steps():
|
|
32
|
+
# run for 4 time units at interval 0.25 -> 16 updates
|
|
33
|
+
end = _run(clock_node(interval=0.25), run_for=4)
|
|
34
|
+
assert end['tick'] == 16
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def test_start_tick_offset_applied():
|
|
38
|
+
end = _run(clock_node(start_tick=100, interval=1.0), run_for=3)
|
|
39
|
+
assert end['tick'] == 103
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def test_does_not_touch_reserved_global_time():
|
|
43
|
+
# Clock must NOT corrupt the engine's master clock: global_time still equals
|
|
44
|
+
# the run duration even with the clock present.
|
|
45
|
+
end = _run(clock_node(interval=1.0), run_for=4)
|
|
46
|
+
assert end['global_time'] == pytest.approx(4.0)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def test_clock_node_shape():
|
|
50
|
+
n = clock_node(tick_path=['n'])
|
|
51
|
+
assert n['_type'] == 'process'
|
|
52
|
+
assert n['address'] == 'local:Clock'
|
|
53
|
+
assert n['inputs'] == {}
|
|
54
|
+
assert n['outputs']['tick'] == ['n']
|
|
55
|
+
assert 'global_time' not in n['outputs']
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def test_clock_takes_no_inputs():
|
|
59
|
+
clk = Clock({}, allocate_core())
|
|
60
|
+
assert clk.inputs() == {}
|
|
61
|
+
assert set(clk.outputs().keys()) == {'tick'}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"""Tests for the reusable Intervention process."""
|
|
2
|
+
import pytest
|
|
3
|
+
|
|
4
|
+
from bigraph_schema import allocate_core
|
|
5
|
+
from process_bigraph import Composite
|
|
6
|
+
|
|
7
|
+
from pbg_basic_processes.intervention import (
|
|
8
|
+
Intervention, register_intervention, intervention_node,
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def _run(node, x0=10.0, steps=3, extra_state=None):
|
|
13
|
+
core = allocate_core()
|
|
14
|
+
# Safe to call even if allocate_core already auto-registered Intervention
|
|
15
|
+
# (installed Process subclasses are auto-discovered, like emitters).
|
|
16
|
+
register_intervention(core)
|
|
17
|
+
state = {'x': float(x0), 'iv': node}
|
|
18
|
+
if extra_state:
|
|
19
|
+
state.update(extra_state)
|
|
20
|
+
comp = Composite({'state': state}, core=core)
|
|
21
|
+
comp.run(steps)
|
|
22
|
+
return comp.state['x']
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def test_register_is_idempotent_and_safe():
|
|
26
|
+
core = allocate_core()
|
|
27
|
+
register_intervention(core)
|
|
28
|
+
assert register_intervention(core) is False
|
|
29
|
+
assert 'Intervention' in (getattr(core, 'link_registry', {}) or {})
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def test_set_clamps_to_value():
|
|
33
|
+
assert _run(intervention_node(['x'], mode='set', value=3.0)) == pytest.approx(3.0)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def test_knockout_zeroes_target():
|
|
37
|
+
assert _run(intervention_node(['x'], mode='knockout')) == pytest.approx(0.0)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def test_scale_multiplies():
|
|
41
|
+
assert _run(intervention_node(['x'], mode='scale', value=0.5), x0=8.0, steps=1) == pytest.approx(4.0)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def test_add_bolus_per_step():
|
|
45
|
+
assert _run(intervention_node(['x'], mode='add', value=2.0), x0=0.0, steps=3) == pytest.approx(6.0)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def test_window_gates_the_intervention():
|
|
49
|
+
assert _run(intervention_node(['x'], mode='set', value=5.0, window=[1, 2]),
|
|
50
|
+
x0=0.0, steps=4) == pytest.approx(5.0)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def test_window_inactive_leaves_target_untouched():
|
|
54
|
+
assert _run(intervention_node(['x'], mode='set', value=99.0, window=[5, 6]),
|
|
55
|
+
x0=7.0, steps=3) == pytest.approx(7.0)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def test_intervention_node_shape():
|
|
59
|
+
n = intervention_node(['nutrient'], mode='set', value=0.0)
|
|
60
|
+
assert n['_type'] == 'process'
|
|
61
|
+
assert n['address'] == 'local:Intervention'
|
|
62
|
+
assert n['inputs']['target'] == ['nutrient']
|
|
63
|
+
assert n['outputs']['target'] == ['nutrient']
|
|
64
|
+
assert n['config']['mode'] == 'set'
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def test_config_schema_present():
|
|
68
|
+
assert 'mode' in Intervention.config_schema
|
|
69
|
+
assert 'window' in Intervention.config_schema
|
|
70
|
+
assert 'use_global_time' in Intervention.config_schema
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def test_decouple_freezes_at_first_active_value():
|
|
74
|
+
assert _run(intervention_node(['x'], mode='decouple'), x0=12.0, steps=3) == pytest.approx(12.0)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def test_remove_is_alias_for_decouple():
|
|
78
|
+
assert _run(intervention_node(['x'], mode='remove'), x0=4.0, steps=2) == pytest.approx(4.0)
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def test_invert_drives_to_negative_current():
|
|
82
|
+
assert _run(intervention_node(['x'], mode='invert'), x0=5.0, steps=1) == pytest.approx(-5.0)
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def test_global_time_node_wires_the_port():
|
|
86
|
+
n = intervention_node(['x'], mode='set', value=1.0, window=[0, 1], use_global_time=True)
|
|
87
|
+
assert n['config']['use_global_time'] is True
|
|
88
|
+
assert n['inputs']['global_time'] == ['global_time']
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"""Tests for the MathExpressionStep."""
|
|
2
|
+
import math
|
|
3
|
+
|
|
4
|
+
import pytest
|
|
5
|
+
|
|
6
|
+
from bigraph_schema import allocate_core
|
|
7
|
+
from process_bigraph import Composite
|
|
8
|
+
|
|
9
|
+
from pbg_basic_processes.math_expression import MathExpressionStep
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def _step_node(expressions, params=None, inputs=None, outputs=None):
|
|
13
|
+
node = {
|
|
14
|
+
'_type': 'step',
|
|
15
|
+
'address': 'local:MathExpressionStep',
|
|
16
|
+
'config': {'expressions': expressions, 'params': params or {}, 'debug': False},
|
|
17
|
+
'inputs': inputs or {},
|
|
18
|
+
'outputs': outputs or {},
|
|
19
|
+
}
|
|
20
|
+
return node
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def _run(state, node, steps=1):
|
|
24
|
+
core = allocate_core()
|
|
25
|
+
state = dict(state)
|
|
26
|
+
state['math'] = node
|
|
27
|
+
comp = Composite({'state': state}, core=core)
|
|
28
|
+
comp.run(steps)
|
|
29
|
+
return comp.state
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def test_simple_expression():
|
|
33
|
+
node = _step_node(
|
|
34
|
+
[{'out': 'z', 'expr': 'a + b'}],
|
|
35
|
+
inputs={'a': ['a'], 'b': ['b']},
|
|
36
|
+
outputs={'z': ['z']},
|
|
37
|
+
)
|
|
38
|
+
end = _run({'a': 2.0, 'b': 3.0, 'z': 0.0}, node)
|
|
39
|
+
assert end['z'] == pytest.approx(5.0)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def test_dependency_order_resolved():
|
|
43
|
+
# w depends on z which depends on a,b — listed out of order on purpose.
|
|
44
|
+
node = _step_node(
|
|
45
|
+
[{'out': 'w', 'expr': 'z * 2'}, {'out': 'z', 'expr': 'a + b'}],
|
|
46
|
+
inputs={'a': ['a'], 'b': ['b']},
|
|
47
|
+
outputs={'z': ['z'], 'w': ['w']},
|
|
48
|
+
)
|
|
49
|
+
end = _run({'a': 1.0, 'b': 4.0, 'z': 0.0, 'w': 0.0}, node)
|
|
50
|
+
assert end['z'] == pytest.approx(5.0)
|
|
51
|
+
assert end['w'] == pytest.approx(10.0)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def test_params_are_constants_not_ports():
|
|
55
|
+
step = MathExpressionStep(
|
|
56
|
+
{'expressions': [{'out': 'y', 'expr': 'k * x'}], 'params': {'k': 3.0}, 'debug': False},
|
|
57
|
+
allocate_core(),
|
|
58
|
+
)
|
|
59
|
+
step.initialize()
|
|
60
|
+
assert 'x' in step.inputs()
|
|
61
|
+
assert 'k' not in step.inputs() # param, not a port
|
|
62
|
+
assert 'y' in step.outputs()
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def test_cycle_raises():
|
|
66
|
+
# initialize() runs during construction, so the ValueError surfaces there.
|
|
67
|
+
with pytest.raises(ValueError, match='Cyclic'):
|
|
68
|
+
MathExpressionStep(
|
|
69
|
+
{'expressions': [{'out': 'a', 'expr': 'b'}, {'out': 'b', 'expr': 'a'}], 'debug': False},
|
|
70
|
+
allocate_core(),
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def test_empty_expressions_raises():
|
|
75
|
+
with pytest.raises(ValueError, match='non-empty'):
|
|
76
|
+
MathExpressionStep({'expressions': [], 'debug': False}, allocate_core())
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def test_nonlinear_with_param():
|
|
80
|
+
node = _step_node(
|
|
81
|
+
[{'out': 'y', 'expr': 'sin(x) + k'}],
|
|
82
|
+
params={'k': 1.0},
|
|
83
|
+
inputs={'x': ['x']},
|
|
84
|
+
outputs={'y': ['y']},
|
|
85
|
+
)
|
|
86
|
+
end = _run({'x': float(math.pi / 2), 'y': 0.0}, node)
|
|
87
|
+
assert end['y'] == pytest.approx(2.0)
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"""The whole point of this package: its processes auto-register into any core.
|
|
2
|
+
|
|
3
|
+
``allocate_core()`` walks every installed ``bigraph-schema`` dependent and
|
|
4
|
+
registers its ``Process`` / ``Step`` subclasses — so simply having
|
|
5
|
+
``pbg-basic-processes`` installed must surface Clock / Intervention /
|
|
6
|
+
MathExpressionStep in the link registry with no explicit ``register_*`` call,
|
|
7
|
+
exactly like ``pbg-emitters``.
|
|
8
|
+
"""
|
|
9
|
+
from bigraph_schema import allocate_core
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def test_processes_auto_register_via_discovery():
|
|
13
|
+
core = allocate_core()
|
|
14
|
+
link_reg = getattr(core, 'link_registry', {}) or {}
|
|
15
|
+
for name in ('Clock', 'Intervention', 'MathExpressionStep'):
|
|
16
|
+
assert name in link_reg, f"{name} was not auto-discovered into the core registry"
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"""Tests for the Tick process and the plotting helpers.
|
|
2
|
+
|
|
3
|
+
These are the pieces process-bigraph Tutorial 3 ("Declarative Math with
|
|
4
|
+
MathExpressionStep") depends on: a continuous-time ``Tick`` wired into a
|
|
5
|
+
``MathExpressionStep``, plus ``plot_timeseries`` / ``plot_single_eval``.
|
|
6
|
+
"""
|
|
7
|
+
import pytest
|
|
8
|
+
|
|
9
|
+
from bigraph_schema import allocate_core
|
|
10
|
+
from process_bigraph import Composite
|
|
11
|
+
|
|
12
|
+
from viva_basic_processes import (
|
|
13
|
+
Tick,
|
|
14
|
+
MathExpressionStep,
|
|
15
|
+
plot_timeseries,
|
|
16
|
+
plot_single_eval,
|
|
17
|
+
tick_node,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def test_tick_auto_registers_via_discovery():
|
|
22
|
+
core = allocate_core()
|
|
23
|
+
link_reg = getattr(core, "link_registry", {}) or {}
|
|
24
|
+
assert "Tick" in link_reg, "Tick was not auto-discovered into the core registry"
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def test_tick_accumulates_time():
|
|
28
|
+
"""A Tick wired to a MathExpressionStep: t advances, z = a*b tracks state."""
|
|
29
|
+
core = allocate_core()
|
|
30
|
+
dt = 0.5
|
|
31
|
+
state = {
|
|
32
|
+
"t": 0.0,
|
|
33
|
+
"a": 2.0,
|
|
34
|
+
"b": 3.0,
|
|
35
|
+
"z": 0.0,
|
|
36
|
+
"tick": tick_node(interval=dt, t_path=("t",)),
|
|
37
|
+
"math": {
|
|
38
|
+
"_type": "step",
|
|
39
|
+
"address": "local:MathExpressionStep",
|
|
40
|
+
"config": {
|
|
41
|
+
"expressions": [{"out": "z", "expr": "a*b + t"}],
|
|
42
|
+
"debug": False,
|
|
43
|
+
},
|
|
44
|
+
"inputs": {"a": ["a"], "b": ["b"], "t": ["t"]},
|
|
45
|
+
"outputs": {"z": ["z"]},
|
|
46
|
+
},
|
|
47
|
+
}
|
|
48
|
+
comp = Composite({"state": state}, core=core)
|
|
49
|
+
comp.run(4 * dt) # four ticks
|
|
50
|
+
|
|
51
|
+
end = comp.state
|
|
52
|
+
assert end["t"] == pytest.approx(4 * dt)
|
|
53
|
+
# z = a*b + t = 6 + 2.0
|
|
54
|
+
assert end["z"] == pytest.approx(2.0 * 3.0 + 4 * dt)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def test_plot_timeseries_returns_figure():
|
|
58
|
+
matplotlib = pytest.importorskip("matplotlib")
|
|
59
|
+
matplotlib.use("Agg") # headless
|
|
60
|
+
records = [{"t": 0.0, "z": 1.0}, {"t": 1.0, "z": 2.0}, {"t": 2.0, "z": 3.0}]
|
|
61
|
+
fig = plot_timeseries(records, ["z"], title="z(t)", legend=False)
|
|
62
|
+
assert fig is not None
|
|
63
|
+
matplotlib.pyplot.close("all")
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def test_plot_single_eval_returns_figure():
|
|
67
|
+
matplotlib = pytest.importorskip("matplotlib")
|
|
68
|
+
matplotlib.use("Agg") # headless
|
|
69
|
+
state = {"a": 2.0, "b": 3.0, "c": 0.5, "z": 6.5}
|
|
70
|
+
fig = plot_single_eval(state, inputs=("a", "b", "c"), outputs=("z",),
|
|
71
|
+
expected={"z": 6.5}, title="single eval")
|
|
72
|
+
assert fig is not None
|
|
73
|
+
matplotlib.pyplot.close("all")
|