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_prior_name(self): """Test prior name is correct in dimension""" dim = Dimension('yolo', 'reciprocal', 1e-10, 1) assert dim.prior_name == 'reciprocal' dim = Dimension('yolo', 'norm', 0.9) assert dim.prior_name == 'norm' dim = Real('yolo', 'uniform', 1, 2) assert dim.prior_name == 'uniform' dim = Integer('yolo1', 'uniform', -3, 6) assert dim.prior_name == 'int_uniform' dim = Integer('yolo1', 'norm', -3, 6) assert dim.prior_name == 'int_norm' categories = {'asdfa': 0.1, 2: 0.2, 3: 0.3, 'lalala': 0.4} dim = Categorical('yolo', categories) assert dim.prior_name == 'choices'
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_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 test_prior_name(self): """Test prior name is correct in dimension""" dim = Dimension("yolo", "reciprocal", 1e-10, 1) assert dim.prior_name == "reciprocal" dim = Dimension("yolo", "norm", 0.9) assert dim.prior_name == "norm" dim = Real("yolo", "uniform", 1, 2) assert dim.prior_name == "uniform" dim = Integer("yolo1", "uniform", -3, 6) assert dim.prior_name == "int_uniform" dim = Integer("yolo1", "norm", -3, 6) assert dim.prior_name == "int_norm" categories = {"asdfa": 0.1, 2: 0.2, 3: 0.3, "lalala": 0.4} dim = Categorical("yolo", categories) assert dim.prior_name == "choices"
def test_cardinality(self): """Check whether space capacity is correct""" 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 = Fidelity('epoch', 1, 9, 3) space.register(dim) assert (4 * 2) * 6 * 1 == space.cardinality dim = Integer('yolo3', 'uniform', -3, 2, shape=(3, 1)) space.register(dim) assert (4 * 2) * 6 * 1 * (2 * 3 * 1) == space.cardinality dim = Real('yolo4', 'norm', 0.9) space.register(dim) assert np.inf == space.cardinality
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
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
def test_cardinality(self): """Check whether space capacity is correct""" 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 = Fidelity("epoch", 1, 9, 3) space.register(dim) assert space.cardinality == (4 ** 2) * (6 + 1) * 1 dim = Integer("yolo3", "uniform", -3, 2, shape=(3, 2)) space.register(dim) assert space.cardinality == (4 ** 2) * (6 + 1) * 1 * ((2 + 1) ** (3 * 2)) dim = Real("yolo4", "norm", 0.9) space.register(dim) assert np.inf == space.cardinality
def test_register_and_contain(self): """Register bunch of dimensions, check if points/name are in space.""" space = Space() trial = Trial(params=[{"name": "no", "value": 0, "type": "integer"}]) assert "yolo" not in space assert trial 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 format_trials.tuple_to_trial((("asdfa", 2), 0, 3.5), space) in space assert format_trials.tuple_to_trial((("asdfa", 2), 7, 3.5), space) not in space
def test_cast_list(self): """Make sure list are cast to float and returned as list of values""" dim = Real('yolo', 'uniform', -3, 4) assert dim.cast(['1', '2']) == [1.0, 2.0]
def test_set_outside_bounds_default_value(self): """Make sure default value is inside the bounds""" with pytest.raises(ValueError): Real('yolo', 'uniform', -3, 2, default_value=5)
def test_no_default_value(self): """Make sure the default value is None""" dim = Real('yolo', 'uniform', -3, 4) assert dim.default_value is None
def test_bad_bounds(self): """Try setting bound with high <= low.""" with pytest.raises(ValueError): Real('yolo', 'norm', 0, 2, low=+2, high=-2, shape=(4, 4)) with pytest.raises(ValueError): Real('yolo', 'norm', 0, 2, low=+2, high=+2, shape=(4, 4))
def test_init_with_default_value(self): """Make sure the default value is set""" dim = Real('yolo', 'uniform', -3, 10, default_value=2.0) assert type(dim.default_value) is float
def space(): """Create a Space with a real dimension and a fidelity value.""" space = Space() space.register(Real('lr', 'uniform', 0, 1)) space.register(Fidelity('epoch', 1, 9, 3)) return space
def test_sample_from_extra_bounds_bad(self): """Randomized test **unsuccessfully** sampling with the extra bounds.""" dim = Real('yolo', 'norm', 0, 2, low=-2, high=+2, shape=(4, 4)) with pytest.raises(ValueError) as exc: dim.sample(8) assert "Improbable bounds" in str(exc.value)
def dim1(): """Create an example of `orion.algo.space.Dimension`.""" dim = Real("yolo0", "norm", 0.9, shape=(3, 2)) return dim
def space2(): """Create a Space with two real dimensions.""" space = Space() space.register(Real("lr", "uniform", 0, 1)) space.register(Real("weight_decay", "uniform", 0, 1)) return space
def dim(): """Create an example of `Dimension`.""" dim = Real("yolo0", "norm", 0.9, shape=(3, 2)) return dim
def test_cast_array(self): """Make sure array are cast to float and returned as array of values""" dim = Real('yolo', 'uniform', -3, 4) assert np.all(dim.cast(np.array(['1', '2'])) == np.array([1.0, 2.0]))
def space(): """Create a Space with a real dimension and a fidelity value.""" space = Space() space.register(Real("lr", "uniform", 0, 1)) space.register(Fidelity("epoch", 1, 9, 3)) return space
def test_no_default_value(self): """Make sure the default value is None""" dim = Real("yolo", "uniform", -3, 4) assert dim.default_value is None
def test_get_prior_string_no_precision(self): """Test that default precision is not included.""" dim = Real("yolo", "uniform", 1, 2, precision=4) assert dim.get_prior_string() == "uniform(1, 3)"
def test_get_prior_string_precision(self): """Test that precision is included.""" dim = Real("yolo", "uniform", 1, 2, precision=5) assert dim.get_prior_string() == "uniform(1, 3, precision=5)"
def logdim(): """Create an log example of `Dimension`.""" dim = Real("yolo4", "reciprocal", 1.0, 10.0, shape=(3, 2)) return dim
def test_cast_list(self): """Make sure list are cast to float and returned as list of values""" dim = Real("yolo", "uniform", -3, 4) assert dim.cast(["1", "2"]) == [1.0, 2.0]
def test_real_grid(): """Test linear real grid""" dim = Real("yolo", "uniform", -5, 10) assert real_grid(dim, 6) == [-5, -3, -1, 1, 3, 5] assert real_grid(dim, 7) == [-5 + 10 / 6.0 * i for i in range(7)]
def dim(): """Create an example of `Dimension`.""" dim = Real('yolo', 'norm', 0.9, shape=(3, 2)) return dim
def test_cast_array(self): """Make sure array are cast to float and returned as array of values""" dim = Real("yolo", "uniform", -3, 4) assert np.all(dim.cast(np.array(["1", "2"])) == np.array([1.0, 2.0]))