ClarusC64 commited on
Commit
3d3ee90
·
verified ·
1 Parent(s): f2bef3f

Create scorer.py

Browse files
Files changed (1) hide show
  1. scorer.py +102 -0
scorer.py ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, confusion_matrix
3
+
4
+ TARGET_LABEL = "label_shock_boundary_transition"
5
+
6
+
7
+ def normalize_label(df):
8
+ label_cols = [c for c in df.columns if c.startswith("label_")]
9
+
10
+ if len(label_cols) == 1:
11
+ return df.rename(columns={label_cols[0]: "label"})
12
+
13
+ if len(label_cols) == 0:
14
+ raise ValueError(
15
+ f"No label column found. Expected {TARGET_LABEL} or a single label_* column."
16
+ )
17
+
18
+ raise ValueError(f"Multiple label columns found: {label_cols}")
19
+
20
+
21
+ def validate_binary_labels(series, name):
22
+ unique_values = set(series.dropna().unique())
23
+ if not unique_values.issubset({0, 1}):
24
+ raise ValueError(
25
+ f"{name} must contain only binary values 0/1. Found: {sorted(unique_values)}"
26
+ )
27
+
28
+
29
+ def validate_lengths(y_true, y_pred):
30
+ if len(y_true) != len(y_pred):
31
+ raise ValueError(
32
+ f"Prediction length mismatch. predictions={len(y_pred)} ground_truth={len(y_true)}"
33
+ )
34
+
35
+
36
+ def validate_prediction_columns(preds):
37
+ allowed = {"scenario_id", "label"}
38
+ extra_cols = [c for c in preds.columns if c not in allowed]
39
+ if extra_cols:
40
+ raise ValueError(
41
+ f"Predictions file should contain only scenario_id and label columns after normalization. Extra columns found: {extra_cols}"
42
+ )
43
+
44
+
45
+ def score(predictions_path, ground_truth_path):
46
+ preds = pd.read_csv(predictions_path)
47
+ truth = pd.read_csv(ground_truth_path)
48
+
49
+ truth = normalize_label(truth)
50
+ preds = normalize_label(preds)
51
+
52
+ if "label" not in preds.columns:
53
+ raise ValueError("Predictions file must contain a label column")
54
+
55
+ if "label" not in truth.columns:
56
+ raise ValueError("Ground truth file must contain a label column")
57
+
58
+ validate_prediction_columns(preds)
59
+
60
+ y_true = truth["label"]
61
+ y_pred = preds["label"]
62
+
63
+ validate_lengths(y_true, y_pred)
64
+ validate_binary_labels(y_true, "Ground truth labels")
65
+ validate_binary_labels(y_pred, "Prediction labels")
66
+
67
+ accuracy = accuracy_score(y_true, y_pred)
68
+ precision = precision_score(y_true, y_pred, zero_division=0)
69
+ recall = recall_score(y_true, y_pred, zero_division=0)
70
+ f1 = f1_score(y_true, y_pred, zero_division=0)
71
+
72
+ cm = confusion_matrix(y_true, y_pred, labels=[0, 1])
73
+ tn, fp, fn, tp = cm.ravel()
74
+
75
+ false_safe_rate = fn / (fn + tp) if (fn + tp) > 0 else 0.0
76
+ true_safe_rate = tn / (tn + fp) if (tn + fp) > 0 else 0.0
77
+ positive_prediction_rate = (tp + fp) / len(y_pred) if len(y_pred) > 0 else 0.0
78
+
79
+ return {
80
+ "target_label": TARGET_LABEL,
81
+ "n_samples": int(len(y_true)),
82
+ "accuracy": float(accuracy),
83
+ "precision": float(precision),
84
+ "recall_cascade_detection": float(recall),
85
+ "false_safe_rate": float(false_safe_rate),
86
+ "true_safe_rate": float(true_safe_rate),
87
+ "positive_prediction_rate": float(positive_prediction_rate),
88
+ "f1": float(f1),
89
+ "confusion_matrix": cm.tolist(),
90
+ }
91
+
92
+
93
+ if __name__ == "__main__":
94
+ import sys
95
+
96
+ if len(sys.argv) != 3:
97
+ raise ValueError("Usage: python scorer.py <predictions.csv> <ground_truth.csv>")
98
+
99
+ predictions_path = sys.argv[1]
100
+ ground_truth_path = sys.argv[2]
101
+
102
+ print(score(predictions_path, ground_truth_path))