Create scorer.py
Browse files
scorer.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
from dataclasses import dataclass
|
| 3 |
+
from typing import Dict, Any, List
|
| 4 |
+
|
| 5 |
+
PATHWAYS = {"viral_triggered","stress_triggered","toxic_exposure","iatrogenic","mixed_or_unknown"}
|
| 6 |
+
|
| 7 |
+
@dataclass
|
| 8 |
+
class ScoreResult:
|
| 9 |
+
score: float
|
| 10 |
+
details: Dict[str, Any]
|
| 11 |
+
|
| 12 |
+
def _extract_ints(text: str) -> List[int]:
|
| 13 |
+
return [int(x) for x in re.findall(r"\b\d{1,3}\b", text) if 0 <= int(x) <= 100]
|
| 14 |
+
|
| 15 |
+
def score(sample: Dict[str, Any], prediction: str) -> ScoreResult:
|
| 16 |
+
p = (prediction or "").lower().strip()
|
| 17 |
+
words_ok = len(p.split()) <= 360
|
| 18 |
+
|
| 19 |
+
pathway_ok = any(x in p for x in PATHWAYS)
|
| 20 |
+
nums = _extract_ints(p)
|
| 21 |
+
has_dist = len(nums) >= 1
|
| 22 |
+
|
| 23 |
+
entry_ref = any(k in p for k in ["entry", "signature", "early", "onset", "phase"])
|
| 24 |
+
basin_ref = "basin" in p
|
| 25 |
+
evidence_ref = any(k in p for k in ["because", "shown by", "align", "matches", "differs"])
|
| 26 |
+
|
| 27 |
+
raw = (
|
| 28 |
+
0.25 * int(words_ok) +
|
| 29 |
+
0.30 * int(pathway_ok) +
|
| 30 |
+
0.20 * int(has_dist) +
|
| 31 |
+
0.10 * int(entry_ref) +
|
| 32 |
+
0.10 * int(basin_ref) +
|
| 33 |
+
0.05 * int(evidence_ref)
|
| 34 |
+
)
|
| 35 |
+
return ScoreResult(score=min(1.0, raw), details={"pathway_ok": pathway_ok, "id": sample.get("id")})
|
| 36 |
+
|
| 37 |
+
def aggregate(results: List[ScoreResult]) -> Dict[str, Any]:
|
| 38 |
+
if not results:
|
| 39 |
+
return {"mean": 0.0, "n": 0}
|
| 40 |
+
return {"mean": sum(r.score for r in results) / len(results), "n": len(results)}
|