Ejemplo n.º 1
0
def test_factor_type():
    lg1 = pbn.LinearGaussianCPD("a", [])
    lg2 = pbn.LinearGaussianCPD("b", ["a"])
    lg3 = pbn.LinearGaussianCPD("c", ["b", "a"])

    assert lg1.type() == pbn.LinearGaussianCPDType()
    assert lg1.type() == lg2.type()
    assert lg1.type() == lg3.type()
    assert lg2.type() == lg3.type()

    c1 = pbn.CKDE("a", [])
    c2 = pbn.CKDE("b", ["a"])
    c3 = pbn.CKDE("c", ["b", "a"])

    assert c1.type() == pbn.CKDEType()
    assert c1.type() == c2.type()
    assert c1.type() == c3.type()
    assert c2.type() == c3.type()

    d1 = pbn.DiscreteFactor("a", [])
    d2 = pbn.DiscreteFactor("b", ["a"])
    d3 = pbn.DiscreteFactor("c", ["b", "a"])

    assert d1.type() == pbn.DiscreteFactorType()
    assert d1.type() == d2.type()
    assert d1.type() == d3.type()
    assert d2.type() == d3.type()

    assert lg1.type() != c1.type()
    assert lg1.type() != d1.type()
    assert c1.type() != d1.type()
Ejemplo n.º 2
0
def test_serialization_unfitted_factor(lg_bytes, ckde_bytes, discrete_bytes,
                                       new_bytes, newbis_bytes):
    loaded_lg = pickle.loads(lg_bytes)
    assert loaded_lg.variable() == "c"
    assert set(loaded_lg.evidence()) == set(["a", "b"])
    assert not loaded_lg.fitted()
    assert loaded_lg.type() == pbn.LinearGaussianCPDType()

    loaded_ckde = pickle.loads(ckde_bytes)
    assert loaded_ckde.variable() == "c"
    assert set(loaded_ckde.evidence()) == set(["a", "b"])
    assert not loaded_ckde.fitted()
    assert loaded_ckde.type() == pbn.CKDEType()

    loaded_discrete = pickle.loads(discrete_bytes)
    assert loaded_discrete.variable() == "c"
    assert set(loaded_discrete.evidence()) == set(["a", "b"])
    assert not loaded_discrete.fitted()
    assert loaded_discrete.type() == pbn.DiscreteFactorType()

    loaded_new = pickle.loads(new_bytes)
    assert loaded_new.variable() == "c"
    assert set(loaded_new.evidence()) == set(["a", "b"])
    assert not loaded_new.fitted()
    assert type(loaded_new.type()) == NewType
    nn = NewFactor("a", [])
    assert loaded_new.type() == nn.type()

    from pybnesian import GaussianNetwork
    dummy_network = GaussianNetwork(["a", "b", "c", "d"])
    assert type(loaded_new.type().new_factor(dummy_network, "a",
                                             [])) == NewFactor

    loaded_newbis = pickle.loads(newbis_bytes)
    assert loaded_newbis.variable() == "c"
    assert set(loaded_newbis.evidence()) == set(["a", "b"])
    assert not loaded_newbis.fitted()
    assert type(loaded_newbis.type()) == NewType
    nnbis = NewFactorBis("a", [])
    assert loaded_newbis.type() == nnbis.type()
    assert type(loaded_newbis.type().new_factor(dummy_network, "a",
                                                [])) == NewFactorBis

    assert loaded_lg.type() != loaded_ckde.type()
    assert loaded_lg.type() != loaded_discrete.type()
    assert loaded_lg.type() != loaded_new.type()
    assert loaded_ckde.type() != loaded_discrete.type()
    assert loaded_ckde.type() != loaded_new.type()
    assert loaded_discrete.type() != loaded_new.type()
    assert loaded_newbis.type() == loaded_new.type()