Ejemplo n.º 1
0
def test_csv_serde():
    """
    Tests CSV serde.
    :return: None.
    """
    try:
        lhs = BbnUtil.get_huang_graph()
        Bbn.to_csv(lhs, 'huang.csv')

        rhs = Bbn.from_csv('huang.csv')

        assert len(lhs.get_nodes()) == len(rhs.get_nodes())
        assert len(lhs.get_edges()) == len(rhs.get_edges())

        lhs_nodes = set([str(node) for node in lhs.get_nodes()])
        rhs_nodes = set([str(node) for node in rhs.get_nodes()])
        for n in lhs_nodes:
            assert n in rhs_nodes

        lhs_edges = set([str(edge) for edge in lhs.get_edges()])
        rhs_edges = set([str(edge) for edge in rhs.get_edges()])
        for e in lhs_edges:
            assert e in rhs_edges
    except:
        assert False
    finally:
        import os

        try:
            os.remove('huang.csv')
        except:
            pass
Ejemplo n.º 2
0
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
Ejemplo n.º 4
0
def test_toplogical_sort_huang():
    """
    Tests toplogical sorting of Huang graph.
    """
    bbn = BbnUtil.get_huang_graph()
    sampler = LogicSampler(bbn)

    assert_almost_equal([0, 1, 2, 3, 4, 5, 6, 7], sampler.nodes)
Ejemplo n.º 5
0
def test_transformer():
    bbn = BbnUtil.get_huang_graph()
    PotentialInitializer.init(bbn)

    ug = Moralizer.moralize(bbn)
    cliques = Triangulator.triangulate(ug)

    join_tree = Transformer.transform(cliques)
Ejemplo n.º 6
0
def test_moralizer():
    """
    Tests moralization.
    :return: None.
    """
    bbn = BbnUtil.get_huang_graph()
    PotentialInitializer.init(bbn)

    ug = Moralizer.moralize(bbn)
Ejemplo n.º 7
0
def test_from_dict():
    """
    Tests creating BBN from dictionary (deserialized from JSON).
    :return: None.
    """
    e_bbn = BbnUtil.get_huang_graph()
    o_bbn = Bbn.from_dict(Bbn.to_dict(e_bbn))

    assert len(e_bbn.get_nodes()) == len(o_bbn.get_nodes())
    assert len(e_bbn.get_edges()) == len(o_bbn.get_edges())
Ejemplo n.º 8
0
def test_triangulator():
    """
    Tests triangulation.
    :return: None.
    """
    bbn = BbnUtil.get_huang_graph()
    PotentialInitializer.init(bbn)

    ug = Moralizer.moralize(bbn)
    cliques = Triangulator.triangulate(ug)
Ejemplo n.º 9
0
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
Ejemplo n.º 10
0
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
Ejemplo n.º 11
0
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)
Ejemplo n.º 12
0
def test_inference_controller():
    bbn = BbnUtil.get_huang_graph()
    join_tree = InferenceController.apply(bbn)

    print('INIT')
    print_potentials(join_tree)

    ev = EvidenceBuilder()\
        .with_node(join_tree.get_bbn_node(0))\
        .with_evidence('on', 1.0)\
        .build()

    join_tree.set_observation(ev)

    print('FIRST')
    print_potentials(join_tree)
Ejemplo n.º 13
0
def test_huang_inference_with_multiple_evidence():
    """
    Tests inference on the Huang graph with a multiple evidence.
    :return: None.
    """
    bbn = BbnUtil.get_huang_graph()

    join_tree = InferenceController.apply(bbn)

    expected = {
        'a': [0.5, 0.5],
        'b': [0.45, 0.55],
        'c': [0.45, 0.55],
        'd': [0.68, 0.32],
        'e': [0.465, 0.535],
        'f': [0.176, 0.824],
        'g': [0.415, 0.585],
        'h': [0.823, 0.177]
    }

    __validate_posterior__(expected, join_tree)

    ev1 = EvidenceBuilder() \
        .with_node(join_tree.get_bbn_node_by_name('a')) \
        .with_evidence('on', 1.0) \
        .build()
    ev2 = EvidenceBuilder() \
        .with_node(join_tree.get_bbn_node_by_name('f')) \
        .with_evidence('on', 1.0) \
        .build()

    join_tree.unobserve_all()
    join_tree.update_evidences([ev1, ev2])

    expected = {
        'a': [1.0, 0.0],
        'b': [0.184, 0.816],
        'c': [0.798, 0.202],
        'd': [0.0370, 0.963],
        'e': [0.0206, 0.979],
        'f': [1.0, 0.0],
        'g': [0.658, 0.342],
        'h': [0.941, 0.0588]
    }

    __validate_posterior__(expected, join_tree)
