Create scorer.py
Browse files
scorer.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dataclasses import dataclass
|
| 2 |
+
from typing import Dict, Any, List
|
| 3 |
+
|
| 4 |
+
@dataclass
|
| 5 |
+
class ScoreResult:
|
| 6 |
+
score: float
|
| 7 |
+
details: Dict[str, Any]
|
| 8 |
+
|
| 9 |
+
def score(sample: Dict[str, Any], prediction: str) -> ScoreResult:
|
| 10 |
+
p = (prediction or "").lower()
|
| 11 |
+
words_ok = len(p.split()) <= 700
|
| 12 |
+
|
| 13 |
+
has_subgroup = "subgroup" in p or "responder" in p
|
| 14 |
+
has_non = "non" in p and "responder" in p
|
| 15 |
+
has_gate = "gate" in p or "context" in p or "only if" in p
|
| 16 |
+
has_class = any(k in p for k in ["masked", "stage", "adherence", "toxicity", "context"])
|
| 17 |
+
has_salvage = "salvage" in p or "niche" in p or "use in" in p
|
| 18 |
+
has_plan = "plan" in p or "trial" in p or "verify" in p
|
| 19 |
+
|
| 20 |
+
raw = (
|
| 21 |
+
0.15 * int(words_ok) +
|
| 22 |
+
0.25 * int(has_subgroup) +
|
| 23 |
+
0.10 * int(has_non) +
|
| 24 |
+
0.20 * int(has_gate) +
|
| 25 |
+
0.10 * int(has_class) +
|
| 26 |
+
0.10 * int(has_salvage) +
|
| 27 |
+
0.10 * int(has_plan)
|
| 28 |
+
)
|
| 29 |
+
return ScoreResult(score=min(1.0, raw), details={"id": sample.get("id")})
|
| 30 |
+
|
| 31 |
+
def aggregate(results: List[ScoreResult]) -> Dict[str, Any]:
|
| 32 |
+
if not results:
|
| 33 |
+
return {"mean": 0.0, "n": 0}
|
| 34 |
+
return {"mean": sum(r.score for r in results) / len(results), "n": len(results)}
|