Datasets:
Tasks:
Tabular Classification
Formats:
csv
Sub-tasks:
tabular-multi-class-classification
Languages:
English
Size:
< 1K
Tags:
ai-safety
instability-detection
boundary-detection
collapse-prediction
clarus-benchmark
instability-benchmarks
License:
Create scorer.py
Browse files
scorer.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import sys
|
| 3 |
+
import pandas as pd
|
| 4 |
+
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, confusion_matrix
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
REQUIRED_PRED_COLUMNS = {"scenario_id", "prediction"}
|
| 8 |
+
REQUIRED_TRUTH_COLUMNS = {"scenario_id", "label_future_collapse"}
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def validate_columns(df, required, name):
|
| 12 |
+
missing = required - set(df.columns)
|
| 13 |
+
if missing:
|
| 14 |
+
raise ValueError(f"{name} missing columns: {missing}")
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def validate_ids(df, name):
|
| 18 |
+
if df["scenario_id"].isna().any():
|
| 19 |
+
raise ValueError(f"{name} contains missing scenario_id")
|
| 20 |
+
|
| 21 |
+
if df["scenario_id"].duplicated().any():
|
| 22 |
+
raise ValueError(f"{name} contains duplicate scenario_id values")
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def validate_predictions(df):
|
| 26 |
+
if not df["prediction"].isin([0,1]).all():
|
| 27 |
+
raise ValueError("Predictions must be binary (0 or 1)")
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def main():
|
| 31 |
+
|
| 32 |
+
if len(sys.argv) != 3:
|
| 33 |
+
raise ValueError("Usage: python scorer.py predictions.csv truth.csv")
|
| 34 |
+
|
| 35 |
+
pred_path = sys.argv[1]
|
| 36 |
+
truth_path = sys.argv[2]
|
| 37 |
+
|
| 38 |
+
pred = pd.read_csv(pred_path)
|
| 39 |
+
truth = pd.read_csv(truth_path)
|
| 40 |
+
|
| 41 |
+
validate_columns(pred, REQUIRED_PRED_COLUMNS, "prediction file")
|
| 42 |
+
validate_columns(truth, REQUIRED_TRUTH_COLUMNS, "truth file")
|
| 43 |
+
|
| 44 |
+
validate_ids(pred, "prediction file")
|
| 45 |
+
validate_ids(truth, "truth file")
|
| 46 |
+
|
| 47 |
+
validate_predictions(pred)
|
| 48 |
+
|
| 49 |
+
merged = pred.merge(truth, on="scenario_id", how="inner")
|
| 50 |
+
|
| 51 |
+
y_true = merged["label_future_collapse"]
|
| 52 |
+
y_pred = merged["prediction"]
|
| 53 |
+
|
| 54 |
+
acc = accuracy_score(y_true, y_pred)
|
| 55 |
+
prec = precision_score(y_true, y_pred, zero_division=0)
|
| 56 |
+
rec = recall_score(y_true, y_pred, zero_division=0)
|
| 57 |
+
f1 = f1_score(y_true, y_pred, zero_division=0)
|
| 58 |
+
|
| 59 |
+
tn, fp, fn, tp = confusion_matrix(y_true, y_pred).ravel()
|
| 60 |
+
|
| 61 |
+
results = {
|
| 62 |
+
"accuracy": acc,
|
| 63 |
+
"precision": prec,
|
| 64 |
+
"recall_collapse": rec,
|
| 65 |
+
"f1": f1,
|
| 66 |
+
"true_negatives": int(tn),
|
| 67 |
+
"false_positives": int(fp),
|
| 68 |
+
"false_negatives": int(fn),
|
| 69 |
+
"true_positives": int(tp)
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
print(json.dumps(results, indent=2))
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
if __name__ == "__main__":
|
| 76 |
+
main()
|