Ejemplo n.º 14
0
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
Ejemplo n.º 15
0
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
Ejemplo n.º 16
0
def test_huang_inference_with_single_evidence():
    """
    Tests inference on the Huang graph with a single evidence.
    :return: None.
    """
    bbn = BbnUtil.get_huang_graph()

    join_tree = InferenceController.apply(bbn)

    expected = {
        'a': [0.5, 0.5],
        'b': [0.45, 0.55],
        'c': [0.45, 0.55],
        'd': [0.68, 0.32],
        'e': [0.465, 0.535],
        'f': [0.176, 0.824],
        'g': [0.415, 0.585],
        'h': [0.823, 0.177]
    }

    __validate_posterior__(expected, join_tree)

    ev = EvidenceBuilder() \
        .with_node(join_tree.get_bbn_node_by_name('a')) \
        .with_evidence('on', 1.0) \
        .build()
    join_tree.unobserve_all()
    join_tree.set_observation(ev)

    expected = {
        'a': [1.0, 0.0],
        'b': [0.5, 0.5],
        'c': [0.7, 0.3],
        'd': [0.7, 0.3],
        'e': [0.39, 0.61],
        'f': [0.18934, 0.81066],
        'g': [0.59, 0.41],
        'h': [0.7826, 0.2174]
    }

    __validate_posterior__(expected, join_tree)
Ejemplo n.º 17
0
def test_simple_inference():
    """
    Tests inference on the Huang graph.
    :return: None.
    """
    bbn = BbnUtil.get_simple()

    join_tree = InferenceController.apply(bbn)

    expected = {
        'a': [0.5, 0.5],
        'b': [0.45, 0.55],
        'c': [0.45, 0.55],
        'd': [0.68, 0.32],
        'e': [0.465, 0.535],
        'f': [0.176, 0.824]
    }

    __validate_posterior__(expected, join_tree)

    __print_potentials__(join_tree)
Ejemplo n.º 18
0
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
Ejemplo n.º 19
0
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
Ejemplo n.º 20
0
import numpy as np

from pybbn.generator.bbngenerator import generate_bbn_to_file
from pybbn.graph.dag import BbnUtil, Bbn

np.random.seed(37)

generate_bbn_to_file(900, 'singly-bbn.csv', 'singly', 10)
generate_bbn_to_file(900, 'multi-bbn.csv', 'multi', 10)

bbn = BbnUtil.get_huang_graph()
Bbn.to_csv(bbn, 'huang.csv')
Ejemplo n.º 21
0
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
Ejemplo n.º 22
0
def test_to_dict():
    """
    Tests creating serializable dictionary representation.
    :return: None.
    """
    bbn = BbnUtil.get_huang_graph()
    d = Bbn.to_dict(bbn)
    j = json.dumps(d, sort_keys=True, indent=2)
    e = """{
  "edges": [
    {
      "ch": 1,
      "pa": 0
    },
    {
      "ch": 2,
      "pa": 0
    },
    {
      "ch": 3,
      "pa": 1
    },
    {
      "ch": 4,
      "pa": 2
    },
    {
      "ch": 5,
      "pa": 3
    },
    {
      "ch": 5,
      "pa": 4
    },
    {
      "ch": 6,
      "pa": 2
    },
    {
      "ch": 7,
      "pa": 4
    },
    {
      "ch": 7,
      "pa": 6
    }
  ],
  "nodes": {
    "0": {
      "probs": [
        0.5,
        0.5
      ],
      "variable": {
        "id": 0,
        "name": "a",
        "values": [
          "on",
          "off"
        ]
      }
    },
    "1": {
      "probs": [
        0.5,
        0.5,
        0.4,
        0.6
      ],
      "variable": {
        "id": 1,
        "name": "b",
        "values": [
          "on",
          "off"
        ]
      }
    },
    "2": {
      "probs": [
        0.7,
        0.3,
        0.2,
        0.8
      ],
      "variable": {
        "id": 2,
        "name": "c",
        "values": [
          "on",
          "off"
        ]
      }
    },
    "3": {
      "probs": [
        0.9,
        0.1,
        0.5,
        0.5
      ],
      "variable": {
        "id": 3,
        "name": "d",
        "values": [
          "on",
          "off"
        ]
      }
    },
    "4": {
      "probs": [
        0.3,
        0.7,
        0.6,
        0.4
      ],
      "variable": {
        "id": 4,
        "name": "e",
        "values": [
          "on",
          "off"
        ]
      }
    },
    "5": {
      "probs": [
        0.01,
        0.99,
        0.01,
        0.99,
        0.01,
        0.99,
        0.99,
        0.01
      ],
      "variable": {
        "id": 5,
        "name": "f",
        "values": [
          "on",
          "off"
        ]
      }
    },
    "6": {
      "probs": [
        0.8,
        0.2,
        0.1,
        0.9
      ],
      "variable": {
        "id": 6,
        "name": "g",
        "values": [
          "on",
          "off"
        ]
      }
    },
    "7": {
      "probs": [
        0.05,
        0.95,
        0.95,
        0.05,
        0.95,
        0.05,
        0.95,
        0.05
      ],
      "variable": {
        "id": 7,
        "name": "h",
        "values": [
          "on",
          "off"
        ]
      }
    }
  }
}"""

    assert len(j) == len(e)
    assert j == e