Create scorer.py
Browse files
scorer.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dataclasses import dataclass
|
| 2 |
+
from typing import Dict, Any, List
|
| 3 |
+
import re
|
| 4 |
+
|
| 5 |
+
REQ = [
|
| 6 |
+
"decoherence_onset_time",
|
| 7 |
+
"precursor_pattern_type",
|
| 8 |
+
"severity_gradient",
|
| 9 |
+
"failure_likelihood_index",
|
| 10 |
+
"estimated_time_to_critical_min",
|
| 11 |
+
"primary_decoupling_channels",
|
| 12 |
+
]
|
| 13 |
+
|
| 14 |
+
@dataclass
|
| 15 |
+
class ScoreResult:
|
| 16 |
+
score: float
|
| 17 |
+
details: Dict[str, Any]
|
| 18 |
+
|
| 19 |
+
def _time_min(p: str):
|
| 20 |
+
# accepts t=38m or 38m
|
| 21 |
+
m = re.search(r"decoherence_onset_time\s*[:=]\s*(t\s*=\s*)?([0-9]+(\.[0-9]+)?)\s*m", p)
|
| 22 |
+
if not m:
|
| 23 |
+
return None
|
| 24 |
+
return float(m.group(2))
|
| 25 |
+
|
| 26 |
+
def _f(p: str, key: str):
|
| 27 |
+
m = re.search(rf"{key}\s*[:=]\s*(0\.\d+|1\.0)\b", p)
|
| 28 |
+
return float(m.group(1)) if m else None
|
| 29 |
+
|
| 30 |
+
def _i(p: str, key: str):
|
| 31 |
+
m = re.search(rf"{key}\s*[:=]\s*(\d+)\b", p)
|
| 32 |
+
return int(m.group(1)) if m else None
|
| 33 |
+
|
| 34 |
+
def _ttc(p: str):
|
| 35 |
+
m = re.search(r"estimated_time_to_critical_min\s*[:=]\s*(\d+)", p)
|
| 36 |
+
return int(m.group(1)) if m else None
|
| 37 |
+
|
| 38 |
+
def score(sample: Dict[str, Any], prediction: str) -> ScoreResult:
|
| 39 |
+
p = (prediction or "").lower()
|
| 40 |
+
words_ok = len(p.split()) <= 1100
|
| 41 |
+
|
| 42 |
+
hits = sum(1 for k in REQ if k in p)
|
| 43 |
+
|
| 44 |
+
t = _time_min(p)
|
| 45 |
+
sev = _f(p, "severity_gradient")
|
| 46 |
+
lik = _f(p, "failure_likelihood_index")
|
| 47 |
+
ttc = _ttc(p)
|
| 48 |
+
|
| 49 |
+
numeric_ok = int(
|
| 50 |
+
t is not None and 0.0 <= t <= 10000.0 and
|
| 51 |
+
sev is not None and 0.0 <= sev <= 1.0 and
|
| 52 |
+
lik is not None and 0.0 <= lik <= 1.0 and
|
| 53 |
+
ttc is not None and 0 <= ttc <= 20000
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
type_ok = int("precursor_pattern_type" in p and len(p) > 70)
|
| 57 |
+
chan_ok = int("primary_decoupling_channels" in p and ("egt" in p or "n1" in p or "ff" in p))
|
| 58 |
+
|
| 59 |
+
# sanity: higher severity should not imply very low likelihood
|
| 60 |
+
sanity = 0
|
| 61 |
+
if sev is not None and lik is not None:
|
| 62 |
+
sanity = int(lik + 0.15 >= sev)
|
| 63 |
+
|
| 64 |
+
raw = (
|
| 65 |
+
0.15 * int(words_ok) +
|
| 66 |
+
0.40 * (hits / len(REQ)) +
|
| 67 |
+
0.20 * numeric_ok +
|
| 68 |
+
0.10 * type_ok +
|
| 69 |
+
0.10 * chan_ok +
|
| 70 |
+
0.05 * sanity
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
return ScoreResult(score=min(1.0, raw), details={"id": sample.get("id"), "hits": hits, "sanity": sanity})
|
| 74 |
+
|
| 75 |
+
def aggregate(results: List[ScoreResult]) -> Dict[str, Any]:
|
| 76 |
+
if not results:
|
| 77 |
+
return {"mean": 0.0, "n": 0}
|
| 78 |
+
return {"mean": sum(r.score for r in results)/len(results), "n": len(results)}
|