erdogant / bnlearn

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

"IndexError: tuple index out of range" #24

Closed Chilkos closed 3 years ago

Chilkos commented 3 years ago

I've just tried to use bnlearn for the first time, and am trying to run the example code from the readme. However, it gives an error when running G = bn.plot(model)

Python 3.8.8 (default, Apr 13 2021, 15:08:03) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import bnlearn as bn
>>> df = bn.import_example()
[bnlearn] >Import dataset..
>>> model = bn.structure_learning.fit(df)
[bnlearn] >Computing best DAG using [hc]
[bnlearn] >Set scoring type at [bic]
>>> G = bn.plot(model)
[bnlearn] >Plot based on BayesianModel
Traceback (most recent call last):
  File "C:\Users\JasperKueppers\anaconda3\envs\BNLEARN\lib\site-packages\networkx\utils\decorators.py", line 396, in _random_state
    random_state_arg = args[random_state_index]
IndexError: tuple index out of range

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\JasperKueppers\anaconda3\envs\BNLEARN\lib\site-packages\bnlearn\bnlearn.py", line 600, in plot
    pos = network.graphlayout(model, pos=pos, scale=scale, layout=layout, verbose=verbose)
  File "C:\Users\JasperKueppers\anaconda3\envs\BNLEARN\lib\site-packages\bnlearn\helpers\network.py", line 405, in graphlayout
    pos = nx.fruchterman_reingold_layout(model, scale=scale, iterations=50)
  File "C:\Users\JasperKueppers\anaconda3\envs\BNLEARN\lib\site-packages\decorator.py", line 231, in fun
    return caller(func, *(extras + args), **kw)
  File "C:\Users\JasperKueppers\anaconda3\envs\BNLEARN\lib\site-packages\networkx\utils\decorators.py", line 400, in _random_state
    raise nx.NetworkXError("random_state_index is incorrect") from e
networkx.exception.NetworkXError: random_state_index is incorrect

This is in a fresh Anaconda installation (Python 3.8.8), with pgmpy and pip installed as per the quickstart/installation guide (using conda and pip respectively)

erdogant commented 3 years ago

Im going to check it. Issues with networkx is most of the time versioning stuf. So you can try python 3.6 or 3.8 and the recommended networkx version (see requirements)

erdogant commented 3 years ago

I did check the working with python 3.8.8 and it seems to work fine. Maybe your networkx version is <2.5?

Can you check the following:

import networkx
print(networkx.__version__)
# This should be >= 2.5

Force update networkx with: pip install -U networkx

Chilkos commented 3 years ago

Seems that I was on version 2.5 of networkx, and updating installed version 2.5.1. Running the example code works now and shows the graph, thanks for the help!