File size: 2,431 Bytes
fd5366a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# 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,
    )