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_order(self):
"""Test that the same space built twice will have the same ordering."""
space1 = Space()
space1.register(Integer('yolo1', 'uniform', -3, 6, shape=(2,)))
space1.register(Integer('yolo2', 'uniform', -3, 6, shape=(2,)))
space1.register(Real('yolo3', 'norm', 0.9))
space1.register(Categorical('yolo4', ('asdfa', 2)))
space2 = Space()
space2.register(Integer('yolo1', 'uniform', -3, 6, shape=(2,)))
space2.register(Real('yolo3', 'norm', 0.9))
space2.register(Categorical('yolo4', ('asdfa', 2)))
space2.register(Integer('yolo2', 'uniform', -3, 6, shape=(2,)))
assert list(space1) == list(space1.keys())
assert list(space2) == list(space2.keys())
assert list(space1.values()) == list(space2.values())
assert list(space1.items()) == list(space2.items())
assert list(space1.keys()) == list(space2.keys())
assert list(space1.values()) == list(space2.values())
assert list(space1.items()) == list(space2.items())
def test_getitem(self):
"""Test getting dimensions from space."""
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['yolo'].type == 'categorical'
assert space[0].type == 'categorical'
with pytest.raises(KeyError):
space['asdf']
with pytest.raises(IndexError):
space[3]
def test_sample_from_extra_bounds_good(self):
"""Randomized test **successful** sampling with the extra bounds."""
dim = Real('yolo', 'norm', 0, 2, low=-5, high=+5, shape=(4, 4))
for _ in range(8):
samples = dim.sample(8)
for sample in samples:
assert sample in dim
def dim():
"""Create an example of `Dimension`."""
dim = Real('yolo', 'norm', 0.9, shape=(3, 2))
return dim
def test_build_a_good_real(self, dimbuilder):
"""Check that non registered names are good, as long as they are in
`scipy.stats.distributions`.
"""
dim = dimbuilder.build('yolo2', 'alpha(0.9, low=0, high=10, shape=2)')
assert isinstance(dim, Real)
assert dim.name == 'yolo2'
assert dim._prior_name == 'alpha'
assert 3.3 not in dim
assert (3.3, 11.1) not in dim
assert (3.3, 6) in dim
assert isinstance(dim.prior, dists.alpha_gen)
def test_precision(self):
"""Test that precision is correctly handled."""
space = Space()
space.register(Real('yolo1', 'norm', 0.9, precision=6))
space.register(Real('yolo2', 'norm', 0.9, precision=None))
space.register(Real('yolo5', 'norm', 0.9))
assert space['yolo1'].precision == 6
assert space['yolo2'].precision is None
assert space['yolo5'].precision == 4
with pytest.raises(TypeError):
space.register(Real('yolo3', 'norm', 0.9, precision=-12))
with pytest.raises(TypeError):
space.register(Real('yolo4', 'norm', 0.9, precision=0.6))
def test_simple_instance(self, seed):
"""Test Real.__init__."""
dim = Real('yolo', 'norm', 0.9)
samples = dim.sample(seed=seed)
assert len(samples) == 1
assert dists.norm.rvs(0.9) == samples[0]
assert dists.norm.interval(1.0, 0.9) == dim.interval()
assert dists.norm.interval(0.5, 0.9) == dim.interval(0.5)
assert 1.0 in dim
assert str(dim) == "Real(name=yolo, prior={norm: (0.9,), {}}, shape=(), default value=None)"
assert dim.name == 'yolo'
assert dim.type == 'real'
assert dim.shape == ()
def test_1d_shape(self, tpe):
"""Test suggest with 1D shape dimensions"""
space = Space()
dim1 = Real('yolo1', 'uniform', -3, 6, shape=(2))
space.register(dim1)
dim2 = Real('yolo2', 'uniform', -2, 4)
space.register(dim2)
tpe.space = space
tpe.n_initial_points = 10
results = numpy.random.random(10)
for i in range(10):
point = tpe.suggest(1)
assert len(point) == 1
assert len(point[0]) == 2
assert len(point[0][0]) == 2
tpe.observe(point, [{'objective': results[i]}])
point = tpe.suggest(1)
assert len(point) == 1
assert len(point[0]) == 2
def test_get_id_multidim(self, b_config):
"""Test valid id for points with dim of shape > 1"""
space = Space()
space.register(Fidelity('epoch', 1, 9, 3))
space.register(Real('lr', 'uniform', 0, 1, shape=2))
asha = ASHA(space, num_brackets=3)
assert asha.get_id(['whatever', [1, 1]]) == asha.get_id(['is here', [1, 1]])
assert asha.get_id(['whatever', [1, 1]]) != asha.get_id(['is here', [2, 2]])
def _real_or_int(kwargs):
return Integer if kwargs.pop('discrete', False) else Real