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(): print(node2.name) print(node2.get_cpd()) # Test node names print(node2.name) 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")
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(): print(node2.name) print(node2.get_cpd()) # Test node names print(node2.name) 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")
# -*- coding: utf-8 -*- import random import numpy from primo.networks import BayesianNetwork from primo.networks import DynamicBayesianNetwork from primo.networks import TwoTBN from primo.nodes import DiscreteNode import primo.inference.particlefilter as pf from primo.io import XMLBIF # Construct a DynmaicBayesianNetwork dbn = DynamicBayesianNetwork() B0 = BayesianNetwork() twoTBN = TwoTBN(XMLBIF.read("Robot_Localization.xmlbif")) # Configure TwoTBN x0 = twoTBN.get_node("x0") x = twoTBN.get_node("x") door = twoTBN.get_node("door") twoTBN.set_initial_node(x0.name, x.name) # Configure initial distribution x0_init = DiscreteNode( x0.name, ["p0", "p1", "p2", "p3", "p4", "p5", "p6", "p7", "p8", "p9"]) B0.add_node(x0_init) cpt_x0_init = numpy.array([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1]) x0_init.set_probability_table(cpt_x0_init, [x0_init]) dbn.B0 = B0