Datasets:
Download scorer.py from ClarusC64/clinical-escalation-discipline-v0.1: direct link, hf CLI and curl.
- Browser
- Download file 3.05 kB
-
https://huggingface.co/datasets/ClarusC64/clinical-escalation-discipline-v0.1/resolve/main/scorer.py
- Command line
-
hf download hf://datasets/ClarusC64/clinical-escalation-discipline-v0.1/scorer.py
-
curl -L -o scorer.py https://huggingface.co/datasets/ClarusC64/clinical-escalation-discipline-v0.1/resolve/main/scorer.py
3.05 kB
| import pandas as pd | |
| from sklearn.metrics import ( | |
| accuracy_score, precision_score, recall_score, | |
| f1_score, confusion_matrix | |
| ) | |
| import json | |
| import sys | |
| def main(): | |
| if len(sys.argv) < 2: | |
| print("Usage: python scorer.py <predictions.csv> [truth.csv]") | |
| sys.exit(1) | |
| predictions_file = sys.argv[1] | |
| truth_file = sys.argv[2] if len(sys.argv) > 2 else "data/test.csv" | |
| try: | |
| pred = pd.read_csv(predictions_file) | |
| except FileNotFoundError: | |
| print(f"Error: predictions file '{predictions_file}' not found.") | |
| sys.exit(1) | |
| try: | |
| truth = pd.read_csv(truth_file) | |
| except FileNotFoundError: | |
| print(f"Error: truth file '{truth_file}' not found.") | |
| sys.exit(1) | |
| required_pred = {"scenario_id", "prediction"} | |
| required_truth = {"scenario_id", "label"} | |
| if not required_pred.issubset(pred.columns): | |
| print("Error: submission must contain columns: scenario_id, prediction") | |
| sys.exit(1) | |
| if not required_truth.issubset(truth.columns): | |
| print("Error: truth file must contain columns: scenario_id, label") | |
| sys.exit(1) | |
| if pred["scenario_id"].duplicated().any(): | |
| dupes = pred.loc[pred["scenario_id"].duplicated(), "scenario_id"].tolist() | |
| print(f"Error: duplicate scenario_id values in submission: {dupes}") | |
| sys.exit(1) | |
| try: | |
| pred["prediction"] = pd.to_numeric(pred["prediction"]) | |
| except Exception: | |
| print("Error: prediction column must contain numeric values.") | |
| sys.exit(1) | |
| if pred["prediction"].isna().any(): | |
| print("Error: prediction column contains missing values.") | |
| sys.exit(1) | |
| non_integer = pred[ | |
| pred["prediction"] != pred["prediction"].astype(int) | |
| ] | |
| if len(non_integer) > 0: | |
| print("Error: prediction column contains non-integer values.") | |
| print("Predictions must be binary: 0 or 1.") | |
| sys.exit(1) | |
| pred["prediction"] = pred["prediction"].astype(int) | |
| invalid = set(pred["prediction"].unique()) - {0, 1} | |
| if invalid: | |
| print(f"Error: prediction column contains invalid values: {invalid}") | |
| print("Predictions must be binary: 0 or 1.") | |
| sys.exit(1) | |
| merged = truth.merge(pred, on="scenario_id", how="left", indicator=True) | |
| missing = merged.loc[merged["_merge"] != "both", "scenario_id"].tolist() | |
| if missing: | |
| print(f"Error: missing predictions for {len(missing)} scenario(s): {missing}") | |
| sys.exit(1) | |
| y_true = merged["label"] | |
| y_pred = merged["prediction"] | |
| results = { | |
| "accuracy": round(float(accuracy_score(y_true, y_pred)), 4), | |
| "precision": round(float(precision_score(y_true, y_pred, zero_division=0)), 4), | |
| "recall": round(float(recall_score(y_true, y_pred, zero_division=0)), 4), | |
| "f1": round(float(f1_score(y_true, y_pred, zero_division=0)), 4), | |
| "confusion_matrix": confusion_matrix(y_true, y_pred).tolist() | |
| } | |
| print(json.dumps(results, indent=2)) | |
| if __name__ == "__main__": | |
| main() |