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

bug 🐛? DAG of interactive plot differs from non-interactive plot #89

Closed thomasmooon closed 9 months ago

thomasmooon commented 10 months ago

dear @erdogant,

running the example below produces 2 plots, an non-interactive and an interactive. As you can see from the screenshots below their DAGs are very different. how can this be 😕 ? project dependencies are listed below.

import bnlearn as bn
# Load asia DAG
model_true = bn.import_DAG('asia')
# plot ground truth
G = bn.plot(model_true)
Gi = bn.plot(model_true, interactive=True)

DAG plot

image

DAG interactive plot

image

environment

image

erdogant commented 10 months ago

In the recent version I did speed up some transformation steps towards the adjacency matrix. I am going to look into it because this is unexpected.

thomasmooon commented 10 months ago

In addition I observed that when using a blacklist of type bw_list_method='edges', with e.g. black_list = [(node_1,node_2)], then is not considered as expected in the interactive plots because blacklisted edges are not taken into account for the interactive plot. The observed behaviour is, that the black_list seems to be ignored.

erdogant commented 10 months ago

I fixed the dynamic vs static plot first. Can you check whether this also works for you? pip install -U bnlearn

erdogant commented 9 months ago

@thomasmooon can you demonstrate an example of the black_list issue you mention? My intention was to exclude some of the edges/nodes prior to the modeling. So I would not expect that these show up during the plotting. But if they do show up, it is not as expected.

thomasmooon commented 9 months ago

@erdogant pip install -U bnlearn successfull upgrading from 0.8.2 to 0.8.3, please find details below. Seems fine.

pip install -U bnlearn output

