import bnlearn as bn

bif_file= 'sprinkler'
bif_file= 'alarm'
bif_file= 'andes'
bif_file= 'asia'
bif_file= 'pathfinder'
bif_file= 'sachs'
bif_file= 'miserables'
bif_file= 'filepath/to/model.bif'
# Load asia DAG
model = bn.import_DAG('asia')
# plot ground truth
G = bn.plot(model)
# Sampling
df = bn.sampling(model, n=10000)
# Structure learning of sampled dataset
model_sl = bn.structure_learning.fit(df, methodtype='hc', scoretype='bic')
# Plot based on structure learning of sampled data
bn.plot(model_sl, pos=G['pos'])
# Compare networks and make plot
bn.compare_networks(model, model_sl, pos=G['pos'])
[bnlearn] >Import <asia>
[bnlearn] >Downloading example [asia] dataset..
[bnlearn] >Extracting files..
[bnlearn] >Loading bif file </opt/anaconda3/lib/python3.8/site-packages/bnlearn/data/asia.bif>
[bnlearn] >Checking CPDs..
[bnlearn] >Check for DAG structure. Correct: True
[bnlearn] >Plot based on BayesianModel

png

[bnlearn] >Forward sampling for 10000 samples..



  0%|          | 0/8 [00:00<?, ?it/s]


[bnlearn] >Computing best DAG using [hc]
[bnlearn] >Set scoring type at [bic]
[bnlearn] >Plot based on BayesianModel
[bnlearn] >Existing coordinates from <pos> are used.

png

[bnlearn] >Existing coordinates from <pos> are used.

png

png

(array([[51,  5],
        [ 4,  4]]),
 target  asia  tub  smoke  lung  bronc  either  xray  dysp
 source                                                   
 asia       0   -1      0     0      0       0     0     0
 tub        2    0      0     0      0       1     0     0
 smoke      0    0      0    -1     -1       0     0     0
 lung       0    0      2     0      0       1     0     0
 bronc      0    0      2     0      0       0     0    -1
 either     0    0      0     0      2       0     1     1
 xray       0    0      0     0      0       0     0     0
 dysp       0    0      0     0      2       0     0     0)
import csv
import codecs
from itertools import islice
df
asia tub smoke lung bronc either xray dysp
0 1 1 0 1 1 1 1 0
1 1 1 0 1 1 1 1 1
2 1 1 1 1 1 1 1 1
3 1 1 1 1 1 1 1 1
4 1 1 0 1 1 1 1 1
… … … … … … … … …
9995 1 1 0 1 1 1 1 1
9996 1 1 1 1 1 1 1 1
9997 1 1 1 1 1 1 1 1
9998 1 1 0 1 1 1 1 1
9999 1 1 0 1 1 1 1 1

10000 rows × 8 columns

df.to_csv('synthetic_asia.csv')
# Load sprinkler DAG
model = bn.import_DAG('sprinkler')
# plot ground truth
G = bn.plot(model)
# Sampling
df = bn.sampling(model, n=10000)
# Structure learning of sampled dataset
model_sl = bn.structure_learning.fit(df, methodtype='hc', scoretype='bic')
# Plot based on structure learning of sampled data
bn.plot(model_sl, pos=G['pos'])
# Compare networks and make plot
bn.compare_networks(model, model_sl, pos=G['pos'])
[bnlearn] >Import <sprinkler>
[bnlearn] >Checking CPDs..
[bnlearn] >Check for DAG structure. Correct: True
[bnlearn] >Plot based on BayesianModel

png

[bnlearn] >Forward sampling for 10000 samples..



  0%|          | 0/4 [00:00<?, ?it/s]


[bnlearn] >Computing best DAG using [hc]
[bnlearn] >Set scoring type at [bic]
[bnlearn] >Plot based on BayesianModel
[bnlearn] >Existing coordinates from <pos> are used.

png

[bnlearn] >Existing coordinates from <pos> are used.

png

png

(array([[8, 4],
        [2, 2]]),
 target     Cloudy  Sprinkler  Rain  Wet_Grass
 source                                       
 Cloudy          0          1     1          2
 Sprinkler       0          0     0         -1
 Rain            0          2     0         -1
 Wet_Grass       0          2     2          0)
