#!/usr/bin/env python3
"""Recompute the prior-work numbers the bounty text quotes, straight from the scored runs.

The bounty description is immutable once it is on chain, so every figure in it has to
come out of scores_{clean,web,hard}.json rather than out of my memory of them. This
writes priors.json; check_bounty.py then greps the description for each one.

One operating point throughout: 0.65, the threshold the extension's own manifest says
it is calibrated to. Quoting a count from one threshold and a worst case from another
is exactly the mistake that cost me a public correction once already.

    python3 priors.py
"""
import collections, json, os

SRC = "/home/agent/work/sieve-test"
MANIFEST = "/tmp/sieve/extension/model_manifest.json"
THRESH = 0.65

man = json.load(open(MANIFEST))
assert str(THRESH) in man["notes"], "manifest no longer names 0.65 as the shipped threshold"

by_source_fp, by_source_n, conditions = collections.Counter(), collections.Counter(), {}
n_real = n_fp = 0
for cond in ["clean", "web", "hard"]:
    rows = [r for r in json.load(open(f"{SRC}/scores_{cond}.json"))
            if r["label"] == 0 and "score" in r]
    fp = [r for r in rows if r["score"] >= THRESH]
    conditions[cond] = {"real": len(rows), "flagged": len(fp)}
    n_real += len(rows)
    n_fp += len(fp)
    for r in rows:
        by_source_n[r["source"]] += 1
    for r in fp:
        by_source_fp[r["source"]] += 1

out = {
    "threshold": THRESH,
    "model_sha256": man["sha256"],
    "model_version": man["version"],
    "n_real": n_real,
    "n_flagged": n_fp,
    "conditions": conditions,
    # sources are pooled across the three conditions, so the denominator is 3x the set size
    "by_source": {s: {"flagged": by_source_fp.get(s, 0), "n": by_source_n[s]}
                  for s in sorted(by_source_n, key=lambda s: (-by_source_fp.get(s, 0), s))},
    "zero_flag_sources": sorted(s for s in by_source_n if not by_source_fp.get(s)),
}
json.dump(out, open(os.path.join(os.path.dirname(os.path.abspath(__file__)), "priors.json"), "w"),
          indent=1)
print(f"{n_fp}/{n_real} real photographs flagged at {THRESH}")
for s, v in list(out["by_source"].items())[:6]:
    print(f"  {s:<26} {v['flagged']}/{v['n']}")
