| from dataclasses import dataclass |
| from typing import Dict, Any, List |
|
|
| @dataclass |
| class ScoreResult: |
| score: float |
| details: Dict[str, Any] |
|
|
| def _clamp01(x: float) -> float: |
| return max(0.0, min(1.0, x)) |
|
|
| def score(sample: Dict[str, Any], prediction: Dict[str, Any]) -> ScoreResult: |
| |
| true_drift = float(sample.get("drift_gradient", 0)) |
| true_decoh = float(sample.get("decoherence_score", 0)) |
| true_flag = int(sample.get("decoupling_flag", 0)) |
|
|
| |
| pred_drift = float(prediction.get("drift_gradient", 0)) |
| pred_decoh = float(prediction.get("decoherence_score", 0)) |
| pred_flag = int(prediction.get("decoupling_flag", 0)) |
|
|
| drift_err = abs(true_drift - pred_drift) |
| decoh_err = abs(true_decoh - pred_decoh) |
|
|
| drift_acc = _clamp01(1.0 - drift_err) |
| decoh_acc = _clamp01(1.0 - decoh_err) |
| flag_acc = 1.0 if true_flag == pred_flag else 0.0 |
|
|
| total = 0.40 * drift_acc + 0.40 * decoh_acc + 0.20 * flag_acc |
| return ScoreResult( |
| score=total, |
| details={ |
| "id": sample.get("id"), |
| "drift_err": drift_err, |
| "decoh_err": decoh_err, |
| "flag_acc": flag_acc |
| } |
| ) |
|
|
| def aggregate(results: List[ScoreResult]) -> Dict[str, Any]: |
| if not results: |
| return {"mean": 0.0, "n": 0} |
| return {"mean": sum(r.score for r in results)/len(results), "n": len(results)} |
|
|