def test_serialization_bn_type(gaussian_type_bytes, spbn_type_bytes, kde_type_bytes, discrete_type_bytes, new_type_bytes, other_type_bytes): loaded_g = pickle.loads(gaussian_type_bytes) new_g = pbn.GaussianNetworkType() assert loaded_g == new_g loaded_s = pickle.loads(spbn_type_bytes) new_s = pbn.SemiparametricBNType() assert loaded_s == new_s loaded_k = pickle.loads(kde_type_bytes) new_k = pbn.KDENetworkType() assert loaded_k == new_k loaded_d = pickle.loads(discrete_type_bytes) new_d = pbn.DiscreteBNType() assert loaded_d == new_d loaded_nn = pickle.loads(new_type_bytes) new_nn = NewBNType() assert loaded_nn == new_nn loaded_o = pickle.loads(other_type_bytes) new_o = OtherBNType() assert loaded_o == new_o assert loaded_o.some_useful_info == "info" m = [loaded_g, loaded_s, loaded_k, loaded_d, loaded_nn, loaded_o] for t in itertools.combinations(m, 2): assert t[0] != t[1]
def test_serialization_dbn_model(dyn_gaussian_bytes, dyn_spbn_bytes, dyn_kde_bytes, dyn_discrete_bytes, dyn_genericbn_bytes, dyn_newbn_bytes, dyn_otherbn_bytes): loaded_g = pickle.loads(dyn_gaussian_bytes) assert set(loaded_g.variables()) == set(["a", "b", "c", "d"]) assert loaded_g.static_bn().arcs() == [("a_t_2", "d_t_1")] assert loaded_g.transition_bn().arcs() == [("c_t_2", "b_t_0")] assert loaded_g.type() == pbn.GaussianNetworkType() loaded_s = pickle.loads(dyn_spbn_bytes) assert set(loaded_s.variables()) == set(["a", "b", "c", "d"]) assert loaded_s.static_bn().arcs() == [("a_t_2", "d_t_1")] assert loaded_s.transition_bn().arcs() == [("c_t_2", "b_t_0")] assert loaded_s.type() == pbn.SemiparametricBNType() node_types = { v + "_t_0": pbn.UnknownFactorType() for v in loaded_s.variables() } node_types["b_t_0"] = pbn.CKDEType() assert loaded_s.transition_bn().node_types() == node_types loaded_k = pickle.loads(dyn_kde_bytes) assert set(loaded_k.variables()) == set(["a", "b", "c", "d"]) assert loaded_k.static_bn().arcs() == [("a_t_2", "d_t_1")] assert loaded_k.transition_bn().arcs() == [("c_t_2", "b_t_0")] assert loaded_k.type() == pbn.KDENetworkType() loaded_d = pickle.loads(dyn_discrete_bytes) assert set(loaded_d.variables()) == set(["a", "b", "c", "d"]) assert loaded_d.static_bn().arcs() == [("a_t_2", "d_t_1")] assert loaded_d.transition_bn().arcs() == [("c_t_2", "b_t_0")] assert loaded_d.type() == pbn.DiscreteBNType() loaded_gen = pickle.loads(dyn_genericbn_bytes) assert set(loaded_gen.variables()) == set(["a", "b", "c", "d"]) assert loaded_gen.static_bn().arcs() == [("a_t_2", "d_t_1")] assert loaded_gen.transition_bn().arcs() == [("a_t_2", "b_t_0")] assert loaded_gen.type() == MyRestrictedGaussianNetworkType() loaded_nn = pickle.loads(dyn_newbn_bytes) assert set(loaded_nn.variables()) == set(["a", "b", "c", "d"]) assert loaded_nn.static_bn().arcs() == [("a_t_2", "d_t_1")] assert loaded_nn.transition_bn().arcs() == [("a_t_2", "b_t_0")] assert loaded_nn.type() == MyRestrictedGaussianNetworkType() loaded_other = pickle.loads(dyn_otherbn_bytes) assert set(loaded_other.variables()) == set(["a", "b", "c", "d"]) assert loaded_other.static_bn().arcs() == [("a_t_2", "d_t_1")] assert loaded_other.transition_bn().arcs() == [("a_t_2", "b_t_0")] assert loaded_other.type() == NonHomogeneousType() assert loaded_other.extra_info == "extra" assert loaded_other.static_bn().node_type( "c_t_1") == pbn.DiscreteFactorType() assert loaded_other.static_bn().node_type("d_t_1") == pbn.CKDEType() assert loaded_other.transition_bn().node_type("d_t_0") == pbn.CKDEType()
def test_serialization_bn_model(gaussian_bytes, spbn_bytes, kde_bytes, discrete_bytes, genericbn_bytes, newbn_bytes, otherbn_bytes): loaded_g = pickle.loads(gaussian_bytes) assert set(loaded_g.nodes()) == set(["a", "b", "c", "d"]) assert loaded_g.arcs() == [("a", "b")] assert loaded_g.type() == pbn.GaussianNetworkType() loaded_s = pickle.loads(spbn_bytes) assert set(loaded_s.nodes()) == set(["a", "b", "c", "d"]) assert loaded_s.arcs() == [("a", "b")] assert loaded_s.type() == pbn.SemiparametricBNType() assert loaded_s.node_types() == { 'a': pbn.UnknownFactorType(), 'b': pbn.CKDEType(), 'c': pbn.UnknownFactorType(), 'd': pbn.UnknownFactorType() } loaded_k = pickle.loads(kde_bytes) assert set(loaded_k.nodes()) == set(["a", "b", "c", "d"]) assert loaded_k.arcs() == [("a", "b")] assert loaded_k.type() == pbn.KDENetworkType() loaded_d = pickle.loads(discrete_bytes) assert set(loaded_d.nodes()) == set(["a", "b", "c", "d"]) assert loaded_d.arcs() == [("a", "b")] assert loaded_d.type() == pbn.DiscreteBNType() loaded_gen = pickle.loads(genericbn_bytes) assert set(loaded_gen.nodes()) == set(["a", "b", "c", "d"]) assert loaded_gen.arcs() == [("a", "b")] assert loaded_gen.type() == MyRestrictedGaussianNetworkType() loaded_nn = pickle.loads(newbn_bytes) assert set(loaded_g.nodes()) == set(["a", "b", "c", "d"]) assert loaded_nn.arcs() == [("a", "b")] assert loaded_nn.type() == MyRestrictedGaussianNetworkType() loaded_o = pickle.loads(otherbn_bytes) assert set(loaded_g.nodes()) == set(["a", "b", "c", "d"]) assert loaded_o.arcs() == [("a", "b")] assert loaded_o.type() == NonHomogeneousType() assert loaded_o.node_types() == { 'a': pbn.UnknownFactorType(), 'b': pbn.LinearGaussianCPDType(), 'c': pbn.CKDEType(), 'd': pbn.DiscreteFactorType() } assert loaded_o.extra_info == "extra" assert loaded_nn.type() != loaded_o.type()
def test_bn_type(): g1 = GaussianNetwork(["a", "b", "c", "d"]) g2 = GaussianNetwork(["a", "b", "c", "d"]) g3 = GaussianNetwork(["a", "b", "c", "d"]) assert g1.type() == pbn.GaussianNetworkType() assert g1.type() == g2.type() assert g1.type() == g3.type() assert g2.type() == g3.type() s1 = SemiparametricBN(["a", "b", "c", "d"]) s2 = SemiparametricBN(["a", "b", "c", "d"]) s3 = SemiparametricBN(["a", "b", "c", "d"]) assert s1.type() == pbn.SemiparametricBNType() assert s1.type() == s2.type() assert s1.type() == s3.type() assert s2.type() == s3.type() k1 = KDENetwork(["a", "b", "c", "d"]) k2 = KDENetwork(["a", "b", "c", "d"]) k3 = KDENetwork(["a", "b", "c", "d"]) assert k1.type() == pbn.KDENetworkType() assert k1.type() == k2.type() assert k1.type() == k3.type() assert k2.type() == k3.type() d1 = DiscreteBN(["a", "b", "c", "d"]) d2 = DiscreteBN(["a", "b", "c", "d"]) d3 = DiscreteBN(["a", "b", "c", "d"]) assert d1.type() == pbn.DiscreteBNType() assert d1.type() == d2.type() assert d1.type() == d3.type() assert d2.type() == d3.type() assert g1.type() != s1.type() assert g1.type() != k1.type() assert g1.type() != d1.type() assert s1.type() != k1.type() assert s1.type() != d1.type() assert k1.type() != d1.type()
def test_serialization_conditional_bn_model( cond_gaussian_bytes, cond_spbn_bytes, cond_kde_bytes, cond_discrete_bytes, cond_genericbn_bytes, cond_newbn_bytes, cond_otherbn_bytes, newbn_bytes, otherbn_bytes): loaded_g = pickle.loads(cond_gaussian_bytes) assert set(loaded_g.nodes()) == set(["c", "d"]) assert set(loaded_g.interface_nodes()) == set(["a", "b"]) assert loaded_g.arcs() == [("a", "c")] assert loaded_g.type() == pbn.GaussianNetworkType() loaded_s = pickle.loads(cond_spbn_bytes) assert set(loaded_s.nodes()) == set(["c", "d"]) assert set(loaded_s.interface_nodes()) == set(["a", "b"]) assert loaded_s.arcs() == [("a", "c")] assert loaded_s.type() == pbn.SemiparametricBNType() assert loaded_s.node_types() == { 'c': pbn.CKDEType(), 'd': pbn.UnknownFactorType() } loaded_k = pickle.loads(cond_kde_bytes) assert set(loaded_k.nodes()) == set(["c", "d"]) assert set(loaded_k.interface_nodes()) == set(["a", "b"]) assert loaded_k.arcs() == [("a", "c")] assert loaded_k.type() == pbn.KDENetworkType() loaded_d = pickle.loads(cond_discrete_bytes) assert set(loaded_d.nodes()) == set(["c", "d"]) assert set(loaded_d.interface_nodes()) == set(["a", "b"]) assert loaded_d.arcs() == [("a", "c")] assert loaded_d.type() == pbn.DiscreteBNType() loaded_gen = pickle.loads(cond_genericbn_bytes) assert set(loaded_gen.nodes()) == set(["c", "d"]) assert set(loaded_gen.interface_nodes()) == set(["a", "b"]) assert loaded_gen.arcs() == [("a", "c")] assert loaded_gen.type() == MyRestrictedGaussianNetworkType() loaded_nn = pickle.loads(cond_newbn_bytes) assert set(loaded_nn.nodes()) == set(["c", "d"]) assert set(loaded_nn.interface_nodes()) == set(["a", "b"]) assert loaded_nn.arcs() == [("a", "c")] assert loaded_nn.type() == MyRestrictedGaussianNetworkType() loaded_o = pickle.loads(cond_otherbn_bytes) assert set(loaded_o.nodes()) == set(["c", "d"]) assert set(loaded_o.interface_nodes()) == set(["a", "b"]) assert loaded_o.arcs() == [("a", "c")] assert loaded_o.type() == NonHomogeneousType() assert loaded_o.node_types() == { 'c': pbn.CKDEType(), 'd': pbn.DiscreteFactorType() } assert loaded_o.extra_info == "extra" assert loaded_nn.type() != loaded_o.type() loaded_unconditional_nn = pickle.loads(newbn_bytes) loaded_unconditional_o = pickle.loads(otherbn_bytes) assert loaded_nn.type() == loaded_unconditional_nn.type() assert loaded_o.type() == loaded_unconditional_o.type()
def discrete_type_bytes(): d = pbn.DiscreteBNType() return pickle.dumps(d)