df.to_csv('synthetic_sprinkler.csv')
# Load earthquake DAG
model = bn.import_DAG('alarm')
# plot ground truth
G = bn.plot(model)
# Sampling
df = bn.sampling(model, n=10000)
# Structure learning of sampled dataset
model_sl = bn.structure_learning.fit(df, methodtype='hc', scoretype='bic')
# Plot based on structure learning of sampled data
bn.plot(model_sl, pos=G['pos'])
# Compare networks and make plot
bn.compare_networks(model, model_sl, pos=G['pos'])
[bnlearn] >Import <alarm>
[bnlearn] >Downloading example [alarm] dataset..
[bnlearn] >Extracting files..
[bnlearn] >Loading bif file </opt/anaconda3/lib/python3.8/site-packages/bnlearn/data/alarm.bif>
[bnlearn] >Checking CPDs..
[bnlearn] >Warning: CPD [BP] does not add up to 1 but is: [[1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]]
[bnlearn] >Warning: CPD [HREKG] does not add up to 1 but is: [[0.9999999 0.9999999 1.       ]
 [0.9999999 1.        1.       ]]
[bnlearn] >Warning: CPD [HRSAT] does not add up to 1 but is: [[0.9999999 0.9999999 1.       ]
 [0.9999999 1.        1.       ]]
[bnlearn] >Warning: CPD [PRESS] does not add up to 1 but is: [[[1. 1. 1. 1.]
  [1. 1. 1. 1.]]

 [[1. 1. 1. 1.]
  [1. 1. 1. 1.]]

 [[1. 1. 1. 1.]
  [1. 1. 1. 1.]]]
[bnlearn] >Check for DAG structure. Correct: True
[bnlearn] >Plot based on BayesianModel

png

[bnlearn] >Forward sampling for 10000 samples..



  0%|          | 0/37 [00:00<?, ?it/s]


[bnlearn] >Computing best DAG using [hc]
[bnlearn] >Set scoring type at [bic]
[bnlearn] >Plot based on BayesianModel
[bnlearn] >Existing coordinates from <pos> are used.

png

[bnlearn] >Existing coordinates from <pos> are used.

png

png

