Metrics Base

Shared validators, decorator factories, and registry classes used across all metric domains. check_inputs and check_variance_validity enforce array compatibility before any computation. require_min_samples short-circuits metrics when the batch is too small. RegressionMetricRegistry and ClassificationMetricRegistry hold the global metric registries populated at import time by the regression and classification modules.

Shared validators, decorators, and registry classes used across metric domains.

class alf_core.utils.metrics.base.ClassificationMetricRegistry[source]

Bases: object

Simple registry for classification metrics not requiring variance information.

get_metrics()[source]

Get all registered metrics that don’t require variance.

Return type:

dict[str, Callable]

Returns:

Dictionary mapping metric names to their functions.

register(name, fn)[source]

Register a classification metric function in the registry.

Parameters:
  • name (str) – Name of the metric.

  • fn (Callable) – The metric function to register.

Return type:

None

class alf_core.utils.metrics.base.RegressionMetricRegistry[source]

Bases: object

Simple registry for metrics with variance requirements.

get_metrics(requires_variance)[source]

Get registered metrics filtered by variance requirement.

Parameters:

requires_variance (bool) – If True, return only metrics that need variance; if False, return only metrics that do not.

Return type:

dict[str, Callable]

Returns:

Dictionary mapping metric names to their functions.

register(name, metric_fn, requires_variance=False)[source]

Register a metric function in the registry.

Parameters:
  • name (str) – Name identifier for the metric.

  • metric_fn (Callable) – Callable function that computes the metric.

  • requires_variance (bool) – Whether this metric requires variance information.

Return type:

None

alf_core.utils.metrics.base.check_inputs(means, targets)[source]

Validate that means and targets arrays are compatible and valid.

Parameters:
  • means (ndarray) – Array of predicted means.

  • targets (ndarray) – Array of target values.

Raises:

ValueError – If shapes don’t match, arrays are empty, or contain NaN values.

Return type:

None

alf_core.utils.metrics.base.check_variance_validity(variances, targets)[source]

Validate that variance array is valid and compatible with targets.

Parameters:
  • variances (ndarray) – Array of predicted variances.

  • targets (ndarray) – Array of target values.

Raises:
  • TypeError – If variances is None.

  • ValueError – If variances contains negative values, length doesn’t match targets, or contains NaN values.

Return type:

None

alf_core.utils.metrics.base.require_min_samples(n)[source]

Decorator factory that requires a minimum number of samples.

If the decorated function is called with fewer than n samples (based on the length of the first positional argument), returns an empty dictionary instead of calling the function.

Parameters:

n (int) – Minimum number of samples required.

Return type:

Callable

Returns:

A decorator that wraps metric functions.