| from dataclasses import dataclass |
| from typing import Dict, Any, List |
|
|
| @dataclass |
| class ScoreResult: |
| score: float |
| details: Dict[str, Any] |
|
|
| def score(sample: Dict[str, Any], prediction: Dict[str, Any]) -> ScoreResult: |
| true_horizon = float(sample.get("failure_horizon_cycles", 0)) |
| pred_horizon = float(prediction.get("failure_horizon_cycles", 0)) |
| true_action = (sample.get("mitigation_action") or "").lower() |
| pred_action = (prediction.get("mitigation_action") or "").lower() |
|
|
| |
| if true_horizon == 0: |
| horizon_score = 0.0 |
| else: |
| error = abs(true_horizon - pred_horizon) / max(true_horizon, 1) |
| horizon_score = max(0.0, 1 - error) |
|
|
| |
| action_score = 1.0 if true_action in pred_action else 0.0 |
|
|
| total = 0.6 * horizon_score + 0.4 * action_score |
| return ScoreResult(score=total, details={"horizon_score": horizon_score, "action_score": action_score}) |
|
|
| 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)} |
|
|