|
| 1 | +""" |
| 2 | +Metric calculation script for ProteinGym benchmark evaluation. |
| 3 | +
|
| 4 | +This script provides functionality to calculate performance metrics for machine learning models |
| 5 | +by comparing actual and predicted values. It computes classification metrics via confusion |
| 6 | +matrix from CSV output files. |
| 7 | +
|
| 8 | +The main function `calc` reads prediction results from a CSV file, generates a confusion matrix |
| 9 | +with comprehensive classification statistics, and outputs all metrics to a CSV file for further analysis. |
| 10 | +
|
| 11 | +Example output CSV: |
| 12 | + | Metric | Value | |
| 13 | + |--------------|------------| |
| 14 | + | Overall ACC | 0.85 | |
| 15 | + | PPV Macro | 'None' | |
| 16 | + | Kappa 95% CI | (0.0, 0.0) | |
| 17 | +
|
| 18 | +Functions: |
| 19 | + calc: Calculate and save performance metrics from prediction output files |
| 20 | +""" |
| 21 | + |
| 22 | +import argparse |
| 23 | +from pathlib import Path |
| 24 | + |
| 25 | +import polars as pl |
| 26 | +from pycm import ConfusionMatrix |
| 27 | + |
| 28 | + |
| 29 | +def calc( |
| 30 | + output: Path, metric: Path, actual_vector_col: str, predict_vector_col: str |
| 31 | +) -> Path: |
| 32 | + """Calculate performance metrics from prediction output and save to CSV. |
| 33 | +
|
| 34 | + Reads prediction results from a CSV file, computes classification metrics using |
| 35 | + a confusion matrix. All metrics are saved to a CSV file. |
| 36 | +
|
| 37 | + Args: |
| 38 | + output: Path to the CSV file containing prediction results |
| 39 | + metric: Path where the calculated metrics CSV will be saved |
| 40 | + actual_vector_col: Column name containing actual/ground truth values |
| 41 | + predict_vector_col: Column name containing predicted values |
| 42 | + """ |
| 43 | + |
| 44 | + print("Start to calculate metrics.") |
| 45 | + |
| 46 | + output_dataframe = pl.read_csv(output) |
| 47 | + |
| 48 | + cm = ConfusionMatrix( |
| 49 | + actual_vector=output_dataframe[actual_vector_col].to_list(), |
| 50 | + predict_vector=output_dataframe[predict_vector_col].to_list(), |
| 51 | + ) |
| 52 | + |
| 53 | + metrics_data = [ |
| 54 | + {"metric_name": key, "metric_value": str(value)} |
| 55 | + for key, value in cm.overall_stat.items() |
| 56 | + ] |
| 57 | + |
| 58 | + metric_dataframe = pl.DataFrame( |
| 59 | + data=metrics_data, |
| 60 | + schema={"metric_name": pl.String, "metric_value": pl.String}, |
| 61 | + ) |
| 62 | + |
| 63 | + metric_dataframe.write_csv(metric) |
| 64 | + |
| 65 | + return metric |
| 66 | + |
| 67 | + |
| 68 | +def main(): |
| 69 | + parser = argparse.ArgumentParser( |
| 70 | + description="Calculate metric for ProteinGym benchmark evaluation." |
| 71 | + ) |
| 72 | + |
| 73 | + parser.add_argument( |
| 74 | + "--output", |
| 75 | + type=Path, |
| 76 | + required=True, |
| 77 | + help="Path to the CSV file containing prediction results", |
| 78 | + ) |
| 79 | + parser.add_argument( |
| 80 | + "--metric", |
| 81 | + type=Path, |
| 82 | + required=True, |
| 83 | + help="Path where the calculated metrics CSV will be saved", |
| 84 | + ) |
| 85 | + parser.add_argument( |
| 86 | + "--actual-vector-col", |
| 87 | + type=str, |
| 88 | + required=True, |
| 89 | + help="Column name containing actual/ground truth values", |
| 90 | + ) |
| 91 | + parser.add_argument( |
| 92 | + "--predict-vector-col", |
| 93 | + type=str, |
| 94 | + required=True, |
| 95 | + help="Column name containing predicted values", |
| 96 | + ) |
| 97 | + |
| 98 | + args = parser.parse_args() |
| 99 | + |
| 100 | + return calc( |
| 101 | + output=args.output, |
| 102 | + metric=args.metric, |
| 103 | + actual_vector_col=args.actual_vector_col, |
| 104 | + predict_vector_col=args.predict_vector_col, |
| 105 | + ) |
| 106 | + |
| 107 | + |
| 108 | +if __name__ == "__main__": |
| 109 | + print(f"Metrics have been saved to {main()}.") |
0 commit comments