def basic_network(only_structure=False): """ Returns a basic valid network to test the APIs """ structure = { "node1" : { "state1" : 0.3, "state2" : 0.7, }, "node2" : { "state1" : 0.7, "state2" : 0.3, }, } if only_structure: return structure Net = BayesNet() # defining a valid network structure: Net.create_network(structure) return Net
def basic_network(only_structure=False): """ Returns a basic valid network to test the APIs """ structure = { "node1": { "state1": 0.3, "state2": 0.7, }, "node2": { "state1": 0.7, "state2": 0.3, }, } if only_structure: return structure Net = BayesNet() # defining a valid network structure: Net.create_network(structure) return Net
def basic_network(only_structure=False): """ create basic network to test the APIs """ Net = BayesNet("continuous") network_struct = { "node1": { "states": ["dim1"], }, "node2" : { "states": ["dim1","dim2"], }, "node3" : { "states": ["dim1","dim2","dim3"], }, "node4" : { "states": ["dim1","dim2","dim3"], }, "node5" : { "states": ["dim1"], }, } Net.create_network(network_struct) return Net
def basic_network(only_structure=False): """ create basic network to test the APIs """ Net = BayesNet("continuous") network_struct = { "node1": { "states": ["dim1"], }, "node2": { "states": ["dim1", "dim2"], }, "node3": { "states": ["dim1", "dim2", "dim3"], }, "node4": { "states": ["dim1", "dim2", "dim3"], }, "node5": { "states": ["dim1"], }, } Net.create_network(network_struct) return Net
class TestCreateNetwork(unittest.TestCase): """ Test cases for network creation """ def setUp(self): self.Net = BayesNet("continuous") self.valid_structure = { "node1": { "states": ["dim1"], }, "node2" : { "states": ["dim1","dim2"], }, "node3" : { "states": ["dim1","dim2","dim3"], }, "node4" : { "states": ["dim1","dim2","dim3"], }, } def test_valid_network(self): """ create network from a valid structure """ self.assertEquals(None,self.Net.create_network(self.valid_structure)) def test_invalid_network(self): """
class TestCreateNetwork(unittest.TestCase): """ Test cases for creating a Network """ def setUp(self): self.Net = BayesNet() # defining a valid network structure: self.valid_structure = { "node1": { "state1": 0.3, "state2": 0.7, }, "node2": { "state1": 0.7, "state2": 0.3, }, } self.invalid_structure = { "node1": { "state1": 0.3, "state2": "invalid probability", }, } def test_valid(self): """ test if a valid network returns None """ # successfully creating a network should return None self.assertEquals(None, self.Net.create_network(self.valid_structure)) def test_invalid(self): """
class TestCreateNetwork(unittest.TestCase): """ Test cases for network creation """ def setUp(self): self.Net = BayesNet("continuous") self.valid_structure = { "node1": { "states": ["dim1"], }, "node2": { "states": ["dim1", "dim2"], }, "node3": { "states": ["dim1", "dim2", "dim3"], }, "node4": { "states": ["dim1", "dim2", "dim3"], }, } def test_valid_network(self): """ create network from a valid structure """ self.assertEquals(None, self.Net.create_network(self.valid_structure)) def test_invalid_network(self): """
class TestCreateNetwork(unittest.TestCase): """ Test cases for creating a Network """ def setUp(self): self.Net = BayesNet() # defining a valid network structure: self.valid_structure = { "node1" : { "state1" : 0.3, "state2" : 0.7, }, "node2" : { "state1" : 0.7, "state2" : 0.3, }, } self.invalid_structure = { "node1" : { "state1" : 0.3, "state2" : "invalid probability", }, } def test_valid(self): """ test if a valid network returns None """ # successfully creating a network should return None self.assertEquals(None,self.Net.create_network(self.valid_structure)) def test_invalid(self): """
class TestMPE(unittest.TestCase): """ Test cases for Most Probable Explanation(MPE) """ def setUp(self): self.Net = BayesNet() network_structure = { "node1" : { "state1" : 0.3, "state2" : 0.7, }, "node2" : { "state1" : 0.7, "state2" : 0.3, }, "node3" : { "state1" : 0.5, "state2" : 0.5 }, } self.Net.create_network(network_structure) def test_MPE_connected(self): """ testing Net.getMPE(NodeName1,NodeName2) between parent and child node(i.e. connected nodes) """ self.Net.createEdge("node1",["node2","node3"]) node1 = self.Net.getNode("node1") node2 = self.Net.getNode("node2") node3 = self.Net.getNode("node3") self.Net.setProbability(node2,["state1", 'state1'],0.3) self.Net.setProbability(node2,["state1", 'state2'],0.1) self.Net.setProbability(node2,["state2", 'state1'],0.7) self.Net.setProbability(node2,["state2", 'state2'],0.9) self.Net.setProbability(node3,["state1", 'state1'],0.4) self.Net.setProbability(node3,["state1", 'state2'],0.0) self.Net.setProbability(node3,["state2", 'state1'],0.6) self.Net.setProbability(node3,["state2", 'state2'],1.0) expected_outcome = {'node1': 'state2', 'node2': 'state2'} mpe = self.Net.getMPE("node1","node2") self.assertDictEqual(expected_outcome,mpe) def test_MPE_disconnected(self): """ testing Net.getMPE(NodeName1,NodeName2) between disconnected nodes """ self.Net.createEdge(["node1","node2"],"node3") node1 = self.Net.getNode("node1") node2 = self.Net.getNode("node2") node3 = self.Net.getNode("node3") self.Net.setProbability(node3,["state1", 'state1','state1'],0.3) self.Net.setProbability(node3,["state1", 'state1','state2'],0.1) self.Net.setProbability(node3,["state1", 'state2','state1'],0.5) self.Net.setProbability(node3,["state1", 'state2','state2'],0.7) self.Net.setProbability(node3,["state2", 'state1','state1'],0.7) self.Net.setProbability(node3,["state2", 'state1','state2'],0.9) self.Net.setProbability(node3,["state2", 'state2','state1'],0.5) self.Net.setProbability(node3,["state2", 'state2','state2'],0.3) expected_outcome = {'node1': 'state2', 'node2': 'state1'} mpe = self.Net.getMPE("node1","node2") self.assertDictEqual(expected_outcome,mpe)
class TestJPD(unittest.TestCase): """ Test cases for Joint Probability Distribution """ def setUp(self): self.Net = BayesNet() network_structure = { "node1" : { "state1" : 0.3, "state2" : 0.7, }, "node2" : { "state1" : 0.7, "state2" : 0.3, }, "node3" : { "state1" : 0.5, "state2" : 0.5 }, } self.Net.create_network(network_structure) def test_JPD_connected(self): """ testing Net.getJPD(NodeName1,NodeName2) between parent and child node(i.e. connected nodes) """ self.Net.createEdge("node1",["node2","node3"]) node1 = self.Net.getNode("node1") node2 = self.Net.getNode("node2") node3 = self.Net.getNode("node3") self.Net.setProbability(node2,["state1", 'state1'],0.3) self.Net.setProbability(node2,["state1", 'state2'],0.1) self.Net.setProbability(node2,["state2", 'state1'],0.7) self.Net.setProbability(node2,["state2", 'state2'],0.9) self.Net.setProbability(node3,["state1", 'state1'],0.4) self.Net.setProbability(node3,["state1", 'state2'],0.0) self.Net.setProbability(node3,["state2", 'state1'],0.6) self.Net.setProbability(node3,["state2", 'state2'],1.0) jpd = self.Net.getJPD("node1","node2") self.assertAlmostEqual(0.07000000029802322,jpd[(('node2', 'state2'), ('node1', 'state1'))]) self.assertAlmostEqual(0.09000000357627869,jpd[(('node2', 'state1'), ('node1', 'state1'))]) self.assertAlmostEqual(0.21000000834465027,jpd[(('node2', 'state1'), ('node1', 'state2'))]) self.assertAlmostEqual(0.6299999952316284,jpd[(('node2', 'state2'), ('node1', 'state2'))]) def test_JPD_disconnected(self): """ testing Net.getJPD(NodeName1,NodeName2) between disconnected nodes """ self.Net.createEdge(["node1","node2"],"node3") node1 = self.Net.getNode("node1") node2 = self.Net.getNode("node2") node3 = self.Net.getNode("node3") self.Net.setProbability(node3,["state1", 'state1','state1'],0.3) self.Net.setProbability(node3,["state1", 'state1','state2'],0.1) self.Net.setProbability(node3,["state1", 'state2','state1'],0.5) self.Net.setProbability(node3,["state1", 'state2','state2'],0.7) self.Net.setProbability(node3,["state2", 'state1','state1'],0.7) self.Net.setProbability(node3,["state2", 'state1','state2'],0.9) self.Net.setProbability(node3,["state2", 'state2','state1'],0.5) self.Net.setProbability(node3,["state2", 'state2','state2'],0.3) jpd = self.Net.getJPD("node1","node2") self.assertAlmostEqual(0.4899999797344208,jpd[(('node2', 'state2'), ('node1', 'state1'))]) self.assertAlmostEqual(0.21000000834465027,jpd[(('node2', 'state1'), ('node1', 'state1'))]) self.assertAlmostEqual(0.09000001102685928,jpd[(('node2', 'state1'), ('node1', 'state2'))]) self.assertAlmostEqual(0.21000000834465027,jpd[(('node2', 'state2'), ('node1', 'state2'))])
class TestMPE(unittest.TestCase): """ Test cases for Most Probable Explanation(MPE) """ def setUp(self): self.Net = BayesNet() network_structure = { "node1": { "state1": 0.3, "state2": 0.7, }, "node2": { "state1": 0.7, "state2": 0.3, }, "node3": { "state1": 0.5, "state2": 0.5 }, } self.Net.create_network(network_structure) def test_MPE_connected(self): """ testing Net.getMPE(NodeName1,NodeName2) between parent and child node(i.e. connected nodes) """ self.Net.createEdge("node1", ["node2", "node3"]) node1 = self.Net.getNode("node1") node2 = self.Net.getNode("node2") node3 = self.Net.getNode("node3") self.Net.setProbability(node2, ["state1", 'state1'], 0.3) self.Net.setProbability(node2, ["state1", 'state2'], 0.1) self.Net.setProbability(node2, ["state2", 'state1'], 0.7) self.Net.setProbability(node2, ["state2", 'state2'], 0.9) self.Net.setProbability(node3, ["state1", 'state1'], 0.4) self.Net.setProbability(node3, ["state1", 'state2'], 0.0) self.Net.setProbability(node3, ["state2", 'state1'], 0.6) self.Net.setProbability(node3, ["state2", 'state2'], 1.0) expected_outcome = {'node1': 'state2', 'node2': 'state2'} mpe = self.Net.getMPE("node1", "node2") self.assertDictEqual(expected_outcome, mpe) def test_MPE_disconnected(self): """ testing Net.getMPE(NodeName1,NodeName2) between disconnected nodes """ self.Net.createEdge(["node1", "node2"], "node3") node1 = self.Net.getNode("node1") node2 = self.Net.getNode("node2") node3 = self.Net.getNode("node3") self.Net.setProbability(node3, ["state1", 'state1', 'state1'], 0.3) self.Net.setProbability(node3, ["state1", 'state1', 'state2'], 0.1) self.Net.setProbability(node3, ["state1", 'state2', 'state1'], 0.5) self.Net.setProbability(node3, ["state1", 'state2', 'state2'], 0.7) self.Net.setProbability(node3, ["state2", 'state1', 'state1'], 0.7) self.Net.setProbability(node3, ["state2", 'state1', 'state2'], 0.9) self.Net.setProbability(node3, ["state2", 'state2', 'state1'], 0.5) self.Net.setProbability(node3, ["state2", 'state2', 'state2'], 0.3) expected_outcome = {'node1': 'state2', 'node2': 'state1'} mpe = self.Net.getMPE("node1", "node2") self.assertDictEqual(expected_outcome, mpe)
class TestJPD(unittest.TestCase): """ Test cases for Joint Probability Distribution """ def setUp(self): self.Net = BayesNet() network_structure = { "node1": { "state1": 0.3, "state2": 0.7, }, "node2": { "state1": 0.7, "state2": 0.3, }, "node3": { "state1": 0.5, "state2": 0.5 }, } self.Net.create_network(network_structure) def test_JPD_connected(self): """ testing Net.getJPD(NodeName1,NodeName2) between parent and child node(i.e. connected nodes) """ self.Net.createEdge("node1", ["node2", "node3"]) node1 = self.Net.getNode("node1") node2 = self.Net.getNode("node2") node3 = self.Net.getNode("node3") self.Net.setProbability(node2, ["state1", 'state1'], 0.3) self.Net.setProbability(node2, ["state1", 'state2'], 0.1) self.Net.setProbability(node2, ["state2", 'state1'], 0.7) self.Net.setProbability(node2, ["state2", 'state2'], 0.9) self.Net.setProbability(node3, ["state1", 'state1'], 0.4) self.Net.setProbability(node3, ["state1", 'state2'], 0.0) self.Net.setProbability(node3, ["state2", 'state1'], 0.6) self.Net.setProbability(node3, ["state2", 'state2'], 1.0) jpd = self.Net.getJPD("node1", "node2") self.assertAlmostEqual(0.07000000029802322, jpd[(('node2', 'state2'), ('node1', 'state1'))]) self.assertAlmostEqual(0.09000000357627869, jpd[(('node2', 'state1'), ('node1', 'state1'))]) self.assertAlmostEqual(0.21000000834465027, jpd[(('node2', 'state1'), ('node1', 'state2'))]) self.assertAlmostEqual(0.6299999952316284, jpd[(('node2', 'state2'), ('node1', 'state2'))]) def test_JPD_disconnected(self): """ testing Net.getJPD(NodeName1,NodeName2) between disconnected nodes """ self.Net.createEdge(["node1", "node2"], "node3") node1 = self.Net.getNode("node1") node2 = self.Net.getNode("node2") node3 = self.Net.getNode("node3") self.Net.setProbability(node3, ["state1", 'state1', 'state1'], 0.3) self.Net.setProbability(node3, ["state1", 'state1', 'state2'], 0.1) self.Net.setProbability(node3, ["state1", 'state2', 'state1'], 0.5) self.Net.setProbability(node3, ["state1", 'state2', 'state2'], 0.7) self.Net.setProbability(node3, ["state2", 'state1', 'state1'], 0.7) self.Net.setProbability(node3, ["state2", 'state1', 'state2'], 0.9) self.Net.setProbability(node3, ["state2", 'state2', 'state1'], 0.5) self.Net.setProbability(node3, ["state2", 'state2', 'state2'], 0.3) jpd = self.Net.getJPD("node1", "node2") self.assertAlmostEqual(0.4899999797344208, jpd[(('node2', 'state2'), ('node1', 'state1'))]) self.assertAlmostEqual(0.21000000834465027, jpd[(('node2', 'state1'), ('node1', 'state1'))]) self.assertAlmostEqual(0.09000001102685928, jpd[(('node2', 'state1'), ('node1', 'state2'))]) self.assertAlmostEqual(0.21000000834465027, jpd[(('node2', 'state2'), ('node1', 'state2'))])