Skip to content

Commit 5ba9be5

Browse files
committed
sampling gets its own module
1 parent 6cc3d76 commit 5ba9be5

File tree

4 files changed

+164
-69
lines changed

4 files changed

+164
-69
lines changed

bnlearn/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
print_CPD,
55
import_DAG,
66
import_example,
7-
sampling,
87
to_undirected,
98
compare_networks,
109
plot,
@@ -33,6 +32,7 @@
3332
import bnlearn.structure_learning as structure_learning
3433
import bnlearn.parameter_learning as parameter_learning
3534
import bnlearn.inference as inference
35+
import bnlearn.sampling as sampling
3636
import bnlearn.network as network
3737
import bnlearn.confmatrix as confmatrix
3838
from bnlearn.impute import knn_imputer, mice_imputer

bnlearn/bnlearn.py

Lines changed: 67 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
from pgmpy.models import BayesianNetwork, NaiveBayes, DynamicBayesianNetwork, MarkovNetwork
2626
from pgmpy.factors.discrete import TabularCPD
27-
from pgmpy.sampling import BayesianModelSampling, GibbsSampling
2827
from pgmpy.metrics import structure_score
2928

3029
from setgraphviz import setgraphviz
@@ -592,76 +591,76 @@ def adjmat2dict(adjmat):
592591

593592

594593
# %% Sampling from model
595-
def sampling(DAG, n=1000, methodtype='bayes', verbose=0):
596-
"""Generate sample(s) using the joint distribution of the network.
594+
# def sampling(DAG, n=1000, methodtype='bayes', verbose=0):
595+
# """Generate sample(s) using the joint distribution of the network.
597596

598-
Parameters
599-
----------
600-
DAG : dict
601-
Contains model and the adjmat of the DAG.
602-
methodtype : str (default: 'bayes')
603-
* 'bayes': Forward sampling using Bayesian.
604-
* 'gibbs' : Gibbs sampling.
605-
n : int, optional
606-
Number of samples to generate. The default is 1000.
607-
verbose : int, optional
608-
Print progress to screen. The default is 3.
609-
0: None, 1: ERROR, 2: WARN, 3: INFO (default), 4: DEBUG, 5: TRACE
597+
# Parameters
598+
# ----------
599+
# DAG : dict
600+
# Contains model and the adjmat of the DAG.
601+
# methodtype : str (default: 'bayes')
602+
# * 'bayes': Forward sampling using Bayesian.
603+
# * 'gibbs' : Gibbs sampling.
604+
# n : int, optional
605+
# Number of samples to generate. The default is 1000.
606+
# verbose : int, optional
607+
# Print progress to screen. The default is 3.
608+
# 0: None, 1: ERROR, 2: WARN, 3: INFO (default), 4: DEBUG, 5: TRACE
610609

611-
Returns
612-
-------
613-
df : pd.DataFrame().
614-
Dataframe containing sampled data from the input DAG model.
610+
# Returns
611+
# -------
612+
# df : pd.DataFrame().
613+
# Dataframe containing sampled data from the input DAG model.
615614

616-
Example
617-
-------
618-
>>> # Example 1
619-
>>>
620-
>>> # Import library
621-
>>> import bnlearn as bn
622-
>>> # Load DAG with model
623-
>>> DAG = bn.import_DAG('sprinkler')
624-
>>> # Sampling
625-
>>> df = bn.sampling(DAG, n=1000, methodtype='bayes')
626-
>>>
627-
>>> # Example 2:
628-
>>>
629-
>>> # Load example dataset
630-
>>> df = bn.import_example('sprinkler')
631-
>>> edges = [('Cloudy', 'Sprinkler'),
632-
>>> ('Cloudy', 'Rain'),
633-
>>> ('Sprinkler', 'Wet_Grass'),
634-
>>> ('Rain', 'Wet_Grass')]
635-
>>>
636-
>>> # Make the actual Bayesian DAG
637-
>>> DAG = bn.make_DAG(edges, verbose=3, methodtype='bayes')
638-
>>> # Fit model
639-
>>> model = bn.parameter_learning.fit(DAG, df, verbose=3, methodtype='bayes')
640-
>>> # Sampling using gibbs
641-
>>> df = bn.sampling(model, n=100, methodtype='gibbs', verbose=0)
615+
# Example
616+
# -------
617+
# >>> # Example 1
618+
# >>>
619+
# >>> # Import library
620+
# >>> import bnlearn as bn
621+
# >>> # Load DAG with model
622+
# >>> DAG = bn.import_DAG('sprinkler')
623+
# >>> # Sampling
624+
# >>> df = bn.sampling(DAG, n=1000, methodtype='bayes')
625+
# >>>
626+
# >>> # Example 2:
627+
# >>>
628+
# >>> # Load example dataset
629+
# >>> df = bn.import_example('sprinkler')
630+
# >>> edges = [('Cloudy', 'Sprinkler'),
631+
# >>> ('Cloudy', 'Rain'),
632+
# >>> ('Sprinkler', 'Wet_Grass'),
633+
# >>> ('Rain', 'Wet_Grass')]
634+
# >>>
635+
# >>> # Make the actual Bayesian DAG
636+
# >>> DAG = bn.make_DAG(edges, verbose=3, methodtype='bayes')
637+
# >>> # Fit model
638+
# >>> model = bn.parameter_learning.fit(DAG, df, verbose=3, methodtype='bayes')
639+
# >>> # Sampling using gibbs
640+
# >>> df = bn.sampling(model, n=100, methodtype='gibbs', verbose=0)
642641

