ClarusC64 commited on
Commit
f0c1a17
·
verified ·
1 Parent(s): 1397329

Create scorer.py

Browse files
Files changed (1) hide show
  1. scorer.py +61 -0
scorer.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from dataclasses import dataclass
2
+ from typing import Dict, Any, List
3
+
4
+ VALID_AXES = {"thermal","cooling","mount","metrology","contamination","none"}
5
+
6
+ @dataclass
7
+ class ScoreResult:
8
+ score: float
9
+ details: Dict[str, Any]
10
+
11
+ def parse(prediction: str):
12
+ # expected: drift_score,drift_flag,dominant_axis
13
+ try:
14
+ parts = [p.strip() for p in prediction.split(",")]
15
+ drift_score = float(parts[0])
16
+ drift_flag = int(parts[1])
17
+ axis = parts[2].lower()
18
+ return drift_score, drift_flag, axis
19
+ except Exception:
20
+ return None, None, None
21
+
22
+ def score(sample: Dict[str, Any], prediction: str) -> ScoreResult:
23
+ ds, df, ax = parse(prediction or "")
24
+ if ds is None:
25
+ return ScoreResult(0.0, {"error":"parse failure"})
26
+
27
+ true_ds = sample.get("overlay_drift_score","")
28
+ true_df = sample.get("drift_flag","")
29
+ true_ax = str(sample.get("dominant_cause_axis","")).lower()
30
+
31
+ try:
32
+ true_ds = float(true_ds)
33
+ true_df = int(true_df)
34
+ except:
35
+ true_ds = None
36
+
37
+ if true_ds is None:
38
+ valid = (0 <= ds <= 1) and (df in (0,1)) and (ax in VALID_AXES)
39
+ return ScoreResult(1.0 if valid else 0.0, {"mode":"format_only"})
40
+
41
+ err = abs(true_ds - ds)
42
+ s = max(0.0, 1.0 - err)
43
+ if df == true_df:
44
+ s += 0.20
45
+ if ax == true_ax:
46
+ s += 0.20
47
+
48
+ return ScoreResult(min(1.0, s), {
49
+ "id": sample.get("id"),
50
+ "pred_drift_score": ds,
51
+ "true_drift_score": true_ds,
52
+ "pred_flag": df,
53
+ "true_flag": true_df,
54
+ "pred_axis": ax,
55
+ "true_axis": true_ax
56
+ })
57
+
58
+ def aggregate(results: List[ScoreResult]) -> Dict[str, Any]:
59
+ if not results:
60
+ return {"mean":0.0,"n":0}
61
+ return {"mean": sum(r.score for r in results)/len(results), "n": len(results)}