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
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())
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())
def get_bbn(fpath): with open(fpath, 'r') as f: start = time.time() bbn = Bbn.from_dict(json.loads(f.read())) if fpath.endswith('.json') else Bbn.from_csv(fpath) stop = time.time() diff = stop - start print(f'{diff:.5f} : load time') start = time.time() jt = InferenceController.apply(bbn) stop = time.time() diff = stop - start print(f'{diff:.5f} : inference time') return bbn, jt
def test_deepcopy(): """ Tests deep copy of join tree. :return: None """ a = BbnNode(Variable(0, 'a', ['t', 'f']), [0.2, 0.8]) b = BbnNode(Variable(1, 'b', ['t', 'f']), [0.1, 0.9, 0.9, 0.1]) bbn = Bbn().add_node(a).add_node(b) \ .add_edge(Edge(a, b, EdgeType.DIRECTED)) lhs = InferenceController.apply(bbn) rhs = copy.deepcopy(lhs) lhs_nodes, rhs_nodes = lhs.get_nodes(), rhs.get_nodes() lhs_edges, rhs_edges = lhs.get_edges(), rhs.get_edges() lhs_neighbors, rhs_neighbors = lhs.neighbors, rhs.neighbors lhs_evidences, rhs_evidences = lhs.evidences, rhs.evidences lhs_potentials, rhs_potentials = lhs.potentials, rhs.potentials assert len(lhs_nodes) == len(rhs_nodes) assert len(lhs_edges) == len(rhs_edges) assert len(lhs_neighbors) == len(rhs_neighbors) assert len(lhs_evidences) == len(rhs_evidences) assert len(lhs_potentials) == len(rhs_potentials) list(lhs.get_nodes())[0].nodes[0].variable.values[0] = 'true' lhs_v = list(lhs.get_nodes())[0].nodes[0].variable.values[0] rhs_v = list(rhs.get_nodes())[0].nodes[0].variable.values[0] assert lhs_v != rhs_v
def test_inference_4(): """ Tests inference on simple customized graph. :return: None. """ a = BbnNode(Variable(0, 'a', ['on', 'off']), [0.7, 0.3]) b = BbnNode(Variable(1, 'b', ['on', 'off']), [0.4, 0.6]) c = BbnNode(Variable(2, 'c', ['on', 'off']), [0.9, 0.1, 0.3, 0.7, 0.5, 0.5, 0.1, 0.9]) e = BbnNode(Variable(4, 'e', ['on', 'off']), [0.6, 0.4, 0.2, 0.8]) bbn = Bbn() \ .add_node(a) \ .add_node(b) \ .add_node(c) \ .add_node(e) \ .add_edge(Edge(a, c, EdgeType.DIRECTED)) \ .add_edge(Edge(b, c, EdgeType.DIRECTED)) \ .add_edge(Edge(c, e, EdgeType.DIRECTED)) join_tree = InferenceController.apply(bbn) expected = { 'a': [0.7, 0.3], 'b': [0.4, 0.6], 'c': [0.456, 0.544], 'e': [0.3824, 0.6176] } __validate_posterior__(expected, join_tree)
def test_sampling_with_rejection(): """ Tests sampling a serial graph with rejection and evidence set. :return: None. """ a = BbnNode(Variable(0, 'a', ['on', 'off']), [0.5, 0.5]) b = BbnNode(Variable(1, 'b', ['on', 'off']), [0.5, 0.5, 0.4, 0.6]) c = BbnNode(Variable(2, 'c', ['on', 'off']), [0.7, 0.3, 0.2, 0.8]) bbn = Bbn() \ .add_node(a) \ .add_node(b) \ .add_node(c) \ .add_edge(Edge(a, b, EdgeType.DIRECTED)) \ .add_edge(Edge(b, c, EdgeType.DIRECTED)) sampler = LogicSampler(bbn) n_samples = 10000 samples = pd.DataFrame( sampler.get_samples(evidence={0: 'on'}, n_samples=n_samples, seed=37)) samples.columns = ['a', 'b', 'c'] assert n_samples == samples.shape[0] assert 3 == samples.shape[1] s_a = samples.a.value_counts() s_b = samples.b.value_counts() s_c = samples.c.value_counts() s_a = s_a / s_a.sum() s_b = s_b / s_b.sum() s_c = s_c / s_c.sum() s_a = s_a.sort_index().values s_b = s_b.sort_index().values s_c = s_c.sort_index().values assert_almost_equal(s_a, np.array([1.0])) assert_almost_equal(s_b, np.array([0.5006, 0.4994])) assert_almost_equal(s_c, np.array([0.5521, 0.4479])) join_tree = InferenceController.apply(bbn) ev = EvidenceBuilder() \ .with_node(join_tree.get_bbn_node_by_name('a')) \ .with_evidence('on', 1.0) \ .build() join_tree.set_observation(ev) posteriors = join_tree.get_posteriors() assert_almost_equal(s_a, np.array([posteriors['a']['on']]), decimal=1) assert_almost_equal(s_b, np.array( [posteriors['b']['off'], posteriors['b']['on']]), decimal=1) assert_almost_equal(s_c, np.array( [posteriors['c']['off'], posteriors['c']['on']]), decimal=1)
def test_from_data_simple(): """ Tests create BBN from data. :return: None. """ a = BbnNode(Variable(0, 'a', ['on', 'off']), [0.5, 0.5]) b = BbnNode(Variable(1, 'b', ['on', 'off']), [0.5, 0.5, 0.4, 0.6]) c = BbnNode(Variable(2, 'c', ['on', 'off']), [0.7, 0.3, 0.2, 0.8]) bbn1 = Bbn() \ .add_node(a) \ .add_node(b) \ .add_node(c) \ .add_edge(Edge(a, b, EdgeType.DIRECTED)) \ .add_edge(Edge(b, c, EdgeType.DIRECTED)) sampler = LogicSampler(bbn1) samples = sampler.get_samples(n_samples=10000, seed=37) i2n = {n.variable.id: n.variable.name for n in bbn1.get_nodes()} samples = pd.DataFrame(samples).rename(columns=i2n) parents = { 'a': [], 'b': ['a'], 'c': ['b'] } bbn2 = Factory.from_data(parents, samples) join_tree1 = InferenceController.apply(bbn1) join_tree2 = InferenceController.apply(bbn2) posteriors1 = join_tree1.get_posteriors() posteriors2 = join_tree2.get_posteriors() for k, v1 in posteriors1.items(): assert k in posteriors2 v2 = posteriors2[k] assert len(v1) == len(v2) for k2 in v1: assert k2 in v2 diff = abs(v1[k2] - v2[k2]) assert diff < 0.01
def test_sampling(): """ Tests sampling a serial graph. :return: None. """ a = BbnNode(Variable(0, 'a', ['on', 'off']), [0.5, 0.5]) b = BbnNode(Variable(1, 'b', ['on', 'off']), [0.5, 0.5, 0.4, 0.6]) c = BbnNode(Variable(2, 'c', ['on', 'off']), [0.7, 0.3, 0.2, 0.8]) bbn = Bbn() \ .add_node(a) \ .add_node(b) \ .add_node(c) \ .add_edge(Edge(a, b, EdgeType.DIRECTED)) \ .add_edge(Edge(b, c, EdgeType.DIRECTED)) sampler = LogicSampler(bbn) n_samples = 10000 samples = pd.DataFrame(sampler.get_samples(n_samples=n_samples, seed=37)) samples.columns = ['a', 'b', 'c'] assert n_samples == samples.shape[0] assert 3 == samples.shape[1] s_a = samples.a.value_counts() s_b = samples.b.value_counts() s_c = samples.c.value_counts() s_a = s_a / s_a.sum() s_b = s_b / s_b.sum() s_c = s_c / s_c.sum() s_a = s_a.sort_index() s_b = s_b.sort_index() s_c = s_c.sort_index() assert_almost_equal(s_a.values, np.array([0.4985, 0.5015])) assert_almost_equal(s_b.values, np.array([0.5502, 0.4498])) assert_almost_equal(s_c.values, np.array([0.5721, 0.4279])) join_tree = InferenceController.apply(bbn) posteriors = join_tree.get_posteriors() assert_almost_equal(s_a.values, np.array( [posteriors['a']['off'], posteriors['a']['on']]), decimal=1) assert_almost_equal(s_b.values, np.array( [posteriors['b']['off'], posteriors['b']['on']]), decimal=1) assert_almost_equal(s_c.values, np.array( [posteriors['c']['off'], posteriors['c']['on']]), decimal=1)
def test_trivial_inference(): """ Tests inference on trivial graphs. :return: None. """ a1 = BbnNode(Variable(0, 'a', ['t', 'f']), [0.2, 0.8]) b1 = BbnNode(Variable(1, 'b', ['t', 'f']), [0.1, 0.9, 0.9, 0.1]) bbn1 = Bbn().add_node(a1).add_node(b1).add_edge( Edge(a1, b1, EdgeType.DIRECTED)) jt1 = InferenceController.apply(bbn1) a2 = BbnNode(Variable(1, 'a', ['t', 'f']), [0.2, 0.8]) b2 = BbnNode(Variable(0, 'b', ['t', 'f']), [0.1, 0.9, 0.9, 0.1]) bbn2 = Bbn().add_node(a2).add_node(b2).add_edge( Edge(a2, b2, EdgeType.DIRECTED)) jt2 = InferenceController.apply(bbn2) a3 = BbnNode(Variable(0, 'a', ['t', 'f']), [0.2, 0.8]) b3 = BbnNode(Variable(1, 'b', ['t', 'f']), [0.1, 0.9]) bbn3 = Bbn().add_node(a3).add_node(b3) jt3 = InferenceController.apply(bbn3) __validate_posterior__({ 'a': [0.2, 0.8], 'b': [0.74, 0.26] }, jt1, debug=False) __validate_posterior__({ 'a': [0.2, 0.8], 'b': [0.74, 0.26] }, jt2, debug=False) __validate_posterior__({ 'a': [0.2, 0.8], 'b': [0.1, 0.9] }, jt3, debug=False)
def test_reapply(): """ Tests reinitializing join tree after updating CPTs. :return: None. """ a = BbnNode(Variable(0, 'a', ['t', 'f']), [0.2, 0.8]) b = BbnNode(Variable(1, 'b', ['t', 'f']), [0.1, 0.9, 0.9, 0.1]) bbn = Bbn().add_node(a).add_node(b) \ .add_edge(Edge(a, b, EdgeType.DIRECTED)) lhs = InferenceController.apply(bbn) rhs = InferenceController.reapply(lhs, { 0: [0.3, 0.7], 1: [0.2, 0.8, 0.8, 0.2] }) lhs_pot = [lhs.get_bbn_potential(n) for n in lhs.get_bbn_nodes()] rhs_pot = [rhs.get_bbn_potential(n) for n in rhs.get_bbn_nodes()] lhs_d = Potential.to_dict(lhs_pot) rhs_d = Potential.to_dict(rhs_pot) # lhs should not match rhs after CPT update for k, prob in lhs_d.items(): assert k in rhs_d assert prob != rhs_d[k] # now create lhs with same params as param used to update old # should match with rhs since params are now the same a = BbnNode(Variable(0, 'a', ['t', 'f']), [0.3, 0.7]) b = BbnNode(Variable(1, 'b', ['t', 'f']), [0.2, 0.8, 0.8, 0.2]) bbn = Bbn().add_node(a).add_node(b) \ .add_edge(Edge(a, b, EdgeType.DIRECTED)) lhs = InferenceController.apply(bbn) lhs_pot = [lhs.get_bbn_potential(n) for n in lhs.get_bbn_nodes()] lhs_d = Potential.to_dict(lhs_pot) for k, prob in lhs_d.items(): assert k in rhs_d assert_almost_equals(prob, rhs_d[k], 0.001)
def build_bbn(variable_profiles, g, p): """ Builds a BBN from a DAG, g, and paremeters, p. :param variable_profiles: Variable profiles. :param g: DAG. :param p: Parameters. :return: BBN. """ bbn = Bbn() nodes = list(g.nodes) bbn_node_dict = {} for idx in nodes: name = g.nodes[idx]['name'] domain = variable_profiles[name] cpt = p[idx] v = Variable(idx, name, domain) n = BbnNode(v, cpt) bbn.add_node(n) bbn_node_dict[idx] = n edges = list(g.edges) for edge in edges: pa = bbn_node_dict[edge[0]] ch = bbn_node_dict[edge[1]] e = Edge(pa, ch, EdgeType.DIRECTED) bbn.add_edge(e) return bbn
def convert_for_exact_inference(g, p): """ Converts the graph and parameters to a BBN. :param g: Directed acyclic graph (DAG in the form of networkx). :param p: Parameters. :return: BBN. """ bbn = Bbn() bbn_nodes = {} for node in g.nodes: id = node params = p[id]['params'].flatten() states = [ 'state{}'.format(state) for state in range(p[id]['shape'][1]) ] v = Variable(id, str(id), states) n = BbnNode(v, params) bbn.add_node(n) bbn_nodes[id] = n for e in g.edges: pa = bbn_nodes[e[0]] ch = bbn_nodes[e[1]] bbn.add_edge(Edge(pa, ch, EdgeType.DIRECTED)) return bbn
def test_inference_var_permutation(): """ Tests inference on graphs where id are reversed. :return: None. """ a1 = BbnNode(Variable(0, 'a', ['t', 'f']), [0.2, 0.8]) b1 = BbnNode(Variable(1, 'b', ['t', 'f']), [0.1, 0.9, 0.9, 0.1]) c1 = BbnNode(Variable(2, 'c', ['t', 'f']), [0.2, 0.8, 0.7, 0.3]) bbn1 = Bbn().add_node(a1).add_node(b1).add_node(c1) \ .add_edge(Edge(a1, b1, EdgeType.DIRECTED)) \ .add_edge(Edge(b1, c1, EdgeType.DIRECTED)) jt1 = InferenceController.apply(bbn1) a2 = BbnNode(Variable(2, 'a', ['t', 'f']), [0.2, 0.8]) b2 = BbnNode(Variable(1, 'b', ['t', 'f']), [0.1, 0.9, 0.9, 0.1]) c2 = BbnNode(Variable(0, 'c', ['t', 'f']), [0.2, 0.8, 0.7, 0.3]) bbn2 = Bbn().add_node(a2).add_node(b2).add_node(c2) \ .add_edge(Edge(a2, b2, EdgeType.DIRECTED)) \ .add_edge(Edge(b2, c2, EdgeType.DIRECTED)) jt2 = InferenceController.apply(bbn2) __validate_posterior__( { 'a': [0.2, 0.8], 'b': [0.74, 0.26], 'c': [0.33, 0.67] }, jt1, debug=False) __validate_posterior__( { 'a': [0.2, 0.8], 'b': [0.74, 0.26], 'c': [0.33, 0.67] }, jt2, debug=False)
def main(): # defining bbn variable to create a bayesian belief network global machine_name global join_tree # enter 1 or 2 as per your choice machine_type = int( input( "Choose : \n 1. Use Existing Machine \n 2. Configure a new machine \n\n" )) # if existing machine then ask for machine name and open it and process further if machine_type == 1: machine_name = str(input("Please input your machine name: ")) machine_name_file = '%s.sav' % machine_name try: join_tree = pickle.load(open(machine_name_file, 'rb')) for node in join_tree.get_bbn_nodes(): print(node) check(machine_name) potential_func() except: print("Machine name does not exists") main() else: machine_name = str(input("Please input your machine name: ")) globals()['machine_%s' % machine_name] = Bbn() # create the nodes create_bbn_nodes() # create the network structure by edges and nodes create_bbn_edges() join_tree = InferenceController.apply(globals()['machine_%s' % machine_name]) filename = '%s.sav' % machine_name pickle.dump(join_tree, open(filename, 'wb')) print(globals()['machine_%s' % machine_name]) check(machine_name) potential_func()
def test_inference_1(): """ Tests inference on the Huang graph with manual construction. :return: None. """ a = BbnNode(Variable(0, 'a', ['on', 'off']), [0.5, 0.5]) b = BbnNode(Variable(1, 'b', ['on', 'off']), [0.5, 0.5, 0.4, 0.6]) c = BbnNode(Variable(2, 'c', ['on', 'off']), [0.7, 0.3, 0.2, 0.8]) d = BbnNode(Variable(3, 'd', ['on', 'off']), [0.9, 0.1, 0.5, 0.5]) e = BbnNode(Variable(4, 'e', ['on', 'off']), [0.3, 0.7, 0.6, 0.4]) f = BbnNode(Variable(5, 'f', ['on', 'off']), [0.01, 0.99, 0.01, 0.99, 0.01, 0.99, 0.99, 0.01]) g = BbnNode(Variable(6, 'g', ['on', 'off']), [0.8, 0.2, 0.1, 0.9]) h = BbnNode(Variable(7, 'h', ['on', 'off']), [0.05, 0.95, 0.95, 0.05, 0.95, 0.05, 0.95, 0.05]) bbn = Bbn() \ .add_node(a) \ .add_node(b) \ .add_node(c) \ .add_node(d) \ .add_node(e) \ .add_node(f) \ .add_node(g) \ .add_node(h) \ .add_edge(Edge(a, b, EdgeType.DIRECTED)) \ .add_edge(Edge(a, c, EdgeType.DIRECTED)) \ .add_edge(Edge(b, d, EdgeType.DIRECTED)) \ .add_edge(Edge(c, e, EdgeType.DIRECTED)) \ .add_edge(Edge(d, f, EdgeType.DIRECTED)) \ .add_edge(Edge(e, f, EdgeType.DIRECTED)) \ .add_edge(Edge(c, g, EdgeType.DIRECTED)) \ .add_edge(Edge(e, h, EdgeType.DIRECTED)) \ .add_edge(Edge(g, h, EdgeType.DIRECTED)) join_tree = InferenceController.apply(bbn) expected = { 'a': [0.5, 0.5], 'b': [0.45, 0.55], 'c': [0.45, 0.55], 'd': [0.680, 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)
def test_github_issue_4(): """ Tests issue #4 https://github.com/vangj/py-bbn/issues/4 :return: None. """ a = BbnNode(Variable(0, 'A', ['T', 'F']), [0.5, 0.5]) b = BbnNode(Variable(1, 'B', ['T', 'F']), [0.2, 0.8, 0.1, 0.9]) c = BbnNode(Variable(2, 'C', ['T', 'F']), [0.5, 0.5, 0.5, 0.5]) d = BbnNode(Variable(3, 'D', ['T', 'F']), [0.5, 0.5, 0.5, 0.5]) e = BbnNode(Variable(4, 'E', ['T', 'F']), [0.5, 0.5, 0.5, 0.5]) f = BbnNode(Variable(5, 'F', ['T', 'F']), [0.5, 0.5, 0.5, 0.5]) g = BbnNode(Variable(6, 'G', ['T', 'F']), [ 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5 ]) bbn = Bbn() \ .add_node(a) \ .add_node(b) \ .add_node(c) \ .add_node(d) \ .add_node(e) \ .add_node(f) \ .add_node(g) \ .add_edge(Edge(a, b, EdgeType.DIRECTED)) \ .add_edge(Edge(a, c, EdgeType.DIRECTED)) \ .add_edge(Edge(b, d, EdgeType.DIRECTED)) \ .add_edge(Edge(b, e, EdgeType.DIRECTED)) \ .add_edge(Edge(c, f, EdgeType.DIRECTED)) \ .add_edge(Edge(e, g, EdgeType.DIRECTED)) \ .add_edge(Edge(d, g, EdgeType.DIRECTED)) \ .add_edge(Edge(f, g, EdgeType.DIRECTED)) join_tree = InferenceController.apply(bbn) expected = { 'A': [0.5, 0.5], 'B': [0.15, 0.85], 'C': [0.5, 0.5], 'D': [0.5, 0.5], 'E': [0.5, 0.5], 'F': [0.5, 0.5], 'G': [0.5, 0.5] } __validate_posterior__(expected, join_tree) __print_potentials__(join_tree)
def test_toplogical_sort_reversed(): """ Tests topological sorting of graph with nodes in reverse order. :return: None. """ a = BbnNode(Variable(0, 'a', ['on', 'off']), [0.7, 0.3, 0.2, 0.8]) b = BbnNode(Variable(1, 'b', ['on', 'off']), [0.5, 0.5, 0.4, 0.6]) c = BbnNode(Variable(2, 'c', ['on', 'off']), [0.5, 0.5]) bbn = Bbn() \ .add_node(a) \ .add_node(b) \ .add_node(c) \ .add_edge(Edge(c, b, EdgeType.DIRECTED)) \ .add_edge(Edge(b, a, EdgeType.DIRECTED)) sampler = LogicSampler(bbn) assert_almost_equal([2, 1, 0], sampler.nodes)
def test_toplogical_sort_mixed(): """ Tests topological sort of diverging structure. :return: None. """ a = BbnNode(Variable(0, 'a', ['on', 'off']), [0.7, 0.3, 0.2, 0.8]) b = BbnNode(Variable(1, 'b', ['on', 'off']), [0.5, 0.5]) c = BbnNode(Variable(2, 'c', ['on', 'off']), [0.5, 0.5, 0.4, 0.6]) bbn = Bbn() \ .add_node(a) \ .add_node(b) \ .add_node(c) \ .add_edge(Edge(b, a, EdgeType.DIRECTED)) \ .add_edge(Edge(b, c, EdgeType.DIRECTED)) sampler = LogicSampler(bbn) assert_almost_equal([1, 0, 2], sampler.nodes)
def test_inference_2(): """ Tests inference on customized graph. :return: None. """ a = BbnNode(Variable(0, 'a', ['on', 'off']), [0.7, 0.3]) b = BbnNode(Variable(1, 'b', ['on', 'off']), [0.4, 0.6]) c = BbnNode(Variable(2, 'c', ['on', 'off']), [0.9, 0.1, 0.3, 0.7, 0.5, 0.5, 0.1, 0.9]) d = BbnNode(Variable(3, 'd', ['on', 'off']), [0.3, 0.7, 0.8, 0.2]) e = BbnNode(Variable(4, 'e', ['on', 'off']), [0.6, 0.4, 0.2, 0.8]) f = BbnNode(Variable(5, 'f', ['on', 'off']), [0.7, 0.3, 0.1, 0.9]) g = BbnNode(Variable(6, 'g', ['on', 'off']), [0.4, 0.6, 0.9, 0.1]) bbn = Bbn() \ .add_node(a) \ .add_node(b) \ .add_node(c) \ .add_node(d) \ .add_node(e) \ .add_node(f) \ .add_node(g) \ .add_edge(Edge(a, c, EdgeType.DIRECTED)) \ .add_edge(Edge(b, c, EdgeType.DIRECTED)) \ .add_edge(Edge(c, d, EdgeType.DIRECTED)) \ .add_edge(Edge(c, e, EdgeType.DIRECTED)) \ .add_edge(Edge(d, f, EdgeType.DIRECTED)) \ .add_edge(Edge(d, g, EdgeType.DIRECTED)) join_tree = InferenceController.apply(bbn) expected = { 'a': [0.7, 0.3], 'b': [0.4, 0.6], 'c': [0.456, 0.544], 'd': [0.572, 0.428], 'e': [0.382, 0.618], 'f': [0.443, 0.557], 'g': [0.614, 0.386] } __validate_posterior__(expected, join_tree)
def test_inference_libpgm2(): """ Tests libpgm graph where ordering messes up computation. :return: None. """ letter = BbnNode(Variable(4, 'Letter', ['weak', 'strong']), [0.1, 0.9, 0.4, 0.6, 0.99, 0.01]) grade = BbnNode( Variable(2, 'Grade', ['a', 'b', 'c']), [0.3, 0.4, 0.3, 0.9, 0.08, 0.02, 0.05, 0.25, 0.7, 0.5, 0.3, 0.2]) intelligence = BbnNode(Variable(3, 'Intelligence', ['low', 'high']), [0.7, 0.3]) sat = BbnNode(Variable(1, 'SAT', ['low', 'high']), [0.95, 0.05, 0.2, 0.8]) difficulty = BbnNode(Variable(0, 'Difficulty', ['easy', 'hard']), [0.6, 0.4]) bbn = Bbn() \ .add_node(letter) \ .add_node(grade) \ .add_node(intelligence) \ .add_node(sat) \ .add_node(difficulty) \ .add_edge(Edge(difficulty, grade, EdgeType.DIRECTED)) \ .add_edge(Edge(intelligence, grade, EdgeType.DIRECTED)) \ .add_edge(Edge(intelligence, sat, EdgeType.DIRECTED)) \ .add_edge(Edge(grade, letter, EdgeType.DIRECTED)) join_tree = InferenceController.apply(bbn) __validate_posterior__( { 'Difficulty': [0.6, 0.4], 'Intelligence': [0.7, 0.3], 'Grade': [0.362, 0.288, 0.350], 'SAT': [0.725, 0.275], 'Letter': [0.498, 0.502] }, join_tree, debug=False)
def test_simple_serde(): """ Tests join tree serde with only 1 clique. :return: None. """ a = BbnNode(Variable(0, 'a', ['t', 'f']), [0.2, 0.8]) b = BbnNode(Variable(1, 'b', ['t', 'f']), [0.1, 0.9, 0.9, 0.1]) bbn = Bbn().add_node(a).add_node(b) \ .add_edge(Edge(a, b, EdgeType.DIRECTED)) lhs = InferenceController.apply(bbn) d = JoinTree.to_dict(lhs) rhs = JoinTree.from_dict(d) rhs = InferenceController.apply_from_serde(rhs) lhs_pot = [lhs.get_bbn_potential(n) for n in lhs.get_bbn_nodes()] rhs_pot = [rhs.get_bbn_potential(n) for n in rhs.get_bbn_nodes()] lhs_pot = Potential.to_dict(lhs_pot) rhs_pot = Potential.to_dict(rhs_pot) assert len(lhs_pot) == len(rhs_pot)
def test_sampler_tables(): """ Tests sampler creation of tables. :return: None. """ a = BbnNode(Variable(0, 'a', ['on', 'off']), [0.5, 0.5]) b = BbnNode(Variable(1, 'b', ['on', 'off']), [0.5, 0.5, 0.4, 0.6]) c = BbnNode(Variable(2, 'c', ['on', 'off']), [0.7, 0.3, 0.2, 0.8]) bbn = Bbn() \ .add_node(a) \ .add_node(b) \ .add_node(c) \ .add_edge(Edge(a, b, EdgeType.DIRECTED)) \ .add_edge(Edge(b, c, EdgeType.DIRECTED)) sampler = LogicSampler(bbn) assert_almost_equal([0, 1, 2], sampler.nodes) tables = sampler.tables assert 3 == len(tables) assert 0 in tables assert 1 in tables assert 2 in tables lhs = np.array(tables[0].probs) rhs = np.array([0.5, 1.0]) assert_almost_equal(lhs, rhs) lhs = np.array(list(tables[1].probs.values())) rhs = np.array([[0.5, 1.0], [0.4, 1.0]]) assert_almost_equal(lhs, rhs) lhs = np.array(list(tables[2].probs.values())) rhs = np.array([[0.7, 1.0], [0.2, 1.0]]) assert_almost_equal(lhs, rhs)
def test_forest_inference(): """ Tests inference on a disconnected DAG; sub-DAGs are a -> b, c -> d and e -> f. :return: None. """ a = BbnNode(Variable(0, 'a', ['t', 'f']), [0.2, 0.8]) b = BbnNode(Variable(1, 'b', ['t', 'f']), [0.1, 0.9, 0.9, 0.1]) c = BbnNode(Variable(2, 'c', ['t', 'f']), [0.2, 0.8]) d = BbnNode(Variable(3, 'd', ['t', 'f']), [0.1, 0.9, 0.9, 0.1]) e = BbnNode(Variable(4, 'e', ['t', 'f']), [0.2, 0.8]) f = BbnNode(Variable(5, 'f', ['t', 'f']), [0.1, 0.9, 0.9, 0.1]) bbn = Bbn().add_node(a).add_node(b).add_node(c).add_node(d).add_node(e).add_node(f) \ .add_edge(Edge(a, b, EdgeType.DIRECTED)) \ .add_edge(Edge(c, d, EdgeType.DIRECTED)) \ .add_edge(Edge(e, f, EdgeType.DIRECTED)) jt = InferenceController.apply(bbn) pot = [jt.get_bbn_potential(n) for n in jt.get_bbn_nodes()] o = Potential.to_dict(pot) e = { '0=t': 0.2, '0=f': 0.8, '1=t': 0.7400000000000001, '1=f': 0.26, '2=t': 0.2, '2=f': 0.8, '3=t': 0.7400000000000001, '3=f': 0.26, '4=t': 0.2, '4=f': 0.8, '5=t': 0.7400000000000001, '5=f': 0.26 } for k, p in e.items(): assert_almost_equals(p, o[k], 0.001)
def get_drug_network(): gender_probs = [0.49, 0.51] drug_probs = [ 0.23323615160349853, 0.7667638483965015, 0.7563025210084033, 0.24369747899159663 ] recovery_probs = [ 0.31000000000000005, 0.69, 0.27, 0.73, 0.13, 0.87, 0.06999999999999995, 0.93 ] X = BbnNode(Variable(1, 'drug', ['false', 'true']), drug_probs) Y = BbnNode(Variable(2, 'recovery', ['false', 'true']), recovery_probs) Z = BbnNode(Variable(0, 'gender', ['female', 'male']), gender_probs) bbn = Bbn() \ .add_node(X) \ .add_node(Y) \ .add_node(Z) \ .add_edge(Edge(Z, X, EdgeType.DIRECTED)) \ .add_edge(Edge(Z, Y, EdgeType.DIRECTED)) \ .add_edge(Edge(X, Y, EdgeType.DIRECTED)) return bbn
from pybbn.graph.variable import Variable from pybbn.pptc.inferencecontroller import InferenceController import networkx as nx # for drawing graphs import matplotlib.pyplot as plt # for drawing graphs if __name__ == '__main__': ap = BbnNode(Variable(0, 'ap', ['yes', 'no']), [0.3, 0.7]) p = BbnNode(Variable(1, 'p', ['yes', 'no']), [0.6, 0.4]) sir = BbnNode(Variable(2, 'sir', ['yes', 'no']), [0.7, 0.3, 0.45, 0.55, 0.55, 0.45, 0.2, 0.8]) wbc = BbnNode(Variable(3, 'wbc', ['high', 'low']), [0.6, 0.4, 0.3, 0.7]) bbn = Bbn() \ .add_node(ap) \ .add_node(p) \ .add_node(sir) \ .add_node(wbc) \ .add_edge(Edge(ap, sir, EdgeType.DIRECTED)) \ .add_edge(Edge(p, sir, EdgeType.DIRECTED)) \ .add_edge(Edge(sir, wbc, EdgeType.DIRECTED)) options = { "font_size": 16, "node_size": 3000, "node_color": "white", "edgecolors": "black", "edge_color": "red", "linewidths": 5, "width": 5, } n, d = bbn.to_nx_graph() nx.draw(n, with_labels=True, labels=d, **options)
import time from pybbn.graph.dag import Bbn from pybbn.pptc.inferencecontroller import InferenceController # deserialization 0.02801 # junction tree 6.10584 start = time.time() bbn = Bbn.from_json('singly-bbn.json') stop = time.time() diff = stop - start print(f'deserialization {diff:.5f}') start = time.time() join_tree = InferenceController.apply(bbn) stop = time.time() diff = stop - start print(f'junction tree {diff:.5f}')
from pybbn.graph.dag import Bbn from pybbn.graph.edge import Edge, EdgeType from pybbn.graph.node import BbnNode from pybbn.graph.variable import Variable # create graph a = BbnNode(Variable(0, 'a', ['t', 'f']), [0.2, 0.8]) b = BbnNode(Variable(1, 'b', ['t', 'f']), [0.1, 0.9, 0.9, 0.1]) bbn = Bbn().add_node(a).add_node(b) \ .add_edge(Edge(a, b, EdgeType.DIRECTED)) # serialize Bbn.to_csv(bbn, 'simple-bbn.csv')
import json import numpy as np from pybbn.generator.bbngenerator import generate_multi_bbn, convert_for_exact_inference from pybbn.graph.dag import Bbn np.random.seed(37) g, p = generate_multi_bbn(900, max_iter=10) s_bbn = convert_for_exact_inference(g, p) with open('multi-bbn.json', 'w') as f: f.write(json.dumps(Bbn.to_dict(s_bbn), sort_keys=True, indent=2))
import functools import json import timeit from pybbn.graph.dag import Bbn from pybbn.pptc.inferencecontroller import InferenceController def do_it(bbn): InferenceController.apply(bbn) with open('singly-bbn.json', 'r') as f: bbn = Bbn.from_dict(json.loads(f.read())) print('finished loading') n = 20 t = timeit.Timer(functools.partial(do_it, bbn)) d = t.timeit(n) / float(n) print(d)