Ejemplo n.º 1
0
def test_to_undirected():
    # TEST 1:
    randdata = ['sprinkler', 'alarm', 'andes', 'asia', 'pathfinder', 'sachs']
    n = np.random.randint(0, len(randdata))
    DAG = bn.import_DAG(randdata[n], CPD=False, verbose=0)
    assert (DAG['adjmat'].sum().sum() * 2) == bn.to_undirected(
        DAG['adjmat']).sum().sum()
Ejemplo n.º 2
0
def test_import_DAG():
    DAG = bn.import_DAG('Sprinkler')
    # TEST 1: check output is unchanged
    assert [*DAG.keys()] == ['model', 'adjmat']
    # TEST 2: Check model output is unchanged
    assert DAG['adjmat'].sum().sum() == 4
    # TEST 3:
    assert 'pgmpy.models.BayesianModel.BayesianModel' in str(type(
        DAG['model']))
    # TEST 4:
    DAG = bn.import_DAG('alarm', verbose=0)
    assert [*DAG.keys()] == ['model', 'adjmat']
    DAG = bn.import_DAG('andes', verbose=0)
    assert [*DAG.keys()] == ['model', 'adjmat']
    DAG = bn.import_DAG('asia', verbose=0)
    assert [*DAG.keys()] == ['model', 'adjmat']
Ejemplo n.º 3
0
def test_inference():
    DAG = bn.import_DAG('sprinkler')
    q1 = bn.inference.fit(DAG,
                          variables=['Wet_Grass'],
                          evidence={
                              'Rain': 1,
                              'Sprinkler': 0,
                              'Cloudy': 1
                          })
    assert 'pgmpy.factors.discrete.DiscreteFactor.DiscreteFactor' in str(
        type(q1))
Ejemplo n.º 4
0
def bayesian_network_datasets(name="asia", samples=10000):
    """
    Generates well known sample/toy datasets for bayesian networks,
    by sampling from existing graph model.

    Parameters
    ----------
    name: str, (default:'asia')
        Name of the model to sample from
    samples: int, (default: 10000)
        Number of observations for our dataset
    Returns
    -------
    pd.DataFrame
    """
    model = bn.import_DAG(name)
    df = bn.sampling(model, n=samples)
    return df
Ejemplo n.º 5
0
        n_wrong_per_episode[e] = wrong_move_counter
        n_timeout_per_episode[e] = timeout_counter

    return n_correct_per_episode, n_wrong_per_episode, n_timeout_per_episode


#SIMULATION PARAMS
robot_assistance = [i for i in range(Robot_Assistance.counter.value)]
robot_feedback = [i for i in range(Robot_Feedback.counter.value)]
epochs = 40

