Delay Embedding — Phase-Space Reconstruction¶
Why this module is exposed¶
Delay embedding is how SPO can recover state-space structure when only scalar observations are available. It lets monitoring and anomaly workflows operate from raw traces without requiring a separate external embedding stack.
The module is intentionally strict at boundaries so a downstream controller uses phase-space features with deterministic semantics.
The monitor.embedding module reconstructs phase-space trajectories from
scalar oscillator traces. It provides delay-coordinate embedding,
Fraser-Swinney average mutual information, nearest-neighbor distances,
and Python-side wrappers for optimal delay and embedding dimension.
Operational use¶
Use this module when a monitor needs geometry and short-term predictability from non-vector observations (for example, single-channel physiology traces mapped into state-space structure).
A practical sequence is:
- Build a validated embedded matrix (
delay_embedorauto_embed); - Select delay/dimension from information-theoretic and neighborhood diagnostics;
- Feed the reconstructed state to downstream monitor logic;
- Validate resulting geometrical signals against replay baselines before using them in policy experiments.
Keep this boundary explicit: embedding is a feature-construction layer, not a replacement for raw trace quality checks.
API¶
from scpn_phase_orchestrator.monitor.embedding import (
delay_embed,
mutual_information,
nearest_neighbor_distances,
optimal_delay,
optimal_dimension,
auto_embed,
)
delay_embed(signal, delay, dimension) returns the standard
delay-coordinate matrix:
mutual_information(signal, lag, n_bins) estimates average mutual
information for delay selection. nearest_neighbor_distances(embedded)
supports false-nearest-neighbor dimension selection.
Direct backend boundary¶
Go, Julia, and Mojo direct bridge calls share the same typed pre-dispatch
contract before optional runtime loading. Signal payloads must be finite
real one-dimensional float64 arrays and must reject numeric-string aliases
before float coercion. Delay, dimension, lag, bin-count, row-count, and
embedding-dimension controls must be integer values in the public API domain.
Embedded nearest-neighbor payloads must be finite real flat float64 arrays
whose length matches T*m, with numeric-string aliases rejected at the same
pre-dispatch boundary.
Boolean aliases, object-dtype complex aliases, complex samples, non-finite samples, numeric-string aliases, non-vector signal payloads, malformed flattened embedding lengths, invalid delay/dimension requests, invalid lags, and invalid bin counts are rejected before shared-library, Julia, or subprocess execution.
Direct backend return payloads are validated before they are handed back to the
public monitor boundary. The public dispatcher repeats the same physics-facing
checks after backend fallback resolution so a shape-correct optional backend
cannot silently return the wrong phase-space reconstruction. Delay-embedding
outputs must have the exact (T_effective, dimension) shape and match the
mathematical indexing x[t + k*tau]; numeric-string aliases and object-dtype
complex aliases are rejected before float coercion; mutual-information outputs
must be finite non-negative real scalars; nearest-neighbor outputs must contain
finite non-negative distances and integral in-range neighbor indices, with
self-neighbors rejected for non-trivial embeddings. Malformed Mojo text output
is normalised to deterministic ValueError failures rather than leaking parser
exceptions.
Invariants¶
delay_embed is exact indexing and should match across backends without
tolerance. Mutual information is non-negative. Nearest-neighbor distances
are finite non-negative values with integer neighbor indices in range and
no self-neighbor for non-trivial inputs. Optional backend outputs that violate
those invariants are rejected and the dispatcher falls back to the next
available backend rather than returning a corrupted embedding or a truncated
neighbor index.
Practical usage profile¶
Teams typically use this module during inspection and replay pipelines:
- validate embedding settings with
optimal_delay/optimal_dimension, - extract trajectory geometry with delay coordinates,
- use downstream monitors on the reconstructed state space instead of direct raw samples.
That pattern keeps signal reconstruction and control logic in one audited path.
embedding ¶
Delay-embedding analysis with a 5-backend fallback chain.
Three compute primitives on the multi-language chain:
- :func:
delay_embed— time-delay embedding matrix. - :func:
mutual_information— Fraser-Swinney 1986 average mutual information. - :func:
nearest_neighbor_distances— brute-forcek=1kNN in the embedded space (consumed by FNN).
Two wrappers stay Python-side (they are control flow over the primitives):
- :func:
optimal_delay— first local minimum of MI (Fraser-Swinney). - :func:
optimal_dimension— Kennel-Brown-Abarbanel 1992 FNN. - :func:
auto_embed— convenience that chainsoptimal_delay,optimal_dimension, and :func:delay_embed.
The Rust backend exposes native optimal_delay_rust and
optimal_dimension_rust entry points; when Rust is active those
wrappers use the native path for maximum throughput. The Python
fallback composes the primitives through the dispatcher.
MI and NN are exposed by Julia / Go / Mojo / Python only — Rust does not expose standalone MI or kNN FFI; those slots dispatch to the next available backend in the chain.
Classes¶
EmbeddingResult
dataclass
¶
Delay-embedding output.
Methods:¶
__post_init__ ¶
Validate and normalise the embedded trajectory record.
Source code in src/scpn_phase_orchestrator/monitor/embedding.py
Functions:¶
delay_embed ¶
Time-delay embedding: v(t) = [x(t), x(t+τ), x(t+2τ), …].
Parameters¶
signal : object
Real-valued time series, shape (T,).
delay : object
Embedding delay τ in samples.
dimension : object
Embedding dimension.
Returns¶
FloatArray
The time-delay embedding, shape (M, dimension).
Raises¶
ValueError
If delay or dimension is non-positive or too large for the signal.
Source code in src/scpn_phase_orchestrator/monitor/embedding.py
mutual_information ¶
Fraser-Swinney 1986 average mutual information at lag.
Parameters¶
signal : object
Real-valued time series, shape (T,).
lag : object
Lag in samples.
n_bins : object
Number of histogram bins.
Returns¶
float The average mutual information at the given lag.
Source code in src/scpn_phase_orchestrator/monitor/embedding.py
nearest_neighbor_distances ¶
Brute-force k = 1 kNN on the rows of embedded.
Parameters¶
embedded : object
Delay-embedded trajectory, shape (M, dimension).
Returns¶
tuple[FloatArray, IntArray] The nearest-neighbour distances and their indices.
Source code in src/scpn_phase_orchestrator/monitor/embedding.py
optimal_delay ¶
First local minimum of :func:mutual_information vs lag.
Parameters¶
signal : object
Real-valued time series, shape (T,).
max_lag : object
Largest lag to search.
n_bins : object
Number of histogram bins.
Returns¶
int The first mutual-information minimum, as a lag in samples.
Source code in src/scpn_phase_orchestrator/monitor/embedding.py
optimal_dimension ¶
optimal_dimension(
signal: object,
delay: object,
max_dim: object = 10,
rtol: object = 15.0,
atol: object = 2.0,
) -> int
Kennel-Brown-Abarbanel 1992 FNN to select embedding dimension.
Parameters¶
signal : object
Real-valued time series, shape (T,).
delay : object
Embedding delay τ in samples.
max_dim : object
Largest embedding dimension to test.
rtol : object
Relative tolerance for the false-nearest-neighbour test.
atol : object
Absolute tolerance for the false-nearest-neighbour test.
Returns¶
int The selected embedding dimension.
Source code in src/scpn_phase_orchestrator/monitor/embedding.py
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 | |
auto_embed ¶
optimal_delay ∘ optimal_dimension ∘ delay_embed.
Parameters¶
signal : object
Real-valued time series, shape (T,).
max_lag : object
Largest lag to search.
max_dim : object
Largest embedding dimension to test.
Returns¶
EmbeddingResult The auto-selected delay/dimension embedding result.