File size: 1,437 Bytes
07c974e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
33
34
35
36
37
38
39
40
41
42
43
44
45
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:
    # Ground truth
    true_drift = float(sample.get("drift_gradient", 0))
    true_decoh = float(sample.get("decoherence_score", 0))
    true_flag = int(sample.get("decoupling_flag", 0))

    # Prediction
    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)     # assumes [0,1]
    decoh_acc = _clamp01(1.0 - decoh_err)     # assumes [0,1]
    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)}