SearchSpace

class baybe.searchspace.core.SearchSpace[source]

Bases: SerialMixin

Class for managing the overall search space.

The search space might be purely discrete, purely continuous, or hybrid. Note that created objects related to the computational representations of parameters (e.g., parameter bounds, computational dataframes, etc.) may use a different parameter order than what is specified through the constructor: While the passed parameter list can contain parameters in arbitrary order, the aforementioned objects (by convention) list discrete parameters first, followed by continuous ones.

Public methods

__init__([discrete, continuous])

Method generated by attrs for class SearchSpace.

estimate_product_space_size(parameters)

Estimate an upper bound for the memory size of a product space.

from_dataframe(df, parameters)

Create a search space from a specified set of parameter configurations.

from_dict(dictionary)

Create an object from its dictionary representation.

from_json(string)

Create an object from its JSON representation.

from_parameter(parameter)

Create a search space from a single parameter.

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

Create a search space from a cartesian product.

get_parameters_by_name(names)

Return parameters with the specified names.

to_dict()

Create an object's dictionary representation.

to_json()

Create an object's JSON representation.

transform([df, allow_missing, allow_extra, data])

Transform parameters from experimental to computational representation.

Public attributes and properties

discrete

The (potentially empty) discrete subspace of the overall search space.

continuous

The (potentially empty) continuous subspace of the overall search space.

constraints

Return the constraints of the search space.

constraints_augmentable

The searchspace constraints that can be considered during augmentation.

contains_mordred

Indicates if any of the discrete parameters uses MORDRED encoding.

contains_rdkit

Indicates if any of the discrete parameters uses RDKIT encoding.

n_tasks

The number of tasks encoded in the search space.

param_bounds_comp

Return bounds as tensor.

parameters

Return the list of parameters of the search space.

task_idx

The column index of the task parameter in computational representation.

type

Return the type of the search space.

__init__(discrete: SubspaceDiscrete = NOTHING, continuous: SubspaceContinuous = NOTHING)

Method generated by attrs for class SearchSpace.

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

static estimate_product_space_size(parameters: Iterable[Parameter])[source]

Estimate an upper bound for the memory size of a product space.

Continuous parameters are ignored because creating a continuous subspace has no considerable memory footprint.

Parameters:

parameters (Iterable[Parameter]) – The parameters spanning the product space.

Return type:

MemorySize

Returns:

The estimated memory size.

classmethod from_dataframe(df: DataFrame, parameters: Sequence[Parameter])[source]

Create a search space from a specified set of parameter configurations.

The way in which the contents of the columns are interpreted depends on the types of the corresponding parameter objects provided. For details, see baybe.searchspace.discrete.SubspaceDiscrete.from_dataframe() and baybe.searchspace.continuous.SubspaceContinuous.from_dataframe().

Parameters:
  • df (DataFrame) – A dataframe whose parameter configurations are used as search space specification.

  • parameters (Sequence[Parameter]) – The corresponding parameter objects, one for each column in the provided dataframe.

Return type:

SearchSpace

Returns:

The created search space.

Raises:

ValueError – If the dataframe columns do not match with the parameters.

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_parameter(parameter: Parameter)[source]

Create a search space from a single parameter.

Parameters:

parameter (Parameter) – The parameter to span the search space.

Return type:

SearchSpace

Returns:

The created search space.

classmethod from_product(parameters: Sequence[Parameter], constraints: Sequence[Constraint] | None = None, empty_encoding: bool = False)[source]

Create a search space from a cartesian product.

In the search space, optional subsequent constraints are applied. That is, the discrete subspace becomes the (filtered) cartesian product containing all discrete parameter combinations while, analogously, the continuous subspace represents the (filtered) cartesian product of all continuous parameters.

Parameters:
  • parameters (Sequence[Parameter]) – The parameters spanning the search space.

  • constraints (Optional[Sequence[Constraint]]) – An optional set of constraints restricting the valid parameter space.

  • empty_encoding (bool) – If True, uses an “empty” encoding for all parameters. This is useful, for instance, in combination with random search strategies that do not read the actual parameter values, since it avoids the (potentially costly) transformation of the parameter values to their computational representation.

Return type:

SearchSpace

Returns:

The constructed search space.

get_parameters_by_name(names: Sequence[str])[source]

Return parameters with the specified names.

Parameters:

names (Sequence[str]) – Sequence of parameter names.

Return type:

tuple[Parameter, ...]

Returns:

The named parameters.

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(df: DataFrame | None = None, /, *, allow_missing: bool = False, allow_extra: bool | None = None, data: DataFrame | None = None)[source]

Transform parameters from experimental to computational representation.

Parameters:
  • df (Optional[DataFrame]) – The dataframe to be transformed. The allowed columns of the dataframe are dictated by the allow_missing and allow_extra flags. The None default value is for temporary backward compatibility only and will be removed in a future version.

  • allow_missing (bool) – If False, each parameter of the space must have (exactly) one corresponding column in the given dataframe. If True, the dataframe may contain only a subset of parameter columns.

  • allow_extra (Optional[bool]) – If False, every column present in the dataframe must correspond to (exactly) one parameter of the space. If True, the dataframe may contain additional non-parameter-related columns, which will be ignored. The None default value is for temporary backward compatibility only and will be removed in a future version.

  • data (Optional[DataFrame]) – Ignore! For backward compatibility only.

Raises:

ValueError – If dataframes are passed to both df and data.

Return type:

DataFrame

Returns:

A corresponding dataframe with parameters in computational representation.

property constraints: tuple[Constraint, ...]

Return the constraints of the search space.

property constraints_augmentable: tuple[Constraint, ...]

The searchspace constraints that can be considered during augmentation.

property contains_mordred: bool

Indicates if any of the discrete parameters uses MORDRED encoding.

property contains_rdkit: bool

Indicates if any of the discrete parameters uses RDKIT encoding.

continuous: SubspaceContinuous

The (potentially empty) continuous subspace of the overall search space.

discrete: SubspaceDiscrete

The (potentially empty) discrete subspace of the overall search space.

property n_tasks: int

The number of tasks encoded in the search space.

property param_bounds_comp: ndarray

Return bounds as tensor.

property parameters: tuple[Parameter, ...]

Return the list of parameters of the search space.

property task_idx: int | None

The column index of the task parameter in computational representation.

property type: SearchSpaceType

Return the type of the search space.