octopus.metrics
Init metrics.
MLType
Metric
Metric instance.
Represents a metric with its configuration and calculation methods.
Source code in octopus/metrics/config.py
direction
property
Optimization direction for Optuna ('maximize' or 'minimize').
calculate(y_true, y_pred, **kwargs)
Calculate metric for classification/regression tasks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_true
|
OctoArrayLike
|
True target values |
required |
y_pred
|
OctoArrayLike
|
Predicted values (predictions or probabilities depending on prediction_type) |
required |
**kwargs
|
Additional keyword arguments passed to metric function |
{}
|
Returns:
| Type | Description |
|---|---|
float
|
Metric value as float |
Raises:
| Type | Description |
|---|---|
ValueError
|
If called on a time-to-event metric |
Source code in octopus/metrics/config.py
calculate_t2e(event_indicator, event_time, estimate, **kwargs)
Calculate metric for time-to-event tasks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_indicator
|
OctoArrayLike
|
Boolean array indicating whether event occurred |
required |
event_time
|
OctoArrayLike
|
Array of event/censoring times |
required |
estimate
|
OctoArrayLike
|
Predicted risk/survival estimates from model |
required |
**kwargs
|
Additional keyword arguments passed to metric function |
{}
|
Returns:
| Type | Description |
|---|---|
float
|
Metric value as float |
Raises:
| Type | Description |
|---|---|
ValueError
|
If called on a non-time-to-event metric |
Source code in octopus/metrics/config.py
Metrics
Central registry for metrics.
Usage
Get metric instance
metric = Metrics.get_instance("AUCROC") metric.calculate(y_true, y_pred)
Get direction
direction = Metrics.get_direction("AUCROC")
Source code in octopus/metrics/core.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | |
get_all_metrics()
classmethod
Get all registered metric factory functions.
Returns:
| Type | Description |
|---|---|
dict[str, Callable[[], Metric]]
|
Dictionary mapping metric names to their factory functions. |
get_by_type(*ml_types)
classmethod
Get list of metric names for specified ML types.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*ml_types
|
MLType
|
One or more MLType enums (e.g., MLType.REGRESSION, MLType.BINARY, MLType.MULTICLASS). |
()
|
Returns:
| Type | Description |
|---|---|
list[str]
|
List of metric names matching any of the specified ML types. |
Example
Metrics.get_by_type(MLType.REGRESSION) ['RMSE', 'MAE', 'R2', ...] Metrics.get_by_type(MLType.BINARY, MLType.MULTICLASS) ['AUCROC', 'ACC', ...]
Source code in octopus/metrics/core.py
get_direction(name)
classmethod
Get the optuna direction by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the metric. |
required |
Returns:
| Type | Description |
|---|---|
MetricDirection
|
MetricDirection.MAXIMIZE if higher_is_better is True, else MetricDirection.MINIMIZE. |
Source code in octopus/metrics/core.py
get_instance(name)
classmethod
Get metric instance by name.
This is the primary method for getting a metric to use for calculation. Returns a Metric instance that has calculate() and calculate_t2e() methods.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the metric to retrieve. |
required |
Returns:
| Type | Description |
|---|---|
Metric
|
Metric instance with calculate methods. |
Raises:
| Type | Description |
|---|---|
UnknownMetricError
|
If no metric with the specified name is found. |
Usage
metric = Metrics.get_instance("AUCROC") value = metric.calculate(y_true, y_pred)
Source code in octopus/metrics/core.py
register(name)
classmethod
Register a metric factory function under a given name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name to register the metric under. |
required |
Returns:
| Type | Description |
|---|---|
Callable[[Callable[[], Metric]], Callable[[], Metric]]
|
Decorator function. |
Source code in octopus/metrics/core.py
PredictionType
Bases: StrEnum
The format in which a metric expects its predictions.
PREDICTIONS: the metric receives hard class labels (0/1 for binary, integer class indices for multiclass, continuous values for regression). For binary classification, these are derived by thresholdingpredict_probaoutput —predictis not called directly.PROBABILITIES: the metric receives raw probability scores or continuous outputs directly frompredict_proba.
Used in:
- Metric.prediction_type: declared per metric in the registry
- metrics/utils.py: used to prepare the correct input before calling
the metric function
- EfsModule: selects the correct column from the CV predictions table
Source code in octopus/types.py
acc_metric()
Accuracy metric configuration.
Source code in octopus/metrics/classification.py
accbal_metric()
Balanced accuracy metric configuration.
Source code in octopus/metrics/classification.py
accbal_multiclass_metric()
Balanced accuracy metric configuration for multiclass problems.
Source code in octopus/metrics/multiclass.py
aucpr_metric()
AUCPR metric configuration.
Source code in octopus/metrics/classification.py
aucroc_macro_multiclass_metric()
AUCROC metric configuration for multiclass problems (macro-average).
Source code in octopus/metrics/multiclass.py
aucroc_metric()
AUCROC metric configuration.
Source code in octopus/metrics/classification.py
aucroc_weighted_multiclass_metric()
AUCROC metric configuration for multiclass problems (weighted-average).
Source code in octopus/metrics/multiclass.py
cindex_metric()
Harrell's concordance index metric configuration.
Source code in octopus/metrics/timetoevent.py
cindex_uno_metric()
Uno's concordance index metric configuration.
Source code in octopus/metrics/timetoevent.py
f1_metric()
F1 metric configuration.
Source code in octopus/metrics/classification.py
logloss_metric()
Log loss metric configuration.
Source code in octopus/metrics/classification.py
mae_metric()
MAE metric configuration.
Source code in octopus/metrics/regression.py
mcc_metric()
Matthews Correlation Coefficient metric configuration.
Source code in octopus/metrics/classification.py
mse_metric()
MSE metric configuration.
Source code in octopus/metrics/regression.py
negbrierscore_metric()
Brier score metric configuration.
Source code in octopus/metrics/classification.py
precision_metric()
Precision metric configuration.
Source code in octopus/metrics/classification.py
r2_metric()
R2 metric configuration.
Source code in octopus/metrics/regression.py
recall_metric()
Recall metric configuration.
Source code in octopus/metrics/classification.py
rmse_metric()
RMSE metric configuration.
Source code in octopus/metrics/regression.py
root_mean_squared_error(y_true, y_pred)
Calculate Root Mean Squared Error (RMSE).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_true
|
ndarray
|
True target values |
required |
y_pred
|
ndarray
|
Predicted target values |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
RMSE value |