(array([[1304,   19],
        [  15,   31]]),
 target        LVFAILURE  HISTORY  LVEDVOLUME  CVP  PCWP  HYPOVOLEMIA  \
 source                                                                 
 LVFAILURE             0        1          -1    0     0            0   
 HISTORY               0        0           0    0     0            0   
 LVEDVOLUME            2        0           0   -1     1            2   
 CVP                   0        0           2    0     0            0   
 PCWP                  0        0           0    0     0            0   
 HYPOVOLEMIA           0        0          -1    0     0            0   
 STROKEVOLUME          0        0           0    0     0            2   
 ERRLOWOUTPUT          0        0           0    0     0            0   
 HRBP                  0        0           0    0     0            0   
 HR                    0        0           0    0     0            0   
 ERRCAUTER             0        0           0    0     0            0   
 HREKG                 0        0           0    0     0            0   
 HRSAT                 0        0           0    0     0            0   
 ANAPHYLAXIS           0        0           0    0     0            0   
 TPR                   0        0           0    0     0            0   
 ARTCO2                0        0           0    0     0            0   
 EXPCO2                0        0           0    0     0            0   
 VENTLUNG              0        0           0    0     0            0   
 INTUBATION            0        0           0    0     0            0   
 MINVOL                0        0           0    0     0            0   
 FIO2                  0        0           0    0     0            0   
 PVSAT                 0        0           0    0     0            0   
 VENTALV               0        0           0    0     0            0   
 SAO2                  0        0           0    0     0            0   
 SHUNT                 0        0           0    0     0            0   
 PULMEMBOLUS           0        0           0    0     0            0   
 PAP                   0        0           0    0     0            0   
 PRESS                 0        0           0    0     0            0   
 KINKEDTUBE            0        0           0    0     0            0   
 VENTTUBE              0        0           0    0     0            0   
 MINVOLSET             0        0           0    0     0            0   
 VENTMACH              0        0           0    0     0            0   
 DISCONNECT            0        0           0    0     0            0   
 CATECHOL              0        0           0    0     0            0   
 INSUFFANESTH          0        0           0    0     0            0   
 CO                    0        0           0    0     0            0   
 BP                    0        0           0    0     0            0   
 
 target        STROKEVOLUME  ERRLOWOUTPUT  HRBP  HR  ...  PRESS  KINKEDTUBE  \
 source                                              ...                      
 LVFAILURE                1             0     0   0  ...      0           0   
 HISTORY                  0             0     0   0  ...      0           0   
 LVEDVOLUME               2             0     0   0  ...      0           0   
 CVP                      0             0     0   0  ...      0           0   
 PCWP                     0             0     0   0  ...      0           0   
 HYPOVOLEMIA             -1             0     0   0  ...      0           0   
 STROKEVOLUME             0             0     0   0  ...      0           0   
 ERRLOWOUTPUT             0             0     1   0  ...      0           0   
 HRBP                     0             0     0   0  ...      0           0   
 HR                       0             0     1   0  ...      0           0   
 ERRCAUTER                0             0     0   0  ...      0           0   
 HREKG                    0             0     0   0  ...      0           0   
 HRSAT                    0             0     0   0  ...      0           0   
 ANAPHYLAXIS              0             0     0   0  ...      0           0   
 TPR                      0             0     0   0  ...      0           0   
 ARTCO2                   0             0     0   0  ...      0           0   
 EXPCO2                   0             0     0   0  ...      0           0   
 VENTLUNG                 0             0     0   0  ...      0           2   
 INTUBATION               0             0     0   0  ...      1           0   
 MINVOL                   0             0     0   0  ...      0           0   
 FIO2                     0             0     0   0  ...      0           0   
 PVSAT                    0             0     0   0  ...      0           0   
 VENTALV                  0             0     0   0  ...      0           0   
 SAO2                     0             0     0   0  ...      0           0   
 SHUNT                    0             0     0   0  ...      0           0   
 PULMEMBOLUS              0             0     0   0  ...      0           0   
 PAP                      0             0     0   0  ...      0           0   
 PRESS                    0             0     0   0  ...      0           0   
 KINKEDTUBE               0             0     0   0  ...      1           0   
 VENTTUBE                 0             0     0   0  ...      1           2   
 MINVOLSET                0             0     0   0  ...      0           0   
 VENTMACH                 0             0     0   0  ...      0           0   
 DISCONNECT               0             0     0   0  ...      0           0   
 CATECHOL                 0             0     0   1  ...      0           0   
 INSUFFANESTH             0             0     0   0  ...      0           0   
 CO                       0             0     0   0  ...      0           0   
 BP                       0             0     0   0  ...      0           0   
 
 target        VENTTUBE  MINVOLSET  VENTMACH  DISCONNECT  CATECHOL  \
 source                                                              
 LVFAILURE            0          0         0           0         0   
 HISTORY              0          0         0           0         0   
 LVEDVOLUME           0          0         0           0         0   
 CVP                  0          0         0           0         0   
 PCWP                 0          0         0           0         0   
 HYPOVOLEMIA          0          0         0           0         0   
 STROKEVOLUME         0          0         0           0         0   
 ERRLOWOUTPUT         0          0         0           0         0   
 HRBP                 0          0         0           0         0   
 HR                   0          0         0           0         0   
 ERRCAUTER            0          0         0           0         0   
 HREKG                0          0         0           0         0   
 HRSAT                0          0         0           0         0   
 ANAPHYLAXIS          0          0         0           0         0   
 TPR                  0          0         0           0         1   
 ARTCO2               0          0         0           0         1   
 EXPCO2               0          0         0           0         0   
 VENTLUNG             2          0         0           0         0   
 INTUBATION           0          0         0           0         0   
 MINVOL               2          0         0           0         0   
 FIO2                 0          0         0           0         0   
 PVSAT                0          0         0           0         0   
 VENTALV              0          0         0           0         0   
 SAO2                 0          0         0           0        -1   
 SHUNT                0          0         0           0         0   
 PULMEMBOLUS          0          0         0           0         0   
 PAP                  0          0         0           0         0   
 PRESS                0          0         0           0         0   
 KINKEDTUBE           0          0         0           0         0   
 VENTTUBE             0          0         2           2         0   
 MINVOLSET            0          0        -1           0         0   
 VENTMACH            -1          2         0           0         0   
 DISCONNECT          -1          0         2           0         0   
 CATECHOL             0          0         0           0         0   
 INSUFFANESTH         0          0         0           0        -1   
 CO                   0          0         0           0         0   
 BP                   0          0         0           0         0   
 
 target        INSUFFANESTH  CO  BP  
 source                              
 LVFAILURE                0   0   0  
 HISTORY                  0   0   0  
 LVEDVOLUME               0   0   0  
 CVP                      0   0   0  
 PCWP                     0   0   0  
 HYPOVOLEMIA              0   0   0  
 STROKEVOLUME             0   1   0  
 ERRLOWOUTPUT             0   0   0  
 HRBP                     0   0   0  
 HR                       0   1   0  
 ERRCAUTER                0   0   0  
 HREKG                    0   0   0  
 HRSAT                    0   0   0  
 ANAPHYLAXIS              0   0   0  
 TPR                      0   0   1  
 ARTCO2                   0   0   0  
 EXPCO2                   0   0   0  
 VENTLUNG                 0   0   0  
 INTUBATION               0   0   0  
 MINVOL                   0   0   0  
 FIO2                     0   0   0  
 PVSAT                    0   0   0  
 VENTALV                  0   0   0  
 SAO2                     0   0   0  
 SHUNT                    0   0   0  
 PULMEMBOLUS              0   0   0  
 PAP                      0   0   0  
 PRESS                    0   0   0  
 KINKEDTUBE               0   0   0  
 VENTTUBE                 0   0   0  
 MINVOLSET                0   0   0  
 VENTMACH                 0   0   0  
 DISCONNECT               0   0   0  
 CATECHOL                 0   0   0  
 INSUFFANESTH             0   0   0  
 CO                       0   0   1  
 BP                       0   0   0  
 
 [37 rows x 37 columns])
