scxpand.util.metrics#
Functions
|
Calculate comprehensive evaluation metrics for T-cell expansion prediction. |
|
|
|
Evaluate model performance, save results, optionally create plots, and report Optuna trial results if provided. |
Extract a metric value from a nested dictionary. |
|
|
- scxpand.util.metrics.calculate_metrics(y_true, y_pred_prob, obs_df, threshold=0.5)#
Calculate comprehensive evaluation metrics for T-cell expansion prediction.
Computes overall classification metrics plus stratified metrics by cell type and tissue type. Includes robust error handling for edge cases.
- Parameters:
y_true (
ndarray|Tensor) – True binary labels (0=not expanded, 1=expanded).y_pred_prob (
ndarray|Tensor) – Predicted probabilities for expansion [0-1].obs_df (
DataFrame) – DataFrame with ‘imputed_labels’ and ‘tissue_type’ columns.threshold (
float(default:0.5)) – Classification threshold for converting probabilities to predictions.
- Return type:
- Returns:
Nested dictionary with ‘overall’ metrics and stratified metrics by tissue_type and imputed_labels combinations. Each contains: - AUROC: Area under ROC curve - F1: F1 score - error_rate: Classification error rate - false_positive_rate, false_negative_rate: FPR and FNR - RMSE: Root mean squared error - positives_rate: Fraction of positive samples
- scxpand.util.metrics.compute_basic_metrics(y_true, y_pred_prob, threshold=0.5)#
- scxpand.util.metrics.evaluate_and_save(y_true, y_pred_prob, obs_df, eval_name='', save_path=None, plots_dir=None, threshold=0.5, trial=None, score_metric='harmonic_avg/AUROC', use_table_format=True)#
Evaluate model performance, save results, optionally create plots, and report Optuna trial results if provided.
- Parameters:
y_true (
ndarray|Tensor) – Ground truth labels [n_eval] (0 or 1)y_pred_prob (
ndarray|Tensor) – Predicted probabilities [n_eval] in range [0, 1]obs_df (
DataFrame) – DataFrame containing observation dataeval_name (
str(default:'')) – Name for the evaluation (used in file names)save_path (
Path|None(default:None)) – Path to save resultsplots_dir (
Path|None(default:None)) – Optional path to save plotsthreshold (
float(default:0.5)) – Classification thresholdtrial (
Trial|None(default:None)) – Optional Optuna trial for reportingscore_metric (
str(default:'harmonic_avg/AUROC')) – Metric key to report to Optunause_table_format (
bool(default:True)) – If True, display metrics in table format
- Returns:
Dictionary containing the evaluation metrics
- Return type:
- scxpand.util.metrics.get_score_from_nested_dict(nested_metrics_dict, metric_name)#
Extract a metric value from a nested dictionary.