ClarusC64 commited on
Commit
7c25da0
·
verified ·
1 Parent(s): 904933c

Create scorer.py

Browse files
Files changed (1) hide show
  1. scorer.py +109 -0
scorer.py ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import csv
2
+ import sys
3
+ from pathlib import Path
4
+
5
+
6
+ def load_csv(path):
7
+ with open(path, "r", encoding="utf-8") as f:
8
+ return list(csv.DictReader(f))
9
+
10
+
11
+ def normalize_binary(value):
12
+ if isinstance(value, int):
13
+ return value
14
+ text = str(value).strip().lower()
15
+ if text in {"1", "true", "yes", "positive"}:
16
+ return 1
17
+ if text in {"0", "false", "no", "negative"}:
18
+ return 0
19
+ raise ValueError(f"Unrecognized binary value: {value}")
20
+
21
+
22
+ def find_label_column(fieldnames):
23
+ matches = [c for c in fieldnames if c.startswith("label_")]
24
+ if len(matches) != 1:
25
+ raise ValueError(f"Expected exactly one label_ column, found: {matches}")
26
+ return matches[0]
27
+
28
+
29
+ def compute_metrics(y_true, y_pred):
30
+ tp = sum(1 for t, p in zip(y_true, y_pred) if t == 1 and p == 1)
31
+ tn = sum(1 for t, p in zip(y_true, y_pred) if t == 0 and p == 0)
32
+ fp = sum(1 for t, p in zip(y_true, y_pred) if t == 0 and p == 1)
33
+ fn = sum(1 for t, p in zip(y_true, y_pred) if t == 1 and p == 0)
34
+
35
+ total = len(y_true)
36
+ accuracy = (tp + tn) / total if total else 0.0
37
+ precision = tp / (tp + fp) if (tp + fp) else 0.0
38
+ recall_successful_stabilization = tp / (tp + fn) if (tp + fn) else 0.0
39
+ failed_rescue_rate = fn / (fn + tp) if (fn + tp) else 0.0
40
+ f1 = (
41
+ 2 * precision * recall_successful_stabilization / (precision + recall_successful_stabilization)
42
+ if (precision + recall_successful_stabilization)
43
+ else 0.0
44
+ )
45
+
46
+ return {
47
+ "accuracy": accuracy,
48
+ "precision": precision,
49
+ "recall_successful_stabilization": recall_successful_stabilization,
50
+ "failed_rescue_rate": failed_rescue_rate,
51
+ "f1": f1,
52
+ "primary_metric": "recall_successful_stabilization",
53
+ "secondary_metric": "failed_rescue_rate",
54
+ "confusion_matrix": {
55
+ "true_positives": tp,
56
+ "false_positives": fp,
57
+ "true_negatives": tn,
58
+ "false_negatives": fn,
59
+ },
60
+ }
61
+
62
+
63
+ def score(reference_path, prediction_path):
64
+ references = load_csv(reference_path)
65
+ predictions = load_csv(prediction_path)
66
+
67
+ if not references:
68
+ raise ValueError("Reference file is empty.")
69
+ if not predictions:
70
+ raise ValueError("Prediction file is empty.")
71
+ if len(references) != len(predictions):
72
+ raise ValueError("Reference and prediction row counts do not match.")
73
+
74
+ label_col = find_label_column(references[0].keys())
75
+
76
+ y_true = [normalize_binary(row[label_col]) for row in references]
77
+
78
+ pred_col = None
79
+ for candidate in ["prediction", "predicted_label", label_col]:
80
+ if candidate in predictions[0]:
81
+ pred_col = candidate
82
+ break
83
+
84
+ if pred_col is None:
85
+ raise ValueError(
86
+ "Prediction file must contain one of: prediction, predicted_label, or the label column name."
87
+ )
88
+
89
+ y_pred = [normalize_binary(row[pred_col]) for row in predictions]
90
+
91
+ return compute_metrics(y_true, y_pred)
92
+
93
+
94
+ if __name__ == "__main__":
95
+ if len(sys.argv) != 3:
96
+ print("Usage: python scorer.py <reference.csv> <predictions.csv>", file=sys.stderr)
97
+ sys.exit(1)
98
+
99
+ ref_path = Path(sys.argv[1])
100
+ pred_path = Path(sys.argv[2])
101
+
102
+ try:
103
+ results = score(ref_path, pred_path)
104
+ except Exception as e:
105
+ print(f"Scoring error: {e}", file=sys.stderr)
106
+ sys.exit(1)
107
+
108
+ for key, value in results.items():
109
+ print(f"{key}: {value}")