File size: 3,977 Bytes
e1ced61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Reproduce all 18 cells and paired intervals from 72,000 saved scoring records."""

from __future__ import annotations

import argparse
import json
import subprocess
import sys
from pathlib import Path

from analyze_crossdomain_paper import CROSS_KEYS, MODELS, analyze
from hf_release_common import read_json, read_rows, write_json
from score_hf_dataset import score


def replay(dataset, output):
    output.mkdir(parents=True, exist_ok=True)
    # Histogram keys are integers in memory and strings in the saved JSON.
    # Compare the same serialized representation used by the original replay.
    result = json.loads(
        json.dumps(analyze(dataset / "inputs", dataset / "expected"), allow_nan=False)
    )
    expected = read_json(dataset / "expected/revision-analysis.json")
    if result != expected:
        differing = [key for key in result if result[key] != expected.get(key)]
        raise ValueError("Numerical replay differs from frozen analysis: " + ", ".join(differing))
    labels = read_rows(dataset / "metadata/views.jsonl.gz")
    for model in MODELS:
        predictions = read_rows(dataset / f"inputs/plotqa/{model}.jsonl.gz") + read_rows(
            dataset / f"inputs/crossdomain/{CROSS_KEYS[model]}.jsonl.gz"
        )
        scored = score(labels, predictions)
        for source, values in scored["by_source"].items():
            archived = result["domains"][source]["models"][model]
            if values["counts"] != archived["counts"] or values["states"] != archived["states"]:
                raise ValueError(
                    "Public prediction scorer disagrees with paper: " + source + "/" + model
                )
        write_json(output / (model + "-score.json"), scored)
    from build_answerability_rewrite_assets import build as build_chart

    result_path = output / "analysis.json"
    write_json(result_path, result)
    subprocess.run(
        [
            sys.executable,
            str(dataset / "scripts/build_crossdomain_paper_assets.py"),
            "--analysis",
            str(result_path),
            "--output",
            str(output / "tables"),
        ],
        check=True,
    )
    build_chart(
        dataset / "inputs",
        result_path,
        output / "charts",
        dataset / "evidence/plotqa/bounded-witness-proofs.jsonl.gz",
    )
    from collections import Counter

    finite = read_rows(dataset / "evidence/finite/original-results.jsonl.gz")
    solver = read_rows(dataset / "evidence/finite/solver-results.jsonl.gz")
    if (
        len(finite) != 1024
        or len(solver) != 1456
        or Counter(row["truth"] for row in solver) != {"ambiguous": 822, "constant": 634}
    ):
        raise ValueError("Finite study counts differ")
    if not all(row["enumeration_status"] == row["smt_status"] == row["truth"] for row in solver):
        raise ValueError("Archived solver decisions disagree")
    report = {
        "status": "passed",
        "saved_records": 72000,
        "model_domain_cells": 18,
        "exact_analysis_replay": True,
        "bootstrap_draws": 10000,
        "new_prediction_scorer_matches_all_18_cells": True,
        "finite_cases": len(finite),
        "solver_cases": len(solver),
        "solver_constructors_rerun": False,
        "inference_calls": 0,
        "gqa_question_text": "ID references; original text verified separately during reconstruction",
    }
    write_json(output / "replay-validation.json", report)
    return report


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--dataset", type=Path, default=Path(__file__).resolve().parents[1])
    parser.add_argument("--output", type=Path, required=True)
    args = parser.parse_args()
    if args.output.resolve().is_relative_to(args.dataset.resolve()):
        parser.error("Write replay outputs outside the frozen dataset directory")
    print(json.dumps(replay(args.dataset, args.output)))