Create scorer.py
Browse files
scorer.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 _clamp01(x: float) -> float:
|
| 10 |
+
return max(0.0, min(1.0, x))
|
| 11 |
+
|
| 12 |
+
def score(sample: Dict[str, Any], prediction: Dict[str, Any]) -> ScoreResult:
|
| 13 |
+
# Ground truth
|
| 14 |
+
true_drift = float(sample.get("drift_gradient", 0))
|
| 15 |
+
true_decoh = float(sample.get("decoherence_score", 0))
|
| 16 |
+
true_flag = int(sample.get("decoupling_flag", 0))
|
| 17 |
+
|
| 18 |
+
# Prediction
|
| 19 |
+
pred_drift = float(prediction.get("drift_gradient", 0))
|
| 20 |
+
pred_decoh = float(prediction.get("decoherence_score", 0))
|
| 21 |
+
pred_flag = int(prediction.get("decoupling_flag", 0))
|
| 22 |
+
|
| 23 |
+
drift_err = abs(true_drift - pred_drift)
|
| 24 |
+
decoh_err = abs(true_decoh - pred_decoh)
|
| 25 |
+
|
| 26 |
+
drift_acc = _clamp01(1.0 - drift_err) # assumes [0,1]
|
| 27 |
+
decoh_acc = _clamp01(1.0 - decoh_err) # assumes [0,1]
|
| 28 |
+
flag_acc = 1.0 if true_flag == pred_flag else 0.0
|
| 29 |
+
|
| 30 |
+
total = 0.40 * drift_acc + 0.40 * decoh_acc + 0.20 * flag_acc
|
| 31 |
+
return ScoreResult(
|
| 32 |
+
score=total,
|
| 33 |
+
details={
|
| 34 |
+
"id": sample.get("id"),
|
| 35 |
+
"drift_err": drift_err,
|
| 36 |
+
"decoh_err": decoh_err,
|
| 37 |
+
"flag_acc": flag_acc
|
| 38 |
+
}
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
def aggregate(results: List[ScoreResult]) -> Dict[str, Any]:
|
| 42 |
+
if not results:
|
| 43 |
+
return {"mean": 0.0, "n": 0}
|
| 44 |
+
return {"mean": sum(r.score for r in results)/len(results), "n": len(results)}
|