File size: 4,795 Bytes
66f9450
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Invariancia interna no trivial (con control negativo).

Mide autonomía *y* distingue el grafo canónico de uno corrompido:

1. Auto-regeneración: etiqueta ≡ regla (fase, pos) aplicada al par.
2. Invariancia bajo índice de tríada.
3. Reconstrucción desde subconjunto (LOO feature lookup sin etiquetas del held-out).
4. **Control negativo:** al corromper una arista del grafo, (1) o (3) debe fallar.

Sin el control negativo, un 1.0 podría ser tautología.
"""
from __future__ import annotations

import copy
import csv
import json
from collections import defaultdict
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]


def parse_node(node_id: str) -> tuple[str, int]:
    parts = node_id.split("_")
    return parts[1], int(parts[2][1:])


def rule_label(src: str, tgt: str) -> str:
    sp, spos = parse_node(src)
    tp, tpos = parse_node(tgt)
    if sp == tp:
        return "none"
    if tpos == 2 - spos:
        return "mirrorOf"
    if (spos in (0, 2) and tpos == 1) or (spos == 1 and tpos in (0, 2)):
        return "creates"
    return "none"


def feature_key(src: str, tgt: str) -> tuple:
    sp, spos = parse_node(src)
    tp, tpos = parse_node(tgt)
    return (sp, spos, tp, tpos)


def self_regen_rate(rows: list[dict]) -> float:
    mism = sum(1 for r in rows if rule_label(r["source"], r["target"]) != r["relation"])
    return 1.0 - mism / max(len(rows), 1)


def triad_invariant(rows: list[dict]) -> bool:
    by_feat: dict[tuple, set[str]] = defaultdict(set)
    for r in rows:
        by_feat[feature_key(r["source"], r["target"])].add(r["relation"])
    return all(len(v) == 1 for v in by_feat.values())


def subset_reconstruction(rows: list[dict]) -> bool:
    for held in range(1, 14):
        train: dict[tuple, str] = {}
        test: list[tuple[tuple, str]] = []
        for r in rows:
            key = feature_key(r["source"], r["target"])
            tid = int(r["triad_index"])
            if tid == held:
                test.append((key, r["relation"]))
            else:
                train[key] = r["relation"]
        if any(train.get(k) != lab for k, lab in test):
            return False
    return True


def corrupt_one_edge(rows: list[dict]) -> list[dict]:
    """Cambia la etiqueta de la primera arista mirrorOf a none (grafo inválido)."""
    out = copy.deepcopy(rows)
    for r in out:
        if r["relation"] == "mirrorOf":
            r["relation"] = "none"
            break
    return out


def main() -> int:
    rows = list(
        csv.DictReader(
            (ROOT / "datasets" / "relation_candidates.csv").open(
                encoding="utf-8", newline=""
            )
        )
    )

    regen = self_regen_rate(rows)
    inv = triad_invariant(rows)
    subset = subset_reconstruction(rows)

    corrupted = corrupt_one_edge(rows)
    # Tras corrupción: o bien auto-regeneración cae, o el grafo ya no coincide con la regla
    corrupted_regen = self_regen_rate(corrupted)
    negative_control_detects = corrupted_regen < 1.0

    autonomy = regen == 1.0 and inv and subset
    non_trivial = autonomy and negative_control_detects

    report = {
        "framing": (
            "Invariancia no trivial: el canónico regenera 1.0; un grafo con una "
            "arista corrupta debe fallar el control negativo."
        ),
        "canonical": {
            "self_regeneration_rate": round(regen, 6),
            "triad_index_invariance": inv,
            "subset_reconstruction": subset,
            "autonomy_measurable": autonomy,
        },
        "negative_control": {
            "description": "primera arista mirrorOf relabelada a none",
            "corrupted_self_regeneration_rate": round(corrupted_regen, 6),
            "detects_corruption": negative_control_detects,
            "interpretation": (
                "si detects_corruption es false, la métrica no distingue "
                "grafo correcto de corrompido (tautología)"
            ),
        },
        "invariance_non_trivial": non_trivial,
    }

    out = ROOT / "experiments" / "results" / "invariance_metrics.json"
    out.parent.mkdir(parents=True, exist_ok=True)
    out.write_text(json.dumps(report, ensure_ascii=False, indent=2, sort_keys=True) + "\n", encoding="utf-8")

    print("Invariancia interna (con control negativo)")
    print(f"  Auto-regeneración canónica: {regen}")
    print(f"  Invariancia bajo índice de tríada: {inv}")
    print(f"  Reconstrucción desde subconjunto: {subset}")
    print(f"  Control negativo detecta corrupción: {negative_control_detects} (rate={corrupted_regen:.6f})")
    print(f"  Invariancia no trivial: {non_trivial}")
    print(f"  Informe: {out}")
    return 0 if non_trivial else 1


if __name__ == "__main__":
    raise SystemExit(main())