Skip to content

Commit 8ffdb4f

Browse files
committed
Small fixes, minor cosmetic changes
1 parent 55ffaf5 commit 8ffdb4f

File tree

3 files changed

+15
-38
lines changed

3 files changed

+15
-38
lines changed

bayes_opt/constraint.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77

88
class ConstraintModel():
9-
"""Model constraints using GPRs.
9+
"""Model constraints using GP regressors.
1010
1111
This class takes the function to optimize as well as the parameters bounds
1212
in order to find which values for the parameters yield the maximum value
@@ -65,7 +65,7 @@ def model(self):
6565
"""Return GP regressors of the constraint function."""
6666
return self._model
6767

68-
def eval(self, **kwargs):
68+
def eval(self, **kwargs: dict):
6969
"""Evaluate the constraint function.
7070
7171
Parameters
@@ -206,7 +206,7 @@ def allowed(self, constraint_values):
206206
207207
Returns
208208
-------
209-
np.ndarrray of shape (n_samples,)
209+
np.ndarrray of shape (n_samples,)
210210
Specifying wheter the constraints are fulfilled.
211211
212212
"""

bayes_opt/domain_reduction.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,21 +117,21 @@ def _update(self, target_space: TargetSpace) -> None:
117117

118118
self.r = self.contraction_rate * self.r
119119

120-
def _trim(self, new_bounds: np.array, global_bounds: np.array) -> np.array:
120+
def _trim(self, new_bounds: np.ndarray, global_bounds: np.ndarray) -> np.ndarray:
121121
"""
122122
Adjust the new_bounds and verify that they adhere to global_bounds and minimum_window.
123123
124124
Parameters
125125
----------
126-
new_bounds : np.array
126+
new_bounds : np.ndarray
127127
The proposed new_bounds that (may) need adjustment.
128128
129-
global_bounds : np.array
129+
global_bounds : np.ndarray
130130
The maximum allowable bounds for each parameter.
131131
132132
Returns
133133
-------
134-
new_bounds : np.array
134+
new_bounds : np.ndarray
135135
The adjusted bounds after enforcing constraints.
136136
"""
137137
#sort bounds
@@ -192,15 +192,15 @@ def _trim(self, new_bounds: np.array, global_bounds: np.array) -> np.array:
192192

193193
return new_bounds
194194

195-
def _window_bounds_compatibility(self, global_bounds: np.array) -> bool:
195+
def _window_bounds_compatibility(self, global_bounds: np.ndarray) -> bool:
196196
"""Check if global bounds are compatible with the minimum window sizes."""
197197
for i, entry in enumerate(global_bounds):
198198
global_window_width = abs(entry[1] - entry[0])
199199
if global_window_width < self.minimum_window[i]:
200200
raise ValueError(
201201
"Global bounds are not compatible with the minimum window size.")
202202

203-
def _create_bounds(self, parameters: dict, bounds: np.array) -> dict:
203+
def _create_bounds(self, parameters: dict, bounds: np.ndarray) -> dict:
204204
return {param: bounds[i, :] for i, param in enumerate(parameters)}
205205

206206
def transform(self, target_space: TargetSpace) -> dict:

bayes_opt/target_space.py

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -205,22 +205,6 @@ def mask(self):
205205

206206
return mask
207207

208-
@property
209-
def mask(self):
210-
'''Returns a boolean array of the points that satisfy the constraint and boundary conditions'''
211-
mask = np.ones_like(self.target, dtype=bool)
212-
213-
# mask points that don't satisfy the constraint
214-
if self._constraint is not None:
215-
mask &= self._constraint.allowed(self._constraint_values)
216-
217-
# mask points that are outside the bounds
218-
if self._bounds is not None:
219-
within_bounds = np.all((self._bounds[:, 0] <= self._params) &
220-
(self._params <= self._bounds[:, 1]), axis=1)
221-
mask &= within_bounds
222-
223-
return mask
224208

225209
def params_to_array(self, params):
226210
"""Convert a dict representation of parameters into an array version.
@@ -235,13 +219,11 @@ def params_to_array(self, params):
235219
np.ndarray
236220
Representation of the parameters as dictionary.
237221
"""
238-
try:
239-
assert set(params) == set(self.keys)
240-
except AssertionError as e:
222+
if not set(params) == set(self.keys):
241223
raise ValueError(
242224
f"Parameters' keys ({sorted(params)}) do " +
243225
f"not match the expected set of keys ({self.keys})."
244-
) from e
226+
)
245227
return np.asarray([params[key] for key in self.keys])
246228

247229
def array_to_params(self, x):
@@ -257,13 +239,11 @@ def array_to_params(self, x):
257239
dict
258240
Representation of the parameters as dictionary.
259241
"""
260-
try:
261-
assert len(x) == len(self.keys)
262-
except AssertionError as e:
242+
if not len(x) == len(self.keys):
263243
raise ValueError(
264244
f"Size of array ({len(x)}) is different than the " +
265245
f"expected number of parameters ({len(self.keys)})."
266-
) from e
246+
)
267247
return dict(zip(self.keys, x))
268248

269249
def _as_array(self, x):
@@ -273,13 +253,11 @@ def _as_array(self, x):
273253
x = self.params_to_array(x)
274254

275255
x = x.ravel()
276-
try:
277-
assert x.size == self.dim
278-
except AssertionError as e:
256+
if not x.size == self.dim:
279257
raise ValueError(
280258
f"Size of array ({len(x)}) is different than the " +
281259
f"expected number of parameters ({len(self.keys)})."
282-
) from e
260+
)
283261
return x
284262

285263
def register(self, params, target, constraint_value=None):
@@ -485,7 +463,6 @@ def res(self):
485463
Does not report if points are within the bounds of the parameter space.
486464
487465
"""
488-
489466
if self._constraint is None:
490467
params = [dict(zip(self.keys, p)) for p in self.params]
491468

0 commit comments

Comments
 (0)