File size: 3,210 Bytes
feb08d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import glob, json, numpy as np, os
from pprint import pprint

def load_latest(pattern):
    files = sorted(glob.glob(pattern))
    return files[-1] if files else None

logs_path = load_latest("experiments/*_logs.json")
phi_path = load_latest("experiments/*_phi_best.npy")
res_path = load_latest("experiments/*_result.json")

print("logs:", logs_path)
print("phi_best:", phi_path)
print("result:", res_path)

if not logs_path:
    raise SystemExit("No logs file found")

logs = json.load(open(logs_path))
res = json.load(open(res_path)) if res_path else {}

# coerce gate values to booleans for all depth entries
def coerce_gates(g):
    if not isinstance(g, dict):
        return g
    out = {}
    for k,v in g.items():
        if isinstance(v, str):
            lv = v.strip().lower()
            if lv in ("true","1","yes"):
                out[k] = True
            elif lv in ("false","0","no"):
                out[k] = False
            else:
                try:
                    out[k] = bool(int(v))
                except Exception:
                    out[k] = v
        else:
            out[k] = v
    return out

for depth_idx, depth in enumerate(logs):
    for entry in depth:
        if 'gates' in entry:
            entry['gates'] = coerce_gates(entry['gates'])

# attach phi_best into the first accepted entry (if not present)
accepted_entry = None
for entry in logs[0]:
    if entry.get('accepted'):
        accepted_entry = entry
        break

phi = np.load(phi_path) if phi_path else None
if accepted_entry is not None:
    if 'candidate_array' not in accepted_entry:
        accepted_entry['candidate_array'] = phi.tolist() if phi is not None else None

# Corrected target from real ARC task 007bbfb7 (Kronecker self-similar)
TARGET_GRID = [
  [0,0,0,0,7,7,0,7,7],
  [0,0,0,7,7,7,7,7,7],
  [0,0,0,0,7,7,0,7,7],
  [0,7,7,0,7,7,0,7,7],
  [7,7,7,7,7,7,7,7,7],
  [0,7,7,0,7,7,0,7,7],
  [0,0,0,0,7,7,0,7,7],
  [0,0,0,7,7,7,7,7,7],
  [0,0,0,0,7,7,0,7,7],
]
TARGET = np.array(TARGET_GRID, dtype=int)

def tile_transform(phi, out_shape):
    a = np.array(phi)
    h_out, w_out = out_shape
    h_in, w_in = a.shape
    reps_h = (h_out + h_in - 1) // h_in
    reps_w = (w_out + w_in - 1) // w_in
    tiled = np.tile(a, (reps_h, reps_w))
    return tiled[:h_out, :w_out]

if accepted_entry is not None and accepted_entry.get('candidate_array') is not None:
    cand = np.array(accepted_entry['candidate_array'], dtype=float)
    if cand.shape != TARGET.shape:
        cand_resized = tile_transform(cand, TARGET.shape)
    else:
        cand_resized = cand
    cand_q = np.rint(cand_resized).astype(int)
    l1 = float(np.sum(np.abs(cand_q - TARGET)))
    print("Recomputed L1 residue for first accepted candidate:", l1)
    print("Candidate unique values:", np.unique(cand_q))
    diff = (cand_q != TARGET).astype(int)
    print("Changed cells count:", int(diff.sum()))
    print("Diff map (1=diff):")
    print(diff)
else:
    print("No candidate array available in logs or phi_best missing.")

# write fixed logs copy
fixed_path = logs_path.replace("_logs.json", "_logs.fixed.json")
with open(fixed_path, "w") as fh:
    json.dump(logs, fh, indent=2)
print("Wrote fixed logs to", fixed_path)