Exemplo n.º 1
0
 def test_interval(self):
     """Test that bounds on variable."""
     dim = Dimension("yolo", "uniform", -3, 4)
     assert dim.interval(1.0) == (
         -3.0,
         1.0,
     )  # reminder that `scale` is not upper bound
Exemplo n.º 2
0
    def test_get_prior_string_default_values(self, monkeypatch):
        """Test that default_value are included."""
        def contains(self, value):
            return True

        monkeypatch.setattr(Dimension, '__contains__', contains)
        dim = Dimension('yolo', 'alpha', 1, 2, default_value=1)
        assert dim.get_prior_string() == 'alpha(1, 2, default_value=1)'
Exemplo n.º 3
0
 def test_get_prior_string(self):
     """Test that prior string can be rebuilt."""
     dim = Dimension('yolo',
                     'alpha',
                     1,
                     2,
                     3,
                     some='args',
                     plus='fluff',
                     n=4)
     assert dim.get_prior_string(
     ) == 'alpha(1, 2, 3, some=\'args\', plus=\'fluff\', n=4)'
Exemplo n.º 4
0
 def test_get_prior_string(self):
     """Test that prior string can be rebuilt."""
     dim = Dimension("yolo",
                     "alpha",
                     1,
                     2,
                     3,
                     some="args",
                     plus="fluff",
                     n=4)
     assert (dim.get_prior_string() ==
             "alpha(1, 2, 3, some='args', plus='fluff', n=4)")
Exemplo n.º 5
0
    def test_simple_instance(self, seed):
        """Test Dimension.__init__."""
        dim = Dimension('yolo', 'norm', 0.9, 0.1)
        samples = dim.sample(seed=seed)
        assert len(samples) == 1
        assert dists.norm.rvs(0.9, 0.1) == samples[0]

        assert dists.norm.interval(1.0, 0.9, 0.1) == dim.interval()
        assert dists.norm.interval(0.5, 0.9, 0.1) == dim.interval(0.5)

        assert str(dim) == "Dimension(name=yolo, prior={norm: (0.9, 0.1), {}}, " \
                           "shape=(), default value=None)"

        assert dim.name == 'yolo'
        assert dim.type == 'dimension'
        assert dim.shape == ()
Exemplo n.º 6
0
 def test_contains_bounds(self):
     """Test __contains__ for bounds."""
     dim = Dimension('yolo', 'uniform', -3, 4)
     assert -3 in dim
     assert -2 in dim
     assert 0 in dim
     assert 0.9999 in dim
     assert 1 not in dim
Exemplo n.º 7
0
 def test_contains_shape(self):
     """Test __contains__ for shape check."""
     dim = Dimension(None, 'uniform', -3, 4, shape=(4, 4))
     assert -3 not in dim
     assert -2 not in dim
     assert 0 not in dim
     assert 0.9999 not in dim
     assert 1 not in dim
     assert dists.uniform.rvs(-3, 4, size=(4, 4)) in dim
Exemplo n.º 8
0
    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'
Exemplo n.º 9
0
    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"
Exemplo n.º 10
0
def _(dim: Dimension):
    lower, upper = dim.interval()
    if dim.shape:
        # Temporary fix pending [#800]
        # ng.p.Array expects an array with a shape or a float
        # an array with an empty shape is not valid
        # so we cast it to a float
        if hasattr(lower, "shape") and lower.shape == ():
            lower = float(lower)

        if hasattr(upper, "shape") and upper.shape == ():
            upper = float(upper)

        return ng.p.Array(lower=lower, upper=upper, shape=_intshape(dim.shape))
    else:
        return ng.p.Scalar(lower=lower, upper=upper)
Exemplo n.º 11
0
    def test_shaped_instance(self, seed):
        """Use shape keyword argument."""
        dim = Dimension('yolo', 'norm', 0.9, shape=(3, 2))
        samples = dim.sample(seed=seed)
        assert len(samples) == 1
        assert_eq(dists.norm.rvs(0.9, size=(3, 2)), samples[0])

        assert dim.shape == (3, 2)

        dim = Dimension('yolo', 'norm', 0.9, shape=4)
        samples = dim.sample(seed=seed)
        assert len(samples) == 1
        assert_eq(dists.norm.rvs(0.9, size=4), samples[0])

        assert dim.shape == (4, )
Exemplo n.º 12
0
 def test_get_prior_string_shape(self):
     """Test that shape is included."""
     dim = Dimension('yolo', 'alpha', 1, 2, shape=(2, 3))
     assert dim.get_prior_string() == 'alpha(1, 2, shape=(2, 3))'
