def test_equal_to_dict(self):
        d = EqualToDict({a: "a", b: "b"})
        # Ensure that we call `EqualTo`.
        self.assertFalse(Item.equal_to_called)
        self.assertEqual(d[a], "a")
        self.assertTrue(Item.equal_to_called)

        self.assertEqual(d[b], "b")
        self.assertTrue(a in d)

        # Ensure hash collision does not occur.
        self.assertEqual(hash(a.value), hash(a))
        self.assertFalse(a.value in d)

        # Obtaining the original representation (e.g. for `pybind11`):
        # - Constructing using `dict` will not be what is desired; the keys at
        # present are not directly convertible, thus would create an error.
        # N.B. At present, this behavior may not be overridable via Python, as
        # copying is done via `dict.update`, which has a special case for
        # `dict`-inheriting types which does not have any hooks for key
        # transformations.
        raw_attempt = dict(d)
        keys = list(raw_attempt.keys())
        self.assertFalse(isinstance(keys[0], Item))
        # - Calling `raw()` should provide the desired behavior.
        raw = d.raw()
        keys = list(raw.keys())
        self.assertTrue(isinstance(keys[0], Item))
Ejemplo n.º 2
0
    def test_equal_to_dict(self):
        d = EqualToDict({a: "a", b: "b"})
        # Ensure that we call `EqualTo`.
        self.assertFalse(Item.equal_to_called)
        self.assertEqual(d[a], "a")
        self.assertTrue(Item.equal_to_called)

        self.assertEqual(d[b], "b")
        self.assertTrue(a in d)

        # Ensure hash collision does not occur.
        self.assertEqual(hash(a.value), hash(a))
        self.assertFalse(a.value in d)

        # Obtaining the original representation (e.g. for `pybind11`):
        # - Constructing using `dict` will not be what is desired; the keys at
        # present are not directly convertible, thus would create an error.
        # N.B. At present, this behavior may not be overridable via Python, as
        # copying is done via `dict.update`, which has a special case for
        # `dict`-inheriting types which does not have any hooks for key
        # transformations.
        raw_attempt = dict(d)
        keys = list(raw_attempt.keys())
        self.assertFalse(isinstance(keys[0], Item))
        # - Calling `raw()` should provide the desired behavior.
        raw = d.raw()
        keys = list(raw.keys())
        self.assertTrue(isinstance(keys[0], Item))
Ejemplo n.º 3
0
 def test_constructor_map(self):
     powers_in = {x: 2, y: 3, z: 4}
     m = sym.Monomial(powers_in)
     powers_out = EqualToDict(m.get_powers())
     self.assertEqual(powers_out[x], 2)
     self.assertEqual(powers_out[y], 3)
     self.assertEqual(powers_out[z], 4)
Ejemplo n.º 4
0
 def test_dreal_satisfiability(self):
     x = Variable("x")
     f = logical_and(x > 1, x < 2)
     interval_box = EqualToDict(
         DrealSolver.CheckSatisfiability(f=f, delta=0.01))
     self.assertEqual(len(interval_box), 1)
     self.assertIsInstance(interval_box[x], DrealSolver.Interval)
Ejemplo n.º 5
0
 def test_minimize(self):
     x = Variable("x")
     delta = 0.01
     interval_box = EqualToDict(
         DrealSolver.Minimize(
             objective=x**2,
             constraint=logical_and(x >= 1, x <= 10),
             delta=delta,
             local_optimization=DrealSolver.LocalOptimization.kUse))
     self.assertEqual(len(interval_box), 1)
     self.assertIsInstance(interval_box[x], DrealSolver.Interval)
     print(interval_box[x].diam())
     self.assertAlmostEqual(interval_box[x].mid(), 1.0, delta=delta)
Ejemplo n.º 6
0
 def test_get_powers(self):
     m = sym.Monomial(x, 2) * sym.Monomial(y)  # m = x²y
     powers = EqualToDict(m.get_powers())
     self.assertEqual(powers[x], 2)
     self.assertEqual(powers[y], 1)
Ejemplo n.º 7
0
                    for comp_k in range(len(logscores_by_comp)):
                        logscores_by_comp[
                            comp_k] -= multivariate_normal.logpdf(
                                val, self.g.means_[comp_k, k],
                                self.g.covariances_[comp_k, k])
            return logsumexp(logscores_by_comp + log_weights)
        elif self.model_type == "kde":
            total_log_density = 0.
            for k, val in enumerate(x[1:]):
                if val != 0:
                    total_log_density += self.kdes[k].logpdf(val)
            return total_log_density


uniform_variable_store = {}
uniform_variable_to_range = EqualToDict()


def random_uniform(name, low, high):
    if name not in uniform_variable_store.keys():
        var = sym.Variable(name, sym.Variable.Type.RANDOM_UNIFORM)
        uniform_variable_store[name] = var
        uniform_variable_to_range[var] = (low, high)
    else:
        var = uniform_variable_store[name]
    return var * (high - low) + low


pattern_index_to_name = ["Random", "Large Spike", "Decreasing", "Small Spike"]
pattern_transition_matrix = np.array([[0.2, 0.3, 0.15, 0.35],
                                      [0.5, 0.05, 0.2, 0.25],
Ejemplo n.º 8
0
 def test_constructor_vars_exponents(self):
     m = sym.Monomial([x, y], [1, 2])
     powers_out = EqualToDict(m.get_powers())
     self.assertEqual(powers_out[x], 1)
     self.assertEqual(powers_out[y], 2)