From ed48b474b95a9b6ea4af76556b8d30cb9ff06be4 Mon Sep 17 00:00:00 2001 From: Yuichi Motoyama Date: Thu, 6 Apr 2023 14:25:34 +0900 Subject: [PATCH] format by black --- examples/multiple_score.py | 2 +- examples/simple.py | 2 +- examples/simple_time.py | 2 +- physbo/gp/core/__init__.py | 3 ++- physbo/gp/core/learning.py | 4 +-- physbo/gp/core/model.py | 2 +- physbo/gp/core/prior.py | 2 +- physbo/gp/cov/gauss.py | 10 ++++---- physbo/gp/lik/gauss.py | 6 ++--- physbo/gp/mean/const.py | 2 +- physbo/gp/mean/zero.py | 2 +- physbo/gp/predictor.py | 2 +- physbo/misc/__init__.py | 2 +- physbo/opt/adam.py | 2 +- physbo/search/discrete/policy.py | 13 +++++++--- physbo/search/discrete/results.py | 8 +++--- physbo/search/discrete_multi/policy.py | 24 ++++++++++++------ physbo/search/discrete_multi/results.py | 8 +++--- physbo/search/score_multi.py | 1 + tests/integrated/test_gp.py | 2 +- tests/integrated/test_interactive_example.py | 14 ++++++++--- tests/integrated/test_multi_objective.py | 20 +++++++++++---- tests/integrated/test_simple_example.py | 16 +++++++++--- tests/unit/test_policy.py | 26 ++++++++++++-------- tests/unit/test_variable.py | 12 ++++----- 25 files changed, 116 insertions(+), 71 deletions(-) diff --git a/examples/multiple_score.py b/examples/multiple_score.py index 1224eac8..541d8811 100644 --- a/examples/multiple_score.py +++ b/examples/multiple_score.py @@ -6,7 +6,7 @@ # Make a set of candidates, test_X D = 2 # The number of params (the dimension of parameter space) Nx = 11 # The number of candidates -N = Nx*Nx +N = Nx * Nx # score = "HVPI" score = "EHVI" diff --git a/examples/simple.py b/examples/simple.py index f296ba33..3e764d86 100644 --- a/examples/simple.py +++ b/examples/simple.py @@ -9,7 +9,7 @@ def simulator(actions: np.ndarray) -> np.ndarray: - """ Objective function + """Objective function Quadratic function, -Σ_i x_i^2 Receives an array of actions (indices of candidates) and returns the corresponding results as an array diff --git a/examples/simple_time.py b/examples/simple_time.py index fd925bb8..8442d6b3 100644 --- a/examples/simple_time.py +++ b/examples/simple_time.py @@ -14,7 +14,7 @@ def simulator(actions: np.ndarray) -> np.ndarray: - """ Objective function + """Objective function Quadratic function, -Σ_i x_i^2 Receives an array of actions (indices of candidates) and returns the corresponding results as an array diff --git a/physbo/gp/core/__init__.py b/physbo/gp/core/__init__.py index 54a1a8ca..d2e597be 100644 --- a/physbo/gp/core/__init__.py +++ b/physbo/gp/core/__init__.py @@ -1,3 +1,4 @@ from .prior import prior from .model import model -#from predictor import predictor + +# from predictor import predictor diff --git a/physbo/gp/core/learning.py b/physbo/gp/core/learning.py index 18bade48..178697e7 100644 --- a/physbo/gp/core/learning.py +++ b/physbo/gp/core/learning.py @@ -309,7 +309,7 @@ def get_one_update(self, params, X, t): class adam(online): - """ default """ + """default""" def __init__(self, gp, config): """ @@ -351,7 +351,7 @@ def get_one_update(self, params, X, t): """ grad = self.gp.get_grad_marlik(params, X, t) self.m = self.m * self.beta + grad * (1 - self.beta) - self.v = self.v * self.gamma + grad ** 2 * (1 - self.gamma) + self.v = self.v * self.gamma + grad**2 * (1 - self.gamma) hat_m = self.m / (1 - self.beta ** (self.num_iter)) hat_v = self.v / (1 - self.gamma ** (self.num_iter)) return -self.alpha * hat_m / (np.sqrt(hat_v) + self.epsilon) diff --git a/physbo/gp/core/model.py b/physbo/gp/core/model.py index 6cf07cd0..0bdd51a7 100644 --- a/physbo/gp/core/model.py +++ b/physbo/gp/core/model.py @@ -301,7 +301,7 @@ def post_sampling(self, X, Z, params=None, N=1, alpha=1): fmean = self.get_post_fmean(X, Z, params=None) fcov = self.get_post_fcov(X, Z, params=None, diag=False) - return np.random.multivariate_normal(fmean, fcov * alpha ** 2, N) + return np.random.multivariate_normal(fmean, fcov * alpha**2, N) def predict_sampling(self, X, Z, params=None, N=1): """ diff --git a/physbo/gp/core/prior.py b/physbo/gp/core/prior.py index 036d16c0..8767d2f0 100644 --- a/physbo/gp/core/prior.py +++ b/physbo/gp/core/prior.py @@ -3,7 +3,7 @@ class prior: - """ prior of gaussian process """ + """prior of gaussian process""" def __init__(self, mean, cov): """ diff --git a/physbo/gp/cov/gauss.py b/physbo/gp/cov/gauss.py index cc6df5a0..c6d02eee 100644 --- a/physbo/gp/cov/gauss.py +++ b/physbo/gp/cov/gauss.py @@ -5,7 +5,7 @@ class gauss: - """ gaussian kernel """ + """gaussian kernel""" def __init__( self, @@ -68,7 +68,7 @@ def print_params(self): print(" Parameters of Gaussian kernel \n ") print(" width = ", +self.width) print(" scale = ", +self.scale) - print(" scale2 = ", +self.scale ** 2) + print(" scale2 = ", +self.scale**2) print(" \n") def prepare(self, params=None): @@ -122,7 +122,7 @@ def get_grad(self, X, params=None): grad[0 : self.num_params - 1, :, :] = grad_width64(X, width, G) else: pairwise_dists = spatial.distance.pdist(X / width, "euclidean") - grad[0, :, :] = G * spatial.distance.squareform(pairwise_dists ** 2) + grad[0, :, :] = G * spatial.distance.squareform(pairwise_dists**2) grad[-1, :, :] = 2 * G return grad @@ -150,7 +150,7 @@ def get_cov(self, X, Z=None, params=None, diag=False): covariant matrix """ params, width, scale = self.prepare(params) - scale2 = scale ** 2 + scale2 = scale**2 if Z is None: if diag: @@ -332,7 +332,7 @@ def rand_expans(self, num_basis, params=None): tupple (W, b, amp) """ params, width, scale = self.prepare(params) - scale2 = scale ** 2 + scale2 = scale**2 amp = np.sqrt((2 * scale2) / num_basis) W = np.random.randn(num_basis, self.num_dim) / width b = np.random.rand(num_basis) * 2 * np.pi diff --git a/physbo/gp/lik/gauss.py b/physbo/gp/lik/gauss.py index d7a1c85f..ab96d0b3 100644 --- a/physbo/gp/lik/gauss.py +++ b/physbo/gp/lik/gauss.py @@ -3,7 +3,7 @@ class gauss: - """ Gaussian likelihood function """ + """Gaussian likelihood function""" def __init__(self, std=1, max_params=1e6, min_params=1e-6): """ @@ -99,7 +99,7 @@ def get_cov(self, num_data, params=None): Diagonal element matrix of exp(2.0*params) """ std = self.trans_params(params) - var = std ** 2 + var = std**2 return var * np.identity(num_data) def get_grad(self, num_data, params=None): @@ -119,7 +119,7 @@ def get_grad(self, num_data, params=None): Diagonal element matrix of 2.0 * exp(2.0*params) """ std = self.trans_params(params) - var = std ** 2 + var = std**2 return var * np.identity(num_data) * 2 def set_params(self, params): diff --git a/physbo/gp/mean/const.py b/physbo/gp/mean/const.py index a41d8826..b845657d 100644 --- a/physbo/gp/mean/const.py +++ b/physbo/gp/mean/const.py @@ -2,7 +2,7 @@ class const: - """ constant """ + """constant""" def __init__(self, params=None, max_params=1e12, min_params=-1e12): """ diff --git a/physbo/gp/mean/zero.py b/physbo/gp/mean/zero.py index 1b09f806..52e4a8bf 100644 --- a/physbo/gp/mean/zero.py +++ b/physbo/gp/mean/zero.py @@ -2,7 +2,7 @@ class zero: - """ zero """ + """zero""" def __init__(self): self.num_params = 0 diff --git a/physbo/gp/predictor.py b/physbo/gp/predictor.py index 1e0f049a..a01c99a3 100644 --- a/physbo/gp/predictor.py +++ b/physbo/gp/predictor.py @@ -2,7 +2,7 @@ class predictor(physbo.predictor.base_predictor): - """ predictor """ + """predictor""" def __init__(self, config, model=None): """ diff --git a/physbo/misc/__init__.py b/physbo/misc/__init__.py index 0cd36bd2..81057bca 100644 --- a/physbo/misc/__init__.py +++ b/physbo/misc/__init__.py @@ -1,4 +1,4 @@ -from .centering import centering +from .centering import centering from .gauss_elim import gauss_elim from .set_config import set_config from ._src.diagAB import diagAB_64 as diagAB diff --git a/physbo/opt/adam.py b/physbo/opt/adam.py index e41fa2ac..f742964e 100644 --- a/physbo/opt/adam.py +++ b/physbo/opt/adam.py @@ -72,7 +72,7 @@ def update(self, params, *args, **kwargs): """ g = self.grad(params, *args, **kwargs) self.m = self.m * self.beta + g * (1 - self.beta) - self.v = self.v * self.gamma + g ** 2 * (1 - self.gamma) + self.v = self.v * self.gamma + g**2 * (1 - self.gamma) hat_m = self.m / (1 - self.beta ** (self.epoch + 1)) hat_v = self.v / (1 - self.gamma ** (self.epoch + 1)) self.epoch += 1 diff --git a/physbo/search/discrete/policy.py b/physbo/search/discrete/policy.py index c69d943e..5930d563 100644 --- a/physbo/search/discrete/policy.py +++ b/physbo/search/discrete/policy.py @@ -60,7 +60,9 @@ def __init__(self, test_X, config=None, initial_data=None, comm=None): self.mpisize = comm.size self.mpirank = comm.rank self.actions = np.array_split(self.actions, self.mpisize)[self.mpirank] - self.config.learning.is_disp = (self.config.learning.is_disp and self.mpirank == 0) + self.config.learning.is_disp = ( + self.config.learning.is_disp and self.mpirank == 0 + ) def set_seed(self, seed): """ @@ -130,7 +132,9 @@ def write( ) self.training.add(X=X, t=t, Z=Z) local_index = np.searchsorted(self.actions, action) - local_index = local_index[np.take(self.actions, local_index, mode='clip') == action] + local_index = local_index[ + np.take(self.actions, local_index, mode="clip") == action + ] self.actions = self._delete_actions(local_index) if self.new_data is None: self.new_data = variable(X=X, t=t, Z=Z) @@ -168,7 +172,6 @@ def random_search( utility.show_interactive_mode(simulator, self.history) for n in range(0, max_num_probes): - time_total = time.time() if is_disp and N > 1: utility.show_start_message_multi_search(self.history.num_runs) @@ -479,7 +482,9 @@ def _get_marginal_score(self, mode, chosen_actions, K, alpha): # draw K samples of the values of objective function of chosen actions new_test_local = self.test.get_subset(chosen_actions) - virtual_t_local = self.predictor.get_predict_samples(self.training, new_test_local, K) + virtual_t_local = self.predictor.get_predict_samples( + self.training, new_test_local, K + ) if self.mpisize == 1: new_test = new_test_local virtual_t = virtual_t_local diff --git a/physbo/search/discrete/results.py b/physbo/search/discrete/results.py index 115054f9..ca9feafa 100644 --- a/physbo/search/discrete/results.py +++ b/physbo/search/discrete/results.py @@ -22,19 +22,19 @@ def __init__(self): @property def time_total(self): - return copy.copy(self.time_total_[0:self.num_runs]) + return copy.copy(self.time_total_[0 : self.num_runs]) @property def time_update_predictor(self): - return copy.copy(self.time_update_predictor_[0:self.num_runs]) + return copy.copy(self.time_update_predictor_[0 : self.num_runs]) @property def time_get_action(self): - return copy.copy(self.time_get_action_[0:self.num_runs]) + return copy.copy(self.time_get_action_[0 : self.num_runs]) @property def time_run_simulator(self): - return copy.copy(self.time_run_simulator_[0:self.num_runs]) + return copy.copy(self.time_run_simulator_[0 : self.num_runs]) def write( self, diff --git a/physbo/search/discrete_multi/policy.py b/physbo/search/discrete_multi/policy.py index e83f72d6..9b54d1ca 100644 --- a/physbo/search/discrete_multi/policy.py +++ b/physbo/search/discrete_multi/policy.py @@ -100,7 +100,9 @@ def write( self.new_data_list[i].add(X=X, t=t[:, i], Z=Z) self.training_list[i].add(X=X, t=t[:, i], Z=Z) local_index = np.searchsorted(self.actions, action) - local_index = local_index[np.take(self.actions, local_index, mode='clip') == action] + local_index = local_index[ + np.take(self.actions, local_index, mode="clip") == action + ] self.actions = self._delete_actions(local_index) def _model(self, i): @@ -123,7 +125,6 @@ def random_search( is_disp=True, disp_pareto_set=False, ): - if self.mpirank != 0: is_disp = False @@ -133,7 +134,6 @@ def random_search( utility.show_interactive_mode(simulator, self.history) for n in range(0, max_num_probes): - time_total = time.time() if is_disp and N > 1: utility.show_start_message_multi_search( @@ -181,7 +181,6 @@ def bayes_search( interval=0, num_rand_basis=0, ): - if self.mpirank != 0: is_disp = False @@ -418,14 +417,18 @@ def _get_marginal_score(self, mode, chosen_actions, K, alpha): virtual_t_list = [np.zeros((K, 0)) for _ in range(self.num_objectives)] for i in range(self.num_objectives): new_test_local = self.test_list[i].get_subset(chosen_actions) - virtual_t_local = self.predictor_list[i].get_predict_samples(self.training_list[i], new_test_local, K) + virtual_t_local = self.predictor_list[i].get_predict_samples( + self.training_list[i], new_test_local, K + ) if self.mpisize == 1: new_test_list[i] = new_test_local virtual_t_list[i] = virtual_t_local else: for nt in self.mpicomm.allgather(new_test_local): new_test_list[i].add(X=nt.X, t=nt.t, Z=nt.Z) - virtual_t_list[i] = np.concatenate(self.mpicomm.allgather(virtual_t_local), axis=1) + virtual_t_list[i] = np.concatenate( + self.mpicomm.allgather(virtual_t_local), axis=1 + ) for k in range(K): predictor_list = [copy.deepcopy(p) for p in self.predictor_list] @@ -438,12 +441,17 @@ def _get_marginal_score(self, mode, chosen_actions, K, alpha): if virtual_train.Z is None: training_list[i].add(virtual_train.X, virtual_train.t) else: - training_list[i].add(virtual_train.X, virtual_train.t, virtual_train.Z) + training_list[i].add( + virtual_train.X, virtual_train.t, virtual_train.Z + ) predictor_list[i].update(training_list[i], virtual_train) f[k, :] = self.get_score( - mode, predictor_list=predictor_list, training_list=training_list, parallel=False + mode, + predictor_list=predictor_list, + training_list=training_list, + parallel=False, ) return np.mean(f, axis=0) diff --git a/physbo/search/discrete_multi/results.py b/physbo/search/discrete_multi/results.py index 522a32c5..bde6e98a 100644 --- a/physbo/search/discrete_multi/results.py +++ b/physbo/search/discrete_multi/results.py @@ -25,19 +25,19 @@ def __init__(self, num_objectives): @property def time_total(self): - return copy.copy(self._time_total[0:self.num_runs]) + return copy.copy(self._time_total[0 : self.num_runs]) @property def time_update_predictor(self): - return copy.copy(self._time_update_predictor[0:self.num_runs]) + return copy.copy(self._time_update_predictor[0 : self.num_runs]) @property def time_get_action(self): - return copy.copy(self._time_get_action[0:self.num_runs]) + return copy.copy(self._time_get_action[0 : self.num_runs]) @property def time_run_simulator(self): - return copy.copy(self._time_run_simulator[0:self.num_runs]) + return copy.copy(self._time_run_simulator[0 : self.num_runs]) def write( self, diff --git a/physbo/search/score_multi.py b/physbo/search/score_multi.py index 2aab90bc..599f49fb 100644 --- a/physbo/search/score_multi.py +++ b/physbo/search/score_multi.py @@ -30,6 +30,7 @@ def score(mode, predictor_list, test, training_list, **kwargs): raise NotImplementedError("mode must be EHVI, HVPI or TS.") return f + def HVPI(fmean, fstd, pareto): """ Calculate Hypervolume-based Probability of Improvement (HVPI). diff --git a/tests/integrated/test_gp.py b/tests/integrated/test_gp.py index 78f1e9a3..849fdd60 100644 --- a/tests/integrated/test_gp.py +++ b/tests/integrated/test_gp.py @@ -17,7 +17,7 @@ def __init__(self, noise=1.0e-5): np.random.seed(12345) self.nslice = 11 self.dim = 2 - self.N = self.nslice ** self.dim + self.N = self.nslice**self.dim self.X = np.zeros((self.N, self.dim)) self.t = np.zeros(self.N) for i, x in enumerate( diff --git a/tests/integrated/test_interactive_example.py b/tests/integrated/test_interactive_example.py index a160512c..f55c5fab 100644 --- a/tests/integrated/test_interactive_example.py +++ b/tests/integrated/test_interactive_example.py @@ -16,9 +16,11 @@ class simulator: def __init__(self): self.nslice = 11 self.dim = 2 - self.N = self.nslice ** self.dim + self.N = self.nslice**self.dim self.X = np.zeros((self.N, self.dim)) - for i, x in enumerate(product(np.linspace(0.0, 1.0, self.nslice), repeat=self.dim)): + for i, x in enumerate( + product(np.linspace(0.0, 1.0, self.nslice), repeat=self.dim) + ): self.X[i, :] = list(x) def __call__(self, actions): @@ -32,14 +34,18 @@ def test_interactive(): policy = physbo.search.discrete.policy(test_X=sim.X) policy.set_seed(12345) - actions = policy.random_search(max_num_probes=1, num_search_each_probe=nrand, simulator=None) + actions = policy.random_search( + max_num_probes=1, num_search_each_probe=nrand, simulator=None + ) targets = sim(actions) print(actions) print(targets) policy.write(actions, targets) physbo.search.utility.show_search_results(policy.history, nrand) - actions = policy.bayes_search(max_num_probes=1, num_search_each_probe=nsearch, simulator=None, score='TS') + actions = policy.bayes_search( + max_num_probes=1, num_search_each_probe=nsearch, simulator=None, score="TS" + ) targets = sim(actions) print(actions) print(targets) diff --git a/tests/integrated/test_multi_objective.py b/tests/integrated/test_multi_objective.py index 43c388cb..61f5074a 100644 --- a/tests/integrated/test_multi_objective.py +++ b/tests/integrated/test_multi_objective.py @@ -39,6 +39,7 @@ def test_multi_objective_EHVI(): assert vid == pytest.approx(vid_ref, rel=1e-3) policy.get_score("EHVI", xs=sim.X) + def test_multi_objective_HVPI(): sim = simulator() nrand = 10 @@ -52,6 +53,7 @@ def test_multi_objective_HVPI(): assert vid == pytest.approx(vid_ref, rel=1e-3) policy.get_score("HVPI", xs=sim.X) + def test_multi_objective_TS(): sim = simulator() nrand = 10 @@ -73,12 +75,15 @@ def test_multi_objective_EHVI_rand(): policy = physbo.search.discrete_multi.policy(test_X=sim.X, num_objectives=2) policy.set_seed(12345) res = policy.random_search(max_num_probes=nrand, simulator=sim) - res = policy.bayes_search(max_num_probes=nsearch, simulator=sim, score="EHVI", num_rand_basis=100) + res = policy.bayes_search( + max_num_probes=nsearch, simulator=sim, score="EHVI", num_rand_basis=100 + ) vid = res.pareto.volume_in_dominance([-1, -1], [0, 0]) - vid_ref = 0.08400891973743863 + vid_ref = 0.08400891973743863 assert vid == pytest.approx(vid_ref, rel=1e-3) policy.get_score("EHVI", xs=sim.X) + def test_multi_objective_HVPI_rand(): sim = simulator() nrand = 10 @@ -86,12 +91,15 @@ def test_multi_objective_HVPI_rand(): policy = physbo.search.discrete_multi.policy(test_X=sim.X, num_objectives=2) policy.set_seed(12345) res = policy.random_search(max_num_probes=nrand, simulator=sim) - res = policy.bayes_search(max_num_probes=nsearch, simulator=sim, score="HVPI", num_rand_basis=100) + res = policy.bayes_search( + max_num_probes=nsearch, simulator=sim, score="HVPI", num_rand_basis=100 + ) vid = res.pareto.volume_in_dominance([-1, -1], [0, 0]) vid_ref = 0.13374086517165518 assert vid == pytest.approx(vid_ref, rel=1e-3) policy.get_score("HVPI", xs=sim.X) + def test_multi_objective_TS_rand(): sim = simulator() nrand = 10 @@ -99,8 +107,10 @@ def test_multi_objective_TS_rand(): policy = physbo.search.discrete_multi.policy(test_X=sim.X, num_objectives=2) policy.set_seed(12345) res = policy.random_search(max_num_probes=nrand, simulator=sim) - res = policy.bayes_search(max_num_probes=nsearch, simulator=sim, score="TS", num_rand_basis=100) + res = policy.bayes_search( + max_num_probes=nsearch, simulator=sim, score="TS", num_rand_basis=100 + ) vid = res.pareto.volume_in_dominance([-1, -1], [0, 0]) - vid_ref = 0.134435814966692 + vid_ref = 0.134435814966692 assert vid == pytest.approx(vid_ref, rel=1e-3) policy.get_score("TS", xs=sim.X) diff --git a/tests/integrated/test_simple_example.py b/tests/integrated/test_simple_example.py index b5598263..73b26432 100644 --- a/tests/integrated/test_simple_example.py +++ b/tests/integrated/test_simple_example.py @@ -16,7 +16,7 @@ class simulator: def __init__(self): self.nslice = 11 self.dim = 2 - self.N = self.nslice ** self.dim + self.N = self.nslice**self.dim self.X = np.zeros((self.N, self.dim)) for i, x in enumerate( product(np.linspace(0.0, 1.0, self.nslice), repeat=self.dim) @@ -63,7 +63,9 @@ def test_bayes_search_EI_rand(): policy = physbo.search.discrete.policy(test_X=sim.X) policy.set_seed(12345) res = policy.random_search(max_num_probes=nrand, simulator=sim) - res = policy.bayes_search(max_num_probes=nsearch, simulator=sim, score="EI", num_rand_basis=100) + res = policy.bayes_search( + max_num_probes=nsearch, simulator=sim, score="EI", num_rand_basis=100 + ) best_fx, best_action = res.export_all_sequence_best_fx() print(best_fx) print(best_action) @@ -71,6 +73,7 @@ def test_bayes_search_EI_rand(): assert best_action[-1] == 60 policy.get_score("EI", xs=sim.X) + def test_bayes_search_PI(): sim = simulator() nrand = 10 @@ -94,7 +97,9 @@ def test_bayes_search_PI_rand(): policy = physbo.search.discrete.policy(test_X=sim.X) policy.set_seed(12345) res = policy.random_search(max_num_probes=nrand, simulator=sim) - res = policy.bayes_search(max_num_probes=nsearch, simulator=sim, score="PI", num_rand_basis=100) + res = policy.bayes_search( + max_num_probes=nsearch, simulator=sim, score="PI", num_rand_basis=100 + ) best_fx, best_action = res.export_all_sequence_best_fx() print(best_fx) print(best_action) @@ -102,6 +107,7 @@ def test_bayes_search_PI_rand(): assert best_action[-1] == 60 policy.get_score("PI", xs=sim.X) + def test_bayes_search_TS(): sim = simulator() nrand = 10 @@ -125,7 +131,9 @@ def test_bayes_search_TS_rand(): policy = physbo.search.discrete.policy(test_X=sim.X) policy.set_seed(12345) res = policy.random_search(max_num_probes=nrand, simulator=sim) - res = policy.bayes_search(max_num_probes=nsearch, simulator=sim, score="TS", num_rand_basis=100) + res = policy.bayes_search( + max_num_probes=nsearch, simulator=sim, score="TS", num_rand_basis=100 + ) best_fx, best_action = res.export_all_sequence_best_fx() print(best_fx) print(best_action) diff --git a/tests/unit/test_policy.py b/tests/unit/test_policy.py index 16d8d7c4..ea1e15a2 100644 --- a/tests/unit/test_policy.py +++ b/tests/unit/test_policy.py @@ -47,7 +47,9 @@ def test_write(policy, X): ACTIONS = np.array([0, 1], np.int32) policy.write(ACTIONS, np.apply_along_axis(simulator, 1, X[ACTIONS])) - numpy.testing.assert_array_equal(ACTIONS, policy.history.chosen_actions[:len(ACTIONS)]) + numpy.testing.assert_array_equal( + ACTIONS, policy.history.chosen_actions[: len(ACTIONS)] + ) assert len(X) - len(ACTIONS) == len(policy.actions) @@ -118,14 +120,16 @@ def test_get_score(policy, mocker, X): policy.set_seed(137) res = policy.get_score("EI", xs=X) - ref = np.array([ - 3.98940120e-07, - 3.98934542e-07, - 3.98924610e-07, - 3.98914969e-07, - 3.98911183e-07, - 3.98914969e-07, - ]) + ref = np.array( + [ + 3.98940120e-07, + 3.98934542e-07, + 3.98924610e-07, + 3.98914969e-07, + 3.98911183e-07, + 3.98914969e-07, + ] + ) numpy.testing.assert_allclose(res, ref) res = policy.get_score("PI", xs=X) @@ -134,7 +138,9 @@ def test_get_score(policy, mocker, X): numpy.testing.assert_allclose(res, ref) res = policy.get_score("TS", xs=X) - ref = np.array([1.00000021, 0.99999978, 0.9999993, 0.9999991, 0.99999905, 0.99999888]) + ref = np.array( + [1.00000021, 0.99999978, 0.9999993, 0.9999991, 0.99999905, 0.99999888] + ) numpy.testing.assert_allclose(res, ref) with pytest.raises(NotImplementedError): diff --git a/tests/unit/test_variable.py b/tests/unit/test_variable.py index e674608c..6b77c999 100644 --- a/tests/unit/test_variable.py +++ b/tests/unit/test_variable.py @@ -12,6 +12,7 @@ def X(): [[0.0, 0.0, 0.0], [1.0, 1.0, 1.0], [2.0, 2.0, 2.0], [3.0, 3.0, 3.0]] ) + @pytest.fixture def t(): return np.array([0.0, 1.0, 2.0, 3.0]) @@ -19,9 +20,8 @@ def t(): @pytest.fixture def Z(): - return np.array( - [[0.0, 0.0], [1.0, 1.0], [2.0, 2.0], [3.0, 3.0]] - ) + return np.array([[0.0, 0.0], [1.0, 1.0], [2.0, 2.0], [3.0, 3.0]]) + @pytest.fixture def variable(X, t, Z): @@ -80,19 +80,19 @@ def test_add(variable, X, t, Z, mocker): def test_add_X(variable, X): n = X.shape[0] variable.add_X(X) - assert np.array_equal(variable.X[n:2*n, :], X) + assert np.array_equal(variable.X[n : 2 * n, :], X) def test_add_t(variable, t): n = t.shape[0] variable.add_t(t) - assert np.array_equal(variable.t[n:2*n], t) + assert np.array_equal(variable.t[n : 2 * n], t) def test_add_Z(variable, Z): n = Z.shape[0] variable.add_Z(Z) - assert np.array_equal(variable.Z[n:2*n, :], Z) + assert np.array_equal(variable.Z[n : 2 * n, :], Z) def test_save_load(variable, tmpdir):