Requirement already satisfied: bnlearn in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (0.8.2)
Collecting bnlearn
  Downloading bnlearn-0.8.3-py3-none-any.whl (68 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 68.9/68.9 kB 1.6 MB/s eta 0:00:00
Requirement already satisfied: pgmpy>=0.1.18 in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from bnlearn) (0.1.24)
Requirement already satisfied: networkx>=2.7.1 in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from bnlearn) (3.2.1)
Requirement already satisfied: matplotlib>=3.3.4 in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from bnlearn) (3.8.1)
Requirement already satisfied: numpy>=1.24.1 in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from bnlearn) (1.26.1)
Requirement already satisfied: pandas==1.5.3 in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from bnlearn) (1.5.3)
Requirement already satisfied: tqdm in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from bnlearn) (4.66.1)
Requirement already satisfied: ismember in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from bnlearn) (1.0.2)
Requirement already satisfied: scikit-learn in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from bnlearn) (1.3.2)
Requirement already satisfied: funcsigs in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from bnlearn) (1.0.2)
Requirement already satisfied: statsmodels in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from bnlearn) (0.14.0)
Requirement already satisfied: python-louvain in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from bnlearn) (0.16)
Requirement already satisfied: packaging in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from bnlearn) (23.2)
Requirement already satisfied: df2onehot in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from bnlearn) (1.0.6)
Requirement already satisfied: fsspec in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from bnlearn) (2023.10.0)
Requirement already satisfied: pypickle in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from bnlearn) (1.1.0)
Requirement already satisfied: tabulate in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from bnlearn) (0.9.0)
Requirement already satisfied: ipywidgets in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from bnlearn) (8.1.1)
Requirement already satisfied: datazets in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from bnlearn) (0.1.9)
Requirement already satisfied: python-dateutil>=2.8.1 in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from pandas==1.5.3->bnlearn) (2.8.2)
Requirement already satisfied: pytz>=2020.1 in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from pandas==1.5.3->bnlearn) (2023.3.post1)
Requirement already satisfied: contourpy>=1.0.1 in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from matplotlib>=3.3.4->bnlearn) (1.2.0)
Requirement already satisfied: cycler>=0.10 in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from matplotlib>=3.3.4->bnlearn) (0.12.1)
Requirement already satisfied: fonttools>=4.22.0 in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from matplotlib>=3.3.4->bnlearn) (4.44.0)
Requirement already satisfied: kiwisolver>=1.3.1 in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from matplotlib>=3.3.4->bnlearn) (1.4.5)
Requirement already satisfied: pillow>=8 in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from matplotlib>=3.3.4->bnlearn) (10.1.0)
Requirement already satisfied: pyparsing>=2.3.1 in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from matplotlib>=3.3.4->bnlearn) (3.1.1)
Requirement already satisfied: scipy in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from pgmpy>=0.1.18->bnlearn) (1.11.3)
Requirement already satisfied: torch in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from pgmpy>=0.1.18->bnlearn) (2.1.0)
Requirement already satisfied: joblib in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from pgmpy>=0.1.18->bnlearn) (1.3.2)
Requirement already satisfied: opt-einsum in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from pgmpy>=0.1.18->bnlearn) (3.3.0)
Requirement already satisfied: requests in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from datazets->bnlearn) (2.31.0)
Requirement already satisfied: comm>=0.1.3 in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from ipywidgets->bnlearn) (0.2.0)
Requirement already satisfied: ipython>=6.1.0 in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from ipywidgets->bnlearn) (8.17.2)
Requirement already satisfied: traitlets>=4.3.1 in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from ipywidgets->bnlearn) (5.13.0)
Requirement already satisfied: widgetsnbextension~=4.0.9 in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from ipywidgets->bnlearn) (4.0.9)
Requirement already satisfied: jupyterlab-widgets~=3.0.9 in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from ipywidgets->bnlearn) (3.0.9)
Requirement already satisfied: threadpoolctl>=2.0.0 in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from scikit-learn->bnlearn) (3.2.0)
Requirement already satisfied: patsy>=0.5.2 in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from statsmodels->bnlearn) (0.5.3)
Requirement already satisfied: decorator in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from ipython>=6.1.0->ipywidgets->bnlearn) (5.1.1)
Requirement already satisfied: jedi>=0.16 in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from ipython>=6.1.0->ipywidgets->bnlearn) (0.19.1)
Requirement already satisfied: matplotlib-inline in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from ipython>=6.1.0->ipywidgets->bnlearn) (0.1.6)
Requirement already satisfied: prompt-toolkit!=3.0.37,<3.1.0,>=3.0.30 in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from ipython>=6.1.0->ipywidgets->bnlearn) (3.0.39)
Requirement already satisfied: pygments>=2.4.0 in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from ipython>=6.1.0->ipywidgets->bnlearn) (2.16.1)
Requirement already satisfied: stack-data in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from ipython>=6.1.0->ipywidgets->bnlearn) (0.6.3)
Requirement already satisfied: exceptiongroup in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from ipython>=6.1.0->ipywidgets->bnlearn) (1.1.3)
Requirement already satisfied: pexpect>4.3 in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from ipython>=6.1.0->ipywidgets->bnlearn) (4.8.0)
Requirement already satisfied: six in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from patsy>=0.5.2->statsmodels->bnlearn) (1.16.0)
Requirement already satisfied: charset-normalizer<4,>=2 in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from requests->datazets->bnlearn) (3.3.2)
Requirement already satisfied: idna<4,>=2.5 in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from requests->datazets->bnlearn) (3.4)
Requirement already satisfied: urllib3<3,>=1.21.1 in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from requests->datazets->bnlearn) (2.0.7)
Requirement already satisfied: certifi>=2017.4.17 in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from requests->datazets->bnlearn) (2023.5.7)
Requirement already satisfied: filelock in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from torch->pgmpy>=0.1.18->bnlearn) (3.13.1)
Requirement already satisfied: typing-extensions in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from torch->pgmpy>=0.1.18->bnlearn) (4.8.0)
Requirement already satisfied: sympy in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from torch->pgmpy>=0.1.18->bnlearn) (1.12)
Requirement already satisfied: jinja2 in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from torch->pgmpy>=0.1.18->bnlearn) (3.1.2)
Requirement already satisfied: nvidia-cuda-nvrtc-cu12==12.1.105 in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from torch->pgmpy>=0.1.18->bnlearn) (12.1.105)
Requirement already satisfied: nvidia-cuda-runtime-cu12==12.1.105 in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from torch->pgmpy>=0.1.18->bnlearn) (12.1.105)
Requirement already satisfied: nvidia-cuda-cupti-cu12==12.1.105 in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from torch->pgmpy>=0.1.18->bnlearn) (12.1.105)
Requirement already satisfied: nvidia-cudnn-cu12==8.9.2.26 in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from torch->pgmpy>=0.1.18->bnlearn) (8.9.2.26)
Requirement already satisfied: nvidia-cublas-cu12==12.1.3.1 in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from torch->pgmpy>=0.1.18->bnlearn) (12.1.3.1)
Requirement already satisfied: nvidia-cufft-cu12==11.0.2.54 in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from torch->pgmpy>=0.1.18->bnlearn) (11.0.2.54)
Requirement already satisfied: nvidia-curand-cu12==10.3.2.106 in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from torch->pgmpy>=0.1.18->bnlearn) (10.3.2.106)
Requirement already satisfied: nvidia-cusolver-cu12==11.4.5.107 in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from torch->pgmpy>=0.1.18->bnlearn) (11.4.5.107)
Requirement already satisfied: nvidia-cusparse-cu12==12.1.0.106 in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from torch->pgmpy>=0.1.18->bnlearn) (12.1.0.106)
Requirement already satisfied: nvidia-nccl-cu12==2.18.1 in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from torch->pgmpy>=0.1.18->bnlearn) (2.18.1)
Requirement already satisfied: nvidia-nvtx-cu12==12.1.105 in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from torch->pgmpy>=0.1.18->bnlearn) (12.1.105)
Requirement already satisfied: triton==2.1.0 in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from torch->pgmpy>=0.1.18->bnlearn) (2.1.0)
Requirement already satisfied: nvidia-nvjitlink-cu12 in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from nvidia-cusolver-cu12==11.4.5.107->torch->pgmpy>=0.1.18->bnlearn) (12.3.52)
Requirement already satisfied: parso<0.9.0,>=0.8.3 in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from jedi>=0.16->ipython>=6.1.0->ipywidgets->bnlearn) (0.8.3)
Requirement already satisfied: ptyprocess>=0.5 in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from pexpect>4.3->ipython>=6.1.0->ipywidgets->bnlearn) (0.7.0)
Requirement already satisfied: wcwidth in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from prompt-toolkit!=3.0.37,<3.1.0,>=3.0.30->ipython>=6.1.0->ipywidgets->bnlearn) (0.2.9)
Requirement already satisfied: MarkupSafe>=2.0 in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from jinja2->torch->pgmpy>=0.1.18->bnlearn) (2.0.1)
Requirement already satisfied: executing>=1.2.0 in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from stack-data->ipython>=6.1.0->ipywidgets->bnlearn) (2.0.1)
Requirement already satisfied: asttokens>=2.1.0 in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from stack-data->ipython>=6.1.0->ipywidgets->bnlearn) (2.4.1)
Requirement already satisfied: pure-eval in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from stack-data->ipython>=6.1.0->ipywidgets->bnlearn) (0.2.2)
Requirement already satisfied: mpmath>=0.19 in /home/tlinden/.conda/envs/bnlearn_eicu/lib/python3.10/site-packages (from sympy->torch->pgmpy>=0.1.18->bnlearn) (1.3.0)
Installing collected packages: bnlearn
  Attempting uninstall: bnlearn
    Found existing installation: bnlearn 0.8.2
    Uninstalling bnlearn-0.8.2:
      Successfully uninstalled bnlearn-0.8

non-interactive plot screenshot

image

interactive plot screenshot

image

thomasmooon commented 9 months ago

@thomasmooon can you demonstrate an example of the black_list issue you mention? My intention was to exclude some of the edges/nodes prior to the modeling. So I would not expect that these show up during the plotting. But if they do show up, it is not as expected.

Unfortunately I am not able to assist in troubleshooting any further as I had to move to another framework due to the issues observed and deadlines with a big DEAD.