| 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 {} |
|
|
| |
| 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']) |
|
|
| |
| 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 |
|
|
| |
| 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.") |
|
|
| |
| 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) |
|
|