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
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)