erdogant / bnlearn

Python package for Causal Discovery by learning the graphical structure of Bayesian networks. Structure Learning, Parameter Learning, Inferences, Sampling methods.
https://erdogant.github.io/bnlearn
Other
482 stars 46 forks source link

KeyError on call to bn.parameter_learning.fit #105

Closed smengel closed 1 month ago

smengel commented 1 month ago

I had a key error occur on the following call to bnlearn:

model = bn.parameter_learning.fit(DAG, df_discrete)


KeyError Traceback (most recent call last) Cell In[24], line 1 ----> 1 model = bn.parameter_learning.fit(DAG, df_discrete)

File C:\ProgramData\miniconda3\envs\ml\Lib\site-packages\bnlearn\parameter_learning.py:118, in fit(model, df, methodtype, scoretype, smooth, n_jobs, verbose) 115 independence_test = model.get('independence_test', None) 117 # Automatically set methodtype to DBN --> 118 if str(model['config'].get('method')).lower()=='dbn': 119 config['method'] = 'DBN' 120 if verbose>=3: print('[bnlearn] >Methodtype is set to DynamicBayesianNetwork (DBN)')

KeyError: 'config'

It worked last spring when I presented it to my class, but now I am receiving the error.

Please help and thank you.

smengel commented 1 month ago

I get the same error on the 'asian' example here, https://erdogant.github.io/bnlearn/pages/html/Examples.html

import bnlearn as bn

Load asia DAG

model_true = bn.import_DAG('asia')

plot ground truth

G = bn.plot(model_true)


KeyError Traceback (most recent call last) Cell In[3], line 1 ----> 1 G = bn.plot(model_true)

File C:\ProgramData\miniconda3\envs\ml\Lib\site-packages\bnlearn\bnlearn.py:1289, in plot(model, pos, scale, interactive, title, node_color, node_size, node_properties, edge_properties, edge_labels, params_interactive, params_static, verbose) 1286 if verbose>=3: print('[bnlearn]> Nothing to plot because no edges are present between nodes. ') 1287 return None -> 1289 if model['config']['method']=='DBN' and interactive: 1290 if verbose>=3: print('[bnlearn]> DynamicBayesianNetwork (DBN) can not be plot with Graphviz.') 1291 return None

KeyError: 'config'

When I look at

model_true.keys()

it shows

dict_keys(['model', 'adjmat'])

Thank you for your help.

erdogant commented 1 month ago

Thanks for the issue. It should be fixed now. The bug was checking a key in a dict that did not exist. update to the latest version with pip install -U bnlearn

smengel commented 1 month ago

Thank you, that worked.

I am now getting a warning on

G = bn.plot(model_true)

[bnlearn] >Set node properties. [bnlearn] >Set edge properties. [bnlearn] >Plot based on Bayesian model [bnlearn] >Warning: [graphviz_layout] layout not found. The layout [spring_layout] is used instead.

I did install graphviz on my computer and put it in the path for all users, and it says it is installed on pip.

If you don't know why, no big deal, but trying to get the nice graphs that you have.

Thank you for your help.

erdogant commented 1 month ago

To plot graphviz use this:

bn.plot_graphviz(model_true) I will update the docs

ps. I did find another issue related to your previous question. The latest version is now 0.10.2. Best to update again.

smengel commented 1 month ago

Thank you.

When using

bn_plot_graphviz(model_true)

it downloaded graphviz again and installed it in my appdata even though it is in the path of my environment variables.

Now it says:

[datazets] >INFO> System found: windows [datazets] >INFO> Graphviz path found in environment.

but won't plot anything.

erdogant commented 1 month ago

The output is a dotgraph now; you need to open it somehow. Some IDE do open it automatically though. Otherwise, you have also other options. The dotgraph is an object and you can use functions such as writing to pdf.

dotgraph = bn_plot_graphviz(model_true)
dotgraph.view(filename=r'c:/temp/dotgraph_bnlearn')
smengel commented 1 month ago

If I do this

G = bn.plot_graphviz(model_true)

and then this

G.view()

it makes a pdf in the working directory and shows it.

Thank you.