"""charts.json from results.json (section D spec)."""
import json

import ws4lib as L

R = json.loads((L.WS4 / "results.json").read_text(encoding="utf-8"))
ARM = {"a": "(a) whole dossier, 1 call", "b": "(b) one section per call", "c": "(c) one claim per call"}


def pct(x):
    return None if x is None else round(100 * x, 1)


charts = []
dvp = R.get("dvp")
q43 = R["q43_clean_incumbent"]
if dvp:
    n_p = dvp["arms"]["a"]["n"]
    # chart 1: catch vs unneeded flags per arm, two series on one % axis
    s_catch = {"name": "Planted defects caught (%)", "marker": "circle", "dash": "solid",
               "values": [{"x": ARM[a], "y": pct(dvp["arms"][a]["share_ci"][0]),
                           "ci95": [pct(dvp["arms"][a]["share_ci"][1]), pct(dvp["arms"][a]["share_ci"][2])],
                           "n": dvp["arms"][a]["n"]} for a in "abc"]}
    s_clean = {"name": "Clean claims flagged (%)", "marker": "square", "dash": "dot",
               "values": [{"x": ARM[a], "y": pct(q43["arms"][a]["share_ci"][0]),
                           "ci95": [pct(q43["arms"][a]["share_ci"][1]), pct(q43["arms"][a]["share_ci"][2])],
                           "n": q43["arms"][a]["n"]} for a in "abc"]}
    charts.append({"title": "Judging one claim per call caught 23% of planted defects against 0% for the one-call check, but it also failed 18% of clean claims", "type": "grouped_bar_with_ci", "x_title": "Validation arm",
                   "y_title": "Share of claims (%)", "series": [s_catch, s_clean],
                   "n_claims": {"planted_defects": n_p, "clean_claims": q43["arms"]["a"]["n"]},
                   "n_recorded_fails": n_p, "split": "dev-side only: 23 ys-glm-bench vendors (2 held-out vendors excluded); "
                   "planted defects on incumbent drafts; clean claims on unmodified incumbent drafts",
                   "direction": "higher is better for defects caught; lower is safer for clean claims flagged",
                   "note": "(c) counts a claim as caught when its call's verdict is not pass; (a),(b) need the "
                           "flagged text to point at the claim (PREREG match rule). Conservative 2-repeat rule."})
    # chart 2: same but flag-matching rule for all arms (sensitivity)
    s2 = {"name": "Planted defects caught (%)", "marker": "circle", "dash": "solid",
          "values": [{"x": ARM[a] + (" [flag match]" if a == "c" else ""), "y": pct(v["share_ci"][0]),
                      "ci95": [pct(v["share_ci"][1]), pct(v["share_ci"][2])], "n": v["n"]}
                     for a, v in (("a", dvp["arms"]["a"]), ("b", dvp["arms"]["b"]),
                                  ("c", dvp["arms_sensitivity"]["c"]))]}
    s2c = {"name": "Clean claims flagged (%)", "marker": "square", "dash": "dot",
           "values": [{"x": ARM[a] + (" [flag match]" if a == "c" else ""), "y": pct(v["share_ci"][0]),
                       "ci95": [pct(v["share_ci"][1]), pct(v["share_ci"][2])], "n": v["n"]}
                      for a, v in (("a", q43["arms"]["a"]), ("b", q43["arms"]["b"]),
                                   ("c", q43["arms_sensitivity"]["c"]))]}
    charts.append({"title": "Counting only flags that point at the defective claim, no arm caught more than 1 in 10 planted defects", "type": "grouped_bar_with_ci", "x_title": "Validation arm (all arms scored by "
                   "whether the flagged text points at the claim)", "y_title": "Share of claims (%)",
                   "series": [s2, s2c], "n_claims": {"planted_defects": n_p, "clean_claims": q43["arms"]["a"]["n"]},
                   "n_recorded_fails": n_p, "split": charts[0]["split"],
                   "direction": charts[0]["direction"]})
cost = R["cost"]
s3 = {"name": "Cost per dossier (US cents, 2 repeats)", "pattern": "solid", "marker": "none",
      "values": [{"x": ARM[a], "y": round(100 * cost[f"incumbent/{a}"]["cost_per_dossier_usd"], 3)} for a in "abc"
                 if f"incumbent/{a}" in cost]}
charts.append({"title": "Judging one claim per call cost about 22 times more per dossier than the one-call check", "type": "bar", "x_title": "Validation arm",
               "y_title": "Cost per dossier (US cents, both repeats)", "series": [s3],
               "n_claims": sum(R["unit_counts"]["incumbent"].values()),
               "n_recorded_fails": R["registered_primary"]["arms"]["a"]["n"],
               "split": "23 incumbent drafts (ys-glm-bench, 2 held-out vendors excluded)",
               "direction": "lower is better"})
ba = R.get("dvp_before_after")
if ba:
    charts.append({"title": "Planting a defect turned a passing per-claim verdict into a fail for only 3 of 31 claims",
                   "type": "bar", "x_title": "Per-claim verdict on the same claim: before planting -> after planting",
                   "y_title": "Planted claims (count)",
                   "series": [{"name": "Planted claims", "pattern": "solid", "marker": "none",
                               "values": [{"x": k, "y": v} for k, v in ba["counts"].items()]}],
                   "n_claims": ba["n"], "n_recorded_fails": ba["n"],
                   "split": "31 ground.py-confirmed planted defects in 22 of the 23 incumbent drafts",
                   "direction": "higher is better for pass -> fail; the other bars are background"})
(L.WS4 / "charts.json").write_text(json.dumps(charts, indent=1), encoding="utf-8")
print(len(charts), "charts")
