Ejemplo n.º 1
0
def test_create_dbn():
    variables = ["a", "b", "c", "d"]
    gbn = DynamicGaussianNetwork(variables, 2)

    assert gbn.markovian_order() == 2
    assert gbn.variables() == ["a", "b", "c", "d"]
    assert gbn.num_variables() == 4
    assert gbn.type() == pbn.GaussianNetworkType()

    transition_nodes = [v + "_t_0" for v in variables]
    static_nodes = [v + "_t_" + str(m) for v in variables for m in range(1, 3)]

    assert set(gbn.static_bn().nodes()) == set(static_nodes)
    assert set(gbn.transition_bn().interface_nodes()) == set(static_nodes)
    assert set(gbn.transition_bn().nodes()) == set(transition_nodes)

    static_bn = GaussianNetwork(static_nodes)
    transition_bn = ConditionalGaussianNetwork(transition_nodes, static_nodes)

    gbn2 = DynamicGaussianNetwork(variables, 2, static_bn, transition_bn)

    wrong_transition_bn = pbn.ConditionalDiscreteBN(transition_nodes,
                                                    static_nodes)

    with pytest.raises(ValueError) as ex:
        gbn3 = DynamicGaussianNetwork(variables, 2, static_bn,
                                      wrong_transition_bn)
    assert "Static and transition Bayesian networks do not have the same type" in str(
        ex.value)

    wrong_static_bn = pbn.DiscreteBN(static_nodes)
    with pytest.raises(ValueError) as ex:
        gbn4 = DynamicGaussianNetwork(variables, 2, wrong_static_bn,
                                      wrong_transition_bn)
    assert "Bayesian networks are not Gaussian." in str(ex.value)
Ejemplo n.º 2
0
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]
Ejemplo n.º 3
0
def test_hc_shortcut_function():
    model = pbn.hc(df, bn_type=pbn.GaussianNetworkType())
    assert type(model) == pbn.GaussianNetwork

    model = pbn.hc(df,
                   bn_type=MyRestrictedGaussianNetworkType(),
                   score="bic",
                   operators=["arcs"])
    assert type(model) == NewBN
Ejemplo n.º 4
0
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()
Ejemplo n.º 5
0
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()
Ejemplo n.º 6
0
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()
Ejemplo n.º 7
0
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()
Ejemplo n.º 8
0
def gaussian_type_bytes():
    g = pbn.GaussianNetworkType()
    return pickle.dumps(g)