in bayesmark/space.py [0:0]
def __init__(self, dtype, default_round, warp="linear", values=None, range_=None):
"""Generic constructor of `Space` class.
Not intended to be called directly but instead by child classes. However, `Space` is not an abstract class and
will not give an error when instantiated.
"""
self.dtype = dtype
assert warp in WARP_DICT, "invalid space %s, allowed spaces are: %s" % (str(warp), str(WARP_DICT.keys()))
self.warp_f = WARP_DICT[warp]
self.unwarp_f = UNWARP_DICT[warp]
# Setup range and rounding if values is suplied
assert (values is None) != (range_ is None)
round_to_values = default_round
if range_ is None: # => value is not None
# Debatable if unique should be done before or after cast. But I
# think after is better, esp. when changing precisions.
values = np.asarray(values, dtype=dtype)
values = np.unique(values) # values now 1D ndarray no matter what
check_array(
values,
"unique values",
pre=True,
ndim=1,
dtype=dtype,
min_size=2,
allow_infinity=False,
allow_nan=False,
)
# Extrapolation might happen due to numerics in type conversions.
# Bounds checking is still done in validate routines.
round_to_values = interp1d(values, values, kind="nearest", fill_value="extrapolate")
range_ = (values[0], values[-1])
# Save values and rounding
# Values is either None or was validated inside if statement
self.values = values
self.round_to_values = round_to_values
# Note that if dtype=None that is the default for asarray.
range_ = np.asarray(range_, dtype=dtype)
check_array(range_, "range", pre=True, shape=(2,), dtype=dtype, unsorted=False)
# Save range info, with input validation and post validation
self.lower, self.upper = range_
# Convert to warped bounds too with lots of post validation
self.lower_warped, self.upper_warped = self.warp_f(range_[..., None]).astype(WARPED_DTYPE, copy=False)
check_array(
self.lower_warped,
"warped lower bound %s(%.1f)" % (warp, self.lower),
ndim=1,
pre=True,
dtype=WARPED_DTYPE,
allow_infinity=False,
allow_nan=False,
)
# Should never happen if warpers are strictly monotonic:
assert np.all(self.lower_warped <= self.upper_warped)
# Make sure a bit bigger to keep away from lower due to numerics
self.upper_warped = np.maximum(self.upper_warped, np.nextafter(self.lower_warped, np.inf))
check_array(
self.upper_warped,
"warped upper bound %s(%.1f)" % (warp, self.upper),
pre=True,
shape=self.lower_warped.shape,
dtype=WARPED_DTYPE,
allow_infinity=False,
allow_nan=False,
)
# Should never happen if warpers are strictly monotonic:
assert np.all(self.lower_warped < self.upper_warped)