Metrics
Available metrics
Metrics are selected by name in the metrics setting of the configuration file. The tables list the names registered by default.
Classification
| Name | Description | Uses |
|---|---|---|
accuracy |
Proportion of all classifications that were correct. | Class labels |
precision |
Proportion of positive classifications that are actually positive. | Class labels |
recall |
Proportion of actual positives that were classified correctly (true positive rate). | Class labels |
f1 |
Harmonic mean of precision and recall. | Class labels |
roc_auc, auroc |
Area under the ROC curve. Both names are the same metric. | Decision function or probabilities |
ap |
Average precision, the area under the precision-recall curve. | Decision function or probabilities |
log_loss |
Logarithmic loss. | Probabilities |
brier_score |
Brier score loss. | Probabilities |
ici |
Integrated calibration index, the mean absolute difference between predicted probabilities and a spline-smoothed calibration curve. | Probabilities |
Metrics that use class labels convert predicted probabilities to labels before they are computed.
Regression
| Name | Description | Uses |
|---|---|---|
rmse |
Root mean squared error. | Point predictions |
mae |
Mean absolute error. | Point predictions |
crps |
Continuous ranked probability score, averaged over samples (lower is better). | Predictive distribution |
Adding a metric
Describe the metric with a MetricSpec and register it with MetricRegistry. It can then be used by name in the configuration file.
from functools import partial
from sklearn.metrics import fbeta_score
from medpipe import MetricRegistry, MetricSpec
MetricRegistry.register_spec(
MetricSpec(
name="f2",
func=partial(fbeta_score, beta=2),
response_method="predict",
display_name="F2",
needs_threshold=True,
)
)
Register the metric before creating the pipeline, so that the configuration is validated against it. A metric that uses a predictive distribution should define a per_sample_func, as explained in confidence intervals.
A complete script that registers a model, a preprocessing operation and a metric is shown in the examples.
core
Core metric functions module.
PredictionBundle
dataclass
Pairs flat point predictions with an optional full predictive
distribution, letting compute_metrics/bootstrap_confidence_intervals
serve both point-based metrics (rmse, mae, accuracy, ...) and
distribution-based metrics (crps) from a single call.
A bare numpy.ndarray remains accepted everywhere a PredictionBundle
is accepted, and is treated as PredictionBundle(point=array, dist=None)
- existing point-array-only callers are unaffected.
Attributes:
| Name | Type | Description |
|---|---|---|
point |
npt.NDArray or None, default=None
|
Flat point predictions (or probabilities/decision scores for
classification), used by every metric except |
dist |
PredictiveDistribution or None, default=None
|
A full predictive distribution object (e.g. returned by
|
Source code in medpipe/metrics/core.py
ici_score
Computes the integrated calibration index using a spline-based curve.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
NDArray
|
Ground truth binary target labels. |
required |
y_pred
|
NDArray
|
Predicted probabilities of shape (n_samples,) or (n_samples, 2). |
required |
cv_splines
|
int
|
Number of internal cross-validation folds |
3
|
Returns:
| Type | Description |
|---|---|
float
|
The integrated calibration index, the mean absolute difference between the predicted probabilities and their spline-smoothed values (lower is better). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the spline calibration fails to produce predictions. |
Source code in medpipe/metrics/core.py
crps_score
Computes the mean Continuous Ranked Probability Score (CRPS) for a
predictive distribution. See _crps_values for the per-sample
computation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
NDArray
|
Ground truth continuous target values of shape (n_samples,). |
required |
dist
|
PredictiveDistribution
|
A predictive distribution object exposing |
required |
Returns:
| Type | Description |
|---|---|
float
|
The mean CRPS across samples (lower is better). |
Source code in medpipe/metrics/core.py
build_scorers
Build the dictionary of scorers for cross-validation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metrics
|
list[str]
|
List of metrics to use. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
scorers |
dict[str, Callable]
|
Dictionary of scorers to pass to cross_validate. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If metrics is not a list of strings. |
ValueError
|
If a metric is not a valid option. |
Source code in medpipe/metrics/core.py
compute_metrics
compute_metrics(
metrics: list[str] | NDArray,
y: NDArray,
y_pred: NDArray | PredictionBundle,
) -> NDArray
Computes metrics based on predicted data.
Scores are located at the same index as in the metrics array.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metrics
|
list[str]
|
List of metrics to use. |
required |
y
|
NDArray
|
Ground truth labels. |
required |
y_pred
|
NDArray or PredictionBundle
|
Predictions from the model, either a flat array of shape
(n_samples,) or (n_samples, 2), or a |
required |
Returns:
| Name | Type | Description |
|---|---|---|
scores |
NDArray
|
Score array of shape (n_metrics,). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If metrics is not a list of strings. If y is not a np.ndarray. If y_pred is not a np.ndarray or PredictionBundle. |
ValueError
|
If there is only one class for AUROC and AP calculations.
If a |
Source code in medpipe/metrics/core.py
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 | |
bootstrap_confidence_intervals
bootstrap_confidence_intervals(
metrics: list[str],
y_true: NDArray,
y_pred: NDArray | PredictionBundle,
n_bootstraps: int = 1000,
ci_level: float = 0.95,
random_state: int | Generator | None = None,
n_jobs: int | None = 1,
) -> dict[str, dict[str, float]]
Compute non-parametric bootstrap confidence intervals for metrics.
Resamples paired target labels and predictions with replacement, evaluating all specified metrics across bootstrap iterations. Edge cases where resamples lack sufficient class diversity (e.g., single-class bootstrap draws) are safely caught and skipped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metrics
|
list[str]
|
Metric identifier keys registered in |
required |
y_true
|
NDArray
|
Ground truth binary target labels of shape (n_samples,). |
required |
y_pred
|
NDArray or PredictionBundle
|
Predicted probabilities or decision values of shape (n_samples,) or
(n_samples, 2), or a |
required |
n_bootstraps
|
int
|
Number of bootstrap resampling iterations. |
1000
|
ci_level
|
float
|
Confidence level for the calculated interval bounds (e.g., 0.95 for 95% CI). |
0.95
|
random_state
|
int, np.random.Generator, or None
|
Seed or random generator instance to ensure reproducible resampling. |
None
|
n_jobs
|
int or None
|
Number of parallel jobs used to compute the per-resample metrics
(e.g. |
1
|
Returns:
| Name | Type | Description |
|---|---|---|
ci_results |
dict[str, dict[str, float]]
|
Dictionary mapping each metric key to a dictionary containing:
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in medpipe/metrics/core.py
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 | |
registry
MetricSpec
dataclass
Encapsulates execution details and metadata for an evaluation metric.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Unique lookup identifier for the metric. |
required |
func
|
Callable
|
Underlying metric evaluation function. |
required |
response_method
|
str or tuple of str
|
Scikit-learn model prediction method required by the metric (e.g., 'predict', 'predict_proba'). |
required |
display_name
|
str
|
Human-readable name used for visual displays and reports. |
required |
sklearn_scorer_name
|
str or None
|
Optional pre-registered scikit-learn scorer string key. |
None
|
needs_threshold
|
bool
|
Whether raw predictions must be rounded to discrete class labels
before being passed to |
False
|
per_sample_func
|
Callable or None
|
For |
None
|
Source code in medpipe/metrics/registry.py
get_scorer
Construct a scikit-learn compatible scorer function for cross-validation.
Returns:
| Name | Type | Description |
|---|---|---|
scorer |
Callable
|
Scikit-learn scorer object suitable for model evaluation or tuning. |
Source code in medpipe/metrics/registry.py
MetricRegistry
Bases: BaseRegistry[MetricSpec]
Registry managing available and custom MetricSpec definitions.
Source code in medpipe/metrics/registry.py
register_spec
classmethod
register_spec(spec: MetricSpec) -> MetricSpec
Register a MetricSpec instance directly into the registry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
MetricSpec
|
The metric specification instance to register. |
required |
Returns:
| Type | Description |
|---|---|
MetricSpec
|
The registered metric specification instance. |
Source code in medpipe/metrics/registry.py
get
classmethod
get(name: str) -> MetricSpec
Retrieve a registered MetricSpec instance by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name identifier of the metric to retrieve. |
required |
Returns:
| Type | Description |
|---|---|
MetricSpec
|
The matching metric specification instance. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the requested metric name is not found in the registry. |