ClarusC64 commited on
Commit
9634e32
·
verified ·
1 Parent(s): 74c1166

Create scorer.py

Browse files
Files changed (1) hide show
  1. scorer.py +70 -0
scorer.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import argparse
2
+ import csv
3
+ import json
4
+ import sys
5
+
6
+ SCORER_VERSION = "1.0"
7
+ LABEL_COL = "label_best_intervention"
8
+ ID_COL = "scenario_id"
9
+
10
+
11
+ def read_csv(path):
12
+ with open(path, "r", encoding="utf-8") as f:
13
+ return list(csv.DictReader(f))
14
+
15
+
16
+ def align(truth, preds):
17
+ truth_map = {r[ID_COL]: r for r in truth}
18
+ pred_map = {r[ID_COL]: r for r in preds}
19
+
20
+ if set(truth_map) != set(pred_map):
21
+ raise ValueError("scenario_id mismatch")
22
+
23
+ y_true = []
24
+ y_pred = []
25
+
26
+ for sid in sorted(truth_map):
27
+ true = truth_map[sid][LABEL_COL]
28
+ pred = pred_map[sid]["prediction"]
29
+
30
+ if pred not in ["A", "B", "C"]:
31
+ raise ValueError(f"Invalid prediction for {sid}")
32
+
33
+ y_true.append(true)
34
+ y_pred.append(pred)
35
+
36
+ return y_true, y_pred
37
+
38
+
39
+ def accuracy(y_true, y_pred):
40
+ correct = sum(t == p for t, p in zip(y_true, y_pred))
41
+ return correct / len(y_true)
42
+
43
+
44
+ def main():
45
+ parser = argparse.ArgumentParser()
46
+ parser.add_argument("--predictions", required=True)
47
+ parser.add_argument("--truth", required=True)
48
+ args = parser.parse_args()
49
+
50
+ try:
51
+ preds = read_csv(args.predictions)
52
+ truth = read_csv(args.truth)
53
+
54
+ y_true, y_pred = align(truth, preds)
55
+
56
+ result = {
57
+ "scorer_version": SCORER_VERSION,
58
+ "accuracy": accuracy(y_true, y_pred),
59
+ "primary_metric": "accuracy"
60
+ }
61
+
62
+ print(json.dumps(result, indent=2))
63
+
64
+ except Exception as e:
65
+ print(e, file=sys.stderr)
66
+ sys.exit(1)
67
+
68
+
69
+ if __name__ == "__main__":
70
+ main()