yang
This project has not described itself yet.
Brought by drakestapleton
yang / scripts / select_block_kernel.py
| 1 | """Score the prespecified blocking-kernel search and its matched validation seeds.""" |
| 2 | import argparse |
| 3 | from dataclasses import dataclass |
| 4 | from math import isfinite, sqrt, tanh |
| 5 | from pathlib import Path |
| 6 | from statistics import fmean, stdev |
| 7 | |
| 8 | |
| 9 | @dataclass(frozen=True) |
| 10 | class Score: |
| 11 | total: float |
| 12 | rectangle: float |
| 13 | crown: float |
| 14 | |
| 15 | |
| 16 | def read_scores(root): |
| 17 | scores = {} |
| 18 | fine_rows = {} |
| 19 | for path in sorted(root.glob("batch.*/quarter-*-seed-*.tsv")): |
| 20 | fields = path.stem.split("-") |
| 21 | quarter, seed = int(fields[1]), int(fields[3]) |
| 22 | text = path.read_text().splitlines() |
| 23 | if text[-1:] != ["CHECK10_GB10_DONE status=PASS"]: |
| 24 | raise ValueError(f"incomplete run: {path}") |
| 25 | rows = [r.split() for r in text if r.startswith("32\t")] |
| 26 | if [int(r[2]) for r in rows] != list(range(4)) or any(r[1] != "2.300000" for r in rows): |
| 27 | raise ValueError(f"missing scales: {path}") |
| 28 | for r in rows: |
| 29 | f, a, rect, square, crown = (abs(float(x)) for x in r[4:9]) |
| 30 | total = float(r[9]) |
| 31 | reconstructed = 9*f + 18*tanh(2*a) + 45*rect + 42*square + 60*crown |
| 32 | if not isfinite(total) or not abs(total - reconstructed) <= 0.000097: |
| 33 | raise ValueError(f"inconsistent score: {path}") |
| 34 | if fine_rows.setdefault(seed, rows[0][3:10]) != rows[0][3:10]: |
| 35 | raise ValueError(f"different fine-field fit within matched seed {seed}") |
| 36 | stats = [r.split() for r in text if r.startswith("projection_min_sq_and_fallbacks")] |
| 37 | if len(stats) != 1 or int(stats[0][2]) != 0 or not float(stats[0][1]) > 0: |
| 38 | raise ValueError(f"projection failure: {path}") |
| 39 | if (seed, quarter) in scores: |
| 40 | raise ValueError(f"duplicate seed and weight: {path}") |
| 41 | scores[seed, quarter] = Score(total, 45*rect, 60*crown) |
| 42 | return scores |
| 43 | |
| 44 | |
| 45 | def se(values): |
| 46 | return stdev(values) / sqrt(len(values)) |
| 47 | |
| 48 | |
| 49 | def main(): |
| 50 | parser = argparse.ArgumentParser(description=__doc__) |
| 51 | parser.add_argument("root", type=Path) |
| 52 | parser.add_argument("--winner", action="store_true") |
| 53 | args = parser.parse_args() |
| 54 | scores = read_scores(args.root) |
| 55 | train = {w: [scores[s, w].total for s in (101, 102, 103)] for w in range(4)} |
| 56 | winner = min(train, key=lambda w: (fmean(train[w]), w)) |
| 57 | if args.winner: |
| 58 | print(winner) |
| 59 | return |
| 60 | |
| 61 | print("# train: quarter units, mean alpha, independent-seed SE") |
| 62 | for w, values in train.items(): |
| 63 | print("train", w, f"{fmean(values):.6f}", f"{se(values):.6f}", sep="\t") |
| 64 | print("winner_quarters", winner, sep="\t") |
| 65 | test = range(201, 207) |
| 66 | expected = {(s, w) for s in (101, 102, 103) for w in range(4)} |
| 67 | expected.update((s, w) for s in test for w in (winner, 2)) |
| 68 | if set(scores) != expected: |
| 69 | raise ValueError("held-out records differ from the prespecified design") |
| 70 | print("# heldout: series, mean, SE, minimum, maximum") |
| 71 | for field in ("total", "rectangle", "crown"): |
| 72 | base = [getattr(scores[s, 2], field) for s in test] |
| 73 | chosen = [getattr(scores[s, winner], field) for s in test] |
| 74 | changes = [a-b for a, b in zip(chosen, base)] |
| 75 | for name, values in (("baseline", base), ("winner", chosen), ("paired_change", changes)): |
| 76 | print("heldout", f"{field}_{name}", |
| 77 | *(f"{v:.6f}" for v in (fmean(values), se(values), min(values), max(values))), |
| 78 | sep="\t") |
| 79 | print("heldout_winner_below_one", sum(scores[s, winner].total < 1 for s in test), len(test), sep="\t") |
| 80 | |
| 81 | |
| 82 | if __name__ == "__main__": |
| 83 | main() |