Create scorer.py
Browse files
scorer.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dataclasses import dataclass
|
| 2 |
+
from typing import Dict, Any, List
|
| 3 |
+
import re
|
| 4 |
+
|
| 5 |
+
REQ = [
|
| 6 |
+
"decoherence_onset_timestamp",
|
| 7 |
+
"coherence_drop_delta",
|
| 8 |
+
"affected_modalities",
|
| 9 |
+
"narrative_conflict_flag",
|
| 10 |
+
"onset_confidence",
|
| 11 |
+
"early_warning_score",
|
| 12 |
+
]
|
| 13 |
+
|
| 14 |
+
@dataclass
|
| 15 |
+
class ScoreResult:
|
| 16 |
+
score: float
|
| 17 |
+
details: Dict[str, Any]
|
| 18 |
+
|
| 19 |
+
def _time_ok(p: str):
|
| 20 |
+
# accepts t=6.2s or 6.2s
|
| 21 |
+
m = re.search(r"decoherence_onset_timestamp\s*[:=]\s*(t\s*=\s*)?([0-9]+(\.[0-9]+)?)\s*s", p)
|
| 22 |
+
if not m:
|
| 23 |
+
return None
|
| 24 |
+
return float(m.group(2))
|
| 25 |
+
|
| 26 |
+
def _f(p: str, key: str):
|
| 27 |
+
m = re.search(rf"{key}\s*[:=]\s*(0\.\d+|1\.0)\b", p)
|
| 28 |
+
return float(m.group(1)) if m else None
|
| 29 |
+
|
| 30 |
+
def _i(p: str, key: str):
|
| 31 |
+
m = re.search(rf"{key}\s*[:=]\s*(\d+)\b", p)
|
| 32 |
+
return int(m.group(1)) if m else None
|
| 33 |
+
|
| 34 |
+
def score(sample: Dict[str, Any], prediction: str) -> ScoreResult:
|
| 35 |
+
p = (prediction or "").lower()
|
| 36 |
+
words_ok = len(p.split()) <= 900
|
| 37 |
+
|
| 38 |
+
hits = sum(1 for k in REQ if k in p)
|
| 39 |
+
|
| 40 |
+
t = _time_ok(p)
|
| 41 |
+
delta = _f(p, "coherence_drop_delta")
|
| 42 |
+
conf = _f(p, "onset_confidence")
|
| 43 |
+
warn = _f(p, "early_warning_score")
|
| 44 |
+
flag = _i(p, "narrative_conflict_flag")
|
| 45 |
+
|
| 46 |
+
numeric_ok = int(
|
| 47 |
+
t is not None and 0.0 <= t <= 120.0 and
|
| 48 |
+
delta is not None and 0.0 <= delta <= 1.0 and
|
| 49 |
+
conf is not None and 0.0 <= conf <= 1.0 and
|
| 50 |
+
warn is not None and 0.0 <= warn <= 1.0 and
|
| 51 |
+
flag is not None and flag in [0, 1]
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
mods_ok = int("affected_modalities" in p and len(p) > 70)
|
| 55 |
+
|
| 56 |
+
# optional sanity: if delta is high, warning should tend high
|
| 57 |
+
sanity = 0
|
| 58 |
+
if delta is not None and warn is not None:
|
| 59 |
+
sanity = int(warn + 0.15 >= delta)
|
| 60 |
+
|
| 61 |
+
raw = (
|
| 62 |
+
0.15 * int(words_ok) +
|
| 63 |
+
0.45 * (hits / len(REQ)) +
|
| 64 |
+
0.20 * numeric_ok +
|
| 65 |
+
0.10 * mods_ok +
|
| 66 |
+
0.10 * sanity
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
return ScoreResult(score=min(1.0, raw), details={"id": sample.get("id"), "hits": hits, "sanity": sanity})
|
| 70 |
+
|
| 71 |
+
def aggregate(results: List[ScoreResult]) -> Dict[str, Any]:
|
| 72 |
+
if not results:
|
| 73 |
+
return {"mean": 0.0, "n": 0}
|
| 74 |
+
return {"mean": sum(r.score for r in results)/len(results), "n": len(results)}
|