Skip to content

Commit

Permalink
[Lint] apply ruff format as all safe fixes (#456)
Browse files Browse the repository at this point in the history
* apply ruff format as all safe fixes

* spelling fixes

* fea: fix tests parameterized **kwargs issue
  • Loading branch information
CompRhys authored Nov 13, 2024
1 parent 811fcbf commit 4f31274
Show file tree
Hide file tree
Showing 262 changed files with 4,247 additions and 2,830 deletions.
19 changes: 14 additions & 5 deletions bofire/benchmarks/LookupTableBenchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class LookupTableBenchmark(Benchmark):
Args:
Benchmark: Subclass of the Benchmark function class.
"""

def __init__(
Expand All @@ -22,7 +23,9 @@ def __init__(
Args:
domain (Domain): Domain of the inputs and outputs
LookUpTable (pd.DataFrame): DataFrame containing the LookUp table.
lookup_table (pd.DataFrame): DataFrame containing the LookUp table.
**kwargs: Additional arguments for the Benchmark class.
"""
super().__init__(**kwargs)

Expand All @@ -31,26 +34,32 @@ def __init__(
self.domain.validate_experiments(self.lookup_table)

def _f(self, sampled: pd.DataFrame, **kwargs) -> pd.DataFrame: # type: ignore
"""return output values for matching SMILE candidates.
"""Return output values for matching SMILE candidates.
Args:
sampled (pd.DataFrame): Input values with input columns only.
**kwargs: Allow additional unused arguments to prevent errors.
Returns:
pd.DataFrame: output values from the LookUpTable. Columns are ouput keys and valid_output keys.
pd.DataFrame: output values from the LookUpTable. Columns are output keys and valid_output keys.
"""
X = sampled.copy()
X["proxy_index"] = X.index
X_temp = pd.merge(
X, self.lookup_table, on=self.domain.inputs.get_keys(), how="left"
X,
self.lookup_table,
on=self.domain.inputs.get_keys(),
how="left",
).dropna()
df = pd.merge(X, X_temp, how="left", indicator=True)
if (
df.loc[df._merge == "left_only", df.columns != "_merge"].proxy_index.size
!= 0
):
indices = df.loc[
df._merge == "left_only", df.columns != "_merge"
df._merge == "left_only",
df.columns != "_merge",
].proxy_index.to_list()
raise ValueError(f"Input combination {indices} not found in Look up table")
Y = X_temp[
Expand Down
9 changes: 8 additions & 1 deletion bofire/benchmarks/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@


AnyMultiBenchmark = Union[
C2DTLZ2, Detergent, DTLZ2, ZDT1, CrossCoupling, SnarBenchmark, BNH, TNK
C2DTLZ2,
Detergent,
DTLZ2,
ZDT1,
CrossCoupling,
SnarBenchmark,
BNH,
TNK,
]
AnySingleBenchmark = Union[
Ackley,
Expand Down
48 changes: 27 additions & 21 deletions bofire/benchmarks/aspen_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from bofire.data_models.domain.api import Domain


# Create a folder for the log file, if not alredy exists.
# Create a folder for the log file, if not already exists.
if not os.path.exists("bofire_logs"):
os.makedirs("bofire_logs")

Expand All @@ -23,12 +23,9 @@

class Aspen_benchmark(Benchmark):
"""This class connects to a Aspen plus file that runs the desired process.
It writes incoming input values into Aspen plus, runs the simulation and returns the results.
When initializing this class, make sure not to block multiple Aspen plus licenses at once
when is not absolutely needed.
Args:
Benchmark: Subclass of the Benchmark function class.
It writes incoming input values into Aspen plus, runs the simulation and
returns the results. When initializing this class, make sure not to block
multiple Aspen plus licenses at once when is not absolutely needed.
"""

def __init__(
Expand All @@ -46,14 +43,22 @@ def __init__(
Args:
filename (str): Filepath of the Aspen plus simulation file.
domain (Domain): Domain of the benchmark setting inclunding bounds and information about input values.
paths (dict[str, str]): A dictionary with the key value pairs "key_of_variable": "path_to_variable".
The keys must be the same as provided in the domain.
translate_into_aspen_readable (Optional: Callable): A function that converts the columns of a candidate dataframe into
integers or floats so Aspen plus is able to read their values.
domain (Domain): Domain of the benchmark setting inclunding bounds
and information about input values.
paths (dict[str, str]): A dictionary with the key value pairs
"key_of_variable": "path_to_variable". The keys must be the
same as provided in the domain.
additional_output_keys: (list, optional): A list of additional output
keys to be retrieved from Aspen. Defaults to None.
translate_into_aspen_readable (Optional: Callable): A function that
converts the columns of a candidate dataframe into integers or
floats so Aspen plus is able to read their values.
**kwargs: Additional arguments for the Benchmark class.
Raises:
ValueError: In case the number of provided variable names does not match the number of provided Aspen variable tree paths.
ValueError: In case the number of provided variable names does not
match the number of provided Aspen variable tree paths.
"""
super().__init__(**kwargs)
if os.path.exists(filename):
Expand All @@ -67,7 +72,7 @@ def __init__(

for key in self.domain.inputs.get_keys() + self.domain.outputs.get_keys():
# Check, if every input and output variable has a path to Aspen provided.
if key not in paths.keys():
if key not in paths:
raise ValueError("Path for " + key + " is not provided.")

self.paths = paths
Expand All @@ -79,6 +84,7 @@ def start_aspen(self):
Raises:
ValueError: In case it is not possible to start Aspen plus.
"""
import win32com.client as win32 # type: ignore

Expand All @@ -105,8 +111,8 @@ def _f(self, candidates: pd.DataFrame) -> pd.DataFrame:
Returns:
pd.DataFrame: Output values from Aspen. The dataframe includes valid_(variable_name) columns for
each output variable when the simulation went successful.
"""
"""
# Only start Aspen, when it is not already blocking.
if self.aspen_is_running is False:
self.start_aspen()
Expand All @@ -127,7 +133,7 @@ def _f(self, candidates: pd.DataFrame) -> pd.DataFrame:
}
add_outputs = {key: [] for key in self.additional_output_keys}

# Iterate through dataframe rows to retrieve multiple input vectors. Running seperate simulations for each.
# Iterate through dataframe rows to retrieve multiple input vectors. Running separate simulations for each.
for index, row in X.iterrows():
logger.info("Writing inputs into Aspen")
# Write input variables corresping to columns into aspen according to predefined paths.
Expand All @@ -150,28 +156,28 @@ def _f(self, candidates: pd.DataFrame) -> pd.DataFrame:
try:
# Check for errors during simulation in Aspen that disqualify the results
status = aspen.Tree.FindNode(
"\\Data\\Results Summary\\Run-Status\\Output\\UOSSTAT2"
"\\Data\\Results Summary\\Run-Status\\Output\\UOSSTAT2",
).Value

if status != 8:
if status == 9:
logger.error(
"Result"
+ " does not converge. Simulation status: "
+ str(status)
+ str(status),
)
elif status == 10:
logger.warning(
"Result"
+ " gives an Aspen warning. Simulation status: "
+ str(status)
+ str(status),
)
else:
logger.warning("Unknown simulation status: " + str(status))

for key in self.domain.outputs.get_keys():
y_outputs[key].append(
aspen.Tree.FindNode(self.paths.get(key)).Value
aspen.Tree.FindNode(self.paths.get(key)).Value,
)
if status == 8:
# Result is valid and add valid_var = 1
Expand All @@ -182,7 +188,7 @@ def _f(self, candidates: pd.DataFrame) -> pd.DataFrame:

for key in self.additional_output_keys:
add_outputs[key].append(
aspen.Tree.FindNode(self.paths.get(key)).Value
aspen.Tree.FindNode(self.paths.get(key)).Value,
)

except ConnectionAbortedError:
Expand Down
8 changes: 4 additions & 4 deletions bofire/benchmarks/benchmark.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from abc import abstractmethod
from typing import Callable, Literal, Optional, Tuple, Union
from typing import Annotated, Callable, Literal, Optional, Tuple, Union

import numpy as np
import pandas as pd
from pydantic import Field, PositiveFloat
from scipy.stats import norm, uniform
from typing_extensions import Annotated

from bofire.data_models.base import BaseModel
from bofire.data_models.domain.api import Domain
Expand Down Expand Up @@ -62,7 +61,8 @@ def f(
ix2 = ix1 <= self.outlier_rate
n_outliers = sum(ix2)
Y.loc[ix2, output_feature] = Y.loc[
ix2, output_feature
ix2,
output_feature,
] + self.outlier_prior.sample(n_outliers)
if return_complete:
return pd.concat([candidates, Y], axis=1)
Expand All @@ -74,7 +74,7 @@ def _f(self, candidates: pd.DataFrame) -> pd.DataFrame:
pass

def get_optima(self) -> pd.DataFrame:
raise NotImplementedError()
raise NotImplementedError

@property
def domain(self) -> Domain:
Expand Down
6 changes: 4 additions & 2 deletions bofire/benchmarks/detergent.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def __init__(self):
[0.8737, 8.7178, 0.0, 0.0, 0.0],
[0.0, 2.6651, 2.3495, 0.046, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0],
]
],
)

self._domain = Domain.from_lists(
Expand Down Expand Up @@ -82,5 +82,7 @@ def _f(self, X: pd.DataFrame) -> pd.DataFrame: # type: ignore
x = np.atleast_2d(X[self.domain.inputs.get_keys()])
xp = np.stack([_poly2(xi) for xi in x], axis=0)
return pd.DataFrame(
xp @ self.coef, columns=self.domain.outputs.get_keys(), index=X.index
xp @ self.coef,
columns=self.domain.outputs.get_keys(),
index=X.index,
)
4 changes: 3 additions & 1 deletion bofire/benchmarks/hyperopt.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ def _f(self, candidates: pd.DataFrame) -> pd.DataFrame:
self.surrogate_data.update_hyperparameters(candidate)
surrogate = surrogates.map(self.surrogate_data)
_, cv_test, _ = surrogate.cross_validate( # type: ignore
self.training_data, folds=self.folds, random_state=self.random_state
self.training_data,
folds=self.folds,
random_state=self.random_state,
)
if i == 0:
results = cv_test.get_metrics(combine_folds=True)
Expand Down
Loading

0 comments on commit 4f31274

Please sign in to comment.