643-
"""
644-
if n<=0: raise ValueError('Number of samples (n) must be 1 or larger!')
645-
if (DAG is None) or ('BayesianNetwork' not in str(type(DAG['model']))):
646-
raise ValueError('The input model (DAG) must contain BayesianNetwork.')
647-
648-
if len(DAG['model'].get_cpds())==0:
649-
raise Exception('[bnlearn] >Error! This is a Bayesian DAG containing only edges, and no CPDs. Tip: you need to specify or learn the CPDs. Try: DAG=bn.parameter_learning.fit(DAG, df). At this point you can make a plot with: bn.plot(DAG).')
650-
return
651-
652-
if methodtype=='bayes':
653-
if verbose>=3: print('[bnlearn] >Bayesian forward sampling for %.0d samples..' %(n))
654-
# Bayesian Forward sampling and make dataframe
655-
infer_model = BayesianModelSampling(DAG['model'])
656-
df = infer_model.forward_sample(size=n, seed=None, show_progress=(True if verbose>=3 else False))
657-
elif methodtype=='gibbs':
658-
if verbose>=3: print('[bnlearn] >Gibbs sampling for %.0d samples..' %(n))
659-
# Gibbs sampling
660-
gibbs = GibbsSampling(DAG['model'])
661-
df = gibbs.sample(size=n, seed=None)
662-
else:
663-
if verbose>=3: print('[bnlearn] >Methodtype [%s] unknown' %(methodtype))
664-
return df
642+
# """
643+
# if n<=0: raise ValueError('Number of samples (n) must be 1 or larger!')
644+
# if (DAG is None) or ('BayesianNetwork' not in str(type(DAG['model']))):
645+
# raise ValueError('The input model (DAG) must contain BayesianNetwork.')
646+
647+
# if len(DAG['model'].get_cpds())==0:
648+
# raise Exception('[bnlearn] >Error! This is a Bayesian DAG containing only edges, and no CPDs. Tip: you need to specify or learn the CPDs. Try: DAG=bn.parameter_learning.fit(DAG, df). At this point you can make a plot with: bn.plot(DAG).')
649+
# return
650+
651+
# if methodtype=='bayes':
652+
# if verbose>=3: print('[bnlearn] >Bayesian forward sampling for %.0d samples..' %(n))
653+
# # Bayesian Forward sampling and make dataframe
654+
# infer_model = BayesianModelSampling(DAG['model'])
655+
# df = infer_model.forward_sample(size=n, seed=None, show_progress=(True if verbose>=3 else False))
656+
# elif methodtype=='gibbs':
657+
# if verbose>=3: print('[bnlearn] >Gibbs sampling for %.0d samples..' %(n))
658+
# # Gibbs sampling
659+
# gibbs = GibbsSampling(DAG['model'])
660+
# df = gibbs.sample(size=n, seed=None)
661+
# else:
662+
# if verbose>=3: print('[bnlearn] >Methodtype [%s] unknown' %(methodtype))
663+
# return df
665664

666665

667666
# %% Convert BIF model to bayesian model
@@ -1630,7 +1629,7 @@ def import_example(data='sprinkler', url=None, sep=',', n=10000, verbose=3):
16301629
if (data=='alarm') or (data=='andes') or (data=='asia') or (data=='sachs') or (data=='water'):
16311630
try:
16321631
DAG = import_DAG(data, verbose=2)
1633-
df = sampling(DAG, n=n, verbose=2)
1632+
df = bnlearn.sampling(DAG, n=n, verbose=2)
16341633
except:
16351634
print('[bnlearn] >Error: Loading data not possible!')
16361635
df = None

