def test_project(self): ans = Domain(['a', 'b'], [10, 20]) res = self.domain.project(['a', 'b']) self.assertEqual(ans, res) ans = Domain(['c', 'b'], [30, 20]) res = self.domain.project(['c', 'b']) self.assertEqual(ans, res)
def test_project(self): proj = self.data.project(['a','b']) ans = Domain(['a','b'],[3,4]) self.assertEqual(proj.domain, ans) proj = self.data.project(('a','b')) self.assertEqual(proj.domain, ans) proj = self.data.project('c') self.assertEqual(proj.domain, Domain(['c'], [5]))
def test_eq(self): attrs = ['a', 'b', 'c', 'd'] shape = [10, 20, 30, 40] ans = Domain(attrs, shape) self.assertEqual(self.domain, ans) attrs = ['b', 'a', 'c', 'd'] ans = Domain(attrs, shape) self.assertNotEqual(self.domain, ans)
def setUp(self): attrs = ['a', 'b', 'c', 'd'] shape = [2, 3, 4, 5] domain = Domain(attrs, shape) cliques = [('a', 'b'), ('b', 'c'), ('c', 'd')] self.model = GraphicalModel(domain, cliques) zeros = { cl: Factor.zeros(domain.project(cl)) for cl in self.model.cliques } self.model.potentials = CliqueVector(zeros)
def test_marginalize(self): ans = Domain(['a', 'b'], [10, 20]) res = self.domain.marginalize(['c', 'd']) self.assertEqual(ans, res) res = self.domain.marginalize(['c', 'd', 'e']) self.assertEqual(ans, res)
def test_expand(self): domain = Domain(['a', 'b', 'c', 'd'], [2, 3, 4, 5]) res = self.factor.expand(domain) self.assertEqual(res.domain, domain) self.assertEqual(res.values.shape, domain.shape) res = res.sum(['d']) * 0.2 self.assertTrue(np.allclose(res.values, self.factor.values))
def test_project(self): res = self.factor.project(['c', 'a'], agg='sum') ans = Domain(['c', 'a'], [4, 2]) self.assertEqual(res.domain, ans) self.assertEqual(res.values.shape, (4, 2)) res = self.factor.project(['c', 'a'], agg='logsumexp') self.assertEqual(res.domain, ans) self.assertEqual(res.values.shape, (4, 2))
def test_binary(self): dom = Domain(['b', 'd', 'e'], [3, 5, 6]) vals = np.random.rand(3, 5, 6) factor = Factor(dom, vals) res = self.factor * factor ans = Domain(['a', 'b', 'c', 'd', 'e'], [2, 3, 4, 5, 6]) self.assertEqual(res.domain, ans) res = self.factor + factor self.assertEqual(res.domain, ans) res = self.factor * 2.0 self.assertEqual(res.domain, self.factor.domain) res = self.factor + 2.0 self.assertEqual(res.domain, self.factor.domain) res = self.factor - 2.0 self.assertEqual(res.domain, self.factor.domain) res = self.factor.exp().log() self.assertEqual(res.domain, self.factor.domain) self.assertTrue(np.allclose(res.values, self.factor.values))
def setUp(self): attrs = ['a', 'b', 'c', 'd', 'e'] shape = [2, 3, 4, 5, 6] self.domain = Domain(attrs, shape) #x = np.random.rand(*shape) self.measurements = [] for i in range(4): I = np.eye(shape[i]) y = np.random.rand(shape[i]) y /= y.sum() self.measurements.append((I, y, 1.0, attrs[i])) self.engine = FactoredInference(self.domain, backend='numpy', log=True)
def setUp(self): attrs = ['a', 'b', 'c'] shape = [2, 3, 4] domain = Domain(attrs, shape) values = np.random.rand(*shape) self.factor = Factor(domain, values)
def test_logsumexp(self): res = self.factor.logsumexp(['a', 'c']) values = self.factor.values ans = np.log(np.sum(np.exp(values), axis=(0, 2))) self.assertEqual(res.domain, Domain(['b'], [3])) self.assertTrue(np.allclose(res.values, ans))
def test_sum(self): res = self.factor.sum(['a', 'b']) self.assertEqual(res.domain, Domain(['c'], [4])) self.assertTrue( np.allclose(res.values, self.factor.values.sum(axis=(0, 1))))
def test_transpose(self): attrs = ['b', 'c', 'a'] tr = self.factor.transpose(attrs) ans = Domain(attrs, [3, 4, 2]) self.assertEqual(tr.domain, ans)
class TestDomain(unittest.TestCase): def setUp(self): attrs = ['a', 'b', 'c', 'd'] shape = [10, 20, 30, 40] self.domain = Domain(attrs, shape) def test_eq(self): attrs = ['a', 'b', 'c', 'd'] shape = [10, 20, 30, 40] ans = Domain(attrs, shape) self.assertEqual(self.domain, ans) attrs = ['b', 'a', 'c', 'd'] ans = Domain(attrs, shape) self.assertNotEqual(self.domain, ans) def test_project(self): ans = Domain(['a', 'b'], [10, 20]) res = self.domain.project(['a', 'b']) self.assertEqual(ans, res) ans = Domain(['c', 'b'], [30, 20]) res = self.domain.project(['c', 'b']) self.assertEqual(ans, res) def test_marginalize(self): ans = Domain(['a', 'b'], [10, 20]) res = self.domain.marginalize(['c', 'd']) self.assertEqual(ans, res) res = self.domain.marginalize(['c', 'd', 'e']) self.assertEqual(ans, res) def test_axes(self): ans = (1, 3) res = self.domain.axes(['b', 'd']) self.assertEqual(ans, res) def test_transpose(self): ans = Domain(['b', 'd', 'a', 'c'], [20, 40, 10, 30]) res = self.domain.transpose(['b', 'd', 'a', 'c']) self.assertEqual(ans, res) def test_merge(self): ans = Domain(['a', 'b', 'c', 'd', 'e', 'f'], [10, 20, 30, 40, 50, 60]) new = Domain(['b', 'd', 'e', 'f'], [20, 40, 50, 60]) res = self.domain.merge(new) self.assertEqual(ans, res) def test_contains(self): new = Domain(['b', 'd'], [20, 40]) self.assertTrue(self.domain.contains(new)) new = Domain(['b', 'e'], [20, 50]) self.assertFalse(self.domain.contains(new)) def test_iter(self): self.assertEqual(len(self.domain), 4) for a, b, c in zip(self.domain, ['a', 'b', 'c', 'd'], [10, 20, 30, 40]): self.assertEqual(a, b) self.assertEqual(self.domain[a], c)
def test_transpose(self): ans = Domain(['b', 'd', 'a', 'c'], [20, 40, 10, 30]) res = self.domain.transpose(['b', 'd', 'a', 'c']) self.assertEqual(ans, res)
def setUp(self): attrs = ['a','b','c','d'] shape = [3,4,5,6] domain = Domain(attrs, shape) self.data = Dataset.synthetic(domain, 100)
def setUp(self): attrs = ['a', 'b', 'c', 'd'] shape = [10, 20, 30, 40] domain = Domain(attrs, shape) cliques = [('a', 'b'), ('b', 'c'), ('c', 'd')] self.tree = JunctionTree(domain, cliques)
def test_contains(self): new = Domain(['b', 'd'], [20, 40]) self.assertTrue(self.domain.contains(new)) new = Domain(['b', 'e'], [20, 50]) self.assertFalse(self.domain.contains(new))
def setUp(self): attrs = ['a', 'b', 'c', 'd'] shape = [10, 20, 30, 40] self.domain = Domain(attrs, shape)
def test_merge(self): ans = Domain(['a', 'b', 'c', 'd', 'e', 'f'], [10, 20, 30, 40, 50, 60]) new = Domain(['b', 'd', 'e', 'f'], [20, 40, 50, 60]) res = self.domain.merge(new) self.assertEqual(ans, res)