Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KeyError on call to bn.parameter_learning.fit #105

Closed
smengel opened this issue Oct 23, 2024 · 7 comments
Closed

KeyError on call to bn.parameter_learning.fit #105

smengel opened this issue Oct 23, 2024 · 7 comments

Comments

@smengel
Copy link

smengel commented Oct 23, 2024

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
Copy link
Author

smengel commented Oct 23, 2024

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
Copy link
Owner

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
Copy link
Author

smengel commented Oct 23, 2024

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
Copy link
Owner

erdogant commented Oct 23, 2024

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
Copy link
Author

smengel commented Oct 23, 2024

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
Copy link
Owner

erdogant commented Oct 23, 2024

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
Copy link
Author

smengel commented Oct 23, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants