SubspaceDiscrete

class baybe.searchspace.discrete.SubspaceDiscrete[source]

Bases: SerialMixin

Class for managing discrete subspaces.

Builds the subspace from parameter definitions and optional constraints, keeps track of search metadata, and provides access to candidate sets and different parameter views.

Public methods

__init__(parameters, exp_rep[, metadata, ...])

Method generated by attrs for class SubspaceDiscrete.

empty()

Create an empty discrete subspace.

from_dataframe(df[, parameters, empty_encoding])

Create a discrete subspace with a specified set of configurations.

from_dict(dictionary)

Create an object from its dictionary representation.

from_json(string)

Create an object from its JSON representation.

from_product(parameters[, constraints, ...])

See baybe.searchspace.core.SearchSpace.

from_simplex(max_sum, simplex_parameters[, ...])

Efficiently create discrete simplex subspaces.

get_candidates([...])

Return the set of candidate parameter settings that can be tested.

mark_as_measured(measurements, ...)

Mark the given elements of the space as measured.

to_dict()

Create an object's dictionary representation.

to_json()

Create an object's JSON representation.

transform(data)

Transform parameters from experimental to computational representation.

Public attributes and properties

parameters

The list of parameters of the subspace.

exp_rep

The experimental representation of the subspace.

metadata

The metadata.

empty_encoding

Flag encoding whether an empty encoding is used.

constraints

A list of constraints for restricting the space.

comp_rep

The computational representation of the space.

is_empty

Return whether this subspace is empty.

param_bounds_comp

Return bounds as tensor.

__init__(parameters: list[DiscreteParameter], exp_rep: DataFrame, metadata: DataFrame = NOTHING, empty_encoding: bool = False, constraints: list[DiscreteConstraint] = NOTHING, comp_rep: DataFrame = NOTHING)

Method generated by attrs for class SubspaceDiscrete.

For details on the parameters, see Public attributes and properties.

classmethod empty()[source]

Create an empty discrete subspace.

Return type:

SubspaceDiscrete

classmethod from_dataframe(df: DataFrame, parameters: list[DiscreteParameter] | None = None, empty_encoding: bool = False)[source]

Create a discrete subspace with a specified set of configurations.

Parameters:
Return type:

SubspaceDiscrete

Returns:

The created discrete subspace.

classmethod from_dict(dictionary: dict)

Create an object from its dictionary representation.

Parameters:

dictionary (dict) – The dictionary representation.

Return type:

TypeVar(_T)

Returns:

The reconstructed object.

classmethod from_json(string: str)

Create an object from its JSON representation.

Parameters:

string (str) – The JSON representation of the object.

Return type:

TypeVar(_T)

Returns:

The reconstructed object.

classmethod from_product(parameters: list[DiscreteParameter], constraints: list[DiscreteConstraint] | None = None, empty_encoding: bool = False)[source]

See baybe.searchspace.core.SearchSpace.

Return type:

SubspaceDiscrete

classmethod from_simplex(max_sum: float, simplex_parameters: list[NumericalDiscreteParameter], product_parameters: list[DiscreteParameter] | None = None, constraints: list[DiscreteConstraint] | None = None, min_nonzero: int = 0, max_nonzero: int | None = None, boundary_only: bool = False, tolerance: float = 1e-06)[source]

Efficiently create discrete simplex subspaces.

The same result can be achieved using baybe.searchspace.discrete.SubspaceDiscrete.from_product() in combination with appropriate constraints. However, such an approach is inefficient because the Cartesian product involved creates an exponentially large set of candidates, most of which do not satisfy the simplex constraints and must be subsequently be filtered out by the method.

By contrast, this method uses a shortcut that removes invalid candidates already during the creation of parameter combinations, resulting in a significantly faster construction.

Parameters:
  • max_sum (float) – The maximum sum of the parameter values defining the simplex size.

  • simplex_parameters (list[NumericalDiscreteParameter]) – The parameters to be used for the simplex construction.

  • product_parameters (Optional[list[DiscreteParameter]]) – Optional parameters that enter in form of a Cartesian product.

  • constraints (Optional[list[DiscreteConstraint]]) – See baybe.searchspace.core.SearchSpace.

  • min_nonzero (int) – Optional restriction on the minimum number of nonzero parameter values in the simplex construction.

  • max_nonzero (Optional[int]) – Optional restriction on the maximum number of nonzero parameter values in the simplex construction.

  • boundary_only (bool) – Flag determining whether to keep only parameter configurations on the simplex boundary.

  • tolerance (float) – Numerical tolerance used to validate the simplex constraint.

Raises:
  • ValueError – If the passed simplex parameters are not suitable for a simplex construction.

  • ValueError – If the passed product parameters are not discrete.

Return type:

SubspaceDiscrete

Returns:

The created simplex subspace.

Note

The achieved efficiency gains can vary depending on the particular order in which the parameters are passed to this method, as the configuration space is built up incrementally from the parameter sequence.

get_candidates(allow_repeated_recommendations: bool = False, allow_recommending_already_measured: bool = False)[source]

Return the set of candidate parameter settings that can be tested.

Parameters:
  • allow_repeated_recommendations (bool) – If True, parameter settings that have already been recommended in an earlier iteration are still considered valid candidates. This is relevant, for instance, when an earlier recommended parameter setting has not been measured by the user (for any reason) after the corresponding recommendation was made.

  • allow_recommending_already_measured (bool) – If True, parameters settings for which there are already target values available are still considered as valid candidates.

Return type:

tuple[DataFrame, DataFrame]

Returns:

The candidate parameter settings both in experimental and computational representation.

mark_as_measured(measurements: DataFrame, numerical_measurements_must_be_within_tolerance: bool)[source]

Mark the given elements of the space as measured.

Parameters:
Return type:

None

to_dict()

Create an object’s dictionary representation.

Return type:

dict

to_json()

Create an object’s JSON representation.

Return type:

str

Returns:

The JSON representation as a string.

transform(data: DataFrame)[source]

Transform parameters from experimental to computational representation.

Continuous parameters and additional columns are ignored.

Parameters:

data (DataFrame) – The data to be transformed. Must contain all specified parameters, can contain more columns.

Return type:

DataFrame

Returns:

A dataframe with the parameters in computational representation.

comp_rep: DataFrame

The computational representation of the space. Technically not required but added as an optional initializer argument to allow ingestion from e.g. serialized objects and thereby speed up construction. If not provided, the default hook will derive it from exp_rep.

constraints: list[DiscreteConstraint]

A list of constraints for restricting the space.

empty_encoding: bool

Flag encoding whether an empty encoding is used.

exp_rep: DataFrame

The experimental representation of the subspace.

property is_empty: bool

Return whether this subspace is empty.

metadata: DataFrame

The metadata.

property param_bounds_comp: ndarray

Return bounds as tensor.

Take bounds from the parameter definitions, but discards bounds belonging to columns that were filtered out during the creation of the space.

parameters: list[DiscreteParameter]

The list of parameters of the subspace.