Create scorer.py
Browse files
scorer.py
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import csv
|
| 4 |
+
import json
|
| 5 |
+
import sys
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
from typing import Dict, List, Tuple
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
PREDICTION_COLUMN_CANDIDATES = [
|
| 11 |
+
"prediction",
|
| 12 |
+
"pred",
|
| 13 |
+
"predicted_label",
|
| 14 |
+
"label",
|
| 15 |
+
]
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def _to_int(value: str) -> int:
|
| 19 |
+
value = str(value).strip()
|
| 20 |
+
return 1 if value in {"1", "1.0", "true", "True"} else 0
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def _load_csv(path: Path) -> List[Dict[str, str]]:
|
| 24 |
+
with path.open("r", encoding="utf-8", newline="") as f:
|
| 25 |
+
return list(csv.DictReader(f))
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def _find_label_column(rows: List[Dict[str, str]]) -> str:
|
| 29 |
+
if not rows:
|
| 30 |
+
raise ValueError("Ground truth file is empty.")
|
| 31 |
+
label_cols = [c for c in rows[0].keys() if c.startswith("label_")]
|
| 32 |
+
if len(label_cols) == 1:
|
| 33 |
+
return label_cols[0]
|
| 34 |
+
raise ValueError(f"Expected one label column, found: {label_cols}")
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def _find_prediction_column(rows: List[Dict[str, str]]) -> str:
|
| 38 |
+
if not rows:
|
| 39 |
+
raise ValueError("Prediction file is empty.")
|
| 40 |
+
|
| 41 |
+
available = set(rows[0].keys())
|
| 42 |
+
|
| 43 |
+
for col in PREDICTION_COLUMN_CANDIDATES:
|
| 44 |
+
if col in available:
|
| 45 |
+
return col
|
| 46 |
+
|
| 47 |
+
label_like_cols = [c for c in rows[0].keys() if c.startswith("label_")]
|
| 48 |
+
if len(label_like_cols) == 1:
|
| 49 |
+
return label_like_cols[0]
|
| 50 |
+
|
| 51 |
+
raise ValueError(
|
| 52 |
+
"No prediction column found. Expected one of "
|
| 53 |
+
f"{PREDICTION_COLUMN_CANDIDATES} or a single label_* column."
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def _confusion(y_true: List[int], y_pred: List[int]) -> Tuple[int, int, int, int]:
|
| 58 |
+
tp = fp = tn = fn = 0
|
| 59 |
+
for yt, yp in zip(y_true, y_pred):
|
| 60 |
+
if yt == 1 and yp == 1:
|
| 61 |
+
tp += 1
|
| 62 |
+
elif yt == 0 and yp == 1:
|
| 63 |
+
fp += 1
|
| 64 |
+
elif yt == 0 and yp == 0:
|
| 65 |
+
tn += 1
|
| 66 |
+
elif yt == 1 and yp == 0:
|
| 67 |
+
fn += 1
|
| 68 |
+
return tp, fp, tn, fn
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
def _safe_div(n: float, d: float) -> float:
|
| 72 |
+
return n / d if d else 0.0
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
def score(ground_truth_path: str, prediction_path: str) -> Dict[str, object]:
|
| 76 |
+
gt_rows = _load_csv(Path(ground_truth_path))
|
| 77 |
+
pred_rows = _load_csv(Path(prediction_path))
|
| 78 |
+
|
| 79 |
+
if len(gt_rows) != len(pred_rows):
|
| 80 |
+
raise ValueError(
|
| 81 |
+
f"Row count mismatch: ground truth has {len(gt_rows)} rows, "
|
| 82 |
+
f"predictions have {len(pred_rows)} rows."
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
label_column = _find_label_column(gt_rows)
|
| 86 |
+
pred_col = _find_prediction_column(pred_rows)
|
| 87 |
+
|
| 88 |
+
y_true = [_to_int(row[label_column]) for row in gt_rows]
|
| 89 |
+
y_pred = [_to_int(row[pred_col]) for row in pred_rows]
|
| 90 |
+
|
| 91 |
+
tp, fp, tn, fn = _confusion(y_true, y_pred)
|
| 92 |
+
|
| 93 |
+
accuracy = _safe_div(tp + tn, tp + tn + fp + fn)
|
| 94 |
+
precision = _safe_div(tp, tp + fp)
|
| 95 |
+
recall_boundary_detection = _safe_div(tp, tp + fn)
|
| 96 |
+
false_safe_rate = _safe_div(fn, fn + tp)
|
| 97 |
+
f1 = _safe_div(
|
| 98 |
+
2 * precision * recall_boundary_detection,
|
| 99 |
+
precision + recall_boundary_detection,
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
return {
|
| 103 |
+
"label_column": label_column,
|
| 104 |
+
"prediction_column": pred_col,
|
| 105 |
+
"accuracy": round(accuracy, 6),
|
| 106 |
+
"precision": round(precision, 6),
|
| 107 |
+
"recall_boundary_detection": round(recall_boundary_detection, 6),
|
| 108 |
+
"false_safe_rate": round(false_safe_rate, 6),
|
| 109 |
+
"f1": round(f1, 6),
|
| 110 |
+
"confusion_matrix": {
|
| 111 |
+
"tp": tp,
|
| 112 |
+
"fp": fp,
|
| 113 |
+
"tn": tn,
|
| 114 |
+
"fn": fn,
|
| 115 |
+
},
|
| 116 |
+
"primary_metric": "recall_boundary_detection",
|
| 117 |
+
"secondary_metric": "false_safe_rate",
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
if __name__ == "__main__":
|
| 122 |
+
if len(sys.argv) != 3:
|
| 123 |
+
raise SystemExit("Usage: python scorer.py <ground_truth.csv> <predictions.csv>")
|
| 124 |
+
|
| 125 |
+
results = score(sys.argv[1], sys.argv[2])
|
| 126 |
+
print(json.dumps(results, indent=2))
|