process-bigraph 1.10.0__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.0/process_bigraph.egg-info → process_bigraph-1.11.0}/PKG-INFO +1 -1
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/composite.py +307 -5
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/composite_spec.py +2 -8
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/nextflow.py +31 -5
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/scheduling.py +118 -2
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/visualizations/time_series.py +7 -2
- {process_bigraph-1.10.0 → process_bigraph-1.11.0/process_bigraph.egg-info}/PKG-INFO +1 -1
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph.egg-info/SOURCES.txt +5 -1
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/pyproject.toml +1 -1
- process_bigraph-1.11.0/tests/test_global_time_precision_default.py +63 -0
- process_bigraph-1.11.0/tests/test_heap_scheduler.py +123 -0
- process_bigraph-1.11.0/tests/test_initial_state_precedence.py +51 -0
- process_bigraph-1.11.0/tests/test_wire_step_layers_root_trigger.py +116 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/AUTHORS.md +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/LICENSE +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/README.md +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/__init__.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/artifacts.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/audit_contracts.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/bundle.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/composite_audit.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/composite_discovery.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/composite_generator.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/config_helpers.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/contract_drift.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/contract_strict.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/core_introspection.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/draft_process.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/emitter.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/events.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/experiments/__init__.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/experiments/minimal_gillespie.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/holes.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/nextflow_deploy.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/orchestration.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/plumbing.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/processes/__init__.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/processes/bigraphical_reactive_system.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/processes/cached.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/processes/dynamic_structure.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/processes/examples.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/processes/growth_division.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/processes/parameter_scan.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/processes/reaction.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/processes/simulation.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/protocols/__init__.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/protocols/_git_worker.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/protocols/clusters/__init__.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/protocols/clusters/ec2_ssm.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/protocols/git.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/protocols/parallel.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/protocols/pool.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/protocols/ray.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/protocols/rest.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/protocols/session.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/run_composite.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/run_step.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/server/__init__.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/server/rest.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/server/start.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/templates.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/types/__init__.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/types/process.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/visualization.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/visualizations/__init__.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/visualizations/distribution.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/visualizations/heatmap.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/visualizations/param_vs_observable.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/visualizations/phase_space.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/visualizations/timeseries_from_observables.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/wiring_translator.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/workflow/__init__.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/workflow/backend.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/workflow/provision.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/workflow/recipe.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph/workflow/tasks.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph.egg-info/dependency_links.txt +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph.egg-info/requires.txt +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/process_bigraph.egg-info/top_level.txt +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/setup.cfg +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/tests/test_composite_generator_analyses.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/tests/test_multirate_scheduling.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/tests/test_process_decorator.py +0 -0
- {process_bigraph-1.10.0 → process_bigraph-1.11.0}/tests/test_reaction_step.py +0 -0
- {process_bigraph-1.10.0 → 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
|
|
@@ -296,6 +297,61 @@ def find_instance_paths(
|
|
|
296
297
|
return hierarchy_depth(instances)
|
|
297
298
|
|
|
298
299
|
|
|
300
|
+
def document_leaf_paths(state, prefix=()):
|
|
301
|
+
"""Yield the path to every non-dict leaf in a raw document state tree.
|
|
302
|
+
|
|
303
|
+
Used to record which stores a document sets explicitly, so a process's
|
|
304
|
+
``initial_state()`` fills only the stores the document left to their type
|
|
305
|
+
default and never overwrites a document-stated value (#200). Dicts recurse;
|
|
306
|
+
every non-dict (scalar, list, array, process node leaf) is a leaf. Paths
|
|
307
|
+
into process nodes are harmless here — they never collide with the store
|
|
308
|
+
paths a port wires to.
|
|
309
|
+
"""
|
|
310
|
+
if isinstance(state, dict):
|
|
311
|
+
for key, value in state.items():
|
|
312
|
+
yield from document_leaf_paths(value, prefix + (key,))
|
|
313
|
+
else:
|
|
314
|
+
yield prefix
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
def without_document_leaves(update, document_leaves, prefix=()):
|
|
318
|
+
"""Return ``update`` with any leaf the document set explicitly removed.
|
|
319
|
+
|
|
320
|
+
A leaf whose absolute path is in ``document_leaves`` is dropped (the
|
|
321
|
+
document value keeps precedence); dicts recurse and now-empty branches are
|
|
322
|
+
pruned so nothing empty is merged (#200).
|
|
323
|
+
"""
|
|
324
|
+
if not isinstance(update, dict):
|
|
325
|
+
return update
|
|
326
|
+
result = {}
|
|
327
|
+
for key, value in update.items():
|
|
328
|
+
child = prefix + (key,)
|
|
329
|
+
if isinstance(value, dict):
|
|
330
|
+
pruned = without_document_leaves(value, document_leaves, child)
|
|
331
|
+
if pruned:
|
|
332
|
+
result[key] = pruned
|
|
333
|
+
elif child not in document_leaves:
|
|
334
|
+
result[key] = value
|
|
335
|
+
return result
|
|
336
|
+
|
|
337
|
+
|
|
338
|
+
def state_has_value_at(state, path):
|
|
339
|
+
"""True iff ``path`` resolves to a present, non-None value in ``state``.
|
|
340
|
+
|
|
341
|
+
Used to keep only the document leaves that realize materialized as actual
|
|
342
|
+
state values. A store declared by type alone (e.g. ``'level': 'float'``)
|
|
343
|
+
moves to the schema and leaves no state value, so it is NOT treated as a
|
|
344
|
+
document-stated value and a process's ``initial_state()`` may fill it (#200).
|
|
345
|
+
"""
|
|
346
|
+
node = state
|
|
347
|
+
for key in path:
|
|
348
|
+
if isinstance(node, dict) and key in node:
|
|
349
|
+
node = node[key]
|
|
350
|
+
else:
|
|
351
|
+
return False
|
|
352
|
+
return node is not None
|
|
353
|
+
|
|
354
|
+
|
|
299
355
|
def interval_time_precision(timestep: float) -> int:
|
|
300
356
|
"""
|
|
301
357
|
Compute the number of decimal places required to represent the given timestep.
|
|
@@ -309,6 +365,21 @@ def interval_time_precision(timestep: float) -> int:
|
|
|
309
365
|
return len(str(timestep).split('.')[1]) if '.' in str(timestep) else 0
|
|
310
366
|
|
|
311
367
|
|
|
368
|
+
def shared_time_precision(intervals) -> Optional[int]:
|
|
369
|
+
"""The common decimal precision of a set of edge intervals, or None.
|
|
370
|
+
|
|
371
|
+
Returns the shared ``interval_time_precision`` when every interval lives on
|
|
372
|
+
one decimal grid (all on the 0.1 grid -> 1, all on the 0.01 grid -> 2,
|
|
373
|
+
etc.). Returns None when there are no intervals, or when they mix grids —
|
|
374
|
+
so a mixed-grid document is never rounded to a grid that does not fit all
|
|
375
|
+
of its processes.
|
|
376
|
+
"""
|
|
377
|
+
precisions = {interval_time_precision(interval) for interval in intervals}
|
|
378
|
+
if len(precisions) == 1:
|
|
379
|
+
return precisions.pop()
|
|
380
|
+
return None
|
|
381
|
+
|
|
382
|
+
|
|
312
383
|
# ===============
|
|
313
384
|
# Process Classes
|
|
314
385
|
# ===============
|
|
@@ -1274,6 +1345,22 @@ class Composite(Process):
|
|
|
1274
1345
|
# parallel_steps and bounded by parallel_workers.
|
|
1275
1346
|
'parallel_processes': 'boolean{false}',
|
|
1276
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}',
|
|
1277
1364
|
# When True, ``initialize`` trusts ``state`` as-is and skips the
|
|
1278
1365
|
# per-process ``link_state`` + ``combine`` pass that normally
|
|
1279
1366
|
# seeds initial values. Set this when loading from a saved
|
|
@@ -1339,6 +1426,11 @@ class Composite(Process):
|
|
|
1339
1426
|
# both into the same clear "not ground" error that names the site.
|
|
1340
1427
|
self._require_ground_input(initial_schema, initial_state)
|
|
1341
1428
|
|
|
1429
|
+
# Record which stores the document sets explicitly, BEFORE realize
|
|
1430
|
+
# fills type defaults everywhere. These values keep precedence over a
|
|
1431
|
+
# process's initial_state() (#200).
|
|
1432
|
+
document_leaves = set(document_leaf_paths(initial_state))
|
|
1433
|
+
|
|
1342
1434
|
# Generate internal schema and state structures using the core engine.
|
|
1343
1435
|
# Top-level realize: realize_root defaults to () so no merges escape.
|
|
1344
1436
|
self.schema, self.state, _ = self.core.realize(
|
|
@@ -1414,11 +1506,25 @@ class Composite(Process):
|
|
|
1414
1506
|
f'{e}'
|
|
1415
1507
|
)
|
|
1416
1508
|
|
|
1417
|
-
# Apply the merged edge_state into the global state
|
|
1509
|
+
# Apply the merged edge_state into the global state. ``self.state``
|
|
1510
|
+
# here is the document with realize's type defaults filled in, so
|
|
1511
|
+
# it must NOT be the winning update — a default 0.0 / False would
|
|
1512
|
+
# outrank a process's initial_state() for overwrite[float]/boolean
|
|
1513
|
+
# ports (#200). Apply edge_state as the update so initial_state()
|
|
1514
|
+
# beats realize-filled defaults, but first drop the leaves the
|
|
1515
|
+
# document set explicitly so those keep precedence over both.
|
|
1418
1516
|
if edge_state:
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1517
|
+
# Keep only the document leaves that realize materialized as
|
|
1518
|
+
# real state values — a type-only store declaration leaves no
|
|
1519
|
+
# value, so initial_state() must still fill it (#200).
|
|
1520
|
+
explicit_leaves = {
|
|
1521
|
+
path for path in document_leaves
|
|
1522
|
+
if state_has_value_at(self.state, path)}
|
|
1523
|
+
edge_state = without_document_leaves(edge_state, explicit_leaves)
|
|
1524
|
+
if edge_state:
|
|
1525
|
+
self.schema, self.state = self.core.combine(
|
|
1526
|
+
self.schema, self.state,
|
|
1527
|
+
edge_schema, edge_state)
|
|
1422
1528
|
|
|
1423
1529
|
# Build the effective bridge dict: conduits are merged into
|
|
1424
1530
|
# outputs for the purposes of process_schema / read_bridge /
|
|
@@ -1476,7 +1582,21 @@ class Composite(Process):
|
|
|
1476
1582
|
self.process_schema[port] = wired
|
|
1477
1583
|
|
|
1478
1584
|
# Set the global time precision used to round step time advances.
|
|
1479
|
-
|
|
1585
|
+
# A plain Composite (built directly, not via CompositeSpec) gets the
|
|
1586
|
+
# same interval-derived default CompositeSpec applies, so float drift
|
|
1587
|
+
# does not defer the last scheduled update (#266) — e.g. 100 steps of
|
|
1588
|
+
# 0.02 reach 2.0000000000000013 and would otherwise skip the 100th.
|
|
1589
|
+
# An explicit config value always wins; a mixed-grid document is left
|
|
1590
|
+
# unrounded (shared_time_precision returns None).
|
|
1591
|
+
global_time_precision = self.config.get('global_time_precision')
|
|
1592
|
+
if global_time_precision is None:
|
|
1593
|
+
global_time_precision = shared_time_precision(
|
|
1594
|
+
edge['interval']
|
|
1595
|
+
for edge in self.edge_paths.values()
|
|
1596
|
+
if isinstance(edge, dict)
|
|
1597
|
+
and isinstance(edge.get('interval'), (int, float))
|
|
1598
|
+
and not isinstance(edge.get('interval'), bool))
|
|
1599
|
+
self.global_time_precision = global_time_precision
|
|
1480
1600
|
|
|
1481
1601
|
# Inner-layer parallelism: optional ThreadPoolExecutor that
|
|
1482
1602
|
# fans out the steps in each layer. See the parallel_steps
|
|
@@ -1489,6 +1609,14 @@ class Composite(Process):
|
|
|
1489
1609
|
self._parallel_workers = self.config.get('parallel_workers')
|
|
1490
1610
|
self._step_executor = None # lazy: created on first run_steps need
|
|
1491
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
|
+
|
|
1492
1620
|
# Initialize a "front" dictionary tracking the next update time and update data per process.
|
|
1493
1621
|
self.front = {
|
|
1494
1622
|
path: empty_front(self.state['global_time'])
|
|
@@ -2712,6 +2840,8 @@ class Composite(Process):
|
|
|
2712
2840
|
if em.detail_timing:
|
|
2713
2841
|
self._profile_per_process = True
|
|
2714
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()
|
|
2715
2845
|
# Rate limit: emit this pair if it is the first run on this Composite or
|
|
2716
2846
|
# ``heartbeat_s`` has elapsed since the last emitted pair. An error at
|
|
2717
2847
|
# the end overrides the decision (see ``finally``), because a failure is
|
|
@@ -2803,6 +2933,32 @@ class Composite(Process):
|
|
|
2803
2933
|
self.run_steps(self.to_run)
|
|
2804
2934
|
self.to_run = []
|
|
2805
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
|
+
|
|
2806
2962
|
def advance_tick(self, end_time: float, force_complete: bool):
|
|
2807
2963
|
"""Execute ONE temporal tick toward ``end_time`` — the body of the
|
|
2808
2964
|
event-scheduling loop. Returns ``(halted, force_complete)``: ``halted``
|
|
@@ -2810,6 +2966,10 @@ class Composite(Process):
|
|
|
2810
2966
|
``force_complete`` is returned because the tick may clear it. The
|
|
2811
2967
|
``temporal`` strategy drives this in a loop while ``Composite`` owns the
|
|
2812
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)
|
|
2813
2973
|
full_step = math.inf
|
|
2814
2974
|
self._run_ticks = getattr(self, '_run_ticks', 0) + 1
|
|
2815
2975
|
self._em.heartbeat(global_time=self.state['global_time'])
|
|
@@ -2878,6 +3038,126 @@ class Composite(Process):
|
|
|
2878
3038
|
|
|
2879
3039
|
return False, force_complete
|
|
2880
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
|
+
|
|
2881
3161
|
def timing_summary(self) -> TimingSummary:
|
|
2882
3162
|
"""Return a ``TimingSummary`` describing the most recent ``run()`` call.
|
|
2883
3163
|
|
|
@@ -3091,6 +3371,28 @@ class Composite(Process):
|
|
|
3091
3371
|
process_time = self.front[path]['time']
|
|
3092
3372
|
|
|
3093
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
|
|
3094
3396
|
# Use future state if already scheduled and saved
|
|
3095
3397
|
if 'future' in self.front[path]:
|
|
3096
3398
|
future_front = self.front[path].pop('future')
|
|
@@ -268,16 +268,10 @@ def _derive_global_time_precision(doc):
|
|
|
268
268
|
document lives on one decimal grid (e.g. all on the 0.1 grid -> 1), else
|
|
269
269
|
``None`` (no intervals, or a mix of grids we must not round across).
|
|
270
270
|
"""
|
|
271
|
-
from process_bigraph.composite import
|
|
271
|
+
from process_bigraph.composite import shared_time_precision
|
|
272
272
|
|
|
273
273
|
state = doc.get("state", doc) if isinstance(doc, dict) else doc
|
|
274
|
-
|
|
275
|
-
interval_time_precision(interval)
|
|
276
|
-
for interval in _iter_edge_intervals(state)
|
|
277
|
-
}
|
|
278
|
-
if len(precisions) == 1:
|
|
279
|
-
return precisions.pop()
|
|
280
|
-
return None
|
|
274
|
+
return shared_time_precision(_iter_edge_intervals(state))
|
|
281
275
|
|
|
282
276
|
|
|
283
277
|
@dataclass
|
|
@@ -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': ''})
|