kedro_datasets_experimental.optuna.StudyDataset

class kedro_datasets_experimental.optuna.StudyDataset(*, backend, database, study_name, load_args=None, version=None, credentials=None, metadata=None)[source]

StudyDataset loads/saves data from/to an optuna Study.

Example usage for the YAML API:

review_prediction_study:
  type: kedro_datasets_experimental.optuna.StudyDataset
  backend: sqlite
  database: data/05_model_input/review_prediction_study.db
  load_args:
    sampler:
      class: TPESampler
      n_startup_trials: 10
      n_ei_candidates: 5
    pruner:
      class: NopPruner
  versioned: true

price_prediction_study:
  type: kedro_datasets_experimental.optuna.StudyDataset
  backend: postgresql
  database: optuna_db
  credentials: dev_optuna_postgresql

Example usage for the Python API:

 from kedro_datasets_experimental.optuna import StudyDataset
 from optuna.distributions import FloatDistribution
 import optuna

 study = optuna.create_study()
 trial = optuna.trial.create_trial(
...     params={"x": 2.0},
...     distributions={"x": FloatDistribution(0, 10)},
...     value=4.0,
... )
 study.add_trial(trial)

 dataset = StudyDataset(backend="sqlite", database="optuna.db")
 dataset.save(study)
 reloaded = dataset.load()
 assert len(reloaded.trials) == 1
 assert reloaded.trials[0].params["x"] == 2.0

Attributes

DEFAULT_LOAD_ARGS

Methods

exists()

Checks whether a dataset's output already exists by calling the provided _exists() method.

from_config(name, config[, load_version, ...])

Create a dataset instance using the configuration provided.

load()

Loads data by delegation to the provided load method.

release()

Release any cached data.

resolve_load_version()

Compute the version the dataset should be loaded with.

resolve_save_version()

Compute the version the dataset should be saved with.

save(study)

Saves data by delegation to the provided save method.

to_config()

Converts the dataset instance into a dictionary-based configuration for serialization.

DEFAULT_LOAD_ARGS: dict[str, Any] = {'pruner': None, 'sampler': None}
__init__(*, backend, database, study_name, load_args=None, version=None, credentials=None, metadata=None)[source]

Creates a new instance of StudyDataset pointing to a concrete optuna Study on a specific relational database.

Parameters:
  • backend (str) – Name of the database backend. This name should correspond to a module in SQLAlchemy.

  • database (str) – Name of the database.

  • study_name (str) – Name of the optuna Study.

  • load_args (Optional[dict[str, Any]]) – Optuna options for loading studies. Accepts a sampler and a pruner. If either are provided, a class matching any Optuna sampler, respecitively pruner class name should be provided, optionally with their argyments. Here you can find all available samplers and pruners and their arguments: - https://optuna.readthedocs.io/en/stable/reference/samplers/index.html - https://optuna.readthedocs.io/en/stable/reference/pruners.html All defaults are preserved.

  • version (Optional[Version]) – If specified, should be an instance of kedro.io.core.Version. If its load attribute is None, the latest version will be loaded. If its save attribute is None, save version will be autogenerated.

  • credentials (Optional[dict[str, Any]]) – Credentials required to get access to the underlying RDB. They can include username, password, host, and port.

  • metadata (Optional[dict[str, Any]]) – Any arbitrary metadata. This is ignored by Kedro, but may be consumed by users or external plugins.

exists()[source]

Checks whether a dataset’s output already exists by calling the provided _exists() method.

Return type:

bool

Returns:

Flag indicating whether the output already exists.

Raises:

DatasetError – when underlying exists method raises error.

classmethod from_config(name, config, load_version=None, save_version=None)[source]

Create a dataset instance using the configuration provided.

Parameters:
  • name (str) – Data set name.

  • config (dict[str, Any]) – Data set config dictionary.

  • load_version (Optional[str]) – Version string to be used for load operation if the dataset is versioned. Has no effect on the dataset if versioning was not enabled.

  • save_version (Optional[str]) – Version string to be used for save operation if the dataset is versioned. Has no effect on the dataset if versioning was not enabled.

Return type:

AbstractDataset

Returns:

An instance of an AbstractDataset subclass.

Raises:

DatasetError – When the function fails to create the dataset from its config.

load()[source]

Loads data by delegation to the provided load method.

Return type:

Study

Returns:

Data returned by the provided load method.

Raises:

DatasetError – When underlying load method raises error.

release()[source]

Release any cached data.

Raises:

DatasetError – when underlying release method raises error.

Return type:

None

resolve_load_version()[source]

Compute the version the dataset should be loaded with.

Return type:

Optional[str]

resolve_save_version()[source]

Compute the version the dataset should be saved with.

Return type:

Optional[str]

save(study)[source]

Saves data by delegation to the provided save method.

Parameters:

data – the value to be saved by provided save method.

Raises:
  • DatasetError – when underlying save method raises error.

  • FileNotFoundError – when save method got file instead of dir, on Windows.

  • NotADirectoryError – when save method got file instead of dir, on Unix.

Return type:

None

to_config()[source]

Converts the dataset instance into a dictionary-based configuration for serialization. Ensures that any subclass-specific details are handled, with additional logic for versioning and caching implemented for CachedDataset.

Adds a key for the dataset’s type using its module and class name and includes the initialization arguments.

For CachedDataset it extracts the underlying dataset’s configuration, handles the versioned flag and removes unnecessary metadata. It also ensures the embedded dataset’s configuration is appropriately flattened or transformed.

If the dataset has a version key, it sets the versioned flag in the configuration.

Removes the metadata key from the configuration if present.

Return type:

dict[str, Any]

Returns:

A dictionary containing the dataset’s type and initialization arguments.