def test_potential_initializer(): """ Tests potential initialization. :return: None. """ bbn = BbnUtil.get_huang_graph() PotentialInitializer.init(bbn)
def test_potential_initializer(): """ Tests potential initialization. :return: None. """ bbn = BbnUtil.get_huang_graph() PotentialInitializer.init(bbn) e_potentials = { '0=on': 0.50000, '0=off': 0.50000, '0=on,1=on': 0.50000, '0=on,1=off': 0.50000, '0=off,1=on': 0.40000, '0=off,1=off': 0.60000, '0=on,2=on': 0.70000, '0=on,2=off': 0.30000, '0=off,2=on': 0.20000, '0=off,2=off': 0.80000, '1=on,3=on': 0.90000, '1=on,3=off': 0.10000, '1=off,3=on': 0.50000, '1=off,3=off': 0.50000, '2=on,4=on': 0.30000, '2=on,4=off': 0.70000, '2=off,4=on': 0.60000, '2=off,4=off': 0.40000, '3=on,4=on,5=on': 0.01000, '3=on,4=on,5=off': 0.99000, '3=on,4=off,5=on': 0.01000, '3=on,4=off,5=off': 0.99000, '3=off,4=on,5=on': 0.01000, '3=off,4=on,5=off': 0.99000, '3=off,4=off,5=on': 0.99000, '3=off,4=off,5=off': 0.01000, '2=on,6=on': 0.80000, '2=on,6=off': 0.20000, '2=off,6=on': 0.10000, '2=off,6=off': 0.90000, '4=on,6=on,7=on': 0.05000, '4=on,6=on,7=off': 0.95000, '4=on,6=off,7=on': 0.95000, '4=on,6=off,7=off': 0.05000, '4=off,6=on,7=on': 0.95000, '4=off,6=on,7=off': 0.05000, '4=off,6=off,7=on': 0.95000, '4=off,6=off,7=off': 0.05000 } o_potentials = '\n'.join([str(node.potential) for node in bbn.get_nodes()]).split('\n') o_potentials = [p.split('|') for p in o_potentials] o_potentials = {tokens[0]: float(tokens[1]) for tokens in o_potentials} assert len(e_potentials) == len(o_potentials) for k, lhs in o_potentials.items(): assert k in e_potentials rhs = e_potentials[k] assert lhs == rhs
def test_transformer(): bbn = BbnUtil.get_huang_graph() PotentialInitializer.init(bbn) ug = Moralizer.moralize(bbn) cliques = Triangulator.triangulate(ug) join_tree = Transformer.transform(cliques)
def test_moralizer(): """ Tests moralization. :return: None. """ bbn = BbnUtil.get_huang_graph() PotentialInitializer.init(bbn) ug = Moralizer.moralize(bbn)
def test_triangulator(): """ Tests triangulation. :return: None. """ bbn = BbnUtil.get_huang_graph() PotentialInitializer.init(bbn) ug = Moralizer.moralize(bbn) cliques = Triangulator.triangulate(ug)
def test_potential_initializer(): bbn = BbnUtil.get_huang_graph() PotentialInitializer.init(bbn) # assert later # for node in bbn.get_nodes(): # potential = node.potential # print('{} {}'.format(node.id, node.variable.name)) # print(potential) # # assert 1 == 2
def test_moralizer(): bbn = BbnUtil.get_huang_graph() PotentialInitializer.init(bbn) ug = Moralizer.moralize(bbn) # assert later # for node in ug.get_nodes(): # print(node) # for edge in ug.get_edges(): # print(edge) # # assert 1 == 2
def test_initializer(): """ Tests initialization. :return: None. """ bbn = BbnUtil.get_huang_graph() PotentialInitializer.init(bbn) ug = Moralizer.moralize(bbn) cliques = Triangulator.triangulate(ug) join_tree = Transformer.transform(cliques) Initializer.initialize(join_tree)
def apply_from_serde(join_tree): """ Applies propagation to join tree from a deserialzed join tree. :param join_tree: Join tree. :return: Join tree (the same one passed in). """ join_tree.listener = None join_tree.evidences = dict() PotentialInitializer.reinit(join_tree) Initializer.initialize(join_tree) Propagator.propagate(join_tree) join_tree.set_listener(InferenceController()) return join_tree
def apply(bbn): """ Sets up the specified BBN for probability propagation in tree clusters (PPTC). :param bbn: BBN graph. :return: Join tree. """ PotentialInitializer.init(bbn) ug = Moralizer.moralize(bbn) cliques = Triangulator.triangulate(ug) join_tree = Transformer.transform(cliques) Initializer.initialize(join_tree) Propagator.propagate(join_tree) join_tree.set_listener(InferenceController()) return join_tree
def test_triangulator(): """ Tests triangulation. :return: None. """ bbn = BbnUtil.get_huang_graph() PotentialInitializer.init(bbn) ug = Moralizer.moralize(bbn) cliques = Triangulator.triangulate(ug) e_cliques = set( ['(d,e,f)', '(e,g,h)', '(c,e,g)', '(a,b,c)', '(b,c,d)', '(c,d,e)']) o_cliques = [str(c) for c in cliques] assert len(e_cliques) == len(o_cliques) for c in e_cliques: assert c in o_cliques
def test_moralizer(): """ Tests moralization. :return: None. """ bbn = BbnUtil.get_huang_graph() PotentialInitializer.init(bbn) ug = Moralizer.moralize(bbn) e_edges = set([ '0--1', '0--2', '1--3', '2--4', '3--5', '4--5', '2--6', '4--7', '6--7', '3--4', '4--6' ]) o_edges = set([str(edge) for edge in ug.get_edges()]) assert len(e_edges) == len(o_edges) for e in e_edges: assert e in o_edges
def reapply(join_tree, cpts): """ Reapply propagation to join tree with new CPTs. The join tree structure is kept but the BBN node CPTs are updated. A new instance/copy of the join tree will be returned. :param join_tree: Join tree. :param cpts: Dictionary of new CPTs. Keys are id's of nodes and values are new CPTs. :return: Join tree. """ jt = copy.deepcopy(join_tree) jt.update_bbn_cpts(cpts) jt.listener = None jt.evidences = dict() PotentialInitializer.reinit(jt) Initializer.initialize(jt) Propagator.propagate(jt) jt.set_listener(InferenceController()) return jt
def test_transformer(): """ Tests transformer. :return: None. """ bbn = BbnUtil.get_huang_graph() PotentialInitializer.init(bbn) ug = Moralizer.moralize(bbn) cliques = Triangulator.triangulate(ug) join_tree = Transformer.transform(cliques) e_nodes = set([ '(d,e,f)', '(e,g,h)', '(c,e,g)', '(a,b,c)', '(b,c,d)', '(c,d,e)', '|(a,b,c) -- b,c -- (b,c,d)|', '|(b,c,d) -- c,d -- (c,d,e)|', '|(c,e,g) -- c,e -- (c,d,e)|', '|(d,e,f) -- d,e -- (c,d,e)|', '|(e,g,h) -- e,g -- (c,e,g)|' ]) e_edges = set([ '(a,b,c)--|(a,b,c) -- b,c -- (b,c,d)|--(b,c,d)', '(b,c,d)--|(b,c,d) -- c,d -- (c,d,e)|--(c,d,e)', '(c,e,g)--|(c,e,g) -- c,e -- (c,d,e)|--(c,d,e)', '(d,e,f)--|(d,e,f) -- d,e -- (c,d,e)|--(c,d,e)', '(e,g,h)--|(e,g,h) -- e,g -- (c,e,g)|--(c,e,g)' ]) o_nodes = [str(n) for n in join_tree.get_nodes()] o_edges = [str(e) for e in join_tree.get_edges()] assert len(e_nodes) == len(o_nodes) for n in e_nodes: assert n in o_nodes assert len(e_edges) == len(o_edges) for e in e_edges: assert e in o_edges
def test_initializer(): """ Tests initialization. :return: None. """ bbn = BbnUtil.get_huang_graph() PotentialInitializer.init(bbn) ug = Moralizer.moralize(bbn) cliques = Triangulator.triangulate(ug) join_tree = Transformer.transform(cliques) Initializer.initialize(join_tree) e_potentials = { '3=on,4=on,5=on': 0.01000, '3=on,4=on,5=off': 0.99000, '3=on,4=off,5=on': 0.01000, '3=on,4=off,5=off': 0.99000, '3=off,4=on,5=on': 0.01000, '3=off,4=on,5=off': 0.99000, '3=off,4=off,5=on': 0.99000, '3=off,4=off,5=off': 0.01000, '4=on,6=on,7=on': 0.05000, '4=on,6=on,7=off': 0.95000, '4=on,6=off,7=on': 0.95000, '4=on,6=off,7=off': 0.05000, '4=off,6=on,7=on': 0.95000, '4=off,6=on,7=off': 0.05000, '4=off,6=off,7=on': 0.95000, '4=off,6=off,7=off': 0.05000, '2=on,4=on,6=on': 0.80000, '2=on,4=on,6=off': 0.20000, '2=on,4=off,6=on': 0.80000, '2=on,4=off,6=off': 0.20000, '2=off,4=on,6=on': 0.10000, '2=off,4=on,6=off': 0.90000, '2=off,4=off,6=on': 0.10000, '2=off,4=off,6=off': 0.90000, '0=on,1=on,2=on': 0.17500, '0=off,1=on,2=on': 0.04000, '0=on,1=on,2=off': 0.07500, '0=off,1=on,2=off': 0.16000, '0=on,1=off,2=on': 0.17500, '0=off,1=off,2=on': 0.06000, '0=on,1=off,2=off': 0.07500, '0=off,1=off,2=off': 0.24000, '1=on,2=on,3=on': 0.90000, '1=off,2=on,3=on': 0.50000, '1=on,2=on,3=off': 0.10000, '1=off,2=on,3=off': 0.50000, '1=on,2=off,3=on': 0.90000, '1=off,2=off,3=on': 0.50000, '1=on,2=off,3=off': 0.10000, '1=off,2=off,3=off': 0.50000, '2=on,3=on,4=on': 0.30000, '2=off,3=on,4=on': 0.60000, '2=on,3=on,4=off': 0.70000, '2=off,3=on,4=off': 0.40000, '2=on,3=off,4=on': 0.30000, '2=off,3=off,4=on': 0.60000, '2=on,3=off,4=off': 0.70000, '2=off,3=off,4=off': 0.40000, '1=on,2=on': 1.00000, '1=on,2=off': 1.00000, '1=off,2=on': 1.00000, '1=off,2=off': 1.00000, '2=on,3=on': 1.00000, '2=on,3=off': 1.00000, '2=off,3=on': 1.00000, '2=off,3=off': 1.00000, '2=on,4=on': 1.00000, '2=on,4=off': 1.00000, '2=off,4=on': 1.00000, '2=off,4=off': 1.00000, '3=on,4=on': 1.00000, '3=on,4=off': 1.00000, '3=off,4=on': 1.00000, '3=off,4=off': 1.00000, '4=on,6=on': 1.00000, '4=on,6=off': 1.00000, '4=off,6=on': 1.00000, '4=off,6=off': 1.00000 } o_potentials = '\n'.join([str(v) for _, v in join_tree.potentials.items()]).split('\n') o_potentials = [p.split('|') for p in o_potentials] o_potentials = {tokens[0]: float(tokens[1]) for tokens in o_potentials} assert len(e_potentials) == len(o_potentials) for k, lhs in o_potentials.items(): assert k in e_potentials rhs = e_potentials[k] assert lhs == rhs
def test_propagator(): """ Tests propagation. :return: None. """ bbn = BbnUtil.get_huang_graph() PotentialInitializer.init(bbn) ug = Moralizer.moralize(bbn) cliques = Triangulator.triangulate(ug) join_tree = Transformer.transform(cliques) Initializer.initialize(join_tree) Propagator.propagate(join_tree) e_potentials = { '3=on,4=on,5=on': 0.00315, '3=on,4=on,5=off': 0.31155, '3=on,4=off,5=on': 0.00365, '3=on,4=off,5=off': 0.36165, '3=off,4=on,5=on': 0.00150, '3=off,4=on,5=off': 0.14880, '3=off,4=off,5=on': 0.16800, '3=off,4=off,5=off': 0.00170, '4=on,6=on,7=on': 0.00705, '4=on,6=on,7=off': 0.13395, '4=on,6=off,7=on': 0.30780, '4=on,6=off,7=off': 0.01620, '4=off,6=on,7=on': 0.26030, '4=off,6=on,7=off': 0.01370, '4=off,6=off,7=on': 0.24795, '4=off,6=off,7=off': 0.01305, '2=on,4=on,6=on': 0.10800, '2=on,4=on,6=off': 0.02700, '2=on,4=off,6=on': 0.25200, '2=on,4=off,6=off': 0.06300, '2=off,4=on,6=on': 0.03300, '2=off,4=on,6=off': 0.29700, '2=off,4=off,6=on': 0.02200, '2=off,4=off,6=off': 0.19800, '0=on,1=on,2=on': 0.17500, '0=off,1=on,2=on': 0.04000, '0=on,1=on,2=off': 0.07500, '0=off,1=on,2=off': 0.16000, '0=on,1=off,2=on': 0.17500, '0=off,1=off,2=on': 0.06000, '0=on,1=off,2=off': 0.07500, '0=off,1=off,2=off': 0.24000, '1=on,2=on,3=on': 0.19350, '1=off,2=on,3=on': 0.11750, '1=on,2=on,3=off': 0.02150, '1=off,2=on,3=off': 0.11750, '1=on,2=off,3=on': 0.21150, '1=off,2=off,3=on': 0.15750, '1=on,2=off,3=off': 0.02350, '1=off,2=off,3=off': 0.15750, '2=on,3=on,4=on': 0.09330, '2=off,3=on,4=on': 0.22140, '2=on,3=on,4=off': 0.21770, '2=off,3=on,4=off': 0.14760, '2=on,3=off,4=on': 0.04170, '2=off,3=off,4=on': 0.10860, '2=on,3=off,4=off': 0.09730, '2=off,3=off,4=off': 0.07240, '1=on,2=on': 0.21500, '1=on,2=off': 0.23500, '1=off,2=on': 0.23500, '1=off,2=off': 0.31500, '2=on,3=on': 0.31100, '2=on,3=off': 0.13900, '2=off,3=on': 0.36900, '2=off,3=off': 0.18100, '2=on,4=on': 0.13500, '2=on,4=off': 0.31500, '2=off,4=on': 0.33000, '2=off,4=off': 0.22000, '3=on,4=on': 0.31470, '3=on,4=off': 0.36530, '3=off,4=on': 0.15030, '3=off,4=off': 0.16970, '4=on,6=on': 0.14100, '4=on,6=off': 0.32400, '4=off,6=on': 0.27400, '4=off,6=off': 0.26100 } o_potentials = '\n'.join([str(v) for _, v in join_tree.potentials.items() ]).split('\n') o_potentials = [p.split('|') for p in o_potentials] o_potentials = {tokens[0]: float(tokens[1]) for tokens in o_potentials} assert len(e_potentials) == len(o_potentials) for k, lhs in o_potentials.items(): assert k in e_potentials rhs = e_potentials[k] assert lhs == rhs