erdogant / bnlearn

Python library for learning the graphical structure of Bayesian networks, parameter learning, inference and sampling methods.
https://erdogant.github.io/bnlearn
Other
463 stars 45 forks source link

Issues on plotting bn model #51

Closed Mikcy1595 closed 2 years ago

Mikcy1595 commented 2 years ago

Hi,

I was following the example instructions, and can not plot the structural learning example 2 (A blank image). I dont know what are the reasons make this issue happen. I guess some packages are incompatible in my working environment. My working environment is : Python 3.8.12 matplotlib 3.5.1

1

Information from console: runfile('C:/Users/Administrator/.spyder-py3/temp.py', wdir='C:/Users/Administrator/.spyder-py3') [bnlearn] >Import dataset.. [bnlearn] >Computing best DAG using [hc] [bnlearn] >Set scoring type at [bic] [bnlearn]> Set node properties. [bnlearn]> Set edge properties. [bnlearn] >Plot based on Bayesian model Traceback (most recent call last):

File ~.spyder-py3\temp.py:14 in G = bn.plot(model)

File D:\Anaconda\envs\env_bnlearn\lib\site-packages\bnlearn\bnlearn.py:935 in plot _plot_static(model, params_static, nodelist, node_colors, node_sizes, G, pos, edge_colors, edge_weights)

File D:\Anaconda\envs\env_bnlearn\lib\site-packages\bnlearn\bnlearn.py:954 in _plot_static nx.draw_networkx_edges(G, pos, arrowstyle=params_static['arrowstyle'], arrowsize=params_static['arrowsize'], edge_color=edge_color, width=edge_weights, alpha=params_static['edge_alpha'])

File D:\Anaconda\envs\env_bnlearn\lib\site-packages\networkx\drawing\nx_pylab.py:889 in draw_networkx_edges edge_viz_obj = _draw_networkx_edges_fancy_arrow_patch()

File D:\Anaconda\envs\env_bnlearn\lib\site-packages\networkx\drawing\nx_pylab.py:867 in _draw_networkx_edges_fancy_arrow_patch ax.add_patch(arrow)

File ~\AppData\Roaming\Python\Python38\site-packages\matplotlib\axes_base.py:2033 in add_patch self._update_patch_limits(p)

File ~\AppData\Roaming\Python\Python38\site-packages\matplotlib\axes_base.py:2051 in _update_patch_limits vertices = patch.get_path().vertices

File ~\AppData\Roaming\Python\Python38\site-packages\matplotlib\patches.py:4128 in get_path _path, fillable = self.get_path_in_displaycoord()