#initialise memory, attention and reactivity varibles
persona_memory = 0
persona_attention = 0
persona_reactivity = 1
persona_cpds = bnlearn.import_DAG('persona_model.bif')
persona_user_model = {
    'cpds': persona_cpds,
    'memory': persona_memory,
    'attention': persona_attention,
    'reactivity': persona_reactivity
}
#initialise memory, attention and reactivity varibles
real_user_memory = 2
real_user_attention = 2
real_user_reactivity = 2
real_user_cpds = bnlearn.import_DAG('user_model.bif')
real_user_model = {
    'cpds': real_user_cpds,
    'memory': real_user_memory,
    'attention': real_user_attention,
Ejemplo n.º 6
0
def test_parameter_learning():
    df = bn.import_example()
    model = bn.import_DAG('sprinkler', CPD=False)
    model_update = bn.parameter_learning.fit(model, df)
    assert [*model_update.keys()] == ['model', 'adjmat', 'config']
Ejemplo n.º 7
0
def test_vec2adjmat():
    DAG = bn.import_DAG('Sprinkler', verbose=0)
    out = bn.adjmat2vec(DAG['adjmat'])
    # TEST: conversion
    assert bn.vec2adjmat(out['source'],
                         out['target']).shape == DAG['adjmat'].shape
Ejemplo n.º 8
0
def test_adjmat2vec():
    DAG = bn.import_DAG('Sprinkler', verbose=0)
    out = bn.adjmat2vec(DAG['adjmat'])
    assert np.all(out['source'] == ['Cloudy', 'Cloudy', 'Sprinkler', 'Rain'])
Ejemplo n.º 9
0
def test_compare_networks():
    DAG = bn.import_DAG('Sprinkler', verbose=0)
    G = bn.compare_networks(DAG, DAG, showfig=False)
    assert np.all(G[0] == [[12, 0], [0, 4]])
Ejemplo n.º 10
0
def test_sampling():
    # TEST 1:
    model = bn.import_DAG('Sprinkler')
    n = np.random.randint(10, 1000)
    df = bn.sampling(model, n=n)
    assert df.shape == (n, 4)
Ejemplo n.º 11
0
def test_structure_learning():
    import bnlearn as bn
    df = bn.import_example()
    model = bn.structure_learning.fit(df)
    assert [*model.keys()] == ['model', 'model_edges', 'adjmat', 'config']
    model = bn.structure_learning.fit(df, methodtype='hc', scoretype='bic')
    assert [*model.keys()] == ['model', 'model_edges', 'adjmat', 'config']
    model = bn.structure_learning.fit(df, methodtype='hc', scoretype='k2')
    assert [*model.keys()] == ['model', 'model_edges', 'adjmat', 'config']
    model = bn.structure_learning.fit(df, methodtype='cs', scoretype='bdeu')
    assert [*model.keys()] == [
        'undirected', 'undirected_edges', 'pdag', 'pdag_edges', 'dag',
        'dag_edges', 'model', 'model_edges', 'adjmat', 'config'
    ]
    model = bn.structure_learning.fit(df, methodtype='cs', scoretype='k2')
    assert [*model.keys()] == [
        'undirected', 'undirected_edges', 'pdag', 'pdag_edges', 'dag',
        'dag_edges', 'model', 'model_edges', 'adjmat', 'config'
    ]
    model = bn.structure_learning.fit(df, methodtype='ex', scoretype='bdeu')
    assert [*model.keys()] == ['model', 'model_edges', 'adjmat', 'config']
    model = bn.structure_learning.fit(df, methodtype='ex', scoretype='k2')
    assert [*model.keys()] == ['model', 'model_edges', 'adjmat', 'config']
    model = bn.structure_learning.fit(df, methodtype='cl', root_node='Cloudy')
    assert [*model.keys()] == ['model', 'model_edges', 'adjmat', 'config']

    # Test the filtering
    DAG = bn.import_DAG('asia')
    # Sampling
    df = bn.sampling(DAG, n=1000)
    # Structure learning of sampled dataset
    model = bn.structure_learning.fit(df)
    assert np.all(
        model['adjmat'].columns.values ==
        ['smoke', 'bronc', 'lung', 'asia', 'tub', 'either', 'dysp', 'xray'])

    # hc Enforce and filtering
    model = bn.structure_learning.fit(df,
                                      methodtype='hc',
                                      white_list=['smoke', 'either'],
                                      bw_list_method='filter')
    assert np.all(model['adjmat'].columns.values == ['smoke', 'either'])
    model = bn.structure_learning.fit(df,
                                      methodtype='hc',
                                      white_list=['smoke', 'either'],
                                      bw_list_method='enforce')
    assert np.all(
        model['adjmat'].columns.values ==
        ['smoke', 'bronc', 'lung', 'asia', 'tub', 'either', 'dysp', 'xray'])
    model = bn.structure_learning.fit(df,
                                      methodtype='hc',
                                      black_list=['smoke', 'either'],
                                      bw_list_method='filter')
    assert np.all(model['adjmat'].columns.values ==
                  ['bronc', 'lung', 'asia', 'tub', 'dysp', 'xray'])
    model = bn.structure_learning.fit(df,
                                      methodtype='hc',
                                      scoretype='bic',
                                      black_list=['smoke', 'either'],
                                      bw_list_method='enforce')
    assert np.all(
        model['adjmat'].columns.values ==
        ['smoke', 'bronc', 'lung', 'asia', 'tub', 'either', 'dysp', 'xray'])
    # hc filter
    model = bn.structure_learning.fit(df,
                                      methodtype='ex',
                                      white_list=['smoke', 'either'],
                                      bw_list_method='filter')
    assert np.all(model['adjmat'].columns.values == ['either', 'smoke'])
    model = bn.structure_learning.fit(
        df,
        methodtype='ex',
        black_list=['asia', 'tub', 'either', 'dysp', 'xray'],
        bw_list_method='filter')
    assert np.all(model['adjmat'].columns.values == ['bronc', 'lung', 'smoke'])
    # cs filter
    model = bn.structure_learning.fit(df,
                                      methodtype='cs',
                                      white_list=['smoke', 'either'],
                                      bw_list_method='filter')
    assert np.all(model['adjmat'].columns.values == ['either', 'smoke'])
    model = bn.structure_learning.fit(
        df,
        methodtype='cs',
        black_list=['asia', 'tub', 'either', 'dysp', 'xray'],
        bw_list_method='filter')
    assert np.all(model['adjmat'].columns.values == ['bronc', 'smoke', 'lung'])
    # cl filter
    model = bn.structure_learning.fit(df,
                                      methodtype='cl',
                                      white_list=['smoke', 'either'],
                                      bw_list_method='filter',
                                      root_node='smoke')
    assert np.all(model['adjmat'].columns.values == ['smoke', 'either'])
Ejemplo n.º 12
0
model_hc_bic = bn.structure_learning.fit(df, methodtype='hc', scoretype='bic')
model_hc_k2 = bn.structure_learning.fit(df, methodtype='hc', scoretype='k2')
model_hc_bdeu = bn.structure_learning.fit(df,
                                          methodtype='hc',
                                          scoretype='bdeu')
model_ex_bic = bn.structure_learning.fit(df, methodtype='ex', scoretype='bic')
model_ex_k2 = bn.structure_learning.fit(df, methodtype='ex', scoretype='k2')
model_ex_bdeu = bn.structure_learning.fit(df,
                                          methodtype='ex',
                                          scoretype='bdeu')

bn.compare_networks(model, model_hc_bic, pos=G['pos'])

# %% Example with dataset
import bnlearn as bn
DAG = bn.import_DAG('sprinkler')
# Print cpds
bn.print_CPD(DAG)
# plot ground truth
G = bn.plot(DAG)
df = bn.sampling(DAG, n=100)

# %% Inference using custom DAG
import bnlearn as bn
# Load asia DAG
df = bn.import_example('asia')
# from tabulate import tabulate
# print(tabulate(df.head(), tablefmt="grid", headers="keys"))
print(df)

edges = [('smoke', 'lung'), ('smoke', 'bronc'), ('lung', 'xray'),
Ejemplo n.º 13
0

def get_kl_divergence(x_distribution, y_distribution):
    x_distribution_list = []
    y_distribution_list = []
    for key in y_distribution.keys():
        x_distribution_list.append(x_distribution.get(key, 0))
        y_distribution_list.append(y_distribution.get(key, 0))
    norm_x = [float(i) / sum(x_distribution_list) for i in x_distribution_list]
    norm_y = [float(i) / sum(y_distribution_list) for i in y_distribution_list]
    return entropy(norm_x, qk=norm_y)


# Load asia DAG
dataset = "child"
model = bn.import_DAG(dc[dataset]["path"])

# plot ground truth
# G = bn.plot(model)
sample_size = [int(1000 * 100 ** (t / 9)) for t in range(0, 10)]
print(sample_size)
# Sampling
true_df = bn.sampling(model, n=100000)
columns = [col for col in true_df.columns]
print("Get true distribution")
true_distribution = get_distribution(true_df, columns)
kl_divergence = dict()

for size in sample_size:
    train_df = true_df.sample(n=size)