HyperparameterTuner#

class openstef_models.integrations.optuna.HyperparameterTuner[ConfigT: BaseConfig](*, config: ConfigT, train_dataset: TimeSeriesDataset, create_workflow: Callable[[ConfigT], CustomForecastingWorkflow], target_quantile: Union[Quantile, Literal['global']], metric_name: str, direction: Literal['maximize', 'minimize'] = 'maximize', n_trials: int = 20, n_jobs: int = 1, seed: int | None = 42, study_name: str = 'hyperparameter_tuning')[source]

Bases: BaseConfig, Generic[ConfigT]

Bayesian hyperparameter tuner powered by Optuna.

Orchestrates an Optuna study over the tunable search spaces declared on HyperParams fields of the provided config. Override methods prefixed with _ to customise study creation, objective evaluation, or parameter suggestion.

Parameters:
model_config: ClassVar[ConfigDict] = {'arbitrary_types_allowed': True, 'extra': 'ignore', 'protected_namespaces': ()}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

config: ConfigT
train_dataset: TimeSeriesDataset
create_workflow: Callable[[ConfigT], CustomForecastingWorkflow]
target_quantile: Union[Quantile, Literal['global']]
metric_name: str
direction: Literal['maximize', 'minimize']
n_trials: int
n_jobs: int
seed: int | None
study_name: str
static suggest_value(trial: Trial, trial_key: str, tuning_range: TuningRange) → bool | int | float | str | None[source]

Suggest a single parameter value via the Optuna trial API.

Override to add custom suggestion logic or constraints.

Return type:

bool | int | float | str | None

Returns:

Suggested value, or None when the range is incomplete.

Parameters:
  • trial (Trial)

  • trial_key (str)

  • tuning_range (TuningRange)

tune(*, show_progress_bar: bool = True) → tuple[ConfigT, Study][source]

Run the Optuna study and return the best config.

Return type:

tuple[ConfigT, Study]

Returns:

(best_config, study) tuple.

Raises:

ValueError – If no tunable fields are found or metric_name is invalid.

Parameters:

show_progress_bar (bool)

fit_with_tuning(*, show_progress_bar: bool = True) → TuningResult[ConfigT][source]

Tune, then fit a final workflow with the best config.

Return type:

TuningResult[ConfigT]

Returns:

TuningResult with the best config and Optuna study.

Raises:

RuntimeError – If the final workflow fit fails after tuning.

Parameters:

show_progress_bar (bool)