# Copyright (c) 2026 CLSA contributors # SPDX-License-Identifier: MIT """Shared figure helpers. Every figure reads results/ only.""" from __future__ import annotations import json import sys from pathlib import Path import matplotlib matplotlib.use("Agg") import matplotlib.pyplot as plt # noqa: E402 ROOT = Path(__file__).resolve().parents[1] RESULTS = ROOT / "results" FIGURES = ROOT / "figures" PAPER_FIG = ROOT / "paper" / "figures" for p in (FIGURES, PAPER_FIG): p.mkdir(parents=True, exist_ok=True) PALETTE = { "clsa": "#1f4e79", "language_first": "#b03a2e", "dense_probe": "#7d3c98", "sae_probe": "#117a65", "behaviour_only": "#7f8c8d", "external_verifier": "#ca6f1e", "external_ttc": "#d4ac0d", "uncertainty_only": "#2874a6", "consequence_rules": "#148f77", "abort_only": "#943126", "clsa_no_admission": "#5d6d7e", "clsa_no_dynamics": "#a6acaf", "clsa_naive_acquire": "#d98880", } DEFAULT = "#333333" def load(name: str) -> dict: path = RESULTS / f"{name}.json" if not path.exists(): raise FileNotFoundError(f"missing results file: {path}") with open(path, "r", encoding="utf-8") as fh: return json.load(fh) def try_load(name: str): try: return load(name) except FileNotFoundError: return None def style() -> None: plt.rcParams.update( { "figure.dpi": 140, "savefig.dpi": 300, "font.size": 9, "axes.spines.top": False, "axes.spines.right": False, "axes.grid": True, "grid.alpha": 0.25, "grid.linewidth": 0.5, "legend.frameon": False, "font.family": "DejaVu Sans", } ) def save(fig, name: str, also_paper: bool = True) -> str: out = FIGURES / f"{name}.pdf" fig.tight_layout() fig.savefig(out, bbox_inches="tight") fig.savefig(FIGURES / f"{name}.png", bbox_inches="tight") if also_paper: fig.savefig(PAPER_FIG / f"{name}.pdf", bbox_inches="tight") plt.close(fig) print("wrote", out) return str(out) def policy_label(name: str) -> str: return name.replace("_", " ").replace("clsa", "CLSA") def bootstrap_ci(value: float, ci: dict, scale: float = 1.0) -> tuple[float, float, float]: return ( value * scale, float(ci.get("lo", value)) * scale, float(ci.get("hi", value)) * scale, )