"""Verify a data snapshot and reproduce the held-out Q95s using only Python 3.11+.""" import csv import hashlib import io import math from pathlib import Path import zipfile def main(): here = Path(__file__).resolve().parent for line in (here / "SHA256SUMS").read_text(encoding="utf-8").splitlines(): digest, name = line.split(" ", 1) with (here/name).open("rb") as stream: if hashlib.file_digest(stream,"sha256").hexdigest() != digest: raise ValueError(name) with (here/"FILES.csv").open(encoding="utf-8",newline="") as stream: files = list(csv.DictReader(stream)) for name in sorted({r["archive"] for r in files}): with zipfile.ZipFile(here/name) as z: rows = [r for r in files if r["archive"] == name] if sorted(z.namelist()) != sorted(r["path"] for r in rows): raise ValueError(f"Invalid archive contents: {name}") for r in rows: with z.open(r["path"]) as stream: if hashlib.file_digest(stream,"sha256").hexdigest() != r["sha256"]: raise ValueError(r["path"]) def quantile(values): values = sorted(values) pos = (len(values)-1)*.95 lo = int(pos) return values[lo] + (pos-lo)*(values[math.ceil(pos)]-values[lo]) with zipfile.ZipFile(here/"shell-benchmark.zip") as z: def read(name): with z.open("results/round4_shell/endspan/"+name+".csv") as stream: return list(csv.DictReader(io.TextIOWrapper(stream,encoding="utf-8",newline=""))) pairs, tests = read("paired"), read("test") for row in tests: draws = [r for r in pairs if r["model"]==row["model"] and r["depth_m"]==row["depth_m"] and int(r["draw"])>0] if sorted(int(r["draw"]) for r in draws) != list(range(1,257)): raise ValueError("Incomplete paired observations") for raw, summary in [("worst_pct","q95_pct"),("worst_vector_rmse_nT","q95_vector_rmse_nT")]: if not math.isclose(quantile(float(r[raw]) for r in draws),float(row[summary]),rel_tol=1e-12): raise ValueError((row["model"], row["depth_m"], summary)) print(f"Verified {len(files)} data files; reproduced {len(tests)*2} held-out Q95 values.") if __name__ == "__main__": main()