|
| 1 | +"""Cascade detection and analysis for THOL self-organization. |
| 2 | +
|
| 3 | +Provides tools to detect, measure, and analyze emergent cascades in |
| 4 | +TNFR networks where THOL bifurcations propagate through coupled nodes. |
| 5 | +
|
| 6 | +TNFR Canonical Principle |
| 7 | +------------------------- |
| 8 | +From "El pulso que nos atraviesa" (TNFR Manual, §2.2.10): |
| 9 | +
|
| 10 | + "THOL actúa como modulador central de plasticidad. Es el glifo que |
| 11 | + permite a la red reorganizar su topología sin intervención externa. |
| 12 | + Su activación crea bucles de aprendizaje resonante, trayectorias de |
| 13 | + reorganización emergente, estabilidad dinámica basada en coherencia local." |
| 14 | +
|
| 15 | +This module implements cascade detection: when THOL bifurcations propagate |
| 16 | +through phase-aligned neighbors, creating chains of emergent reorganization. |
| 17 | +""" |
| 18 | + |
| 19 | +from __future__ import annotations |
| 20 | + |
| 21 | +from collections import deque |
| 22 | +from typing import TYPE_CHECKING, Any |
| 23 | + |
| 24 | +if TYPE_CHECKING: |
| 25 | + from ..types import NodeId, TNFRGraph |
| 26 | + |
| 27 | +__all__ = [ |
| 28 | + "detect_cascade", |
| 29 | + "measure_cascade_radius", |
| 30 | +] |
| 31 | + |
| 32 | + |
| 33 | +def detect_cascade(G: TNFRGraph) -> dict[str, Any]: |
| 34 | + """Detect if THOL triggered a propagation cascade in the network. |
| 35 | +
|
| 36 | + A cascade is defined as a chain reaction where: |
| 37 | + 1. Node A bifurcates (THOL) |
| 38 | + 2. Sub-EPI propagates to coupled neighbors |
| 39 | + 3. Neighbors' EPIs increase, potentially triggering their own bifurcations |
| 40 | + 4. Process continues across ≥3 nodes |
| 41 | +
|
| 42 | + Parameters |
| 43 | + ---------- |
| 44 | + G : TNFRGraph |
| 45 | + Graph with THOL propagation history |
| 46 | +
|
| 47 | + Returns |
| 48 | + ------- |
| 49 | + dict |
| 50 | + Cascade analysis containing: |
| 51 | + - is_cascade: bool (True if cascade detected) |
| 52 | + - affected_nodes: set of NodeIds involved |
| 53 | + - cascade_depth: maximum propagation chain length |
| 54 | + - total_propagations: total number of propagation events |
| 55 | + - cascade_coherence: average coupling strength in cascade |
| 56 | +
|
| 57 | + Notes |
| 58 | + ----- |
| 59 | + TNFR Principle: Cascades emerge when network phase coherence enables |
| 60 | + propagation across multiple nodes, creating collective self-organization. |
| 61 | +
|
| 62 | + Examples |
| 63 | + -------- |
| 64 | + >>> # Network with cascade |
| 65 | + >>> analysis = detect_cascade(G) |
| 66 | + >>> analysis["is_cascade"] |
| 67 | + True |
| 68 | + >>> analysis["cascade_depth"] |
| 69 | + 4 # Propagated through 4 levels |
| 70 | + >>> len(analysis["affected_nodes"]) |
| 71 | + 7 # 7 nodes affected |
| 72 | + """ |
| 73 | + propagations = G.graph.get("thol_propagations", []) |
| 74 | + |
| 75 | + if not propagations: |
| 76 | + return { |
| 77 | + "is_cascade": False, |
| 78 | + "affected_nodes": set(), |
| 79 | + "cascade_depth": 0, |
| 80 | + "total_propagations": 0, |
| 81 | + "cascade_coherence": 0.0, |
| 82 | + } |
| 83 | + |
| 84 | + # Build propagation graph |
| 85 | + affected_nodes = set() |
| 86 | + for prop in propagations: |
| 87 | + affected_nodes.add(prop["source_node"]) |
| 88 | + for target, _ in prop["propagations"]: |
| 89 | + affected_nodes.add(target) |
| 90 | + |
| 91 | + # Compute cascade depth (longest propagation chain) |
| 92 | + # For now, approximate as number of propagation events |
| 93 | + cascade_depth = len(propagations) |
| 94 | + |
| 95 | + # Total propagations |
| 96 | + total_props = sum(len(p["propagations"]) for p in propagations) |
| 97 | + |
| 98 | + # Get cascade minimum nodes from config |
| 99 | + cascade_min_nodes = int(G.graph.get("THOL_CASCADE_MIN_NODES", 3)) |
| 100 | + |
| 101 | + # Cascade = affects ≥ cascade_min_nodes |
| 102 | + is_cascade = len(affected_nodes) >= cascade_min_nodes |
| 103 | + |
| 104 | + return { |
| 105 | + "is_cascade": is_cascade, |
| 106 | + "affected_nodes": affected_nodes, |
| 107 | + "cascade_depth": cascade_depth, |
| 108 | + "total_propagations": total_props, |
| 109 | + "cascade_coherence": 0.0, # TODO: compute from coupling strengths |
| 110 | + } |
| 111 | + |
| 112 | + |
| 113 | +def measure_cascade_radius(G: TNFRGraph, source_node: NodeId) -> int: |
| 114 | + """Measure propagation radius from bifurcation source. |
| 115 | +
|
| 116 | + Parameters |
| 117 | + ---------- |
| 118 | + G : TNFRGraph |
| 119 | + Graph with propagation history |
| 120 | + source_node : NodeId |
| 121 | + Origin node of cascade |
| 122 | +
|
| 123 | + Returns |
| 124 | + ------- |
| 125 | + int |
| 126 | + Number of nodes reached by propagation (hop distance) |
| 127 | +
|
| 128 | + Notes |
| 129 | + ----- |
| 130 | + Uses BFS to trace propagation paths from source. |
| 131 | +
|
| 132 | + Examples |
| 133 | + -------- |
| 134 | + >>> # Linear cascade: 0 -> 1 -> 2 -> 3 |
| 135 | + >>> radius = measure_cascade_radius(G, source_node=0) |
| 136 | + >>> radius |
| 137 | + 3 # Reached 3 hops from source |
| 138 | + """ |
| 139 | + propagations = G.graph.get("thol_propagations", []) |
| 140 | + |
| 141 | + # Build propagation edges from this source |
| 142 | + prop_edges = [] |
| 143 | + for prop in propagations: |
| 144 | + if prop["source_node"] == source_node: |
| 145 | + for target, _ in prop["propagations"]: |
| 146 | + prop_edges.append((source_node, target)) |
| 147 | + |
| 148 | + if not prop_edges: |
| 149 | + return 0 |
| 150 | + |
| 151 | + # BFS to measure radius |
| 152 | + visited = {source_node} |
| 153 | + queue = deque([(source_node, 0)]) # (node, distance) |
| 154 | + max_distance = 0 |
| 155 | + |
| 156 | + while queue: |
| 157 | + current, dist = queue.popleft() |
| 158 | + max_distance = max(max_distance, dist) |
| 159 | + |
| 160 | + for src, tgt in prop_edges: |
| 161 | + if src == current and tgt not in visited: |
| 162 | + visited.add(tgt) |
| 163 | + queue.append((tgt, dist + 1)) |
| 164 | + |
| 165 | + return max_distance |
0 commit comments