ClarusC64 commited on
Commit
3fbc8ba
·
verified ·
1 Parent(s): 6f62e7d

Create scorer.py

Browse files
Files changed (1) hide show
  1. scorer.py +65 -0
scorer.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sys
2
+ import pandas as pd
3
+ from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, confusion_matrix
4
+
5
+
6
+ def resolve_label(df: pd.DataFrame) -> pd.Series:
7
+ label_cols = [c for c in df.columns if c.startswith("label_")]
8
+ if len(label_cols) == 1:
9
+ return df[label_cols[0]]
10
+ raise ValueError(f"Expected one label column, found: {label_cols}")
11
+
12
+
13
+ def align_frames(preds: pd.DataFrame, truth: pd.DataFrame) -> tuple[pd.DataFrame, pd.DataFrame]:
14
+ if len(preds) != len(truth):
15
+ raise ValueError(
16
+ f"Row count mismatch: predictions has {len(preds)} rows, truth has {len(truth)} rows"
17
+ )
18
+
19
+ if "scenario_id" in preds.columns and "scenario_id" in truth.columns:
20
+ preds = preds.sort_values("scenario_id").reset_index(drop=True)
21
+ truth = truth.sort_values("scenario_id").reset_index(drop=True)
22
+
23
+ if not preds["scenario_id"].equals(truth["scenario_id"]):
24
+ raise ValueError("scenario_id mismatch after alignment between predictions and truth")
25
+
26
+ return preds, truth
27
+
28
+
29
+ def score(predictions_path: str, ground_truth_path: str) -> dict:
30
+ preds = pd.read_csv(predictions_path)
31
+ truth = pd.read_csv(ground_truth_path)
32
+
33
+ preds, truth = align_frames(preds, truth)
34
+
35
+ y_pred = resolve_label(preds)
36
+ y_true = resolve_label(truth)
37
+
38
+ accuracy = accuracy_score(y_true, y_pred)
39
+ precision = precision_score(y_true, y_pred, zero_division=0)
40
+ recall = recall_score(y_true, y_pred, zero_division=0)
41
+ f1 = f1_score(y_true, y_pred, zero_division=0)
42
+
43
+ tn, fp, fn, tp = confusion_matrix(y_true, y_pred, labels=[0, 1]).ravel()
44
+
45
+ false_safe_rate = fn / (fn + tp) if (fn + tp) > 0 else 0.0
46
+
47
+ return {
48
+ "accuracy": accuracy,
49
+ "precision": precision,
50
+ "recall_cascade_detection": recall,
51
+ "false_safe_rate": false_safe_rate,
52
+ "f1": f1,
53
+ "confusion_matrix": {
54
+ "tp": int(tp),
55
+ "fp": int(fp),
56
+ "tn": int(tn),
57
+ "fn": int(fn),
58
+ },
59
+ }
60
+
61
+
62
+ if __name__ == "__main__":
63
+ if len(sys.argv) != 3:
64
+ raise SystemExit("Usage: python scorer.py <predictions.csv> <ground_truth.csv>")
65
+ print(score(sys.argv[1], sys.argv[2]))