File ~\AppData\Roaming\Python\Python38\site-packages\matplotlib\patches.py:4141 in get_path_in_displaycoord _path = self.get_connectionstyle()(posA, posB,

File D:\Anaconda\envs\env_bnlearn\lib\site-packages\networkx\drawing\nx_pylab.py:794 in _connectionstyle ret = base_connection_style(posA, posB, *args, **kwargs)

File ~\AppData\Roaming\Python\Python38\site-packages\matplotlib\patches.py:2493 in call shrunk_path = self._shrink(clipped_path, shrinkA, shrinkB)

File ~\AppData\Roaming\Python\Python38\site-packages\matplotlib\patches.py:2474 in _shrink left, path = split_path_inout(path, insideA)

File ~\AppData\Roaming\Python\Python38\site-packages\matplotlib\bezier.py:350 in split_path_inout ctl_points, command = next(path_iter)

StopIteration

erdogant commented 2 years ago

I'm going to look into this.

erdogant commented 2 years ago

Your versions looks good. I tried various examples but no errors. Can you show the code to reproduce the results?

plb41586 commented 2 years ago

I've got the same issue when running the sprinkler dataset example from the documentation. Python Version: Python 3.9.7 pip list returns: Package Version


argon2-cffi 21.3.0 argon2-cffi-bindings 21.2.0 asttokens 2.0.5 attrs 21.4.0 backcall 0.2.0 beautifulsoup4 4.10.0 bleach 4.1.0 bnlearn 0.6.3 certifi 2021.10.8 cffi 1.15.0 charset-normalizer 2.0.12 click 8.0.4 community 1.0.0b1 cycler 0.11.0 debugpy 1.6.0 decorator 5.1.1 defusedxml 0.7.1 df2onehot 1.0.1 entrypoints 0.4 executing 0.8.3 Flask 2.0.3 fonttools 4.31.2 fsspec 2022.2.0 funcsigs 1.0.2 idna 3.3 ipykernel 6.9.2 ipython 8.1.1 ipython-genutils 0.2.0 ipywidgets 7.7.0 ismember 1.0.1 itsdangerous 2.1.2 jedi 0.18.1 Jinja2 3.1.1 joblib 1.1.0 jsonpickle 2.1.0 jsonschema 4.4.0 jupyter-client 7.1.2 jupyter-core 4.9.2 jupyterlab-pygments 0.1.2 jupyterlab-widgets 1.1.0 kiwisolver 1.4.0 MarkupSafe 2.1.1 matplotlib 3.5.1 matplotlib-inline 0.1.3 mistune 0.8.4 nbclient 0.5.13 nbconvert 6.4.4 nbformat 5.2.0 nest-asyncio 1.5.4 networkx 2.7.1 notebook 6.4.10 numpy 1.22.3 packaging 21.3 pandas 1.4.1 pandocfilters 1.5.0 parso 0.8.3 patsy 0.5.2 pexpect 4.8.0 pgmpy 0.1.17 pickleshare 0.7.5 Pillow 9.0.1 pip 20.3.4 pkg-resources 0.0.0 prometheus-client 0.13.1 prompt-toolkit 3.0.28 psutil 5.9.0 ptyprocess 0.7.0 pure-eval 0.2.2 pycparser 2.21 Pygments 2.11.2 pyparsing 3.0.7 pypickle 1.1.0 pyrsistent 0.18.1 python-dateutil 2.8.2 pytz 2022.1 pyvis 0.1.9 pyzmq 22.3.0 requests 2.27.1 scikit-learn 1.0.2 scipy 1.8.0 Send2Trash 1.8.0 setuptools 44.1.1 six 1.16.0 sklearn 0.0 soupsieve 2.3.1 stack-data 0.2.0 statsmodels 0.13.2 tabulate 0.8.9 terminado 0.13.3 testpath 0.6.0 threadpoolctl 3.1.0 torch 1.11.0 tornado 6.1 tqdm 4.63.1 traitlets 5.1.1 typing-extensions 4.1.1 urllib3 1.26.9 wcwidth 0.2.5 webencodings 0.5.1 Werkzeug 2.0.3 wget 3.2 widgetsnbextension 3.6.0 OS is Pop OS

erdogant commented 2 years ago

I see. the issue is due to the new version of networkx. I released a new update

Update with: pip install -U bnlearn

Make sure to have networkx version>= 2.7.1 If required, force to new update too with: pip install -U networkx

paulaten commented 2 years ago

Hello!

I've updated both libraries and the error shown here by Mikcy1595https://github.com/erdogant/bnlearn/issues/51#issue-1167237566 doesn't appear, but neither does the plot.

My console run says: [bnlearn]> Set node properties. [bnlearn]> Set edge properties. [bnlearn] >Plot based on Bayesian model

and that's all. Is there something I'm missing?

Rest of my libraries are updated to the latest version.

My code loooks like this:

data = pd.DataFrame(data_dict) DAG = bn.structure_learning.fit(data) bn.plot(DAG, params_static={'layout': 'spectral_layout'})

Thank you so much!

erdogant commented 2 years ago

Have you tried the example code below already? I am not sure what your data looks like.

import bnlearn as bn
df = bn.import_example()
model = bn.structure_learning.fit(df)
G=bn.plot(model)

Can you show the information in the DAG? Maybe there is nothing to plot?

paulaten commented 2 years ago

I'm programming in PyCharm Community Edition 2021.3.3 with Python 3.8.10. Here is my data attatched:

velas_rsi.csv

When I run my code in the Python Console

imagen

import pandas as pd
import bnlearn as bn
data = pd.read_csv('velas_rsi.csv')
model = bn.structure_learning.fit(data)
G=bn.plot(model)

The Figure_1 appears, Figure_1

And my model looks like this:

{'model': <pgmpy.base.DAG.DAG object at 0x000002C50BB403A0>, 'model_edges': [('Vela', 'RSI')], 'adjmat': target   Vela    RSI
source              
Vela    False   True
RSI     False  False, 'config': {'method': 'hc', 'scoring': 'bic', 'black_list': None, 'white_list': None, 'bw_list_method': None, 'max_indegree': None, 'tabu_length': 100, 'epsilon': 0.0001, 'max_iter': 1000000.0, 'root_node': None, 'class_node': None, 'fixed_edges': set(), 'return_all_dags': False, 'verbose': 3}}

But, if I run a python script (.py) in PyCharm the figure doesn't appear. I've tried both executions with your import example and the same thing happens.

imagen

erdogant commented 2 years ago

Ok! The output looks good. You only have two variables in the dataset and an edge is detected. I see that the plot also appears now. I guess problem solved?

Rens660 commented 2 years ago

Hello erdogant,

I had similar issues but running pip install -U bnlearn updating it to version 0.7.0 worked. I get the plot now. networkx is indeed at version 2.7.1.