class TestTableCPDFactor(unittest.TestCase): def setUp(self): skel = GraphSkeleton() skel.load("unittestdict.txt") skel.toporder() nodedata = NodeData() nodedata.load("unittestdict.txt") self.instance = DiscreteBayesianNetwork(skel, nodedata) self.factor = TableCPDFactor("Grade", self.instance) self.factor2 = TableCPDFactor("Letter", self.instance) def test_constructor(self): product = 1 for var in self.factor.card: product *= var self.assertTrue(len(self.factor.vals) == product) for i in range(1, len(self.factor.scope)): self.assertTrue(self.factor.stride[self.factor.scope[i]] == self.factor.stride[self.factor.scope[i-1]] * self.factor.card[i-1]) def test_multiplyfactor(self): self.factor.multiplyfactor(self.factor2) a = [0.03, 0.16000000000000003, 0.297, 0.09000000000000001, 0.032, 0.0198, 0.005000000000000001, 0.1, 0.693, 0.05, 0.12, 0.198, 0.27, 0.24, 0.003, 0.81, 0.048, 0.0002, 0.045000000000000005, 0.15, 0.006999999999999999, 0.45, 0.18, 0.002] b = [3, 2, 2, 2] c = ['Grade', 'Intelligence', 'Difficulty', 'Letter'] d = {'Grade': 1, 'Intelligence': 3, 'Letter': 12, 'Difficulty': 6} self.assertEqual(self.factor.vals, a) self.assertEqual(self.factor.card, b) self.assertEqual(self.factor.scope, c) self.assertEqual(self.factor.stride, d) def test_sumout(self): self.factor.sumout("Difficulty") a = [0.35, 0.65, 1.0, 1.4, 0.38, 0.22] b = [3, 2] c = ['Grade', 'Intelligence'] d = {'Grade': 1, 'Intelligence': 3} self.assertEqual(self.factor.vals, a) self.assertEqual(self.factor.card, b) self.assertEqual(self.factor.scope, c) self.assertEqual(self.factor.stride, d) def test_reducefactor(self): self.factor.reducefactor("Difficulty", 'easy') a = [0.3, 0.4, 0.3, 0.9, 0.08, 0.02] b = [3, 2] c = ['Grade', 'Intelligence'] d = {'Grade': 1, 'Intelligence': 3} self.assertEqual(self.factor.vals, a) self.assertEqual(self.factor.card, b) self.assertEqual(self.factor.scope, c) self.assertEqual(self.factor.stride, d) def test_copy(self): copy = self.factor.copy() self.assertTrue((copy is self.factor) == False) self.assertEqual(copy.vals, self.factor.vals) self.assertEqual(copy.card, self.factor.card) self.assertEqual(copy.scope, self.factor.scope) self.assertEqual(copy.stride, self.factor.stride)
class TestRegressionTableCPDFactor(unittest.TestCase): def setUp(self): skel = GraphSkeleton() skel.load("unittestdict.txt") skel.toporder() nodedata = NodeData.load("unittestdict.txt") self.instance = DiscreteBayesianNetwork(nodedata) self.oldfactor = oldTableCPDFactor("Grade", self.instance) self.oldfactor2 = oldTableCPDFactor("Letter", self.instance) self.factor = TableCPDFactor("Grade", self.instance) self.factor2 = TableCPDFactor("Letter", self.instance) def test_constructor(self): product = 1 for var in self.factor.card: product *= var self.assertTrue(len(self.factor.vals) == product) for i in range(1, len(self.factor.scope)): self.assertTrue(self.factor.stride[self.factor.scope[i]] == self.factor.stride[self.factor.scope[i - 1]] * self.factor.card[i - 1]) def test_multiplyfactor(self): self.factor.multiplyfactor(self.factor2) self.oldfactor.multiplyfactor(self.oldfactor2) self.assertEqual(self.factor.vals, self.oldfactor.vals) self.assertEqual(self.factor.card, self.oldfactor.card) self.assertEqual(self.factor.scope, self.oldfactor.scope) self.assertEqual(self.factor.stride, self.oldfactor.stride) def test_sumout(self): factor = self.factor.sumout("Difficulty") self.oldfactor.sumout("Difficulty") self.assertEqual(factor.vals, self.oldfactor.vals) self.assertEqual(factor.card, self.oldfactor.card) self.assertEqual(factor.scope, self.oldfactor.scope) self.assertEqual(factor.stride, self.oldfactor.stride) def test_reducefactor(self): factor = self.factor.reducefactor("Difficulty", 'easy') self.oldfactor.reducefactor("Difficulty", 'easy') self.assertEqual(factor.vals, self.oldfactor.vals) self.assertEqual(factor.card, self.oldfactor.card) self.assertEqual(factor.scope, self.oldfactor.scope) self.assertEqual(factor.stride, self.oldfactor.stride)
class TestRegressionTableCPDFactor(unittest.TestCase): def setUp(self): skel = GraphSkeleton() skel.load("unittestdict.txt") skel.toporder() nodedata = NodeData.load("unittestdict.txt") self.instance = DiscreteBayesianNetwork(nodedata) self.oldfactor = oldTableCPDFactor("Grade", self.instance) self.oldfactor2 = oldTableCPDFactor("Letter", self.instance) self.factor = TableCPDFactor("Grade", self.instance) self.factor2 = TableCPDFactor("Letter", self.instance) def test_constructor(self): product = 1 for var in self.factor.card: product *= var self.assertTrue(len(self.factor.vals) == product) for i in range(1, len(self.factor.scope)): self.assertTrue(self.factor.stride[self.factor.scope[i]] == self.factor.stride[self.factor.scope[i-1]] * self.factor.card[i-1]) def test_multiplyfactor(self): self.factor.multiplyfactor(self.factor2) self.oldfactor.multiplyfactor(self.oldfactor2) self.assertEqual(self.factor.vals, self.oldfactor.vals) self.assertEqual(self.factor.card, self.oldfactor.card) self.assertEqual(self.factor.scope, self.oldfactor.scope) self.assertEqual(self.factor.stride, self.oldfactor.stride) def test_sumout(self): factor = self.factor.sumout("Difficulty") self.oldfactor.sumout("Difficulty") self.assertEqual(factor.vals, self.oldfactor.vals) self.assertEqual(factor.card, self.oldfactor.card) self.assertEqual(factor.scope, self.oldfactor.scope) self.assertEqual(factor.stride, self.oldfactor.stride) def test_reducefactor(self): factor = self.factor.reducefactor("Difficulty", 'easy') self.oldfactor.reducefactor("Difficulty", 'easy') self.assertEqual(factor.vals, self.oldfactor.vals) self.assertEqual(factor.card, self.oldfactor.card) self.assertEqual(factor.scope, self.oldfactor.scope) self.assertEqual(factor.stride, self.oldfactor.stride)