process-bigraph 1.10.1__tar.gz → 1.11.0__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.
- {process_bigraph-1.10.1/process_bigraph.egg-info → process_bigraph-1.11.0}/PKG-INFO +1 -1
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/composite.py +199 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/nextflow.py +31 -5
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/scheduling.py +118 -2
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/visualizations/time_series.py +7 -2
- {process_bigraph-1.10.1 → process_bigraph-1.11.0/process_bigraph.egg-info}/PKG-INFO +1 -1
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph.egg-info/SOURCES.txt +3 -1
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/pyproject.toml +1 -1
- process_bigraph-1.11.0/tests/test_heap_scheduler.py +123 -0
- process_bigraph-1.11.0/tests/test_wire_step_layers_root_trigger.py +116 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/AUTHORS.md +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/LICENSE +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/README.md +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/__init__.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/artifacts.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/audit_contracts.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/bundle.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/composite_audit.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/composite_discovery.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/composite_generator.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/composite_spec.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/config_helpers.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/contract_drift.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/contract_strict.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/core_introspection.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/draft_process.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/emitter.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/events.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/experiments/__init__.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/experiments/minimal_gillespie.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/holes.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/nextflow_deploy.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/orchestration.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/plumbing.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/processes/__init__.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/processes/bigraphical_reactive_system.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/processes/cached.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/processes/dynamic_structure.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/processes/examples.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/processes/growth_division.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/processes/parameter_scan.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/processes/reaction.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/processes/simulation.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/protocols/__init__.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/protocols/_git_worker.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/protocols/clusters/__init__.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/protocols/clusters/ec2_ssm.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/protocols/git.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/protocols/parallel.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/protocols/pool.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/protocols/ray.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/protocols/rest.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/protocols/session.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/run_composite.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/run_step.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/server/__init__.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/server/rest.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/server/start.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/templates.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/types/__init__.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/types/process.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/visualization.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/visualizations/__init__.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/visualizations/distribution.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/visualizations/heatmap.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/visualizations/param_vs_observable.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/visualizations/phase_space.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/visualizations/timeseries_from_observables.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/wiring_translator.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/workflow/__init__.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/workflow/backend.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/workflow/provision.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/workflow/recipe.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/workflow/tasks.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph.egg-info/dependency_links.txt +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph.egg-info/requires.txt +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph.egg-info/top_level.txt +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/setup.cfg +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/tests/test_composite_generator_analyses.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/tests/test_global_time_precision_default.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/tests/test_initial_state_precedence.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/tests/test_multirate_scheduling.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/tests/test_process_decorator.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/tests/test_reaction_step.py +0 -0
- {process_bigraph-1.10.1 → process_bigraph-1.11.0}/tests/test_units.py +0 -0
|
@@ -15,6 +15,7 @@ Used as part of the Vivarium 2.0 ecosystem for modular biological modeling.
|
|
|
15
15
|
import os
|
|
16
16
|
import json
|
|
17
17
|
import math
|
|
18
|
+
import heapq
|
|
18
19
|
import inspect
|
|
19
20
|
import time as _time
|
|
20
21
|
import contextvars
|
|
@@ -1344,6 +1345,22 @@ class Composite(Process):
|
|
|
1344
1345
|
# parallel_steps and bounded by parallel_workers.
|
|
1345
1346
|
'parallel_processes': 'boolean{false}',
|
|
1346
1347
|
'parallel_workers': 'maybe[integer]',
|
|
1348
|
+
# Temporal-scheduler strategy for the serial per-tick loop:
|
|
1349
|
+
# 'scan' — the original loop that calls run_process on every
|
|
1350
|
+
# process every tick (O(P) per tick; O(P^2) total when
|
|
1351
|
+
# intervals differ, because tick count grows with P).
|
|
1352
|
+
# 'heap' — an event min-heap keyed on each process's next due time:
|
|
1353
|
+
# pop only the due processes, skip the wasted input slice
|
|
1354
|
+
# for a process whose next step overruns the run horizon,
|
|
1355
|
+
# and drop such a process until the next run() (it cannot
|
|
1356
|
+
# fire again this run). ~O(P log P); bit-identical to 'scan'.
|
|
1357
|
+
# 5x-55x faster on many-rate composites, ~5% slower on
|
|
1358
|
+
# single-rate ones (heap bookkeeping with nothing to gain).
|
|
1359
|
+
# 'auto' — (default) use 'heap' when the composite has more than one
|
|
1360
|
+
# distinct process interval, else 'scan'. Never engages for
|
|
1361
|
+
# parallel_processes or runtime-batched (tick_lifecycle)
|
|
1362
|
+
# composites, which keep the scan path.
|
|
1363
|
+
'scheduler': 'string{auto}',
|
|
1347
1364
|
# When True, ``initialize`` trusts ``state`` as-is and skips the
|
|
1348
1365
|
# per-process ``link_state`` + ``combine`` pass that normally
|
|
1349
1366
|
# seeds initial values. Set this when loading from a saved
|
|
@@ -1592,6 +1609,14 @@ class Composite(Process):
|
|
|
1592
1609
|
self._parallel_workers = self.config.get('parallel_workers')
|
|
1593
1610
|
self._step_executor = None # lazy: created on first run_steps need
|
|
1594
1611
|
|
|
1612
|
+
# Temporal scheduler strategy (see the 'scheduler' config field).
|
|
1613
|
+
self._scheduler_mode = self.config.get('scheduler') or 'auto'
|
|
1614
|
+
if self._scheduler_mode not in ('auto', 'heap', 'scan'):
|
|
1615
|
+
raise ValueError(
|
|
1616
|
+
f"scheduler must be 'auto', 'heap' or 'scan', got {self._scheduler_mode!r}")
|
|
1617
|
+
self._use_heap = False # decided per run() in _decide_scheduler
|
|
1618
|
+
self._event_heap = None # event min-heap, (re)built lazily by the heap path
|
|
1619
|
+
|
|
1595
1620
|
# Initialize a "front" dictionary tracking the next update time and update data per process.
|
|
1596
1621
|
self.front = {
|
|
1597
1622
|
path: empty_front(self.state['global_time'])
|
|
@@ -2815,6 +2840,8 @@ class Composite(Process):
|
|
|
2815
2840
|
if em.detail_timing:
|
|
2816
2841
|
self._profile_per_process = True
|
|
2817
2842
|
self._run_ticks = 0 # per-Composite, so nested runs do not clobber the outer count
|
|
2843
|
+
self._event_heap = None # (re)built lazily by the heap path
|
|
2844
|
+
self._use_heap = self._decide_scheduler()
|
|
2818
2845
|
# Rate limit: emit this pair if it is the first run on this Composite or
|
|
2819
2846
|
# ``heartbeat_s`` has elapsed since the last emitted pair. An error at
|
|
2820
2847
|
# the end overrides the decision (see ``finally``), because a failure is
|
|
@@ -2906,6 +2933,32 @@ class Composite(Process):
|
|
|
2906
2933
|
self.run_steps(self.to_run)
|
|
2907
2934
|
self.to_run = []
|
|
2908
2935
|
|
|
2936
|
+
def _decide_scheduler(self) -> bool:
|
|
2937
|
+
"""Decide whether this run() uses the event-heap tick loop.
|
|
2938
|
+
|
|
2939
|
+
Called once per run(). The heap path is only safe/beneficial for the
|
|
2940
|
+
serial, non-runtime-batched loop; parallel_processes and tick_lifecycle
|
|
2941
|
+
(sharded/Ray) composites keep the scan path. In 'auto' mode the heap is
|
|
2942
|
+
used only when more than one distinct process interval is present (a
|
|
2943
|
+
single-rate composite gains nothing and pays heap bookkeeping)."""
|
|
2944
|
+
if self._scheduler_mode == 'scan':
|
|
2945
|
+
return False
|
|
2946
|
+
if self._parallel_processes:
|
|
2947
|
+
return False
|
|
2948
|
+
managed, groups = self._partition_processes_by_runtime()
|
|
2949
|
+
if managed or groups:
|
|
2950
|
+
return False
|
|
2951
|
+
if self._scheduler_mode == 'heap':
|
|
2952
|
+
return True
|
|
2953
|
+
# 'auto': use the heap only for genuinely multi-rate composites.
|
|
2954
|
+
seen = set()
|
|
2955
|
+
for path in self.process_paths:
|
|
2956
|
+
process = get_path(self.state, path)
|
|
2957
|
+
seen.add(process.get('interval'))
|
|
2958
|
+
if len(seen) > 1:
|
|
2959
|
+
return True
|
|
2960
|
+
return False
|
|
2961
|
+
|
|
2909
2962
|
def advance_tick(self, end_time: float, force_complete: bool):
|
|
2910
2963
|
"""Execute ONE temporal tick toward ``end_time`` — the body of the
|
|
2911
2964
|
event-scheduling loop. Returns ``(halted, force_complete)``: ``halted``
|
|
@@ -2913,6 +2966,10 @@ class Composite(Process):
|
|
|
2913
2966
|
``force_complete`` is returned because the tick may clear it. The
|
|
2914
2967
|
``temporal`` strategy drives this in a loop while ``Composite`` owns the
|
|
2915
2968
|
tick mechanics (front, invoke, apply), keeping strategies thin."""
|
|
2969
|
+
# Event-heap scheduler (O(P log P) vs the O(P) per-tick scan below).
|
|
2970
|
+
# Decided once per run() in _decide_scheduler.
|
|
2971
|
+
if self._use_heap:
|
|
2972
|
+
return self._advance_tick_heap(end_time, force_complete)
|
|
2916
2973
|
full_step = math.inf
|
|
2917
2974
|
self._run_ticks = getattr(self, '_run_ticks', 0) + 1
|
|
2918
2975
|
self._em.heartbeat(global_time=self.state['global_time'])
|
|
@@ -2981,6 +3038,126 @@ class Composite(Process):
|
|
|
2981
3038
|
|
|
2982
3039
|
return False, force_complete
|
|
2983
3040
|
|
|
3041
|
+
def _advance_tick_heap(self, end_time: float, force_complete: bool):
|
|
3042
|
+
"""Event-min-heap equivalent of ``advance_tick``.
|
|
3043
|
+
|
|
3044
|
+
Same semantics, but the O(P) per-tick scans (discover due processes,
|
|
3045
|
+
compute the not-due ``full_step`` floor, find the next event, and
|
|
3046
|
+
collect applicable updates) become heap pops. Entries are
|
|
3047
|
+
``(time, seq, path)``; a popped entry is stale (skipped) when it no
|
|
3048
|
+
longer matches ``self.front[path]['time']`` (lazy deletion). The heap
|
|
3049
|
+
is rebuilt whenever structure changes (``self._event_heap = None``)."""
|
|
3050
|
+
full_step = math.inf
|
|
3051
|
+
self._run_ticks = getattr(self, '_run_ticks', 0) + 1
|
|
3052
|
+
self._em.heartbeat(global_time=self.state['global_time'])
|
|
3053
|
+
gt = self.state['global_time']
|
|
3054
|
+
|
|
3055
|
+
h = self._event_heap
|
|
3056
|
+
if h is None:
|
|
3057
|
+
# Mirror run_process's lazy front init: a newly added process path
|
|
3058
|
+
# (e.g. a division daughter) may be in process_paths but not yet in
|
|
3059
|
+
# front. empty_front schedules it at the current time (due now),
|
|
3060
|
+
# matching stock behavior.
|
|
3061
|
+
for p in self.process_paths:
|
|
3062
|
+
if p not in self.front:
|
|
3063
|
+
self.front[p] = empty_front(self.state['global_time'])
|
|
3064
|
+
h = self._event_heap = [
|
|
3065
|
+
(self.front[p]['time'], i, p)
|
|
3066
|
+
for i, p in enumerate(self.process_paths)]
|
|
3067
|
+
heapq.heapify(h)
|
|
3068
|
+
self._heap_seq = len(h)
|
|
3069
|
+
|
|
3070
|
+
# Discover due processes (front time <= gt) via heap pops.
|
|
3071
|
+
due = []
|
|
3072
|
+
while h and h[0][0] <= gt:
|
|
3073
|
+
t, _s, p = heapq.heappop(h)
|
|
3074
|
+
fp = self.front.get(p)
|
|
3075
|
+
if fp is None or fp['time'] != t:
|
|
3076
|
+
continue # stale entry
|
|
3077
|
+
due.append(p)
|
|
3078
|
+
# Not-due floor on full_step = (min remaining event time) - gt.
|
|
3079
|
+
if h:
|
|
3080
|
+
delay = h[0][0] - gt
|
|
3081
|
+
if delay < full_step:
|
|
3082
|
+
full_step = delay
|
|
3083
|
+
# Run due processes (run_process folds their futures into full_step,
|
|
3084
|
+
# incl. the future>end_time guard), then re-push their new times.
|
|
3085
|
+
for p in due:
|
|
3086
|
+
process = get_path(self.state, p)
|
|
3087
|
+
full_step = self.run_process(p, process, end_time, full_step, force_complete)
|
|
3088
|
+
fp = self.front[p]
|
|
3089
|
+
if fp['time'] > gt:
|
|
3090
|
+
# Advanced normally — reschedule at its next event time.
|
|
3091
|
+
heapq.heappush(h, (fp['time'], self._heap_seq, p))
|
|
3092
|
+
self._heap_seq += 1
|
|
3093
|
+
elif not force_complete and fp['time'] + process['interval'] > end_time:
|
|
3094
|
+
# Horizon overrun: front['time'] did not advance and the next
|
|
3095
|
+
# step falls past this run's end. It cannot fire again this run,
|
|
3096
|
+
# so DROP it from the heap instead of re-popping it every
|
|
3097
|
+
# remaining tick (this is what kills the O(P x tail-ticks)
|
|
3098
|
+
# re-examination). The heap is rebuilt at the next run() from
|
|
3099
|
+
# ``front``, so the process returns deferred exactly as before.
|
|
3100
|
+
pass
|
|
3101
|
+
else:
|
|
3102
|
+
# Did not advance but not a horizon overrun (rare catch-up) —
|
|
3103
|
+
# keep it schedulable so it can fire again.
|
|
3104
|
+
heapq.heappush(h, (fp['time'], self._heap_seq, p))
|
|
3105
|
+
self._heap_seq += 1
|
|
3106
|
+
|
|
3107
|
+
if full_step == math.inf:
|
|
3108
|
+
next_event = end_time
|
|
3109
|
+
if h and h[0][0] < next_event:
|
|
3110
|
+
next_event = h[0][0]
|
|
3111
|
+
self.state['global_time'] = next_event
|
|
3112
|
+
|
|
3113
|
+
elif self.state['global_time'] + full_step <= end_time:
|
|
3114
|
+
self.state['global_time'] += full_step
|
|
3115
|
+
new_gt = self.state['global_time']
|
|
3116
|
+
updates = []
|
|
3117
|
+
update_paths_local = []
|
|
3118
|
+
# Applicable updates: front time <= new_gt. Pop them, collect any
|
|
3119
|
+
# pending delta, and re-push (they stay scheduled for their next run).
|
|
3120
|
+
apply_batch = []
|
|
3121
|
+
while h and h[0][0] <= new_gt:
|
|
3122
|
+
t, _s, p = heapq.heappop(h)
|
|
3123
|
+
fp = self.front.get(p)
|
|
3124
|
+
if fp is None or fp['time'] != t:
|
|
3125
|
+
continue
|
|
3126
|
+
apply_batch.append((p, fp))
|
|
3127
|
+
for p, fp in apply_batch:
|
|
3128
|
+
if fp['update']:
|
|
3129
|
+
updates.append(fp['update'])
|
|
3130
|
+
fp['update'] = {}
|
|
3131
|
+
update_paths_local.append(p)
|
|
3132
|
+
heapq.heappush(h, (fp['time'], self._heap_seq, p))
|
|
3133
|
+
self._heap_seq += 1
|
|
3134
|
+
|
|
3135
|
+
fw_start = _time.monotonic()
|
|
3136
|
+
self._flush_protocol_runtimes()
|
|
3137
|
+
update_paths = self.apply_updates(updates)
|
|
3138
|
+
update_paths.append(('global_time',))
|
|
3139
|
+
self.expire_process_paths(update_paths)
|
|
3140
|
+
self.steps_run = set()
|
|
3141
|
+
if (getattr(self, '_halt_after_structural', False)
|
|
3142
|
+
and getattr(self, '_last_apply_structural', False)):
|
|
3143
|
+
self._event_heap = None # structure changed — rebuild next run
|
|
3144
|
+
self.framework_time += _time.monotonic() - fw_start
|
|
3145
|
+
return True, force_complete
|
|
3146
|
+
self.trigger_steps(update_paths)
|
|
3147
|
+
# A structural apply can add/remove process paths; drop the heap so
|
|
3148
|
+
# it rebuilds from the new front on the next tick.
|
|
3149
|
+
if getattr(self, '_last_apply_structural', False):
|
|
3150
|
+
self._event_heap = None
|
|
3151
|
+
self.framework_time += _time.monotonic() - fw_start
|
|
3152
|
+
|
|
3153
|
+
else:
|
|
3154
|
+
self.state['global_time'] = end_time
|
|
3155
|
+
|
|
3156
|
+
if force_complete and self.state['global_time'] == end_time:
|
|
3157
|
+
force_complete = False
|
|
3158
|
+
|
|
3159
|
+
return False, force_complete
|
|
3160
|
+
|
|
2984
3161
|
def timing_summary(self) -> TimingSummary:
|
|
2985
3162
|
"""Return a ``TimingSummary`` describing the most recent ``run()`` call.
|
|
2986
3163
|
|
|
@@ -3194,6 +3371,28 @@ class Composite(Process):
|
|
|
3194
3371
|
process_time = self.front[path]['time']
|
|
3195
3372
|
|
|
3196
3373
|
if process_time <= self.state['global_time']:
|
|
3374
|
+
# Heap path only: skip the wasted input slice for a process whose
|
|
3375
|
+
# next step overruns the run horizon. Such a process is deferred to
|
|
3376
|
+
# the next run() anyway (its update is skipped and front['time']
|
|
3377
|
+
# stays put), so the slice here would be discarded — yet the code
|
|
3378
|
+
# below slices BEFORE the future>end_time guard, so without this a
|
|
3379
|
+
# large-interval process is re-sliced every remaining tick (O(P x
|
|
3380
|
+
# ticks) waste; ~99% of all view-slicing on many-rate runs). Only
|
|
3381
|
+
# sound when the interval cannot change, i.e. the default
|
|
3382
|
+
# (non-adaptive) calculate_timestep; adaptive processes and the scan
|
|
3383
|
+
# path keep the original behavior.
|
|
3384
|
+
if (self._use_heap
|
|
3385
|
+
and 'future' not in self.front[path]
|
|
3386
|
+
and type(process['instance']).calculate_timestep
|
|
3387
|
+
is Process.calculate_timestep):
|
|
3388
|
+
prospective = process_time + process['interval']
|
|
3389
|
+
if self.global_time_precision:
|
|
3390
|
+
prospective = round(prospective, self.global_time_precision)
|
|
3391
|
+
if not force_complete and prospective > end_time:
|
|
3392
|
+
delay = prospective - self.state['global_time']
|
|
3393
|
+
if delay < full_step:
|
|
3394
|
+
full_step = delay
|
|
3395
|
+
return full_step
|
|
3197
3396
|
# Use future state if already scheduled and saved
|
|
3198
3397
|
if 'future' in self.front[path]:
|
|
3199
3398
|
future_front = self.front[path].pop('future')
|
|
@@ -207,7 +207,7 @@ def _script_body(instance: Any,
|
|
|
207
207
|
f'--in {port}="${{{port}}}"' for port in inputs_wires
|
|
208
208
|
)
|
|
209
209
|
out_flags = ' '.join(
|
|
210
|
-
f'--out {port}
|
|
210
|
+
f'--out {port}=.{port}.json' for port in outputs_wires
|
|
211
211
|
)
|
|
212
212
|
parts = [
|
|
213
213
|
f'{python} -m process_bigraph.run_step',
|
|
@@ -336,7 +336,12 @@ def _process_block(step_name: str,
|
|
|
336
336
|
lines.append(' output:')
|
|
337
337
|
for port in outputs_wires:
|
|
338
338
|
if port not in class_overrides and uses_run_step:
|
|
339
|
-
|
|
339
|
+
# run_step writes the value manifest as a DOTFILE (.<port>.json;
|
|
340
|
+
# see _script_body). Dotfiles keep an override glob on the SAME
|
|
341
|
+
# port (e.g. `path "sweep_*"`) from also matching the manifest --
|
|
342
|
+
# which would collide an N->1 gather. An explicit (non-glob) decl
|
|
343
|
+
# still captures the dotfile, so plain 1->1 wires are unaffected.
|
|
344
|
+
decl = f'path ".{port}.json"'
|
|
340
345
|
else:
|
|
341
346
|
decl = _port_to_nextflow_decl(
|
|
342
347
|
port, step_outputs_schema.get(port, {}), class_overrides)
|
|
@@ -546,6 +551,21 @@ def render_composite(composite: Any, options: Optional[Dict[str, Any]] = None) -
|
|
|
546
551
|
workflow_name = options.get('workflow_name', 'main')
|
|
547
552
|
header = options.get('header', 'nextflow.enable.dsl=2\n')
|
|
548
553
|
python = options.get('python', 'python')
|
|
554
|
+
name_prefix = options.get('_name_prefix', '')
|
|
555
|
+
|
|
556
|
+
def _proc_name(path: Path) -> str:
|
|
557
|
+
"""Process name made globally unique by the ancestor sub-workflow prefix.
|
|
558
|
+
|
|
559
|
+
Nextflow DSL2 process names are GLOBAL. ``_path_to_step_name`` only makes
|
|
560
|
+
a FULL path unique, but the recursive sub-composite render sees inner-only
|
|
561
|
+
paths, so two sibling sub-composites that each contain a like-named step
|
|
562
|
+
(e.g. ``lineage_s0``) would emit the same ``process`` twice and DSL2 would
|
|
563
|
+
not compile. ``_name_prefix`` carries the chain of enclosing
|
|
564
|
+
composite-node names (``runs_v0`` -> ``runs_v0_lineage_s0``). Channel and
|
|
565
|
+
variable names are workflow-local and are deliberately NOT prefixed.
|
|
566
|
+
"""
|
|
567
|
+
base = _path_to_step_name(path)
|
|
568
|
+
return f'{name_prefix}_{base}' if name_prefix else base
|
|
549
569
|
|
|
550
570
|
step_paths = composite.step_paths
|
|
551
571
|
step_dependencies = getattr(composite, 'step_dependencies', {}) or {}
|
|
@@ -611,13 +631,13 @@ def render_composite(composite: Any, options: Optional[Dict[str, Any]] = None) -
|
|
|
611
631
|
for step_path in order:
|
|
612
632
|
if step_path not in step_paths:
|
|
613
633
|
# A composite node: reserve its ordered slot; the loop below fills it.
|
|
614
|
-
workflow_lines.append(f'@@SUBWF:{
|
|
634
|
+
workflow_lines.append(f'@@SUBWF:{_proc_name(step_path)}@@')
|
|
615
635
|
continue
|
|
616
636
|
step = step_paths[step_path]
|
|
617
637
|
instance = step['instance']
|
|
618
638
|
inputs_wires = step.get('inputs') or {}
|
|
619
639
|
outputs_wires = step.get('outputs') or {}
|
|
620
|
-
name =
|
|
640
|
+
name = _proc_name(step_path)
|
|
621
641
|
|
|
622
642
|
if _is_plumbing(instance):
|
|
623
643
|
workflow_lines.append(
|
|
@@ -690,7 +710,7 @@ def render_composite(composite: Any, options: Optional[Dict[str, Any]] = None) -
|
|
|
690
710
|
instance = node.get('instance')
|
|
691
711
|
if not isinstance(instance, _Composite):
|
|
692
712
|
continue
|
|
693
|
-
name =
|
|
713
|
+
name = _proc_name(node_path)
|
|
694
714
|
inputs_wires = node.get('inputs') or {}
|
|
695
715
|
outputs_wires = node.get('outputs') or {}
|
|
696
716
|
inner_steps = getattr(instance, 'step_paths', {}) or {}
|
|
@@ -702,6 +722,12 @@ def render_composite(composite: Any, options: Optional[Dict[str, Any]] = None) -
|
|
|
702
722
|
if nest and inner_steps:
|
|
703
723
|
sub_opts = dict(options)
|
|
704
724
|
sub_opts['workflow_name'] = name
|
|
725
|
+
# Thread the (already-prefixed) sub-workflow name down as the prefix
|
|
726
|
+
# for every process DEFINED inside it, so a sibling sub-composite's
|
|
727
|
+
# like-named step becomes a distinct global process name. `name`
|
|
728
|
+
# itself is globally unique (it carries this scope's own prefix), so
|
|
729
|
+
# the inner scope accumulates the full ancestor chain.
|
|
730
|
+
sub_opts['_name_prefix'] = name
|
|
705
731
|
sub_opts['_emit_ports'] = list(outputs_wires)
|
|
706
732
|
sub_opts['_take_ports'] = list(inputs_wires)
|
|
707
733
|
sub_text = render_composite(instance, {**sub_opts, 'header': ''})
|
|
@@ -189,6 +189,13 @@ def wire_step_layers(
|
|
|
189
189
|
step['_triggers'] = {'_flow_in': 'integer'}
|
|
190
190
|
else:
|
|
191
191
|
step['_triggers'] = {root_trigger: 'float'}
|
|
192
|
+
# A trigger only fires a step if it matches one of the
|
|
193
|
+
# step's input ports, so wire the root-trigger port itself
|
|
194
|
+
# when the step does not already carry it.
|
|
195
|
+
if root_trigger not in step['inputs']:
|
|
196
|
+
step['inputs'][root_trigger] = [root_trigger]
|
|
197
|
+
if root_trigger not in step['_inputs']:
|
|
198
|
+
step['_inputs'][root_trigger] = 'float'
|
|
192
199
|
|
|
193
200
|
# Wire _flow_out as an output port
|
|
194
201
|
step['outputs']['_flow_out'] = [flow_path, out_token]
|
|
@@ -352,6 +359,11 @@ def build_step_network(steps):
|
|
|
352
359
|
# that READS a container it would otherwise propagate down to child leaves
|
|
353
360
|
# is an accumulator, not a fresh producer (A3).
|
|
354
361
|
full_inputs_by_step = {}
|
|
362
|
+
# leaf path (tuple) -> set of step_keys that READ-MODIFY-WRITE it in place
|
|
363
|
+
# (A2 suppressed them as producers of their own accessed store). The
|
|
364
|
+
# pure-reader ordering pass below uses this to order a reader after the
|
|
365
|
+
# accumulators without ordering the accumulators among themselves.
|
|
366
|
+
inout_writers = {}
|
|
355
367
|
|
|
356
368
|
for step_key, step in steps.items():
|
|
357
369
|
schema = step['instance'].interface()
|
|
@@ -456,6 +468,7 @@ def build_step_network(steps):
|
|
|
456
468
|
# in place, like a shared accumulator). Test against the FULL
|
|
457
469
|
# inputs() leaves, not the trigger-narrowed input_paths.
|
|
458
470
|
if tuple(explode) in full_input_leaves or explode in input_paths:
|
|
471
|
+
inout_writers.setdefault(tuple(explode), set()).add(step_key)
|
|
459
472
|
continue
|
|
460
473
|
path = tuple(explode)
|
|
461
474
|
nodes.setdefault(path, {'before': set(), 'after': set()})
|
|
@@ -544,6 +557,86 @@ def build_step_network(steps):
|
|
|
544
557
|
if producer not in node['after']:
|
|
545
558
|
node['before'].add(producer)
|
|
546
559
|
|
|
560
|
+
# Pure-reader ordering (the A2/A3 counterpart). A2/A3 drop an inout
|
|
561
|
+
# accumulator (a step that read-modify-writes a store) from the ``before``
|
|
562
|
+
# producer set of that store's child leaves, which correctly breaks the
|
|
563
|
+
# accumulator-PEER cycle (two accumulators of one store do not produce what
|
|
564
|
+
# each other consumes — v2ecoli ``division`` ↔ ``unique_update_*``). But a
|
|
565
|
+
# PURE READER of that store — a step that reads the settled value and never
|
|
566
|
+
# writes it (a contract/listener bridge reading ``dry_mass``, a tank reading
|
|
567
|
+
# an exchange) — genuinely needs the accumulators to have run, and the
|
|
568
|
+
# dropped ``before`` edge left it unordered (process-bigraph #259 regression:
|
|
569
|
+
# ``contract_out`` read ``dry_mass`` mid-accumulation). We cannot restore the
|
|
570
|
+
# edge through ``before`` — that is a per-PATH set shared by every consumer,
|
|
571
|
+
# so it would also re-order the accumulators against each other and rebuild
|
|
572
|
+
# the very cycle A2/A3 removed. Instead record a per-STEP predecessor edge
|
|
573
|
+
# accumulator → pure-reader: it orders the reader after the accumulators
|
|
574
|
+
# WITHOUT ordering the accumulators among themselves.
|
|
575
|
+
#
|
|
576
|
+
# A pure reader of leaf ``L``: reads ``L`` (exactly, or via an ancestor
|
|
577
|
+
# container of ``L``) and is not itself any writer of ``L`` (neither a
|
|
578
|
+
# recorded producer nor an inout writer). ``determine_steps`` and
|
|
579
|
+
# ``find_step_cycle`` both honour ``_extra_predecessors``.
|
|
580
|
+
writers_of_leaf = {}
|
|
581
|
+
for p, node in nodes.items():
|
|
582
|
+
if node['before']:
|
|
583
|
+
writers_of_leaf.setdefault(p, set()).update(node['before'])
|
|
584
|
+
for p, w in inout_writers.items():
|
|
585
|
+
writers_of_leaf.setdefault(p, set()).update(w)
|
|
586
|
+
reads_by_step = {
|
|
587
|
+
s: {tuple(x) for x in (ancestors[s]['input_paths'] or [])}
|
|
588
|
+
for s in ancestors}
|
|
589
|
+
|
|
590
|
+
# Base producer→consumer adjacency (the ordering edges already implied by
|
|
591
|
+
# ``before``/``after``), used to add a pure-reader edge ONLY where it stays
|
|
592
|
+
# acyclic: if the reader can already reach the accumulator, the two are in a
|
|
593
|
+
# genuine zero-time feedback loop (the reader also feeds the accumulator) and
|
|
594
|
+
# a hard ``accumulator → reader`` edge would close a cycle. Those are left to
|
|
595
|
+
# the scheduler's existing priority-based cycle resolution; we only hard-order
|
|
596
|
+
# the acyclic readers that A2/A3 wrongly left dangling.
|
|
597
|
+
base_adj = {s: set() for s in ancestors}
|
|
598
|
+
for node in nodes.values():
|
|
599
|
+
for producer in (node.get('before') or ()):
|
|
600
|
+
if producer not in base_adj:
|
|
601
|
+
continue
|
|
602
|
+
for consumer in (node.get('after') or ()):
|
|
603
|
+
if consumer != producer:
|
|
604
|
+
base_adj[producer].add(consumer)
|
|
605
|
+
|
|
606
|
+
def _reaches(src, dst):
|
|
607
|
+
if src == dst:
|
|
608
|
+
return True
|
|
609
|
+
seen = {src}
|
|
610
|
+
stack = [src]
|
|
611
|
+
while stack:
|
|
612
|
+
cur = stack.pop()
|
|
613
|
+
for nxt in base_adj.get(cur, ()):
|
|
614
|
+
if nxt == dst:
|
|
615
|
+
return True
|
|
616
|
+
if nxt not in seen:
|
|
617
|
+
seen.add(nxt)
|
|
618
|
+
stack.append(nxt)
|
|
619
|
+
return False
|
|
620
|
+
|
|
621
|
+
extra_predecessors = {s: set() for s in ancestors}
|
|
622
|
+
for leaf, accumulators in inout_writers.items():
|
|
623
|
+
leaf_writers = writers_of_leaf.get(leaf, set())
|
|
624
|
+
for step_key, reads in reads_by_step.items():
|
|
625
|
+
if step_key in accumulators or step_key in leaf_writers:
|
|
626
|
+
continue # a writer of this leaf, not a pure reader
|
|
627
|
+
reads_leaf = leaf in reads or any(
|
|
628
|
+
len(i) <= len(leaf) and tuple(leaf[:len(i)]) == i for i in reads)
|
|
629
|
+
if not reads_leaf:
|
|
630
|
+
continue
|
|
631
|
+
for acc in accumulators:
|
|
632
|
+
if acc == step_key:
|
|
633
|
+
continue
|
|
634
|
+
# Skip the edge if the reader already reaches the accumulator
|
|
635
|
+
# (acc → reader would close a feedback cycle); priority resolves
|
|
636
|
+
# those. Otherwise hard-order reader after acc.
|
|
637
|
+
if not _reaches(step_key, acc):
|
|
638
|
+
extra_predecessors[step_key].add(acc)
|
|
639
|
+
|
|
547
640
|
# Precompute each step's direct dependents (= the set of steps
|
|
548
641
|
# that consume something this step writes). ``find_downstream``
|
|
549
642
|
# was reconstructing this every tick by walking
|
|
@@ -557,7 +650,16 @@ def build_step_network(steps):
|
|
|
557
650
|
node = nodes.get(subpath)
|
|
558
651
|
if node is not None:
|
|
559
652
|
direct.update(node['after'])
|
|
653
|
+
ancestor['_extra_predecessors'] = frozenset(extra_predecessors[step_key])
|
|
560
654
|
ancestor['_direct_dependents'] = frozenset(direct)
|
|
655
|
+
# A pure reader is a dependent of each accumulator that precedes it, so the
|
|
656
|
+
# hot-path downstream BFS (``find_downstream``) re-triggers it when the
|
|
657
|
+
# accumulator updates.
|
|
658
|
+
for reader, preds in extra_predecessors.items():
|
|
659
|
+
for pred in preds:
|
|
660
|
+
if pred in ancestors:
|
|
661
|
+
ancestors[pred]['_direct_dependents'] = frozenset(
|
|
662
|
+
ancestors[pred]['_direct_dependents'] | {reader})
|
|
561
663
|
|
|
562
664
|
return ancestors, nodes
|
|
563
665
|
|
|
@@ -619,6 +721,13 @@ def find_step_cycle(ancestors, nodes):
|
|
|
619
721
|
for consumer in after:
|
|
620
722
|
if consumer != producer:
|
|
621
723
|
graph[producer].add(consumer)
|
|
724
|
+
# Per-step pure-reader edges (accumulator -> reader) carry the same ordering
|
|
725
|
+
# constraint as a ``before`` -> ``after`` edge and must be visible to cycle
|
|
726
|
+
# detection, so a genuine reader↔accumulator feedback is still caught.
|
|
727
|
+
for step_key, ancestor in ancestors.items():
|
|
728
|
+
for pred in ancestor.get('_extra_predecessors', ()):
|
|
729
|
+
if pred in graph and pred != step_key:
|
|
730
|
+
graph[pred].add(step_key)
|
|
622
731
|
|
|
623
732
|
# Iterative DFS with three-colour marking (white/grey/black). A grey
|
|
624
733
|
# successor is a back-edge, i.e. a cycle; parent pointers reconstruct it.
|
|
@@ -782,8 +891,15 @@ def determine_steps(steps, remaining, fulfilled):
|
|
|
782
891
|
|
|
783
892
|
for step_path in list(remaining):
|
|
784
893
|
step_inputs = steps[step_path].get('input_paths', []) or []
|
|
785
|
-
if all(len(fulfilled[input]) == 0 for input in step_inputs):
|
|
786
|
-
|
|
894
|
+
if not all(len(fulfilled[input]) == 0 for input in step_inputs):
|
|
895
|
+
continue
|
|
896
|
+
# A pure reader waits on its inout accumulators too (see
|
|
897
|
+
# ``_extra_predecessors`` in build_step_network): it is ready only once
|
|
898
|
+
# every accumulator it reads from has already run this cycle.
|
|
899
|
+
extra = steps[step_path].get('_extra_predecessors', ())
|
|
900
|
+
if any(pred in remaining for pred in extra):
|
|
901
|
+
continue
|
|
902
|
+
to_run.append(step_path)
|
|
787
903
|
|
|
788
904
|
if not to_run:
|
|
789
905
|
# All remaining steps are in cycles — pick the highest priority one.
|
{process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/visualizations/time_series.py
RENAMED
|
@@ -16,10 +16,12 @@ class TimeSeriesPlot(Visualization):
|
|
|
16
16
|
Inputs (declared types):
|
|
17
17
|
observable: list[float] — trajectory values, or list-of-lists for multi-run
|
|
18
18
|
time: list[float] — same shape as observable
|
|
19
|
+
run_labels: list[string] — one legend label per run (cross-run figures)
|
|
19
20
|
"""
|
|
20
21
|
|
|
21
22
|
def inputs(self):
|
|
22
|
-
return {'observable': 'list[float]', 'time': 'list[float]'
|
|
23
|
+
return {'observable': 'list[float]', 'time': 'list[float]',
|
|
24
|
+
'run_labels': 'list[string]'}
|
|
23
25
|
|
|
24
26
|
def update(self, state):
|
|
25
27
|
obs = state.get('observable')
|
|
@@ -31,7 +33,10 @@ class TimeSeriesPlot(Visualization):
|
|
|
31
33
|
runs = list(zip(ts, obs))
|
|
32
34
|
else:
|
|
33
35
|
runs = [(ts, obs)]
|
|
34
|
-
|
|
36
|
+
# Prefer the declared `run_labels` input port; fall back to the legacy
|
|
37
|
+
# `_run_labels` state key for back-compat (a leading-underscore key is a
|
|
38
|
+
# bigraph-schema key and can't be wired as a port — see #1474).
|
|
39
|
+
labels = state.get('run_labels') or state.get('_run_labels') or [''] * len(runs)
|
|
35
40
|
overlays = state.get('_overlays') or []
|
|
36
41
|
|
|
37
42
|
traces = []
|
|
@@ -74,8 +74,10 @@ process_bigraph/workflow/recipe.py
|
|
|
74
74
|
process_bigraph/workflow/tasks.py
|
|
75
75
|
tests/test_composite_generator_analyses.py
|
|
76
76
|
tests/test_global_time_precision_default.py
|
|
77
|
+
tests/test_heap_scheduler.py
|
|
77
78
|
tests/test_initial_state_precedence.py
|
|
78
79
|
tests/test_multirate_scheduling.py
|
|
79
80
|
tests/test_process_decorator.py
|
|
80
81
|
tests/test_reaction_step.py
|
|
81
|
-
tests/test_units.py
|
|
82
|
+
tests/test_units.py
|
|
83
|
+
tests/test_wire_step_layers_root_trigger.py
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
"""Regression tests for the event-heap temporal scheduler (`scheduler` config).
|
|
2
|
+
|
|
3
|
+
The heap path must be BIT-IDENTICAL to the original 'scan' path while cutting
|
|
4
|
+
the O(P^2) multi-rate cost to ~O(P log P). These tests pin both: agreement
|
|
5
|
+
(heap == scan on state) and the perf guarantee (no wasted per-tick re-slicing
|
|
6
|
+
of horizon-overrun processes).
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import inspect
|
|
10
|
+
import sys
|
|
11
|
+
|
|
12
|
+
import pytest
|
|
13
|
+
|
|
14
|
+
from bigraph_schema import allocate_core
|
|
15
|
+
from process_bigraph.composite import Process, Composite
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class Decay(Process):
|
|
19
|
+
config_schema = {'rate': {'_type': 'float', '_default': '0.99'}}
|
|
20
|
+
|
|
21
|
+
def inputs(self):
|
|
22
|
+
return {'x': 'float'}
|
|
23
|
+
|
|
24
|
+
def outputs(self):
|
|
25
|
+
return {'x': 'float'}
|
|
26
|
+
|
|
27
|
+
def update(self, state, interval):
|
|
28
|
+
return {'x': state['x'] * self.config['rate']}
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class MapChurn(Process):
|
|
32
|
+
"""Adds and removes `churn` agents per tick — exercises _add/_remove and
|
|
33
|
+
the structural-subtree realize path under the heap."""
|
|
34
|
+
config_schema = {'churn': {'_type': 'integer', '_default': '3'}}
|
|
35
|
+
|
|
36
|
+
def inputs(self):
|
|
37
|
+
return {'agents': {'_type': 'map', '_value': 'float'}}
|
|
38
|
+
|
|
39
|
+
def outputs(self):
|
|
40
|
+
return {'agents': {'_type': 'map', '_value': 'float'}}
|
|
41
|
+
|
|
42
|
+
def update(self, state, interval):
|
|
43
|
+
ids = list(state['agents'].keys())
|
|
44
|
+
c = min(self.config['churn'], len(ids))
|
|
45
|
+
self._n = getattr(self, '_n', 0)
|
|
46
|
+
add = []
|
|
47
|
+
for _ in range(c):
|
|
48
|
+
self._n += 1
|
|
49
|
+
add.append((f'z{self._n}', 1.0))
|
|
50
|
+
return {'agents': {'_remove': ids[:c], '_add': add}}
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
@pytest.fixture
|
|
54
|
+
def core():
|
|
55
|
+
return allocate_core(top=dict(inspect.getmembers(sys.modules[__name__])))
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def _timescales(core, k, distinct, scheduler):
|
|
59
|
+
state = {}
|
|
60
|
+
for i in range(k):
|
|
61
|
+
state[f'x_{i}'] = 1.0
|
|
62
|
+
state[f'p_{i}'] = {
|
|
63
|
+
'_type': 'process', 'address': 'local:Decay',
|
|
64
|
+
'config': {'rate': 0.99},
|
|
65
|
+
'interval': 1.0 + (i * 0.017 if distinct else 0.0),
|
|
66
|
+
'inputs': {'x': [f'x_{i}']}, 'outputs': {'x': [f'x_{i}']}}
|
|
67
|
+
return Composite({'state': state, 'scheduler': scheduler}, core=core)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def _final(composite):
|
|
71
|
+
return {k: v for k, v in composite.state.items() if isinstance(v, float)}
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
@pytest.mark.parametrize('distinct', [True, False])
|
|
75
|
+
def test_heap_matches_scan(core, distinct):
|
|
76
|
+
scan = _timescales(core, 40, distinct, 'scan'); scan.run(15.0)
|
|
77
|
+
heap = _timescales(core, 40, distinct, 'heap'); heap.run(15.0)
|
|
78
|
+
assert _final(scan) == _final(heap)
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def test_auto_uses_heap_only_when_multirate(core):
|
|
82
|
+
multi = _timescales(core, 10, True, 'auto'); multi.run(5.0)
|
|
83
|
+
single = _timescales(core, 10, False, 'auto'); single.run(5.0)
|
|
84
|
+
assert multi._use_heap is True
|
|
85
|
+
assert single._use_heap is False
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def test_heap_avoids_wasted_reslicing(core):
|
|
89
|
+
"""On a multi-rate composite the heap must slice ~once per real firing,
|
|
90
|
+
not O(P x ticks). Compare _cached_view call counts heap vs scan."""
|
|
91
|
+
def count_slices(scheduler):
|
|
92
|
+
c = _timescales(core, 60, True, scheduler)
|
|
93
|
+
n = [0]; orig = c._cached_view
|
|
94
|
+
c._cached_view = lambda *a, **k: (n.__setitem__(0, n[0] + 1), orig(*a, **k))[1]
|
|
95
|
+
c.run(15.0)
|
|
96
|
+
return n[0]
|
|
97
|
+
heap_slices = count_slices('heap')
|
|
98
|
+
scan_slices = count_slices('scan')
|
|
99
|
+
# Heap removes the wasted horizon-overrun re-slices: a large fraction fewer.
|
|
100
|
+
assert heap_slices < scan_slices / 3
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def test_heap_matches_scan_structural(core):
|
|
104
|
+
"""Division/removal (_add/_remove) must agree between heap and scan."""
|
|
105
|
+
def build(scheduler):
|
|
106
|
+
agents = {f'a{i}': 1.0 for i in range(20)}
|
|
107
|
+
return Composite({'state': {
|
|
108
|
+
'agents': agents,
|
|
109
|
+
'churn': {
|
|
110
|
+
'_type': 'process', 'address': 'local:MapChurn',
|
|
111
|
+
'config': {'churn': 3}, 'interval': 1.0,
|
|
112
|
+
'inputs': {'agents': ['agents']},
|
|
113
|
+
'outputs': {'agents': ['agents']}}},
|
|
114
|
+
'scheduler': scheduler}, core=core)
|
|
115
|
+
scan = build('scan'); scan.run(10.0)
|
|
116
|
+
heap = build('heap'); heap.run(10.0)
|
|
117
|
+
assert set(scan.state['agents']) == set(heap.state['agents'])
|
|
118
|
+
assert len(heap.state['agents']) == 20
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def test_invalid_scheduler_rejected(core):
|
|
122
|
+
with pytest.raises(ValueError):
|
|
123
|
+
Composite({'state': {}, 'scheduler': 'bogus'}, core=core)
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"""wire_step_layers must wire the root-trigger input port itself.
|
|
2
|
+
|
|
3
|
+
A layer-0 step whose own ports do not include `global_time` used to fire once
|
|
4
|
+
at init and never again, because `_triggers={'global_time': ...}` only matches
|
|
5
|
+
a step's *input ports*.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import inspect
|
|
9
|
+
import sys
|
|
10
|
+
|
|
11
|
+
import pytest
|
|
12
|
+
|
|
13
|
+
from bigraph_schema import allocate_core
|
|
14
|
+
from process_bigraph.composite import Process, Step, Composite
|
|
15
|
+
from process_bigraph import wire_step_layers
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class Tick(Process):
|
|
19
|
+
def inputs(self):
|
|
20
|
+
return {}
|
|
21
|
+
|
|
22
|
+
def outputs(self):
|
|
23
|
+
return {'t': 'float'}
|
|
24
|
+
|
|
25
|
+
def update(self, state, interval):
|
|
26
|
+
return {'t': interval}
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class CountNoClock(Step):
|
|
30
|
+
"""Reads/writes a domain store only (no global_time port)."""
|
|
31
|
+
|
|
32
|
+
def inputs(self):
|
|
33
|
+
return {'n': 'integer'}
|
|
34
|
+
|
|
35
|
+
def outputs(self):
|
|
36
|
+
return {'n': 'integer'}
|
|
37
|
+
|
|
38
|
+
def update(self, state):
|
|
39
|
+
return {'n': 1}
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class CountWithClock(CountNoClock):
|
|
43
|
+
def inputs(self):
|
|
44
|
+
return {'n': 'integer', 'global_time': 'float'}
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
@pytest.fixture
|
|
48
|
+
def core():
|
|
49
|
+
return allocate_core(top=dict(inspect.getmembers(sys.modules[__name__])))
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def _build(core, cls, hand_wired_clock=False, n_steps=1, parallel=False):
|
|
53
|
+
state = {
|
|
54
|
+
'n': 0,
|
|
55
|
+
'm': 0,
|
|
56
|
+
'tick': {'_type': 'process', 'address': 'local:Tick', 'config': {},
|
|
57
|
+
'interval': 1.0, 'inputs': {}, 'outputs': {'t': ['t']}},
|
|
58
|
+
'count': {'_type': 'step', 'address': f'local:{cls.__name__}', 'config': {},
|
|
59
|
+
'inputs': {'n': ['n']}, 'outputs': {'n': ['n']}},
|
|
60
|
+
'count2': {'_type': 'step', 'address': f'local:{cls.__name__}', 'config': {},
|
|
61
|
+
'inputs': {'n': ['m']}, 'outputs': {'n': ['m']}},
|
|
62
|
+
}
|
|
63
|
+
if hand_wired_clock:
|
|
64
|
+
for k in ('count', 'count2'):
|
|
65
|
+
state[k]['inputs']['global_time'] = ['global_time']
|
|
66
|
+
wire_step_layers(state, {'count': [], 'count2': []})
|
|
67
|
+
return Composite({'state': state, 'parallel_steps': parallel}, core=core)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def _run(core, cls, **kw):
|
|
71
|
+
sim = _build(core, cls, **kw)
|
|
72
|
+
sim.run(5.0)
|
|
73
|
+
return sim.state['n'], sim.state['m']
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def test_layer0_step_without_clock_port_refires_every_tick(core):
|
|
77
|
+
n, m = _run(core, CountNoClock)
|
|
78
|
+
# init fire + one per tick over 5 ticks; must advance well past 1
|
|
79
|
+
assert n > 1 and m > 1
|
|
80
|
+
assert (n, m) == _run(core, CountWithClock, hand_wired_clock=True)
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def test_existing_clock_port_is_unperturbed(core):
|
|
84
|
+
a = _run(core, CountWithClock, hand_wired_clock=True)
|
|
85
|
+
# Hand-wired run is the pre-fix behaviour (port already present, so the
|
|
86
|
+
# fix adds nothing); pin its exact values.
|
|
87
|
+
assert a == (6, 6)
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def test_parallel_steps_bit_identical(core):
|
|
91
|
+
assert _run(core, CountNoClock, parallel=True) == _run(core, CountNoClock)
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
_SPANS = []
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
class SlowNoClock(CountNoClock):
|
|
98
|
+
def update(self, state):
|
|
99
|
+
import time
|
|
100
|
+
t0 = time.perf_counter()
|
|
101
|
+
time.sleep(0.05)
|
|
102
|
+
_SPANS.append((t0, time.perf_counter()))
|
|
103
|
+
return {'n': 1}
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def test_parallel_steps_overlap_and_refire(core):
|
|
107
|
+
_SPANS.clear()
|
|
108
|
+
n, m = _run(core, SlowNoClock, parallel=True)
|
|
109
|
+
par = (n, m)
|
|
110
|
+
spans = sorted(_SPANS)
|
|
111
|
+
assert n > 1 and m > 1
|
|
112
|
+
assert any(b[0] < a[1] for a, b in zip(spans, spans[1:])) # overlap
|
|
113
|
+
_SPANS.clear()
|
|
114
|
+
assert par == _run(core, SlowNoClock, parallel=False)
|
|
115
|
+
spans = sorted(_SPANS)
|
|
116
|
+
assert not any(b[0] < a[1] for a, b in zip(spans, spans[1:])) # serial
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/experiments/minimal_gillespie.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/processes/dynamic_structure.py
RENAMED
|
File without changes
|
|
File without changes
|
{process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/processes/growth_division.py
RENAMED
|
File without changes
|
{process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/processes/parameter_scan.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/protocols/clusters/__init__.py
RENAMED
|
File without changes
|
{process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/protocols/clusters/ec2_ssm.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/visualizations/__init__.py
RENAMED
|
File without changes
|
{process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/visualizations/distribution.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
{process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph/visualizations/phase_space.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{process_bigraph-1.10.1 → process_bigraph-1.11.0}/process_bigraph.egg-info/dependency_links.txt
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{process_bigraph-1.10.1 → process_bigraph-1.11.0}/tests/test_composite_generator_analyses.py
RENAMED
|
File without changes
|
{process_bigraph-1.10.1 → process_bigraph-1.11.0}/tests/test_global_time_precision_default.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|