nirs4all.controllers.models.factory module

Model Factory - Framework-specific model instantiation for controllers

This module provides a factory for building machine learning models from various configurations. Relocated from utils/model_builder.py to be co-located with model controllers.

Supports multiple input formats: - String: file path or class path - Dict: configuration with class, import, or function keys - Instance: pre-built model object - Callable: function or class to instantiate

Lazy loading pattern: Framework-specific imports are only loaded when actually needed to avoid import overhead at startup.

class nirs4all.controllers.models.factory.ModelFactory[source]

Bases: object

Factory class for building machine learning models from various configurations.

Supports multiple input formats: - String: file path or class path - Dict: configuration with class, import, or function keys - Instance: pre-built model object - Callable: function or class to instantiate

Also provides helper utilities for controllers: - filter_params: Filter parameters to match callable signature - compute_input_shape: Compute input shape from dataset - detect_framework: Detect framework from model instance - get_num_classes: Extract number of classes for classification

static build_single_model(model_config, dataset, force_params={})[source]

Build a single model from the given configuration.

Parameters:
  • model_config – Configuration for the model. Can be a string (file path or class path), dict (with ‘class’, ‘import’, or ‘function’ keys), instance, or callable.

  • dataset – Dataset object used to determine input dimensions and classification settings.

  • force_params – Dictionary of parameters to force override on the model.

Returns:

The built model instance.

Raises:

ValueError – If the model_config format is invalid.

static compute_input_shape(dataset, framework: str)[source]

Compute input shape from dataset based on framework.

Parameters:
  • dataset – Dataset with X array

  • framework – Framework name (‘sklearn’, ‘tensorflow’, ‘pytorch’, etc.)

Returns:

Input shape appropriate for framework (int or tuple)

static detect_framework(model) str[source]

Detect the framework from the model instance.

This is a helper for controllers to determine framework type. Returns simplified framework names for routing logic.

Parameters:

model – Model instance or class.

Returns:

String representing the framework (‘sklearn’, ‘tensorflow’, ‘pytorch’, ‘xgboost’, ‘lightgbm’, ‘catboost’, or ‘unknown’).

static filter_params(callable_obj, params: dict) dict[source]

Filter parameters to only those accepted by callable’s signature.

Parameters:
  • callable_obj – Class or function to check signature

  • params – Dictionary of parameters

Returns:

Filtered parameters matching signature

static get_num_classes(dataset) int[source]

Extract number of classes for classification tasks.

Parameters:

dataset – Dataset with y labels and task type

Returns:

Number of unique classes, or None if not classification

static import_class(class_path)[source]

Import a class from a module path.

Parameters:

class_path – String path like ‘module.submodule.ClassName’.

Returns:

The imported class.

Raises:

ImportError – If TensorFlow or PyTorch is required but not available.

static import_object(object_path)[source]

Import an object from a module path.

Parameters:

object_path – String path like ‘module.submodule.object_name’.

Returns:

The imported object.

Raises:

ImportError – If TensorFlow or PyTorch is required but not available.

static is_meta_estimator(model) bool[source]

Check if model is a stacking/voting/ensemble meta-estimator.

Meta-estimators contain nested estimators (base learners) and typically a final_estimator (meta-learner). These require special handling for serialization, cloning, and building.

Parameters:

model – Model instance, class, or configuration dict to check.

Returns:

True if model is a meta-estimator type (StackingRegressor, StackingClassifier, VotingRegressor, VotingClassifier), False otherwise.

static prepare_and_call(callable_obj, params_from_caller=None, force_params_from_caller=None)[source]

Prepare arguments and call a callable with proper parameter handling.

Parameters:
  • callable_obj – The callable to invoke.

  • params_from_caller – Parameters from the caller.

  • force_params_from_caller – Force parameters that take precedence.

Returns:

The result of calling the callable.

Raises:

TypeError – If arguments cannot be bound to the callable.

static reconstruct_object(obj, params=None, force_params=None)[source]

Reconstruct an object using its current attributes as default values.

Parameters:
  • obj – The object to be reconstructed.

  • params – Dictionary of parameters to overwrite the object’s current parameters.

  • force_params – Dictionary of parameters that take precedence over both the object’s current parameters and params.

Returns:

A new instance of the object with the updated parameters.

Raises:

TypeError – If a required parameter is missing and no default value is provided.