Skip to content

Commit

Permalink
Do black
Browse files Browse the repository at this point in the history
  • Loading branch information
jungtaekkim committed May 20, 2024
1 parent 44a04a7 commit 7d24f44
Show file tree
Hide file tree
Showing 88 changed files with 8,875 additions and 4,875 deletions.
2 changes: 1 addition & 1 deletion bayeso/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
package, implemented in Python."""


__version__ = '0.6.1'
__version__ = "0.6.1"
53 changes: 36 additions & 17 deletions bayeso/acquisition.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@


@utils_common.validate_types
def pi(pred_mean: np.ndarray, pred_std: np.ndarray, Y_train: np.ndarray,
jitter: float=constants.JITTER_ACQ
def pi(
pred_mean: np.ndarray,
pred_std: np.ndarray,
Y_train: np.ndarray,
jitter: float = constants.JITTER_ACQ,
) -> np.ndarray:
"""
It is a probability of improvement criterion.
Expand Down Expand Up @@ -46,13 +49,17 @@ def pi(pred_mean: np.ndarray, pred_std: np.ndarray, Y_train: np.ndarray,
assert len(Y_train.shape) == 2
assert pred_mean.shape[0] == pred_std.shape[0]

with np.errstate(divide='ignore'):
with np.errstate(divide="ignore"):
val_z = (np.min(Y_train) - pred_mean) / (pred_std + jitter)
return scipy.stats.norm.cdf(val_z)


@utils_common.validate_types
def ei(pred_mean: np.ndarray, pred_std: np.ndarray, Y_train: np.ndarray,
jitter: float=constants.JITTER_ACQ
def ei(
pred_mean: np.ndarray,
pred_std: np.ndarray,
Y_train: np.ndarray,
jitter: float = constants.JITTER_ACQ,
) -> np.ndarray:
"""
It is an expected improvement criterion.
Expand Down Expand Up @@ -82,16 +89,20 @@ def ei(pred_mean: np.ndarray, pred_std: np.ndarray, Y_train: np.ndarray,
assert len(Y_train.shape) == 2
assert pred_mean.shape[0] == pred_std.shape[0]

with np.errstate(divide='ignore'):
with np.errstate(divide="ignore"):
val_z = (np.min(Y_train) - pred_mean) / (pred_std + jitter)
return (np.min(Y_train) - pred_mean) * scipy.stats.norm.cdf(val_z) \
+ pred_std * scipy.stats.norm.pdf(val_z)
return (np.min(Y_train) - pred_mean) * scipy.stats.norm.cdf(
val_z
) + pred_std * scipy.stats.norm.pdf(val_z)


@utils_common.validate_types
def ucb(pred_mean: np.ndarray, pred_std: np.ndarray,
Y_train: constants.TYPING_UNION_ARRAY_NONE=None,
kappa: float=2.0,
increase_kappa: bool=True
def ucb(
pred_mean: np.ndarray,
pred_std: np.ndarray,
Y_train: constants.TYPING_UNION_ARRAY_NONE = None,
kappa: float = 2.0,
increase_kappa: bool = True,
) -> np.ndarray:
"""
It is a Gaussian process upper confidence bound criterion.
Expand Down Expand Up @@ -136,9 +147,14 @@ def ucb(pred_mean: np.ndarray, pred_std: np.ndarray,
kappa_ = kappa
return -pred_mean + kappa_ * pred_std


@utils_common.validate_types
def aei(pred_mean: np.ndarray, pred_std: np.ndarray, Y_train: np.ndarray, noise: float,
jitter: float=constants.JITTER_ACQ
def aei(
pred_mean: np.ndarray,
pred_std: np.ndarray,
Y_train: np.ndarray,
noise: float,
jitter: float = constants.JITTER_ACQ,
) -> np.ndarray:
"""
It is an augmented expected improvement criterion.
Expand Down Expand Up @@ -171,13 +187,15 @@ def aei(pred_mean: np.ndarray, pred_std: np.ndarray, Y_train: np.ndarray, noise:
assert len(Y_train.shape) == 2
assert pred_mean.shape[0] == pred_std.shape[0]

with np.errstate(divide='ignore'):
with np.errstate(divide="ignore"):
val_z = (np.min(Y_train) - pred_mean) / (pred_std + jitter)
val_ei = (np.min(Y_train) - pred_mean) * scipy.stats.norm.cdf(val_z) \
+ pred_std * scipy.stats.norm.pdf(val_z)
val_ei = (np.min(Y_train) - pred_mean) * scipy.stats.norm.cdf(
val_z
) + pred_std * scipy.stats.norm.pdf(val_z)
val_aei = val_ei * (1.0 - noise / np.sqrt(pred_std**2 + noise**2))
return val_aei


@utils_common.validate_types
def pure_exploit(pred_mean: np.ndarray) -> np.ndarray:
"""
Expand All @@ -198,6 +216,7 @@ def pure_exploit(pred_mean: np.ndarray) -> np.ndarray:

return -pred_mean


@utils_common.validate_types
def pure_explore(pred_std: np.ndarray) -> np.ndarray:
"""
Expand Down
90 changes: 54 additions & 36 deletions bayeso/bo/base_bo.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,15 @@ class BaseBO(abc.ABC):
"""

def __init__(self,
def __init__(
self,
range_X: np.ndarray,
str_surrogate: str,
str_acq: str,
str_optimizer_method_bo: str,
normalize_Y: bool,
str_exp: str,
debug: bool
debug: bool,
):
"""
Constructor method
Expand All @@ -60,26 +61,32 @@ def __init__(self,
assert len(range_X.shape) == 2
assert range_X.shape[1] == 2
assert (range_X[:, 0] <= range_X[:, 1]).all()
assert str_surrogate in constants.ALLOWED_SURROGATE \
+ constants.ALLOWED_SURROGATE_TREES
assert (
str_surrogate
in constants.ALLOWED_SURROGATE + constants.ALLOWED_SURROGATE_TREES
)
assert str_acq in constants.ALLOWED_BO_ACQ
assert str_optimizer_method_bo in constants.ALLOWED_OPTIMIZER_METHOD_BO \
assert (
str_optimizer_method_bo
in constants.ALLOWED_OPTIMIZER_METHOD_BO
+ constants.ALLOWED_OPTIMIZER_METHOD_BO_TREES
)

self.range_X = range_X
self.num_dim = range_X.shape[0]
self.str_surrogate = str_surrogate
self.str_acq = str_acq
self.str_optimizer_method_bo = utils_bo.check_optimizer_method_bo(
str_optimizer_method_bo, range_X.shape[0], debug)
str_optimizer_method_bo, range_X.shape[0], debug
)
self.normalize_Y = normalize_Y
self.str_exp = str_exp
self.debug = debug

if str_exp is not None:
self.logger = utils_logger.get_logger(f'bo_w_{str_surrogate}_{str_exp}')
self.logger = utils_logger.get_logger(f"bo_w_{str_surrogate}_{str_exp}")
else:
self.logger = utils_logger.get_logger(f'bo_w_{str_surrogate}')
self.logger = utils_logger.get_logger(f"bo_w_{str_surrogate}")

def _get_random_state(self, seed: constants.TYPING_UNION_INT_NONE):
"""
Expand Down Expand Up @@ -118,7 +125,7 @@ def _get_bounds(self) -> constants.TYPING_LIST:
list_bounds.append(tuple(elem))
return list_bounds

def _get_samples_grid(self, num_grids: int=constants.NUM_GRIDS_AO) -> np.ndarray:
def _get_samples_grid(self, num_grids: int = constants.NUM_GRIDS_AO) -> np.ndarray:
"""
It returns grids of `self.range_X`.
Expand All @@ -137,8 +144,8 @@ def _get_samples_grid(self, num_grids: int=constants.NUM_GRIDS_AO) -> np.ndarray
initials = utils_common.get_grids(self.range_X, num_grids)
return initials

def _get_samples_uniform(self, num_samples: int,
seed: constants.TYPING_UNION_INT_NONE=None
def _get_samples_uniform(
self, num_samples: int, seed: constants.TYPING_UNION_INT_NONE = None
) -> np.ndarray:
"""
It returns `num_samples` examples uniformly sampled.
Expand Down Expand Up @@ -169,8 +176,8 @@ def _get_samples_uniform(self, num_samples: int,
initials = np.array(list_initials)
return initials

def _get_samples_gaussian(self, num_samples: int,
seed: constants.TYPING_UNION_INT_NONE=None
def _get_samples_gaussian(
self, num_samples: int, seed: constants.TYPING_UNION_INT_NONE = None
) -> np.ndarray:
"""
It returns `num_samples` examples sampled from Gaussian distribution.
Expand Down Expand Up @@ -208,8 +215,8 @@ def _get_samples_gaussian(self, num_samples: int,
initials = np.array(list_initials)
return initials

def _get_samples_sobol(self, num_samples: int,
seed: constants.TYPING_UNION_INT_NONE=None
def _get_samples_sobol(
self, num_samples: int, seed: constants.TYPING_UNION_INT_NONE = None
) -> np.ndarray:
"""
It returns `num_samples` examples sampled from Sobol' sequence.
Expand All @@ -232,12 +239,14 @@ def _get_samples_sobol(self, num_samples: int,
sampler = scsqmc.Sobol(self.num_dim, scramble=True, seed=seed)
samples = sampler.random(num_samples)

samples = samples * (self.range_X[:, 1].flatten() - self.range_X[:, 0].flatten()) \
samples = (
samples * (self.range_X[:, 1].flatten() - self.range_X[:, 0].flatten())
+ self.range_X[:, 0].flatten()
)
return samples

def _get_samples_halton(self, num_samples: int,
seed: constants.TYPING_UNION_INT_NONE=None
def _get_samples_halton(
self, num_samples: int, seed: constants.TYPING_UNION_INT_NONE = None
) -> np.ndarray:
"""
It returns `num_samples` examples sampled by Halton algorithm.
Expand All @@ -260,13 +269,17 @@ def _get_samples_halton(self, num_samples: int,
sampler = scsqmc.Halton(self.num_dim, scramble=True, seed=seed)
samples = sampler.random(num_samples)

samples = samples * (self.range_X[:, 1].flatten() - self.range_X[:, 0].flatten()) \
samples = (
samples * (self.range_X[:, 1].flatten() - self.range_X[:, 0].flatten())
+ self.range_X[:, 0].flatten()
)
return samples

def get_samples(self, str_sampling_method: str,
num_samples: int=constants.NUM_SAMPLES_AO,
seed: constants.TYPING_UNION_INT_NONE=None,
def get_samples(
self,
str_sampling_method: str,
num_samples: int = constants.NUM_SAMPLES_AO,
seed: constants.TYPING_UNION_INT_NONE = None,
) -> np.ndarray:
"""
It returns `num_samples` examples, sampled by a sampling method `str_sampling_method`.
Expand All @@ -290,29 +303,34 @@ def get_samples(self, str_sampling_method: str,
assert isinstance(seed, (int, constants.TYPE_NONE))
assert str_sampling_method in constants.ALLOWED_SAMPLING_METHOD

if str_sampling_method == 'grid':
if str_sampling_method == "grid":
if self.debug:
self.logger.debug('For this option, num_samples is used as num_grids.')
self.logger.debug("For this option, num_samples is used as num_grids.")
samples = self._get_samples_grid(num_grids=num_samples)
elif str_sampling_method == 'uniform':
elif str_sampling_method == "uniform":
samples = self._get_samples_uniform(num_samples, seed=seed)
elif str_sampling_method == 'gaussian':
elif str_sampling_method == "gaussian":
samples = self._get_samples_gaussian(num_samples, seed=seed)
elif str_sampling_method == 'sobol':
elif str_sampling_method == "sobol":
samples = self._get_samples_sobol(num_samples, seed=seed)
elif str_sampling_method == 'halton':
elif str_sampling_method == "halton":
samples = self._get_samples_halton(num_samples, seed=seed)
else:
raise NotImplementedError('get_samples: allowed str_sampling_method,\
but it is not implemented.')
raise NotImplementedError(
"get_samples: allowed str_sampling_method,\
but it is not implemented."
)

if self.debug:
self.logger.debug('samples:\n%s', utils_logger.get_str_array(samples))
self.logger.debug("samples:\n%s", utils_logger.get_str_array(samples))

return samples

def get_initials(self, str_initial_method: str, num_initials: int,
seed: constants.TYPING_UNION_INT_NONE=None,
def get_initials(
self,
str_initial_method: str,
num_initials: int,
seed: constants.TYPING_UNION_INT_NONE = None,
) -> np.ndarray:
"""
It returns `num_initials` examples, sampled by a sampling method `str_initial_method`.
Expand All @@ -339,21 +357,21 @@ def get_initials(self, str_initial_method: str, num_initials: int,
return self.get_samples(str_initial_method, num_samples=num_initials, seed=seed)

@abc.abstractmethod
def compute_posteriors(self): # pragma: no cover
def compute_posteriors(self): # pragma: no cover
"""
It is an abstract method.
"""

@abc.abstractmethod
def compute_acquisitions(self): # pragma: no cover
def compute_acquisitions(self): # pragma: no cover
"""
It is an abstract method.
"""

@abc.abstractmethod
def optimize(self): # pragma: no cover
def optimize(self): # pragma: no cover
"""
It is an abstract method.
Expand Down
Loading

0 comments on commit 7d24f44

Please sign in to comment.