File size: 1,146 Bytes
99541ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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()

    # horizon accuracy
    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 match
    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)}