Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added plot_gp_slice.py #499

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

added plot_gp_slice.py #499

wants to merge 1 commit into from

Conversation

Jimbo994
Copy link
Collaborator

Added a plot function to plot the "slice" of a GP prediction given some fixed input features, two input features that are varied between the bounds of the domain and the output feature of interest.

To run a quick test:

import pandas as pd
import numpy as np
from bofire.data_models.features.api import ContinuousInput, ContinuousOutput
from bofire.data_models.constraints.api import LinearInequalityConstraint, NChooseKConstraint
from bofire.data_models.domain.api import Domain, Inputs, Outputs, Constraints
from bofire.data_models.surrogates.api import SingleTaskGPSurrogate
from bofire.runners.api import hyperoptimize

from bofire.plot.plot_gp_slice import plot_gp_slice

import plotly.graph_objects as go

import bofire.surrogates.api as surrogates

# Define the domain
inputs = Inputs(
    features=[
        ContinuousInput(key="x1", bounds=(0, 1)),
        ContinuousInput(key="x2", bounds=(0, 1)),
        ContinuousInput(key="x3", bounds=(0, 1)),
    ]
)

constraints = Constraints(
    constraints=[
        LinearInequalityConstraint(features=["x1", "x2", "x3"], coefficients=[1, 1, 1], rhs=2),
        NChooseKConstraint(features=["x1", "x2"], min_count=1, max_count=2, none_also_valid=False),
    ]
)

outputs = Outputs(
    features=[
        ContinuousOutput(key="y")
    ]
)

domain = Domain(inputs=inputs, constraints=constraints, outputs=outputs)

# Generate some synthetic data
data = pd.DataFrame({
    "x1": np.random.rand(100),
    "x2": np.random.rand(100),
    "x3": np.random.rand(100),
})
data["y"] = data["x1"] + data["x2"] + np.random.normal(0, 0.1, 100)

# add a valid_y column
data["valid_y"] = 1

# Define the surrogate model
surrogate_data = SingleTaskGPSurrogate(inputs=domain.inputs.get_by_keys(domain.inputs.get_keys()), outputs=domain.outputs)

# Optimize the surrogate model
opt_surrogate_data, perf = hyperoptimize(surrogate_data=surrogate_data, training_data=data, folds=2)

# Fit the surrogate model
surrogate_gp = surrogates.map(opt_surrogate_data)

surrogate_gp.fit(data)

# Define fixed input features
fixed_input_features = pd.Series({"x3": 0.5})

# Plot the GP slice
fig, fig_sd = plot_gp_slice(
    domain=domain,
    model=surrogate_gp,
    fixed_input_features=fixed_input_features,
    input_features=["x1", "x2"],
    output_feature="y",
    resolution=100,
    observed_data=data
)

fig.show()
fig_sd.show()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants