Skip to content

Commit

Permalink
Merge pull request #180 from ChristopherMayes/bugfixes
Browse files Browse the repository at this point in the history
implement bugfix for adding constants + update tests
  • Loading branch information
ChristopherMayes authored Dec 16, 2023
2 parents 53bd9d6 + 5ffd6e9 commit 41c35aa
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
18 changes: 18 additions & 0 deletions tests/test_xopt.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,24 @@ def test_evaluate(self):
out = xopt.evaluate({"x1": 0.4, "x2": 0.3})
assert isinstance(out, dict)

# test with vocs that uses "x1" as a constant
test_vocs = deepcopy(TEST_VOCS_BASE)

evaluator = Evaluator(function=xtest_callable)
generator = RandomGenerator(vocs=test_vocs)

xopt = Xopt(generator=generator, evaluator=evaluator, vocs=test_vocs)

test_vocs.variables = {"x2": [0, 1]}
test_vocs.constants = {"x1": 2.0}

out = xopt.evaluate({"x2": 0.2})
assert isinstance(out, dict)

xopt.evaluate_data({"x2": 0.2})
assert len(xopt.data) == 1
assert xopt.data["x1"].iloc[0] == 2.0

def test_evaluate_data(self):
evaluator = Evaluator(function=xtest_callable)
generator = RandomGenerator(vocs=deepcopy(TEST_VOCS_BASE))
Expand Down
18 changes: 15 additions & 3 deletions xopt/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import logging
from copy import deepcopy
from typing import Any, Dict, List, Optional, Union

import numpy as np
Expand Down Expand Up @@ -275,7 +276,13 @@ def evaluate(self, input_dict: Dict):
The result of the evaluation.
"""
self.vocs.validate_input_data(DataFrame(input_dict, index=[0]))
inputs = deepcopy(input_dict)

# add constants to input data
for name, value in self.vocs.constants.items():
inputs[name] = value

self.vocs.validate_input_data(DataFrame(inputs, index=[0]))
return self.evaluator.evaluate(input_dict)

def evaluate_data(
Expand Down Expand Up @@ -309,12 +316,17 @@ def evaluate_data(
# translate input data into pandas dataframes
if not isinstance(input_data, DataFrame):
try:
input_data = DataFrame(input_data)
input_data = DataFrame(deepcopy(input_data))
except ValueError:
input_data = DataFrame(input_data, index=[0])
input_data = DataFrame(deepcopy(input_data), index=[0])

logger.debug(f"Evaluating {len(input_data)} inputs")
self.vocs.validate_input_data(input_data)

# add constants to input data
for name, value in self.vocs.constants.items():
input_data[name] = value

output_data = self.evaluator.evaluate_data(input_data)

if self.strict:
Expand Down
5 changes: 3 additions & 2 deletions xopt/vocs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import numpy as np
import pandas as pd
import yaml
from pandas import DataFrame
from pydantic import ConfigDict, conlist, Field

from xopt.pydantic import XoptBaseModel
Expand Down Expand Up @@ -502,14 +503,14 @@ def select_best(self, data: pd.DataFrame, n=1):
OBJECTIVE_WEIGHT = {"MINIMIZE": 1.0, "MAXIMIZE": -1.0}


def form_variable_data(variables: Dict, data, prefix="variable_"):
def form_variable_data(variables: Union[Dict, DataFrame], data, prefix="variable_"):
"""
Use variables dict to form a dataframe.
"""
if not variables:
return pd.DataFrame([])

if not isinstance(data, pd.DataFrame):
if not isinstance(data, DataFrame):
data = pd.DataFrame(data)

# Pick out columns in right order
Expand Down

0 comments on commit 41c35aa

Please sign in to comment.