import re from dataclasses import dataclass from typing import Dict, Any, List SYSTEMS = { "sleep_circadian", "autonomic", "immune_inflammatory", "metabolic", "neurocognitive", "gut_microbiome", "behavior_load", "subjective_experience", } VECTOR_KEYS = ["direction=", "magnitude=", "velocity=", "coupling_loss=", "onset_time=", "cross_modal_consensus="] @dataclass class ScoreResult: score: float details: Dict[str, Any] def _ints(text: str) -> List[int]: return [int(x) for x in re.findall(r"\b\d{1,3}\b", text) if 0 <= int(x) <= 100] def score(sample: Dict[str, Any], prediction: str) -> ScoreResult: p = (prediction or "").lower().strip() words_ok = len(p.split()) <= 360 keys_ok = sum(1 for k in VECTOR_KEYS if k in p) >= 4 sys_ok = any(s in p for s in SYSTEMS) nums = _ints(p) has_sev = len(nums) >= 1 evidence_ref = any(k in p for k in ["envelope", "beyond", "break", "coupling", "predicts", "decouples"]) raw = ( 0.20 * int(words_ok) + 0.30 * int(keys_ok) + 0.20 * int(sys_ok) + 0.20 * int(has_sev) + 0.10 * int(evidence_ref) ) return ScoreResult(score=min(1.0, raw), details={"id": sample.get("id"), "keys_ok": keys_ok, "sys_ok": sys_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)}