| import argparse |
| import json |
| import logging |
| import sys |
| from datetime import datetime, timezone |
| from pathlib import Path |
|
|
| import pandas as pd |
| from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, confusion_matrix |
|
|
|
|
| SCORER_VERSION = "1.3.0" |
|
|
|
|
| def setup_logging(verbose: bool): |
| level = logging.DEBUG if verbose else logging.INFO |
| logging.basicConfig( |
| level=level, |
| format="%(asctime)s | %(levelname)s | %(message)s", |
| ) |
|
|
|
|
| def read_csv_checked(path: str, name: str) -> pd.DataFrame: |
| file_path = Path(path) |
|
|
| if not file_path.exists(): |
| raise FileNotFoundError(f"{name} file does not exist: {path}") |
|
|
| if not file_path.is_file(): |
| raise ValueError(f"{name} path is not a file: {path}") |
|
|
| try: |
| return pd.read_csv(file_path) |
| except Exception as e: |
| raise ValueError(f"Could not read {name} CSV at {path}: {e}") |
|
|
|
|
| def validate_output_path(path: str): |
| output_path = Path(path) |
| parent = output_path.parent |
|
|
| if parent and not parent.exists(): |
| raise FileNotFoundError(f"Output directory does not exist: {parent}") |
|
|
| if output_path.exists() and output_path.is_dir(): |
| raise ValueError(f"Output path is a directory, not a file: {path}") |
|
|
|
|
| def validate_columns(df, required, name): |
| missing = [c for c in required if c not in df.columns] |
| if missing: |
| raise ValueError(f"{name} missing required columns: {missing}") |
|
|
|
|
| def validate_no_duplicates(df, column, name): |
| dupes = df[df[column].duplicated()][column].tolist() |
| if dupes: |
| raise ValueError(f"{name} contains duplicate {column} values: {dupes}") |
|
|
|
|
| def validate_binary_integer_column(df, column, name): |
| invalid = [] |
|
|
| for idx, value in df[column].items(): |
| if pd.isna(value): |
| invalid.append({"row": int(idx), column: None}) |
| continue |
|
|
| if not isinstance(value, (int, bool)) and not ( |
| isinstance(value, float) and value.is_integer() |
| ): |
| invalid.append({"row": int(idx), column: value}) |
| continue |
|
|
| if int(value) not in [0, 1]: |
| invalid.append({"row": int(idx), column: value}) |
|
|
| if invalid: |
| raise ValueError(f"{name} has invalid binary integer values in {column}: {invalid}") |
|
|
| df[column] = df[column].astype(int) |
|
|
|
|
| def dataset_integrity_report(truth, corr_threshold, min_positive_rate, max_positive_rate): |
| feature_cols = [ |
| c for c in truth.columns |
| if c not in ["scenario_id", "label"] |
| and pd.api.types.is_numeric_dtype(truth[c]) |
| ] |
|
|
| skipped_non_numeric = [ |
| c for c in truth.columns |
| if c not in ["scenario_id", "label"] |
| and not pd.api.types.is_numeric_dtype(truth[c]) |
| ] |
|
|
| label_counts = truth["label"].value_counts().to_dict() |
| total = len(truth) |
|
|
| positive_rate = float(label_counts.get(1, 0) / total) if total else 0.0 |
|
|
| label_balance = { |
| "label_0": int(label_counts.get(0, 0)), |
| "label_1": int(label_counts.get(1, 0)), |
| "positive_rate": positive_rate, |
| } |
|
|
| correlations = {} |
|
|
| for col in feature_cols: |
| corr = truth[col].corr(truth["label"]) |
| if pd.isna(corr): |
| corr = 0.0 |
| correlations[col] = float(corr) |
|
|
| high_corr_features = { |
| col: corr |
| for col, corr in correlations.items() |
| if abs(corr) >= corr_threshold |
| } |
|
|
| return { |
| "num_rows": int(total), |
| "num_features_checked": int(len(feature_cols)), |
| "skipped_non_numeric_features": skipped_non_numeric, |
| "label_balance": label_balance, |
| "correlation_threshold": float(corr_threshold), |
| "balance_range": { |
| "min_positive_rate": float(min_positive_rate), |
| "max_positive_rate": float(max_positive_rate), |
| }, |
| "max_abs_feature_label_correlation": float( |
| max([abs(v) for v in correlations.values()], default=0.0) |
| ), |
| "high_corr_features": high_corr_features, |
| "passes_basic_integrity_check": ( |
| min_positive_rate <= positive_rate <= max_positive_rate |
| and len(high_corr_features) == 0 |
| ), |
| } |
|
|
|
|
| def run_scoring(args): |
| logging.info("Loading predictions from %s", args.predictions) |
| pred = read_csv_checked(args.predictions, "predictions") |
|
|
| logging.info("Loading truth from %s", args.truth) |
| truth = read_csv_checked(args.truth, "truth") |
|
|
| validate_columns(pred, ["scenario_id", "prediction"], "predictions") |
| validate_columns(truth, ["scenario_id", "label"], "truth") |
|
|
| validate_no_duplicates(pred, "scenario_id", "predictions") |
| validate_no_duplicates(truth, "scenario_id", "truth") |
|
|
| validate_binary_integer_column(pred, "prediction", "predictions") |
| validate_binary_integer_column(truth, "label", "truth") |
|
|
| merged = truth[["scenario_id", "label"]].merge( |
| pred[["scenario_id", "prediction"]], |
| on="scenario_id", |
| how="left", |
| indicator=True, |
| ) |
|
|
| missing = merged[merged["_merge"] == "left_only"]["scenario_id"].tolist() |
| if missing: |
| raise ValueError(f"Missing predictions for scenario_id: {missing}") |
|
|
| extra = pred[~pred["scenario_id"].isin(truth["scenario_id"])]["scenario_id"].tolist() |
| if extra: |
| raise ValueError(f"Predictions contain unknown scenario_id: {extra}") |
|
|
| y_true = merged["label"].astype(int) |
| y_pred = merged["prediction"].astype(int) |
|
|
| pred_counts = y_pred.value_counts().to_dict() |
| warnings = [] |
|
|
| if len(pred_counts) == 1: |
| warning = "Degenerate prediction set: all predictions are one class." |
| logging.warning(warning) |
| warnings.append(warning) |
|
|
| metrics = { |
| "scorer_version": SCORER_VERSION, |
| "timestamp_utc": datetime.now(timezone.utc).isoformat(), |
| "status": "success", |
| "num_examples": int(len(merged)), |
| "prediction_distribution": { |
| "predicted_0": int(pred_counts.get(0, 0)), |
| "predicted_1": int(pred_counts.get(1, 0)), |
| }, |
| "warnings": warnings, |
| "accuracy": float(accuracy_score(y_true, y_pred)), |
| "precision": float(precision_score(y_true, y_pred, zero_division=0)), |
| "recall": float(recall_score(y_true, y_pred, zero_division=0)), |
| "f1": float(f1_score(y_true, y_pred, zero_division=0)), |
| "confusion_matrix": { |
| "labels": [0, 1], |
| "matrix": confusion_matrix(y_true, y_pred, labels=[0, 1]).tolist(), |
| }, |
| "dataset_integrity": dataset_integrity_report( |
| truth, |
| corr_threshold=args.corr_threshold, |
| min_positive_rate=args.min_positive_rate, |
| max_positive_rate=args.max_positive_rate, |
| ), |
| } |
|
|
| return metrics |
|
|
|
|
| def main(): |
| parser = argparse.ArgumentParser( |
| description="ClarusC64 binary prediction scorer with CI-safe validation and dataset integrity checks" |
| ) |
|
|
| parser.add_argument( |
| "--predictions", |
| required=True, |
| help="CSV file with scenario_id,prediction", |
| ) |
|
|
| parser.add_argument( |
| "--truth", |
| default="data/test.csv", |
| help="Truth CSV with scenario_id,label. Default: data/test.csv", |
| ) |
|
|
| parser.add_argument( |
| "--output", |
| default="metrics.json", |
| help="Output JSON file. Default: metrics.json", |
| ) |
|
|
| parser.add_argument( |
| "--corr-threshold", |
| type=float, |
| default=0.30, |
| help="Feature-label correlation warning threshold. Default: 0.30", |
| ) |
|
|
| parser.add_argument( |
| "--min-positive-rate", |
| type=float, |
| default=0.35, |
| help="Minimum expected positive label rate. Default: 0.35", |
| ) |
|
|
| parser.add_argument( |
| "--max-positive-rate", |
| type=float, |
| default=0.65, |
| help="Maximum expected positive label rate. Default: 0.65", |
| ) |
|
|
| parser.add_argument( |
| "--verbose", |
| action="store_true", |
| help="Enable verbose logging", |
| ) |
|
|
| args = parser.parse_args() |
|
|
| setup_logging(args.verbose) |
|
|
| try: |
| validate_output_path(args.output) |
|
|
| metrics = run_scoring(args) |
|
|
| with open(args.output, "w", encoding="utf-8") as f: |
| json.dump(metrics, f, indent=2, allow_nan=False) |
|
|
| print(json.dumps(metrics, indent=2, allow_nan=False)) |
| sys.exit(0) |
|
|
| except Exception as e: |
| logging.error(str(e)) |
|
|
| error = { |
| "scorer_version": SCORER_VERSION, |
| "timestamp_utc": datetime.now(timezone.utc).isoformat(), |
| "status": "error", |
| "message": str(e), |
| } |
|
|
| print(json.dumps(error, indent=2), file=sys.stderr) |
| sys.exit(1) |
|
|
|
|
| if __name__ == "__main__": |
| main() |