bnlearn/examples.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
import bnlearn as bn
2+
3+
# Load asia DAG
4+
model = bn.import_DAG('asia')
5+
# plot ground truth
6+
G = bn.plot(model)
7+
8+
Gi = bn.plot(model, interactive=True)
9+
10+
bn.print_CPD(model)
11+
12+
# Lets create an example dataset with 100 samples and make inferences on the entire dataset.
13+
df = bn.sampling(model, n=10000)
14+
15+
116
# %%
217
import bnlearn as bn
318
# Load example DataFrame

bnlearn/sampling.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
"""This is a module of bnlearn for the generation of synthetic data."""
2+
# ------------------------------------
3+
# Name : sampling.py
4+
# Author : E.Taskesen
5+
# Contact : erdogant@gmail.com
6+
# Licence : See licences
7+
# ------------------------------------
8+
9+
from pgmpy.sampling import BayesianModelSampling, GibbsSampling
10+
11+
# %% Sampling from model
12+
def sampling(DAG, n=1000, methodtype='bayes', verbose=0):
13+
"""Generate sample(s) using the joint distribution of the network.
14+
15+
Parameters
16+
----------
17+
DAG : dict
18+
Contains model and the adjmat of the DAG.
19+
methodtype : str (default: 'bayes')
20+
* 'bayes': Forward sampling using Bayesian.
21+
* 'gibbs' : Gibbs sampling.
22+
n : int, optional
23+
Number of samples to generate. The default is 1000.
24+
verbose : int, optional
25+
Print progress to screen. The default is 3.
26+
0: None, 1: ERROR, 2: WARN, 3: INFO (default), 4: DEBUG, 5: TRACE
27+
28+
Returns
29+
-------
30+
df : pd.DataFrame().
31+
Dataframe containing sampled data from the input DAG model.
32+
33+
Example
34+
-------
35+
>>> # Example 1
36+
>>>
37+
>>> # Import library
38+
>>> import bnlearn as bn
39+
>>> # Load DAG with model
40+
>>> DAG = bn.import_DAG('sprinkler')
41+
>>> # Sampling
42+
>>> df = bn.sampling(DAG, n=1000, methodtype='bayes')
43+
>>>
44+
>>> # Example 2:
45+
>>>
46+
>>> # Load example dataset
47+
>>> df = bn.import_example('sprinkler')
48+
>>> edges = [('Cloudy', 'Sprinkler'),
49+
>>> ('Cloudy', 'Rain'),
50+
>>> ('Sprinkler', 'Wet_Grass'),
51+
>>> ('Rain', 'Wet_Grass')]
52+
>>>
53+
>>> # Make the actual Bayesian DAG
54+
>>> DAG = bn.make_DAG(edges, verbose=3, methodtype='bayes')
55+
>>> # Fit model
56+
>>> model = bn.parameter_learning.fit(DAG, df, verbose=3, methodtype='bayes')
57+
>>> # Sampling using gibbs
58+
>>> df = bn.sampling(model, n=100, methodtype='gibbs', verbose=0)
59+
60+
"""
61+
if n<=0: raise ValueError('Number of samples (n) must be 1 or larger!')
62+
if (DAG is None) or ('BayesianNetwork' not in str(type(DAG['model']))):
63+
raise ValueError('The input model (DAG) must contain BayesianNetwork.')
64+
65+
if len(DAG['model'].get_cpds())==0:
66+
raise Exception('[bnlearn] >Error! This is a Bayesian DAG containing only edges, and no CPDs. Tip: you need to specify or learn the CPDs. Try: DAG=bn.parameter_learning.fit(DAG, df). At this point you can make a plot with: bn.plot(DAG).')
67+
return
68+
69+
if methodtype=='bayes':
70+
if verbose>=3: print('[bnlearn] >Bayesian forward sampling for %.0d samples..' %(n))
71+
# Bayesian Forward sampling and make dataframe
72+
infer_model = BayesianModelSampling(DAG['model'])
73+
df = infer_model.forward_sample(size=n, seed=None, show_progress=(True if verbose>=3 else False))
74+
elif methodtype=='gibbs':
75+
if verbose>=3: print('[bnlearn] >Gibbs sampling for %.0d samples..' %(n))
76+
# Gibbs sampling
77+
gibbs = GibbsSampling(DAG['model'])
78+
df = gibbs.sample(size=n, seed=None)
79+
else:
80+
if verbose>=3: print('[bnlearn] >Methodtype [%s] unknown' %(methodtype))
81+
return df

0 commit comments

Comments
 (0)