import re from dataclasses import dataclass from typing import Dict, Any, List BASINS = { "improved_stable_recovery", "delayed_recovery", "complication_basin", "iatrogenic_worsening", "unstable_oscillation", "no_material_change", } @dataclass class ScoreResult: score: float details: Dict[str, Any] def score(sample: Dict[str, Any], prediction: str) -> ScoreResult: p = (prediction or "").lower().strip() words_ok = len(p.split()) <= 360 seq_ok = any(k in p for k in ["t+","day","week"]) and any(ch in p for ch in [":", ";"]) cross_ok = any(k in p for k in ["immune", "renal", "cardio", "neuro", "resp", "gi", "autonomic", "metabolic", "subjective"]) div_ok = any(k in p for k in ["time_to_divergence", "divergence", "t+"]) and bool(re.search(r"\b\d+\s*(h|hour|day|week)s?\b", p)) basin_ok = any(b in p for b in BASINS) raw = ( 0.20 * int(words_ok) + 0.30 * int(seq_ok) + 0.25 * int(cross_ok) + 0.15 * int(div_ok) + 0.10 * int(basin_ok) ) return ScoreResult(score=min(1.0, raw), details={"id": sample.get("id"), "seq_ok": seq_ok, "basin_ok": basin_ok}) def aggregate(results: List[ScoreResult]) -> Dict[str, Any]: if not results: return {"mean": 0.0, "n": 0} return {"mean": sum(r.score for r in results) / len(results), "n": len(results)}