File size: 2,968 Bytes
8b9defb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# SPDX-License-Identifier: Apache-2.0
"""Check that the dataset, results, Space, and write-up describe the same run."""

from __future__ import annotations

import hashlib
import json
import re
import sys
from pathlib import Path

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


def sha256(path: Path) -> str:
    h = hashlib.sha256()
    h.update(path.read_bytes())
    return h.hexdigest()


def main() -> int:
    errors = []
    gen = json.loads((ROOT / "dataset" / "generation.json").read_text(encoding="utf-8"))
    results_path = ROOT / "results" / "eval_results.json"
    doc = json.loads(results_path.read_text(encoding="utf-8"))
    if doc["dataset_fingerprint"] != gen["fingerprint"]:
        errors.append("results fingerprint != dataset/generation.json")
    if doc["n_rows"] != gen["counts"]["n"]:
        errors.append("row count mismatch")

    import pyarrow.parquet as pq

    n_public = pq.read_table(ROOT / "dataset" / "data" / "public.parquet").num_rows
    n_held = pq.read_table(ROOT / "dataset" / "data" / "held_out.parquet").num_rows
    if n_public + n_held != doc["n_rows"]:
        errors.append("parquet row count != results")
    card = (ROOT / "dataset" / "README.md").read_text(encoding="utf-8")
    if gen["fingerprint"] not in card:
        errors.append("dataset card missing fingerprint")
    if f"**{doc['n_rows']}**" not in card and f"Rows: **{doc['n_rows']}**" not in card:
        errors.append("dataset card missing row count")

    space_results = ROOT / "space" / "results.json"
    if sha256(space_results) != sha256(results_path):
        # indent may differ; compare parsed lock fields instead
        space_doc = json.loads(space_results.read_text(encoding="utf-8"))
        if space_doc.get("metrics_lock") != doc.get("metrics_lock"):
            errors.append("space/results.json metrics_lock != eval_results.json")
    html = (ROOT / "space" / "index.html").read_text(encoding="utf-8")
    if doc["dataset_fingerprint"] not in html:
        errors.append("space page missing dataset fingerprint")

    writeup = ROOT / "writeup" / "FINDINGS.md"
    if not writeup.exists():
        errors.append("writeup/FINDINGS.md missing")
    else:
        text = writeup.read_text(encoding="utf-8")
        match = re.search(r"<!-- BEGIN METRICS LOCK\n(.*?)\nEND METRICS LOCK -->", text, re.S)
        if not match:
            errors.append("write-up has no metrics lock")
        else:
            locked = json.loads(match.group(1))
            if locked != doc["metrics_lock"]:
                errors.append("write-up metrics lock != results metrics_lock")
        if gen["fingerprint"] not in text:
            errors.append("write-up missing dataset fingerprint")

    if errors:
        print("INCONSISTENT")
        for err in errors:
            print(" -", err)
        return 1
    print("consistent", doc["dataset_fingerprint"][:12], "rows", doc["n_rows"])
    return 0


if __name__ == "__main__":
    sys.exit(main())