"""The scorer: every benchmark number, computed from the artifacts alone. python -m scoring # from the vybbench1.0/ directory Reads ``data/`` (the cases), ``starts/`` (each edit's shared starting design), ``outputs/`` (every model's generations), and ``verdicts/`` (the recorded judge verdicts), and prints the three score tables. No model APIs and no keys: the one non-deterministic step, the edit judge, ships as recorded verdicts. The benchmark runner scores with these same functions, so the published scorer is the code that produced the numbers. """ from __future__ import annotations import json import statistics from concurrent.futures import ProcessPoolExecutor from functools import partial from pathlib import Path from PIL import Image from . import colors, edits, fonts from .cases import color_cases, edit_cases, font_cases ROOT = Path(__file__).resolve().parents[1] OUTPUTS = ROOT / "outputs" VERDICTS = ROOT / "verdicts" ARMS = ("ours", "gpt", "muse", "recraft") def slug(case_id: str) -> str: return case_id.replace("/", "_").replace("#", "").lower() def _attempt_paths(arm: str, subtest: str, case_id: str) -> list[Path]: directory = OUTPUTS / arm / subtest / slug(case_id) return sorted(directory.glob("a*.png")) if directory.is_dir() else [] def _percentiles(values: list[float], points: tuple[int, ...]) -> dict: if not values: return {} ranked = sorted(values) out = {"mean": statistics.fmean(ranked), "median": statistics.median(ranked)} for p in points: out[f"p{p}"] = ranked[min(len(ranked) - 1, int(round(p / 100 * (len(ranked) - 1))))] return out def _font_case_ious(case, arms: tuple[str, ...]) -> dict: """One case's per-arm attempt IoUs; alignment search is CPU-heavy, so score_fonts fans cases out over processes.""" reference = Image.open(ROOT / "references" / f"{slug(case.case_id)}.png") components = fonts.max_ink_components(case.word) out = {} for arm in arms: ious = [ 1.0 - result.mismatch_fraction for path in _attempt_paths(arm, "fonts", case.case_id) if (result := fonts.font_mismatch( reference, Image.open(path), components)).valid ] if ious: out[arm] = ious return out def score_fonts(arms: tuple[str, ...] = ARMS) -> dict: """Per-arm IoU distribution over every scored attempt, plus the recorded measurement noise floor over the same word set.""" cases = font_cases() with ProcessPoolExecutor() as pool: case_scores = list(pool.map(partial(_font_case_ious, arms=arms), cases)) per_arm = { arm: [iou for scores in case_scores for iou in scores.get(arm, [])] for arm in arms } summary = {arm: _percentiles(vals, (20, 10, 5)) for arm, vals in per_arm.items()} summary["noise_floor"] = _percentiles( [1.0 - case.noise_floor for case in cases], (20, 10, 5), ) return summary def score_colors(arms: tuple[str, ...] = ARMS) -> dict: """Per-arm max-channel error distribution over every scored attempt.""" per_arm: dict = {arm: [] for arm in arms} for case in color_cases(): for arm in arms: per_arm[arm] += [ float(result.channel_error) for path in _attempt_paths(arm, "colors", case.case_id) if (result := colors.color_error(Image.open(path), case.hex)).valid ] return {arm: _percentiles(vals, (80, 90, 95)) for arm, vals in per_arm.items()} def score_edits(arms: tuple[str, ...] = ARMS) -> dict: """Two numbers per arm, each over every generation: the whole-image change, and the rate at which the judge accepted the requested change as made.""" change: dict = {arm: [] for arm in arms} attempts: dict = {arm: [] for arm in arms} for case in edit_cases(): start = Image.open(ROOT / case.start_png) for arm in arms: change[arm] += [ edits.whole_change(start, Image.open(path)) for path in _attempt_paths(arm, "edits", case.design_id) ] attempts[arm] += [ json.loads(path.read_text())["satisfied"] for path in sorted( VERDICTS.glob(f"{slug(case.design_id)}_{arm}_a*.json") ) ] summary = {} for arm in arms: summary[arm] = { **_percentiles(change[arm], (80, 90, 95)), "satisfied_rate": ( statistics.fmean(attempts[arm]) if attempts[arm] else None ), } return summary def print_summary(label: str, summary: dict) -> None: print(f"\n=== {label} ===") for arm, stats in summary.items(): line = " ".join( f"{key}={value:.4f}" if isinstance(value, float) else f"{key}={value}" for key, value in stats.items() ) print(f" {arm:12s} {line}") def main() -> None: print_summary("fonts (IoU, higher is better)", score_fonts()) print_summary("colors (max channel error, lower is better)", score_colors()) print_summary("edits (fraction changed, lower is better)", score_edits()) if __name__ == "__main__": main()