Datasets:
Create scorer.py
Browse files
scorer.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dataclasses import dataclass
|
| 2 |
+
from typing import Dict, Any, List
|
| 3 |
+
import re
|
| 4 |
+
|
| 5 |
+
REQ = [
|
| 6 |
+
"platform_coherence_score",
|
| 7 |
+
"zone_pressure_asymmetry_index",
|
| 8 |
+
"vortex_system_integrity_flags",
|
| 9 |
+
"localized_collapse_zones",
|
| 10 |
+
"balance_shift_risk_score",
|
| 11 |
+
]
|
| 12 |
+
|
| 13 |
+
ZONE_HINT = ["floor_right", "floor_left", "diffuser", "front_wing", "none"]
|
| 14 |
+
FLAG_HINT = ["ok", "fail", "weak", "stall", "transient"]
|
| 15 |
+
|
| 16 |
+
@dataclass
|
| 17 |
+
class ScoreResult:
|
| 18 |
+
score: float
|
| 19 |
+
details: Dict[str, Any]
|
| 20 |
+
|
| 21 |
+
def _has_float(p: str) -> bool:
|
| 22 |
+
return bool(re.search(r"\b0\.\d+\b", p)) or "1.0" in p
|
| 23 |
+
|
| 24 |
+
def _has_zone(p: str) -> bool:
|
| 25 |
+
return any(z in p for z in ZONE_HINT)
|
| 26 |
+
|
| 27 |
+
def _has_flag(p: str) -> bool:
|
| 28 |
+
return any(f in p for f in FLAG_HINT)
|
| 29 |
+
|
| 30 |
+
def score(sample: Dict[str, Any], prediction: str) -> ScoreResult:
|
| 31 |
+
p = (prediction or "").lower()
|
| 32 |
+
words_ok = len(p.split()) <= 1000
|
| 33 |
+
|
| 34 |
+
hits = sum(1 for k in REQ if k in p)
|
| 35 |
+
has_float = _has_float(p)
|
| 36 |
+
has_zone = _has_zone(p)
|
| 37 |
+
has_flag = _has_flag(p)
|
| 38 |
+
|
| 39 |
+
raw = (
|
| 40 |
+
0.20 * int(words_ok) +
|
| 41 |
+
0.60 * (hits / len(REQ)) +
|
| 42 |
+
0.10 * int(has_float) +
|
| 43 |
+
0.05 * int(has_zone) +
|
| 44 |
+
0.05 * int(has_flag)
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
return ScoreResult(score=min(1.0, raw), details={"id": sample.get("id"), "hits": hits})
|
| 48 |
+
|
| 49 |
+
def aggregate(results: List[ScoreResult]) -> Dict[str, Any]:
|
| 50 |
+
if not results:
|
| 51 |
+
return {"mean": 0.0, "n": 0}
|
| 52 |
+
return {"mean": sum(r.score for r in results)/len(results), "n": len(results)}
|