File size: 7,267 Bytes
d1cfb66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
"""Discrete relaxation by prefix-consistency pruning.

This is the retrieval mechanism for the unlabeled-basin-relaxation branch. It is
named honestly: it is ELIMINATION, not graded settling. Each evidence step is a
constraint; instance-walks whose typed canonical signature is not prefix-consistent
with the evidence are pruned. The surviving set is the current active basin.

Why this is "relaxation" and not "lookup":
  - The query is a GROWING evidence walk, fed one step at a time.
  - After each step the active set is recomputed (constraints tighten monotonically).
  - The dynamical quantity we care about is not "did we find the right family" but
    "how much can we perturb the evidence before the active basin changes" (basin
    depth). That is measured by re-relaxing after removing/corrupting steps.

The relaxation supports two signature variants on the same index:
  * typed    : node ids canonicalized, EDGE LABELS (edge_type + direction) kept.
  * labelfree: edge labels collapsed. Tests whether structure alone (phase 8)
               produces basins, or whether typed relations are required.

Index = all instance-walks. Query = concrete evidence walk (a prefix of a held-out
instance, or a perturbed version). Canonicalization strips concrete labels, so a
query matches an instance iff they share canonical structure -- identity-free.
"""

from __future__ import annotations

from collections import Counter
from collections.abc import Sequence
from dataclasses import dataclass, field

from generator import Instance
from signature import label_free_canonical_signature, typed_canonical_signature

VARIANT = ("typed", "labelfree")


def _signature(walk, variant: str):
    if variant == "typed":
        return typed_canonical_signature(walk)
    return label_free_canonical_signature(walk)


def _prefix_view(sig, length: int):
    """Return (node_trace_prefix, edge_label_prefix) truncated to `length` nodes.

    Edge labels align with the TARGET step (see signature.py); step 0 has the
    sentinel label. So node_trace[:length] pairs with edge_label_trace[:length].
    """
    return (sig.node_trace[:length], sig.edge_label_trace[:length])


@dataclass(frozen=True)
class RelaxationState:
    """Snapshot of the active basin after a given evidence length."""
    evidence_length: int
    active_instances: tuple[str, ...]          # instance focal labels
    active_families: tuple[str, ...]           # family of each active instance
    family_counts: tuple[tuple[str, int], ...]  # sorted desc by count
    n_active: int
    n_distinct_families: int

    @property
    def dominant_family(self) -> str | None:
        if not self.family_counts:
            return None
        top = self.family_counts[0][1]
        tied = [f for f, c in self.family_counts if c == top]
        return None if len(tied) > 1 else self.family_counts[0][0]  # None = ambiguous/tied


@dataclass
class RelaxationIndex:
    variant: str
    instances: list[Instance] = field(default_factory=list)
    # cached full signatures
    _sigs: dict[str, object] = field(default_factory=dict)  # focal_label -> TypedSignature

    def add(self, inst: Instance) -> None:
        self.instances.append(inst)
        self._sigs[inst.focal_node()] = _signature(inst.walk, self.variant)

    def add_all(self, insts: Sequence[Instance]) -> None:
        for inst in insts:
            self.add(inst)

    def relax(self, evidence_walk, length: int | None = None) -> RelaxationState:
        """Run discrete relaxation for `length` evidence steps (default: full walk).

        Keeps instances whose canonical signature truncated to `length` matches the
        evidence's canonical signature truncated to `length`.
        """
        if length is None:
            length = len(evidence_walk)
        q_sig = _signature(evidence_walk, self.variant)
        q_node_pref, q_edge_pref = _prefix_view(q_sig, length)
        active: list[Instance] = []
        for inst in self.instances:
            isig = self._sigs[inst.focal_node()]
            i_node_pref, i_edge_pref = _prefix_view(isig, length)
            if i_node_pref == q_node_pref and i_edge_pref == q_edge_pref:
                active.append(inst)
        return self._snapshot(length, active)

    def relax_trajectory(self, evidence_walk) -> list[RelaxationState]:
        """Full relaxation trajectory: active basin after EACH evidence step.

        The narrowing curve. Returns len(walk) states (step 1 .. step N).
        """
        return [self.relax(evidence_walk, length=L) for L in range(1, len(evidence_walk) + 1)]

    # -- perturbation (the dynamical measurement) -----------------------------

    def perturb_drop(self, evidence_walk, drop_index: int) -> RelaxationState:
        """Drop evidence step at `drop_index` (0-based, 0 = root; dropping root is invalid).

        Returns the re-relaxed active basin. Recomputes canonical signature on the
        shortened concrete walk (node ids re-number, which is the point: the basin
        is defined by concrete evidence, not by a fixed index).
        """
        if drop_index == 0:
            raise ValueError("cannot drop the root step (index 0)")
        shortened = [evidence_walk[i] for i in range(len(evidence_walk)) if i != drop_index]
        return self.relax(shortened)

    def basin_stability(self, evidence_walk) -> dict:
        """For each droppable evidence step, does the dominant family survive?

        Compares the full-evidence dominant family against the dominant family
        after removing each single non-root step. A step is 'load-bearing' if
        dropping it changes the dominant family (incl. collapsing to ambiguity).
        """
        full = self.relax(evidence_walk)
        base_family = full.dominant_family
        results = []
        for i in range(1, len(evidence_walk)):
            perturbed = self.perturb_drop(evidence_walk, i)
            survived = (perturbed.dominant_family == base_family) if base_family else False
            results.append({
                "dropped_step": i,
                "dominant_after": perturbed.dominant_family,
                "n_active_after": perturbed.n_active,
                "n_families_after": perturbed.n_distinct_families,
                "survived": survived,
            })
        n_survived = sum(r["survived"] for r in results)
        return {
            "base_family": base_family,
            "n_droppable_steps": len(results),
            "n_survived": n_survived,
            "stability_rate": round(n_survived / len(results), 4) if results else 0.0,
            "per_step": results,
        }

    # -- internals ------------------------------------------------------------

    def _snapshot(self, length: int, active: list[Instance]) -> RelaxationState:
        fam_counts = Counter(i.family for i in active)
        sorted_counts = tuple(sorted(fam_counts.items(), key=lambda kv: (-kv[1], kv[0])))
        return RelaxationState(
            evidence_length=length,
            active_instances=tuple(i.focal_node() for i in active),
            active_families=tuple(i.family for i in active),
            family_counts=sorted_counts,
            n_active=len(active),
            n_distinct_families=len(fam_counts),
        )