Create scorer.py
Browse files
scorer.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
from dataclasses import dataclass
|
| 3 |
+
from typing import Dict, Any, List
|
| 4 |
+
|
| 5 |
+
SYSTEMS = {
|
| 6 |
+
"sleep_circadian",
|
| 7 |
+
"autonomic",
|
| 8 |
+
"immune_inflammatory",
|
| 9 |
+
"metabolic",
|
| 10 |
+
"neurocognitive",
|
| 11 |
+
"gut_microbiome",
|
| 12 |
+
"behavior_load",
|
| 13 |
+
"subjective_experience",
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
VECTOR_KEYS = ["direction=", "magnitude=", "velocity=", "coupling_loss=", "onset_time=", "cross_modal_consensus="]
|
| 17 |
+
|
| 18 |
+
@dataclass
|
| 19 |
+
class ScoreResult:
|
| 20 |
+
score: float
|
| 21 |
+
details: Dict[str, Any]
|
| 22 |
+
|
| 23 |
+
def _ints(text: str) -> List[int]:
|
| 24 |
+
return [int(x) for x in re.findall(r"\b\d{1,3}\b", text) if 0 <= int(x) <= 100]
|
| 25 |
+
|
| 26 |
+
def score(sample: Dict[str, Any], prediction: str) -> ScoreResult:
|
| 27 |
+
p = (prediction or "").lower().strip()
|
| 28 |
+
words_ok = len(p.split()) <= 360
|
| 29 |
+
|
| 30 |
+
keys_ok = sum(1 for k in VECTOR_KEYS if k in p) >= 4
|
| 31 |
+
sys_ok = any(s in p for s in SYSTEMS)
|
| 32 |
+
|
| 33 |
+
nums = _ints(p)
|
| 34 |
+
has_sev = len(nums) >= 1
|
| 35 |
+
|
| 36 |
+
evidence_ref = any(k in p for k in ["envelope", "beyond", "break", "coupling", "predicts", "decouples"])
|
| 37 |
+
|
| 38 |
+
raw = (
|
| 39 |
+
0.20 * int(words_ok) +
|
| 40 |
+
0.30 * int(keys_ok) +
|
| 41 |
+
0.20 * int(sys_ok) +
|
| 42 |
+
0.20 * int(has_sev) +
|
| 43 |
+
0.10 * int(evidence_ref)
|
| 44 |
+
)
|
| 45 |
+
return ScoreResult(score=min(1.0, raw), details={"id": sample.get("id"), "keys_ok": keys_ok, "sys_ok": sys_ok})
|
| 46 |
+
|
| 47 |
+
def aggregate(results: List[ScoreResult]) -> Dict[str, Any]:
|
| 48 |
+
if not results:
|
| 49 |
+
return {"mean": 0.0, "n": 0}
|
| 50 |
+
return {"mean": sum(r.score for r in results) / len(results), "n": len(results)}
|