nirs4all.controllers.models.utilities module

Model Controller Utilities - Task detection, loss/metric configuration, and scoring

This module provides controller-specific utilities that were relocated from utils/model_utils.py to be co-located with model controllers.

Provides: - Task type detection and enumeration - Default loss function and metric selection based on task type - Loss function validation - Score formatting utilities

class nirs4all.controllers.models.utilities.ModelControllerUtils[source]

Bases: object

Utilities for model controller configuration and evaluation.

DEFAULT_LOSSES = {TaskType.BINARY_CLASSIFICATION: 'binary_crossentropy', TaskType.MULTICLASS_CLASSIFICATION: 'sparse_categorical_crossentropy', TaskType.REGRESSION: 'mse'}
DEFAULT_METRICS = {TaskType.BINARY_CLASSIFICATION: ['balanced_accuracy', 'accuracy', 'auc'], TaskType.MULTICLASS_CLASSIFICATION: ['balanced_accuracy', 'accuracy', 'categorical_accuracy'], TaskType.REGRESSION: ['mae', 'mse']}
SKLEARN_SCORING = {TaskType.BINARY_CLASSIFICATION: 'balanced_accuracy', TaskType.MULTICLASS_CLASSIFICATION: 'balanced_accuracy', TaskType.REGRESSION: 'neg_mean_squared_error'}
static detect_task_type(y: ndarray, threshold: float = 0.05) TaskType[source]

Detect task type based on target values.

Delegates to standalone task_detection module to avoid circular imports.

Parameters:
  • y – Target values array

  • threshold – Threshold for determining if values are continuous (regression) vs discrete (classification).

Returns:

Detected task type

Return type:

TaskType

static format_scores(scores: Dict[str, float], precision: int = 4) str[source]

Format scores dictionary for pretty printing.

Parameters:
  • scores – Dictionary of scores

  • precision – Number of decimal places

Returns:

Formatted scores string

Return type:

str

static get_best_score_metric(task_type: TaskType) Tuple[str, bool][source]

Get the primary metric for determining “best” score.

Parameters:

task_type – Task type

Returns:

(metric_name, higher_is_better)

Return type:

Tuple[str, bool]

static get_default_loss(task_type: TaskType, framework: str = 'sklearn') str[source]

Get default loss function for task type and framework.

Parameters:
  • task_type – Detected task type

  • framework – ML framework (“sklearn”, “tensorflow”, “pytorch”)

Returns:

Default loss function name

Return type:

str

static get_default_metrics(task_type: TaskType, framework: str = 'sklearn') List[str][source]

Get default metrics for task type and framework.

Parameters:
  • task_type – Detected task type

  • framework – ML framework (“sklearn”, “tensorflow”, “pytorch”)

Returns:

List of default metric names

Return type:

List[str]

static get_scoring_metric(task_type: TaskType, framework: str = 'sklearn') str[source]

Get default scoring metric for hyperparameter optimization.

Parameters:
  • task_type – Detected task type

  • framework – ML framework

Returns:

Scoring metric name

Return type:

str

static validate_loss_compatibility(loss: str, task_type: TaskType, framework: str = 'sklearn') bool[source]

Validate if loss function is compatible with task type.

Parameters:
  • loss – Loss function name

  • task_type – Task type

  • framework – ML framework

Returns:

True if compatible, False otherwise

Return type:

bool

nirs4all.controllers.models.utilities.ModelUtils

alias of ModelControllerUtils