Create scorer.py
Browse files
scorer.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dataclasses import dataclass
|
| 2 |
+
from typing import Dict, Any, List
|
| 3 |
+
import re
|
| 4 |
+
|
| 5 |
+
@dataclass
|
| 6 |
+
class ScoreResult:
|
| 7 |
+
score: float
|
| 8 |
+
details: Dict[str, Any]
|
| 9 |
+
|
| 10 |
+
def _extract_horizon(text: str) -> int:
|
| 11 |
+
m = re.search(r"(horizon|cycles)\s*[:=]?\s*(\d+)", (text or "").lower())
|
| 12 |
+
if m:
|
| 13 |
+
return int(m.group(2))
|
| 14 |
+
return -1
|
| 15 |
+
|
| 16 |
+
def _extract_risk(text: str) -> float:
|
| 17 |
+
m = re.search(r"(risk|cd).*?([0]\.\d+|1\.0)", (text or "").lower())
|
| 18 |
+
if m:
|
| 19 |
+
try:
|
| 20 |
+
return float(m.group(2))
|
| 21 |
+
except:
|
| 22 |
+
return -1.0
|
| 23 |
+
return -1.0
|
| 24 |
+
|
| 25 |
+
def _extract_action(text: str) -> str:
|
| 26 |
+
t = (text or "").lower()
|
| 27 |
+
for k in [
|
| 28 |
+
"none","retune_timing","pulse_shape_adjust","pre_pulse_boost",
|
| 29 |
+
"stability_tune","source_recalibration","power_derate",
|
| 30 |
+
"full_timing_reset","shutdown_prepare","halt_source"
|
| 31 |
+
]:
|
| 32 |
+
if k in t:
|
| 33 |
+
return k
|
| 34 |
+
return ""
|
| 35 |
+
|
| 36 |
+
def score(sample: Dict[str, Any], prediction: str) -> ScoreResult:
|
| 37 |
+
p = prediction or ""
|
| 38 |
+
horizon = _extract_horizon(p)
|
| 39 |
+
risk = _extract_risk(p)
|
| 40 |
+
action = _extract_action(p)
|
| 41 |
+
|
| 42 |
+
true_h_raw = sample.get("event_horizon_cycles", "")
|
| 43 |
+
true_r_raw = sample.get("cd_uniformity_risk_score", "")
|
| 44 |
+
true_a_raw = (sample.get("intervention_action", "") or "").lower()
|
| 45 |
+
|
| 46 |
+
# If no ground truth in test row → structure check only
|
| 47 |
+
if true_h_raw in ("", None) and true_r_raw in ("", None) and true_a_raw == "":
|
| 48 |
+
s = 0.0
|
| 49 |
+
s += 0.4 * int(horizon > 0)
|
| 50 |
+
s += 0.3 * int(0.0 <= risk <= 1.0)
|
| 51 |
+
s += 0.3 * int(action != "")
|
| 52 |
+
return ScoreResult(score=s, details={"mode": "format_only", "horizon": horizon, "risk": risk, "action": action})
|
| 53 |
+
|
| 54 |
+
try:
|
| 55 |
+
true_h = int(true_h_raw)
|
| 56 |
+
except:
|
| 57 |
+
true_h = -1
|
| 58 |
+
|
| 59 |
+
try:
|
| 60 |
+
true_r = float(true_r_raw)
|
| 61 |
+
except:
|
| 62 |
+
true_r = -1.0
|
| 63 |
+
|
| 64 |
+
h_score = 1.0 if horizon > 0 and true_h > 0 else 0.0
|
| 65 |
+
r_score = 1.0 - abs(risk - true_r) if true_r >= 0 and 0 <= risk <= 1 else 0.0
|
| 66 |
+
a_score = 1.0 if action == true_a_raw else 0.0
|
| 67 |
+
|
| 68 |
+
final = 0.4 * h_score + 0.4 * max(0.0, r_score) + 0.2 * a_score
|
| 69 |
+
return ScoreResult(score=max(0.0, min(1.0, final)), details={"h": horizon, "r": risk, "a": action})
|
| 70 |
+
|
| 71 |
+
def aggregate(results: List[ScoreResult]) -> Dict[str, Any]:
|
| 72 |
+
if not results:
|
| 73 |
+
return {"mean": 0.0, "n": 0}
|
| 74 |
+
return {"mean": sum(r.score for r in results)/len(results), "n": len(results)}
|