df.to_csv('synthetic_alarm.csv')
# Load sachs DAG
model = bn.import_DAG('sachs')
# plot ground truth
G = bn.plot(model)
# Sampling
df = bn.sampling(model, n=10000)
# Structure learning of sampled dataset
model_sl = bn.structure_learning.fit(df, methodtype='hc', scoretype='bic')
# Plot based on structure learning of sampled data
bn.plot(model_sl, pos=G['pos'])
# Compare networks and make plot
bn.compare_networks(model, model_sl, pos=G['pos'])
[bnlearn] >Import <sachs>
[bnlearn] >Downloading example [sachs] dataset..
[bnlearn] >Extracting files..
[bnlearn] >Loading bif file </opt/anaconda3/lib/python3.8/site-packages/bnlearn/data/sachs.bif>
[bnlearn] >Checking CPDs..
[bnlearn] >Warning: CPD [Akt] does not add up to 1 but is: [[1.         1.         1.        ]
 [1.         1.         0.99999998]
 [0.99999992 1.         1.        ]]
[bnlearn] >Warning: CPD [Erk] does not add up to 1 but is: [[1.         1.         1.        ]
 [1.         1.         1.        ]
 [1.         1.         0.99999999]]
[bnlearn] >Warning: CPD [Jnk] does not add up to 1 but is: [[1.         1.         1.        ]
 [1.00000001 0.99999993 1.        ]
 [0.99999997 1.         1.        ]]
[bnlearn] >Warning: CPD [Mek] does not add up to 1 but is: [[[1.         1.         1.        ]
  [1.         1.         1.        ]
  [1.         1.         0.9999999 ]]

 [[1.00000002 1.         1.        ]
  [0.99999994 1.         1.        ]
  [1.00000001 1.         0.9999999 ]]

 [[0.99999996 1.         1.        ]
  [1.         1.         1.        ]
  [1.         1.         0.9999999 ]]]
[bnlearn] >Warning: CPD [P38] does not add up to 1 but is: [[1.         1.         1.        ]
 [1.         1.00000003 1.        ]
 [0.99999999 1.         1.        ]]
[bnlearn] >Warning: CPD [PIP2] does not add up to 1 but is: [[0.99999997 1.         1.        ]
 [0.99999999 1.         1.        ]
 [1.         1.00000001 1.00000001]]
[bnlearn] >Warning: CPD [PIP3] does not add up to 1 but is: [1.0000001 1.        1.0000001]
[bnlearn] >Warning: CPD [PKA] does not add up to 1 but is: [0.9999999  1.00000001 1.        ]
[bnlearn] >Warning: CPD [PKC] does not add up to 1 but is: 0.9999999999999999
[bnlearn] >Warning: CPD [Raf] does not add up to 1 but is: [[1.         1.         1.        ]
 [1.         1.         1.00000001]
 [1.00000001 1.         1.        ]]
[bnlearn] >Check for DAG structure. Correct: True
[bnlearn] >Plot based on BayesianModel

png

[bnlearn] >Forward sampling for 10000 samples..



  0%|          | 0/11 [00:00<?, ?it/s]


[bnlearn] >Warning: Computing DAG with 11 nodes can take a very long time!
[bnlearn] >Computing best DAG using [hc]
[bnlearn] >Set scoring type at [bic]
[bnlearn] >Plot based on BayesianModel
[bnlearn] >Existing coordinates from <pos> are used.

png

[bnlearn] >Existing coordinates from <pos> are used.

png

png

(array([[97,  7],
        [ 7, 10]]),
 target  Erk  Akt  PKA  Mek  Jnk  PKC  Raf  P38  PIP3  PIP2  Plcg
 source                                                          
 Erk       0    1    0    0    0    0    0    0     0     0     0
 Akt       0    0    0    0    0    0    0    0     0     0     0
 PKA       1    1    0   -1    1    2   -1    1     0     0     0
 Mek       1    0    2    0    0    2    0    0     0     0     0
 Jnk       0    0    0    0    0    0    0    0     0     0     0
 PKC       0    0   -1   -1    1    0   -1    1     0     0     0
 Raf       0    0    2    1    0    2    0    0     0     0     0
 P38       0    0    0    0    0    0    0    0     0     0     0
 PIP3      0    0    0    0    0    0    0    0     0     1     2
 PIP2      0    0    0    0    0    0    0    0     0     0     2
 Plcg      0    0    0    0    0    0    0    0    -1    -1     0)
df.to_csv('synthetic_sachs.csv')