Exemplo n.º 13
0
 def test_no_prior(self):
     """Test that giving a null prior defaults prior_name to `None`."""
     dim = Dimension('yolo', None)
     print(dim._prior_name)
     assert dim.prior is None
     assert dim._prior_name is 'None'
Exemplo n.º 14
0
 def test_no_default_value(self):
     """Test that no giving a default value assigns None"""
     dim = Dimension('yolo', 'uniform', -3, 4)
     assert dim.default_value is None
Exemplo n.º 15
0
 def test_init_with_default_value(self):
     """Make sure the __contains__ method does not work"""
     with pytest.raises(NotImplementedError):
         Dimension('yolo', 'uniform', -3, 4, default_value=4)
Exemplo n.º 16
0
 def test_set_bad_name(self):
     """Try setting a name other than str or None."""
     dim = Dimension('yolo', 'uniform', -3, 4, shape=(4, 4))
     with pytest.raises(TypeError):
         dim.name = 4
Exemplo n.º 17
0
 def test_get_prior_string_shape(self):
     """Test that shape is included."""
     dim = Dimension("yolo", "alpha", 1, 2, shape=(2, 3))
     assert dim.get_prior_string() == "alpha(1, 2, shape=(2, 3))"
Exemplo n.º 18
0
 def test_contains_bounds(self):
     """Test __contains__ for bounds."""
     dim = Dimension('yolo', 'uniform', -3, 4)
     with pytest.raises(NotImplementedError):
         assert -3 in dim
Exemplo n.º 19
0
 def test_many_samples(self, seed):
     """More than 1."""
     dim = Dimension('yolo', 'uniform', -3, 4, shape=(4, 4))
     samples = dim.sample(n_samples=4, seed=seed)
     assert len(samples) == 4
     assert_eq(dists.uniform.rvs(-3, 4, size=(4, 4)), samples[0])
Exemplo n.º 20
0
 def test_ban_rng_kwarg(self):
     """Should not be able to use 'random_state' kwarg."""
     with pytest.raises(ValueError):
         Dimension('yolo', 'norm', 0.9, random_state=8)
Exemplo n.º 21
0
 def test_get_prior_string_normal(self):
     """Test that special norm prior name is replaced properly."""
     dim = Dimension("yolo", "norm", 1e-10, 1)
     assert dim.get_prior_string() == "normal(1e-10, 1)"
Exemplo n.º 22
0
 def test_get_prior_string_loguniform(self):
     """Test that special loguniform prior name is replaced properly."""
     dim = Dimension("yolo", "reciprocal", 1e-10, 1)
     assert dim.get_prior_string() == "loguniform(1e-10, 1)"
Exemplo n.º 23
0
 def test_ban_size_kwarg(self):
     """Should not be able to use 'size' kwarg."""
     with pytest.raises(ValueError):
         Dimension('yolo', 'norm', 0.9, size=(3, 2))
Exemplo n.º 24
0
    def test_contains_shape(self):
        """Test __contains__ for shape check."""
        dim = Dimension(None, 'uniform', -3, 4, shape=(4, 4))

        with pytest.raises(NotImplementedError):
            assert dists.uniform.rvs(-3, 4, size=(4, 4)) in dim
Exemplo n.º 25
0
 def test_ban_seed_kwarg(self):
     """Should not be able to use 'seed' kwarg."""
     with pytest.raises(ValueError):
         Dimension('yolo', 'norm', 0.9, seed=8)
Exemplo n.º 26
0
 def test_get_prior_string_uniform(self):
     """Test special uniform args are handled properly."""
     dim = Dimension("yolo", "uniform", 1, 2)
     assert dim.get_prior_string() == "uniform(1, 3)"
Exemplo n.º 27
0
 def test_with_predefined_dist(self, seed):
     """Use an already defined distribution object as prior arg."""
     dim = Dimension('yolo', dists.norm, 0.9)
     samples = dim.sample(seed=seed)
     assert len(samples) == 1
     assert dists.norm.rvs(0.9) == samples[0]
Exemplo n.º 28
0
 def test_ban_discrete_kwarg(self):
     """Do not allow use for 'discrete' kwarg, because now there's `_Discrete`."""
     with pytest.raises(ValueError) as exc:
         Dimension('yolo', 'uniform', -3, 4, shape=(4, 4), discrete=True)
     assert "pure `_Discrete`" in str(exc.value)