Skip to content

example_bayesian_optimization_parser

Bayesian Optimization Parser example

ExampleBayesianOptimizationParser

ExampleBayesianOptimizationParser()

Bases: Parser

An example I/O parser for testing the Bayesian Optimization sampler.

Methods

read_output_file(params: dict, run_dir: str) -> dict
    Reads the output file and returs a dictionary with the
    input settings and the output file.

write_input_file():
    This is a dummy function that is not actually needed.

collect_sample_information(run_dir: str) -> dict
    Collects the scoring information needed for Bayesian
    optimization.

Initializes the ExampleBayesianOptimizationParser object.

Source code in src/enchanted_surrogates/parsers/example_bayesian_optimization_parser.py
31
32
33
34
35
36
def __init__(self):
    """
    Initializes the ExampleBayesianOptimizationParser object.

    """
    pass

collect_sample_information

collect_sample_information(run_dir, observations)

Reads the information from the run directory that is needed for the Bayesian optimization sampler.

Parameters:

Name Type Description Default
run_dir str

Directory where the run has been conducted.

required
observations dict

Dictionary of observations (not actually used by this example)

required

Returns:

Name Type Description
dict

Dictionary containing the information needed for the BO routines.

Source code in src/enchanted_surrogates/parsers/example_bayesian_optimization_parser.py
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
def collect_sample_information(self, run_dir: str, observations: dict):
    """
    Reads the information from the run directory that is needed for the
    Bayesian optimization sampler.

    Args:
        run_dir (str): Directory where the run has been conducted.
        observations (dict): Dictionary of observations
                             (not actually used by this example)

    Returns:
        dict: Dictionary containing the information needed for the BO
              routines.
    """
    # Read the sample input vector as dumped in the pkl file
    datavec = pd.read_csv(os.path.join(run_dir, "enchanted_datapoint.csv"))
    inputs = {"x": float(datavec["x"].iloc[0])}
    # This computes a distance metric for the current.
    # Mean absolute error is chosen here.
    outputdict = self.read_output_file(run_dir)
    distances = np.array([2.0 - outputdict["output"]])
    inputnd = []
    for key in inputs:
        inputnd.append(inputs[key])
    inputnd = np.array(inputnd)
    outputdict = {
        "run_dir": run_dir,
        "inputs": inputnd,
        "distances": distances,
        "failure": 0,
    }
    return outputdict

read_output_file

read_output_file(run_dir)

Reads the output files from the run directory

Parameters:

Name Type Description Default
run_dir str

Directory where the output file is located.

required

Returns:

Name Type Description
dict

Dictionary containing the settings and the output dictionaries.

Source code in src/enchanted_surrogates/parsers/example_bayesian_optimization_parser.py
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
def read_output_file(self, run_dir: str):
    """
    Reads the output files from the run directory

    Args:
        run_dir (str): Directory where the output file is located.

    Returns:
        dict: Dictionary containing the settings and the output
              dictionaries.

    """
    datavec = pd.read_csv(os.path.join(run_dir, "enchanted_datapoint.csv"))
    inputs = float(datavec["x"].iloc[0])
    output = float(datavec["output"].iloc[0])
    outputdict = {"input": inputs, "output": output}
    return outputdict

write_input_file

write_input_file()

This is not actually needed.

Source code in src/enchanted_surrogates/parsers/example_bayesian_optimization_parser.py
38
39
40
41
42
def write_input_file(self):
    """
    This is not actually needed.
    """
    pass