ClarusC64 commited on
Commit
ae97fcd
·
verified ·
1 Parent(s): 7603ddb

Create scorer.py

Browse files
Files changed (1) hide show
  1. scorer.py +49 -0
scorer.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ from dataclasses import dataclass
3
+ from typing import Dict, Any, List
4
+
5
+ RHYTHMS = {
6
+ "circadian",
7
+ "weekly_load",
8
+ "menstrual_cycle",
9
+ "seasonal",
10
+ "post_infection_recovery",
11
+ "training_cycle",
12
+ "medication_cycle",
13
+ "none_detected",
14
+ }
15
+
16
+ @dataclass
17
+ class ScoreResult:
18
+ score: float
19
+ details: Dict[str, Any]
20
+
21
+ def _has(t: str, pats: List[str]) -> bool:
22
+ t = (t or "").lower()
23
+ return any(re.search(p, t) for p in pats)
24
+
25
+ def score(sample: Dict[str, Any], prediction: str) -> ScoreResult:
26
+ p = (prediction or "").lower().strip()
27
+ words_ok = len(p.split()) <= 320
28
+
29
+ signature_ref = _has(p, [r"signature=", r"personal_baseline_signature", r"baseline signature"])
30
+ envelope_ref = _has(p, [r"envelope=", r"variability", r"±", r"range"])
31
+ rhythm_ok = any(r in p for r in RHYTHMS)
32
+
33
+ coupling_ref = _has(p, [r"coupl", r"predict", r"tracks", r"rebound", r"anchors"])
34
+ marker_ref = _has(p, [r"rhr", r"hrv", r"crp", r"sleep", r"hba1c", r"tsh", r"ferritin"])
35
+
36
+ raw = (
37
+ 0.25 * int(words_ok) +
38
+ 0.25 * int(signature_ref) +
39
+ 0.20 * int(envelope_ref) +
40
+ 0.15 * int(rhythm_ok) +
41
+ 0.10 * int(coupling_ref) +
42
+ 0.05 * int(marker_ref)
43
+ )
44
+ return ScoreResult(score=min(1.0, raw), details={"id": sample.get("id"), "rhythm_ok": rhythm_ok})
45
+
46
+ def aggregate(results: List[ScoreResult]) -> Dict[str, Any]:
47
+ if not results:
48
+ return {"mean": 0.0, "n": 0}
49
+ return {"mean": sum(r.score for r in results) / len(results), "n": len(results)}