"""Score prediction files on Clinical3D-Bench. python -m clinical3d_eval.score --data /path/to/Clinical3D-Bench --preds preds/*.jsonl [--subset full|debiased|mini] A prediction file has one JSON row per question with "id" and "pred" (the option index, 0 = A; null if unparsed). Several files for one model (e.g. seeds) are averaged with --average. Accuracy per type pools its subtasks by question; Avg. is the macro average over the eight types and sets the rank. Each type's accuracy is rounded to 0.1 % before the macro average. full all 3,368 questions (the paper's main table) debiased without the 739 blind-flagged questions mini the 400-question subset the physicians answered (Clinical3D-Bench-mini) """ import argparse import json from pathlib import Path TYPES = ['Count', 'Re-ID', 'Light owner', 'Patient frame', 'Device ref.', 'Direction', 'Route', 'Passability'] def score(questions, preds): """preds: {id: option index or None}. Missing questions count as wrong.""" acc = {} for t in TYPES: qs = [q for q in questions if q['type'] == t] acc[t] = round(sum(preds.get(q['id']) == q['answer'] for q in qs) / len(qs), 3) if qs else None vals = [v for v in acc.values() if v is not None] return dict(types={t: None if v is None else round(100 * v, 1) for t, v in acc.items()}, avg=round(100 * sum(vals) / len(vals), 1), n=len(questions), answered=sum(q['id'] in preds for q in questions), unparsed=sum(q['id'] in preds and preds[q['id']] is None for q in questions)) def load_preds(path): return {r['id']: r['pred'] for r in map(json.loads, Path(path).read_text().splitlines())} def main(): ap = argparse.ArgumentParser() ap.add_argument('--data', type=Path, required=True) ap.add_argument('--preds', type=Path, nargs='+', required=True) ap.add_argument('--subset', choices=['full', 'debiased', 'mini'], default='full') ap.add_argument('--average', action='store_true', help='average the given files (seeds of one model)') ap.add_argument('--json', type=Path, help='also write the results here') a = ap.parse_args() qs = [json.loads(l) for l in (a.data / 'questions.jsonl').read_text().splitlines()] if a.subset == 'debiased': qs = [q for q in qs if not q['blind_flagged']] elif a.subset == 'mini': qs = [q for q in qs if q['in_mini']] res = {p.stem: score(qs, load_preds(p)) for p in a.preds} if a.average: rs = list(res.values()) types = {t: None if rs[0]['types'][t] is None else round(sum(r['types'][t] for r in rs) / len(rs), 1) for t in TYPES} res = {'average': dict(types=types, avg=round(sum(r['avg'] for r in rs) / len(rs), 1), n=rs[0]['n'], runs=len(rs))} print(f"{'model':32s} " + ' '.join(f'{t[:8]:>8s}' for t in TYPES) + ' Avg.') for k, r in res.items(): print(f'{k:32s} ' + ' '.join(f"{'-' if r['types'][t] is None else r['types'][t]:>8}" for t in TYPES) + f" {r['avg']:6.1f}" + (f" ({r['answered']}/{r['n']} answered, {r['unparsed']} unparsed)" if 'answered' in r else '')) if a.json: a.json.write_text(json.dumps(res, indent=1)) if __name__ == '__main__': main()