Пример #1
0
def test_singly_connected():
    """
    Tests generating singly-connected BBN.
    :return: None.
    """
    g, p = generate_singly_bbn(3)

    assert len(g.nodes) == 3
    assert len(g.edges) == 2
Пример #2
0
def test_convert_for_exact_inference():
    """
    Tests converting graph and params to a BBN.
    :return: None
    """

    g, p = generate_singly_bbn(2)
    bbn = convert_for_exact_inference(g, p)

    assert len(bbn.nodes) == 2
    assert len(bbn.edges) == 1
Пример #3
0
def test_generated_serde():
    """
    Tests serde of generated BBN.
    :return: Nonde.
    """
    g, p = generate_singly_bbn(100, max_iter=10)
    e_bbn = convert_for_exact_inference(g, p)
    d = Bbn.to_dict(e_bbn)
    s = json.dumps(d, sort_keys=True, indent=2)
    d = json.loads(s)
    o_bbn = Bbn.from_dict(d)

    assert len(e_bbn.get_nodes()) == len(o_bbn.get_nodes())
    assert len(e_bbn.get_edges()) == len(o_bbn.get_edges())
Пример #4
0
def test_convert_for_drawing():
    """
    Tests converting BBN to networkx DAG.
    :return: None
    """

    g, p = generate_singly_bbn(2)
    bbn = convert_for_exact_inference(g, p)

    graph = convert_for_drawing(bbn)
    nodes = list(graph.nodes)
    edges = list(graph.edges)

    assert len(nodes) == 2
    assert len(edges) == 1
Пример #5
0
import numpy as np

from pybbn.generator.bbngenerator import generate_singly_bbn, convert_for_exact_inference, convert_for_drawing

# very important to set the seed for reproducible results
np.random.seed(37)

# this method generates the graph, g, and probabilities, p
# note we are generating a singly-connected graph
g, p = generate_singly_bbn(5, max_iter=5)

# you have to convert g and p to a BBN
bbn = convert_for_exact_inference(g, p)

# you can convert the BBN to a nx graph for visualization
nx_graph = convert_for_drawing(bbn)
Пример #6
0
import json

import numpy as np

from pybbn.generator.bbngenerator import generate_singly_bbn, convert_for_exact_inference
from pybbn.graph.dag import Bbn

np.random.seed(37)

g, p = generate_singly_bbn(900, max_iter=10)
s_bbn = convert_for_exact_inference(g, p)

with open('singly-bbn.json', 'w') as f:
    f.write(json.dumps(Bbn.to_dict(s_bbn), sort_keys=True, indent=2))