def generate_BayesNet(root): ''' Generate a BayesNet from a XMLBIF. This method is used internally. Do not call it outside this class. ''' network = BayesNet() bif_nodes = root.getElementsByTagName("BIF") if len(bif_nodes) != 1: raise Exception("More than one or none <BIF>-tag in document.") network_nodes = bif_nodes[0].getElementsByTagName("NETWORK") if len(network_nodes) != 1: raise Exception("More than one or none <NETWORK>-tag in document.") variable_nodes = network_nodes[0].getElementsByTagName("VARIABLE") for variable_node in variable_nodes: name = "Unnamed node" value_range = [] position = (0, 0) for name_node in variable_node.getElementsByTagName("NAME"): name = XMLBIF.get_node_text(name_node.childNodes) break for output_node in variable_node.getElementsByTagName("OUTCOME"): value_range.append(XMLBIF.get_node_text(output_node.childNodes)) for position_node in variable_node.getElementsByTagName("PROPERTY"): position = XMLBIF.get_node_position_from_text(position_node.childNodes) break new_node = DiscreteNode(name, value_range) new_node.position = position network.add_node(new_node) definition_nodes = network_nodes[0].getElementsByTagName("DEFINITION") for definition_node in definition_nodes: node = None for for_node in definition_node.getElementsByTagName("FOR"): name = XMLBIF.get_node_text(for_node.childNodes) node = network.get_node(name) break if node == None: continue for given_node in definition_node.getElementsByTagName("GIVEN"): parent_name = XMLBIF.get_node_text(given_node.childNodes) parent_node = network.get_node(parent_name) node.announce_parent(parent_node) for table_node in definition_node.getElementsByTagName("TABLE"): table = XMLBIF.get_node_table_from_text(table_node.childNodes) node.get_cpd().get_table().T.flat = table break return network
class ImportExportTest(unittest.TestCase): def setUp(self): # Create BayesNet self.bn = BayesNet(); # Create Nodes weather0 = DiscreteNode("Weather0", ["Sun", "Rain"]) weather = DiscreteNode("Weather", ["Sun", "Rain"]) ice_cream_eaten = DiscreteNode("Ice Cream Eaten", [True, False]) # Add nodes self.bn.add_node(weather0) self.bn.add_node(weather) self.bn.add_node(ice_cream_eaten) # Add edges self.bn.add_edge(weather, ice_cream_eaten) self.bn.add_edge(weather0, weather); # Set probabilities cpt_weather0 = numpy.array([.6, .4]) weather0.set_probability_table(cpt_weather0, [weather0]) cpt_weather = numpy.array([[.7, .5], [.3, .5]]) weather.set_probability_table(cpt_weather, [weather0, weather]) ice_cream_eaten.set_probability(.9, [(ice_cream_eaten, True), (weather, "Sun")]) ice_cream_eaten.set_probability(.1, [(ice_cream_eaten, False), (weather, "Sun")]) ice_cream_eaten.set_probability(.2, [(ice_cream_eaten, True), (weather, "Rain")]) ice_cream_eaten.set_probability(.8, [(ice_cream_eaten, False), (weather, "Rain")]) def test_import_export(self): # write BN xmlbif = XMLBIF(self.bn, "Test Net") xmlbif.write("test_out.xmlbif") # read BN bn2 = XMLBIF.read("test_out.xmlbif") for node1 in self.bn.get_nodes(): name_found = False cpd_equal = False value_range_equal = False str_equal = False pos_equal = False for node2 in bn2.get_nodes(): # Test node names if node1.name == node2.name: name_found = True cpd_equal = node1.get_cpd == node2.get_cpd value_range_equal = node1.get_value_range() == node2.get_value_range() str_equal = str(node1) == str(node2) pos_equal = node1.pos == node2.pos self.assertTrue(name_found) self.assertTrue(cpd_equal) self.assertTrue(value_range_equal) self.assertTrue(str_equal) self.assertTrue(pos_equal) # remove file os.remove("test_out.xmlbif")
class NodeAddAndRemoveTestCase(unittest.TestCase): def setUp(self): self.bn = BayesNet() def tearDown(self): self.bn = None def test_clear_and_len(self): self.assertFalse(0 == len(self.bn)) self.assertFalse(0 == self.bn.number_of_nodes()) self.bn.clear() self.assertEqual(0, len(self.bn)) self.assertEqual(0, self.bn.number_of_nodes()) def test_add_node(self): self.bn.clear() n = DiscreteNode("Some Node", [True, False]) self.bn.add_node(n) self.assertEqual(n, self.bn.get_node("Some Node")) self.assertTrue(n in self.bn.get_nodes(["Some Node"])) node_with_same_name = DiscreteNode("Some Node", [True, False]) self.assertRaises(Exception, self.bn.add_node, node_with_same_name) def test_remove_node(self): self.bn.clear() n = DiscreteNode("Some Node to remove", [True, False]) self.bn.add_node(n) self.bn.remove_node(n) self.assertFalse(n in self.bn.get_nodes([])) def test_add_edge(self): self.bn.clear() n1 = DiscreteNode("1", [True, False]) n2 = DiscreteNode("2", [True, False]) self.bn.add_node(n1) self.bn.add_node(n2) self.bn.add_edge(n1, n2) self.assertTrue(n1 in self.bn.get_parents(n2)) self.assertTrue(n2 in self.bn.get_children(n1)) def test_remove_edge(self): self.bn.clear() n1 = DiscreteNode("1", [True, False]) n2 = DiscreteNode("2", [True, False]) self.bn.add_node(n1) self.bn.add_node(n2) self.bn.add_edge(n1, n2) self.assertEqual([n1], self.bn.get_parents(n2)) self.bn.remove_edge(n1, n2) self.assertEqual([], self.bn.get_parents(n2)) def test_is_valid(self): self.bn.clear() n1 = DiscreteNode("1", [True, False]) n2 = DiscreteNode("2", [True, False]) self.bn.add_node(n1) self.bn.add_node(n2) self.bn.add_edge(n1, n2) self.assertTrue(self.bn.is_valid()) self.bn.add_edge(n1, n1) self.assertFalse(self.bn.is_valid()) self.bn.remove_edge(n1, n1) self.assertTrue(self.bn.is_valid()) n3 = DiscreteNode("3", [True, False]) n4 = DiscreteNode("4", [True, False]) self.bn.add_node(n3) self.bn.add_node(n4) self.assertTrue(self.bn.is_valid()) self.bn.add_edge(n2, n3) self.assertTrue(self.bn.is_valid()) self.bn.add_edge(n3, n4) self.assertTrue(self.bn.is_valid()) self.bn.add_edge(n4, n1) self.assertFalse(self.bn.is_valid())
#!/usr/bin/env python # -*- coding: utf-8 -*- from primo.core import BayesNet from primo.reasoning import DiscreteNode import numpy bn = BayesNet() burglary = DiscreteNode("Burglary", ["Intruder", "Safe"]) alarm = DiscreteNode("Alarm", ["Ringing", "Silent", "Kaputt"]) earthquake = DiscreteNode("Earthquake", ["Shaking", "Calm"]) john_calls = DiscreteNode("John calls", ["Calling", "Not Calling"]) mary_calls = DiscreteNode("Mary calls", ["Calling", "Not Calling"]) bn.add_node(burglary) bn.add_node(alarm) bn.add_node(earthquake) bn.add_node(john_calls) bn.add_node(mary_calls) bn.add_edge(burglary, alarm) bn.add_edge(earthquake, alarm) bn.add_edge(alarm, john_calls) bn.add_edge(alarm, mary_calls) burglary.set_probability(0.2, [(burglary, "Intruder")]) alarm.set_probability(0.1, [(alarm, "Ringing"), (burglary, "Safe"), (earthquake, "Calm")])
#!/usr/bin/env python # -*- coding: utf-8 -*- from primo.core import BayesNet from primo.reasoning import DiscreteNode from primo.reasoning import MarkovChainSampler from primo.reasoning import GibbsTransitionModel from primo.reasoning.density import ProbabilityTable import numpy #Construct some simple BayesianNetwork bn = BayesNet() burglary = DiscreteNode("Burglary", ["Intruder","Safe"]) alarm = DiscreteNode("Alarm", ["Ringing", "Silent","Kaputt"]) bn.add_node(burglary) bn.add_node(alarm) bn.add_edge(burglary,alarm) burglary_cpt=numpy.array([0.2,0.8]) burglary.set_probability_table(burglary_cpt, [burglary]) alarm_cpt=numpy.array([[0.8,0.15,0.05],[0.05,0.9,0.05]]) alarm.set_probability_table(alarm_cpt, [burglary,alarm]) #Construct a Markov Chain by sampling states from this Network transition_model = GibbsTransitionModel()
from primo.reasoning import DiscreteNode import primo.reasoning.particlebased.ParticleFilterDBN as pf import numpy #Construct some simple DynmaicBayesianNetwork B0 = BayesNet() dbn = DynamicBayesNet() twoTBN = TwoTBN() weather0_init = DiscreteNode("Weather0", ["Sun", "Rain"]) weather0 = DiscreteNode("Weather0", ["Sun", "Rain"]) weather = DiscreteNode("Weather", ["Sun", "Rain"]) ice_cream_eaten = DiscreteNode("Ice Cream Eaten", [True, False]) B0.add_node(weather0_init) twoTBN.add_node(weather0, True) twoTBN.add_node(weather) twoTBN.add_node(ice_cream_eaten) twoTBN.add_edge(weather, ice_cream_eaten) twoTBN.add_edge(weather0, weather); cpt_weather0_init = numpy.array([.6, .4]) weather0_init.set_probability_table(cpt_weather0_init, [weather0_init]) cpt_weather0 = numpy.array([.6, .4]) weather0.set_probability_table(cpt_weather0, [weather0]) cpt_weather = numpy.array([[.7, .5], [.3, .5]])