"""Recompute the 001C teacher-forced NLL analysis from its retained raw archive.""" from __future__ import annotations import argparse import gzip import json from pathlib import Path from experiment_001c_gate_pairing import analyze_records def _find_root() -> Path: source = Path(__file__).resolve() for candidate in (source.parents[1], source.parents[2]): if (candidate / "experiments").is_dir() and (candidate / "results/experiment_001c_gate_pairing_v1").is_dir(): return candidate raise RuntimeError("could not locate the research archive root") ROOT = _find_root() RAW_PATH = ROOT / "results/experiment_001c_gate_pairing_v1/raw_results.json.gz" SAVED_ANALYSIS_PATH = ROOT / "results/experiment_001c_gate_pairing_v1/analysis.json" def main() -> None: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--raw", type=Path, default=RAW_PATH, help="Compressed raw record archive.") parser.add_argument( "--output", type=Path, default=ROOT / "results/recomputed_analysis/001c_analysis.json", help="Derived JSON output path; does not overwrite the retained analysis.", ) args = parser.parse_args() raw_path = args.raw if args.raw.is_absolute() else ROOT / args.raw output_path = args.output if args.output.is_absolute() else ROOT / args.output with gzip.open(raw_path, "rt", encoding="utf-8") as handle: archive = json.load(handle) result = analyze_records(archive["records"]) manifest = archive.get("manifest", {}) result.update( { "manifest_sha256": archive.get("manifest_sha256"), "source_sha256": manifest.get("source_sha256"), "calibration_artifact_sha256": manifest.get("calibration_artifact_sha256"), } ) saved = json.loads(SAVED_ANALYSIS_PATH.read_text(encoding="utf-8")) if SAVED_ANALYSIS_PATH.exists() else archive.get("analysis", {}) checks = { "mean_question_weighted_difference": abs(result["mean_question_weighted_difference"] - saved["mean_question_weighted_difference"]) < 1e-12, "question_cluster_bootstrap_90": all(abs(a - b) < 1e-12 for a, b in zip(result["question_cluster_bootstrap_90"], saved["question_cluster_bootstrap_90"])), "question_cluster_bootstrap_95": all(abs(a - b) < 1e-12 for a, b in zip(result["question_cluster_bootstrap_95"], saved["question_cluster_bootstrap_95"])), "question_and_token_counts": result["n_question_clusters"] == saved["n_question_clusters"] and result["n_eligible_answer_tokens"] == saved["n_eligible_answer_tokens"], } result["analysis_only_reproduction_matches_saved_aggregates"] = all(checks.values()) result["reproduction_checks"] = checks output_path.parent.mkdir(parents=True, exist_ok=True) output_path.write_text(json.dumps(result, ensure_ascii=False, indent=2) + "\n", encoding="utf-8") print( json.dumps( { "output": str(output_path), "matches_saved_aggregates": result["analysis_only_reproduction_matches_saved_aggregates"], "n_question_clusters": result["n_question_clusters"], "n_eligible_answer_tokens": result["n_eligible_answer_tokens"], "mean_shuffled_minus_dynamic_nats_per_token": result["mean_question_weighted_difference"], "ci95": result["question_cluster_bootstrap_95"], "ci90": result["question_cluster_bootstrap_90"], "checks": checks, }, indent=2, ) ) if __name__ == "__main__": main()