"""Applies the leader selection rule of amendment 3.4 E2 to the current validation scores and writes a JSON file (it never writes leaders.json or selection.json). usage: python -I select_leaders.py --scores DATA/outputs/r5/scores --preds DATA/outputs/r5/preds --out DATA/outputs/r5/leaders_selection.json [--arms-info DATA/outputs/r5/arms_info.json] [--models-tsv a.tsv,b.tsv] [--vlm-arms x,y] [--checkpoint short] [--B 10000] Rule (V1 CER, plain greedy figure, at the P_short checkpoint 'short'; strict order, the arm name breaks an exact tie): (a) the two arms with the lowest V1 CER are the leaders (two seeds each); (b) one extra arm: the lowest V1 CER among arms whose size class (below 0.5B, 0.5B to 1.5B, above 1.5B) contains neither leader (one seed); (c) one extra arm: the next lowest arm not yet chosen, in any class (one seed). Only arms that reached the full P_short of 150,000 samples are eligible: an arms_info.json flag or a meta.json train_flag that says 'short of P', or samples_seen below 150,000, excludes an arm. The output lists, for each arm, V1 CER with its 95% form-bootstrap interval, size class, samples reached and the reason for any exclusion, the paired intervals of V1 CER differences among the six lowest arms (clarification H14), the arms that are not scored or not finished (the pick is then a recorded deviation), and the arms outside the VLM allow-list that were not considered. The allow-list is the arms of the models tables (--models-tsv, a comma list; VLMA's models_r5.tsv and VLMB's models_r5_vlmb.tsv) plus --vlm-arms and the arms_info.json entries with role vlm. Unique parameter counts come from arms_info.json, else from the models tables. Reads only validation score files and meta.json files; run r5_score.sbatch first so that every arm with predictions has a score. """ import argparse import hashlib import json import os import sys import time HERE = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, os.path.normpath(os.path.join(HERE, "..", "..", "src"))) def main(): ap = argparse.ArgumentParser() ap.add_argument("--scores", required=True) ap.add_argument("--preds", required=True) ap.add_argument("--out", required=True) ap.add_argument("--arms-info", default=None) ap.add_argument("--models-tsv", default=None, help="models tables of the VLM arms (comma list); their arms are the VLM allow-list") ap.add_argument("--vlm-arms", default="", help="comma list of further VLM arm names") ap.add_argument("--checkpoint", default="short") ap.add_argument("--B", type=int, default=10000) a = ap.parse_args() import r5eval from r5eval import ioutil, selection raw = json.load(open(a.arms_info, encoding="utf-8")) if a.arms_info else {} info = selection.clean_arms_info(raw) params = selection.read_params(a.models_tsv) selection.fill_params(info, params) allow = set(params) | {x for x in a.vlm_arms.split(",") if x} | {k for k, v in info.items() if v.get("role") == "vlm"} res = selection.leaders(a.scores, a.preds, info, allow or None, a.checkpoint, B=a.B) res["generated_utc"] = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()) res["inputs"] = {"scores": a.scores, "preds": a.preds, "arms_info": a.arms_info, "arms_info_sha256": hashlib.sha256(open(a.arms_info, "rb").read()).hexdigest() if a.arms_info else None, "models_tsv": a.models_tsv, "vlm_arms": a.vlm_arms, "n_arms_in_allow_list": len(allow), "B": a.B, "report_code_sha256": r5eval.report_code_sha256()} ioutil.write_json_atomic(a.out, res) with open(a.out + ".sha256", "w") as f: f.write("%s %s %s\n" % (hashlib.sha256(open(a.out, "rb").read()).hexdigest(), os.path.basename(a.out), res["generated_utc"])) print("leaders:", ", ".join("%s (V1 %.4f, %s)" % (x["arm"], x["v1_cer"], x["size_class"]) for x in res["leaders"])) for k in ("extra_b", "extra_c"): x = res[k] print("%s: %s" % (k, "%s (V1 %.4f, %s)" % (x["arm"], x["v1_cer"], x["size_class"]) if x else "none")) for w in res["warnings"]: print("WARNING:", w) print("written", a.out) if __name__ == "__main__": main()