Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_bad_setitem(self):
"""Check exceptions in setting items in Space."""
space = Space()
# The name of an integer must be a of `str` type.
# Integers are reversed for indexing the OrderedDict.
with pytest.raises(TypeError) as exc:
space[5] = Integer('yolo', 'uniform', -3, 6)
assert "string" in str(exc.value)
# Only object of type `Dimension` are allowed in `Space`.
with pytest.raises(TypeError) as exc:
space['ispis'] = 'nope'
assert "Dimension" in str(exc.value)
# Cannot register something with the same name.
space.register(Integer('yolo', 'uniform', -3, 6))
with pytest.raises(ValueError) as exc:
space.register(Real('yolo', 'uniform', 0, 6))
assert "another name" in str(exc.value)
def space_each_type(dim, dim2):
"""Create an example `Space`."""
space = Space()
space.register(dim)
space.register(dim2)
space.register(Integer('yolo3', 'randint', 3, 10))
return space
def test_scipy_integer_dist_interval_bug(self):
"""Scipy does not return the correct answer for integer distributions."""
dim = Integer('yolo', 'randint', -3, 6)
assert dim.interval() == (-3, 6)
assert dim.interval(1.0) == (-3, 6)
assert dim.interval(0.9999) == (-3, 6)
dim = Integer('yolo2', 'randint', -2, 4, loc=8)
assert dim.interval() == (6, 12)
def test_interval(self):
"""Check whether interval is cool."""
space = Space()
probs = (0.1, 0.2, 0.3, 0.4)
categories = ('asdfa', 2, 3, 4)
dim = Categorical('yolo', OrderedDict(zip(categories, probs)), shape=2)
space.register(dim)
dim = Integer('yolo2', 'uniform', -3, 6)
space.register(dim)
dim = Real('yolo3', 'norm', 0.9)
space.register(dim)
assert space.interval() == [categories, (-3, 3), (-np.inf, np.inf)]
def test_set_outside_bounds_default_value(self):
"""Make sure the default value is inside the bounds of the dimensions"""
with pytest.raises(ValueError):
Integer('yolo', 'uniform', -3, 2, default_value=4)
def test_contains(self):
"""Check for integer test."""
dim = Integer('yolo', 'uniform', -3, 6)
assert 0.1 not in dim
assert (0.1, -0.2) not in dim
assert 0 in dim
assert (1, 2) not in dim
assert 6 not in dim
assert -3 in dim
assert -4 not in dim
def test_sample(self, seed):
"""Check whether sampling works correctly."""
space = Space()
probs = (0.1, 0.2, 0.3, 0.4)
categories = ('asdfa', 2, 3, 4)
dim1 = Categorical('yolo', OrderedDict(zip(categories, probs)), shape=(2, 2))
space.register(dim1)
dim2 = Integer('yolo2', 'uniform', -3, 6)
space.register(dim2)
dim3 = Real('yolo3', 'norm', 0.9)
space.register(dim3)
point = space.sample(seed=seed)
test_point = [(dim1.sample()[0],
dim2.sample()[0],
dim3.sample()[0]), ]
assert len(point) == len(test_point) == 1
assert len(point[0]) == len(test_point[0]) == 3
assert np.all(point[0][0] == test_point[0][0])
assert point[0][1] == test_point[0][1]
assert point[0][2] == test_point[0][2]
points = space.sample(2, seed=seed)
points1 = dim1.sample(2)
def test_suggest_in_finite_cardinality(self):
"""Test that suggest None when search space is empty"""
space = Space()
space.register(Integer('yolo1', 'uniform', 0, 6))
space.register(Fidelity('epoch', 1, 9, 3))
hyperband = Hyperband(space, repetitions=1)
for i in range(6):
hyperband.observe([(1, i)], [{'objective': i}])
assert hyperband.suggest() is None
def test_cast_list(self):
"""Make sure list are cast to int and returned as list of values"""
dim = Integer('yolo', 'uniform', -3, 4)
assert dim.cast(['1', '2']) == [1, 2]
def test_register_and_contain(self):
"""Register bunch of dimensions, check if points/name are in space."""
space = Space()
assert 'yolo' not in space
assert (('asdfa', 2), 0, 3.5) not in space
categories = {'asdfa': 0.1, 2: 0.2, 3: 0.3, 4: 0.4}
dim = Categorical('yolo', categories, shape=2)
space.register(dim)
dim = Integer('yolo2', 'uniform', -3, 6)
space.register(dim)
dim = Real('yolo3', 'norm', 0.9)
space.register(dim)
assert 'yolo' in space
assert 'yolo2' in space
assert 'yolo3' in space
assert (('asdfa', 2), 0, 3.5) in space
assert (('asdfa', 2), 7, 3.5) not in space