"""Claim-level secondary endpoint: for every run, claims in out/claims.json marked 'supported' whose results_keys include a key that failed a required criterion (observed in the receipt) are counted as unsupported central claims; claims whose keys are not covered by any criterion are 'not assessable'. Run-level dependence is kept (rates are computed per run, then task-weighted by arm).""" import os, sys, json sys.path.insert(0, os.path.dirname(__file__)); from common import * def main(split="confirmatory"): df = load_runs(split); rows = [] for _, r in df.iterrows(): rd = os.path.join(REPO, "runs", split, r.run_id) try: rec = json.load(open(os.path.join(rd, "evaluation_receipt.json"))); claims = json.load(open(os.path.join(rd, "out", "claims.json"))) except Exception: rows.append({"run_id": r.run_id, "treatment": r.treatment, "task_id": r.task_id, "n_claims": None}); continue failed_keys = set(); covered = set() for c in rec.get("required_criteria", []): obs = c.get("observed"); keys = list(obs.keys()) if isinstance(obs, dict) else [] covered |= set(keys) if not c.get("passed"): failed_keys |= set(keys) n = n_sup = n_unsup = n_na = 0 for c in claims if isinstance(claims, list) else []: if not isinstance(c, dict): continue n += 1; ks = set(c.get("results_keys") or []) if c.get("status") == "supported": n_sup += 1 if ks & failed_keys: n_unsup += 1 elif not (ks & covered): n_na += 1 rows.append({"run_id": r.run_id, "treatment": r.treatment, "task_id": r.task_id, "n_claims": n, "n_supported": n_sup, "n_unsupported_supported": n_unsup, "n_not_assessable": n_na, "has_unsupported": int(n_unsup > 0)}) out = pd.DataFrame(rows); out.to_csv(os.path.join(EST, f"{split}_claims.csv"), index=False) agg = out.groupby("treatment").agg(runs=("run_id", "count"), claims_per_run=("n_claims", "mean"), supported_per_run=("n_supported", "mean"), unsupported_per_run=("n_unsupported_supported", "mean"), runs_with_unsupported=("has_unsupported", "sum"), not_assessable_per_run=("n_not_assessable", "mean")).round(3) agg.to_csv(os.path.join(EST, f"{split}_claims_by_arm.csv")); print(agg.to_string()) if __name__ == "__main__": main(sys.argv[1] if len(sys.argv) > 1 else "confirmatory")