ClarusC64's picture
Create scorer.py
ae97fcd verified
Raw History Blame Contribute Delete
1.53 kB
import re
from dataclasses import dataclass
from typing import Dict, Any, List
RHYTHMS = {
"circadian",
"weekly_load",
"menstrual_cycle",
"seasonal",
"post_infection_recovery",
"training_cycle",
"medication_cycle",
"none_detected",
}
@dataclass
class ScoreResult:
score: float
details: Dict[str, Any]
def _has(t: str, pats: List[str]) -> bool:
t = (t or "").lower()
return any(re.search(p, t) for p in pats)
def score(sample: Dict[str, Any], prediction: str) -> ScoreResult:
p = (prediction or "").lower().strip()
words_ok = len(p.split()) <= 320
signature_ref = _has(p, [r"signature=", r"personal_baseline_signature", r"baseline signature"])
envelope_ref = _has(p, [r"envelope=", r"variability", r"±", r"range"])
rhythm_ok = any(r in p for r in RHYTHMS)
coupling_ref = _has(p, [r"coupl", r"predict", r"tracks", r"rebound", r"anchors"])
marker_ref = _has(p, [r"rhr", r"hrv", r"crp", r"sleep", r"hba1c", r"tsh", r"ferritin"])
raw = (
0.25 * int(words_ok) +
0.25 * int(signature_ref) +
0.20 * int(envelope_ref) +
0.15 * int(rhythm_ok) +
0.10 * int(coupling_ref) +
0.05 * int(marker_ref)
)
return ScoreResult(score=min(1.0, raw), details={"id": sample.get("id"), "rhythm_ok": rhythm_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)}