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

ValueError in Interactive Plot: [tooltip text] must be of same length as the number of nodes #98

Open KevinHooah opened 5 months ago

KevinHooah commented 5 months ago

Hello,

When I am going to plot the interactive figure (the static figure works fine), I get the following errors:

ValueError                                Traceback (most recent call last)
[<ipython-input-20-bda0a8197673>](https://localhost:8080/#) in <cell line: 1>()
----> 1 bn.plot(model2_update, title = 'DAG for SJV', interactive=True)

2 frames
[/usr/local/lib/python3.10/dist-packages/bnlearn/bnlearn.py](https://localhost:8080/#) in plot(model, pos, scale, interactive, title, node_color, node_size, node_properties, edge_properties, params_interactive, params_static, verbose)
   1096         print("number of node: ", len(nodelist), "number of tip", len(tooltip))
   1097         # Make interactive plot
-> 1098         fig = _plot_interactive(params_interactive,
   1099                                 nodelist,
   1100                                 node_colors,

[/usr/local/lib/python3.10/dist-packages/bnlearn/bnlearn.py](https://localhost:8080/#) in _plot_interactive(params_interactive, nodelist, node_colors, node_sizes, edgelist, edge_colors, edge_weights, title, tooltip, verbose)
   1204     _, IB = ismember([*d3.D3graph.node_properties.keys()], nodelist)
   1205 
-> 1206     d3.D3graph.set_node_properties(tooltip=np.array(tooltip)[IB],
   1207                                    size=np.array(node_sizes)[IB],
   1208                                    color=np.array(node_colors)[IB],

[/usr/local/lib/python3.10/dist-packages/d3graph/d3graph.py](https://localhost:8080/#) in set_node_properties(self, label, marker, tooltip, color, opacity, size, edge_color, edge_size, fontcolor, fontsize, cmap, scaler, minmax)
    449         else:
    450             tooltip = np.array([''] * nodecount)
--> 451         if len(tooltip) != nodecount: raise ValueError("[tooltip text] must be of same length as the number of nodes")

    452 
    453         # ############ Set node color #############

ValueError: [tooltip text] must be of same length as the number of nodes

I check the number of node and the number of tip in the bnlearn.py's plot function, before it calls fig = _plot_interactive, the number of nodes equals the number of tip.

I also get these output before the error pops out, incase it is helpful:

[d3blocks] >INFO> Cleaning edge_properties and config parameters..
[d3blocks] >INFO> Converting source-target into adjacency matrix..
[d3blocks] >INFO> Making the matrix symmetric..
[d3blocks] >INFO> Set directed=True to see the markers!
[d3blocks] >INFO> Keep only edges with weight>0
[d3blocks] >INFO> Converting source-target into adjacency matrix..
[d3blocks] >INFO> Making the matrix symmetric..
[d3blocks] >INFO> Converting adjacency matrix into source-target..
[d3blocks] >INFO> Number of unique nodes: 6
[d3blocks] >INFO> Slider range is set to [0, 10]
[d3blocks] >INFO> Write to path: [/tmp/tmpyfm91v_7/d3graph.html]
[d3blocks] >INFO> File already exists and will be overwritten: [/tmp/tmpyfm91v_7/d3graph.html]
[bnlearn] >Set node properties.
[bnlearn]> Set edge weights based on the [chi_square] test statistic.
[bnlearn] >Converting source-target into adjacency matrix..
[bnlearn] >Making the matrix symmetric..
[bnlearn] >Set edge properties.
[d3blocks] >INFO> Keep only edges with weight>0
[d3blocks] >INFO> Converting source-target into adjacency matrix..
[d3blocks] >INFO> Making the matrix symmetric..

Thank you for your help in advance.

erdogant commented 5 months ago

Thank you for mentioning this. I fixed this issue before. Can you force update to the latest version of bnlearn and d3blocks?

pip install -U bnlearn pip install -U d3blocks

I created a new release of bnlearn where I included d3blocks with a minimum version of 1.4.9

mich415 commented 5 months ago

I'm seeing the same issue (same output as original post) even though it appears I have the latest versions of bnlearn (0.8.5) and d3blocks (1.4.9). Do you have any idea why the issue may persist?

erdogant commented 5 months ago

mmm. I need more information. Which python version are u using and does it brake also in toy examples?

mich415 commented 5 months ago

python version is 3.12.2, and the examples for the interactive plots in the docs do work.

erdogant commented 5 months ago

Can you shown an example with data where it does not work

erdogant commented 4 months ago

Do you maybe have not-unique node names?

Loominarty commented 1 month ago

Hello,

Having the exact same problem. Nodes have unique names in my case.

Here is a snapshot of my data: Engine Status Speed Status_Alpha Alpha_Temp Status_Beta
0 0 0.25 0 170 0
1 0 0.21 0 170 0
2 0 0.25 0 170 0
3 0 0.21 0 170 0
4 0 0.21 0 170 0

Beta _Temp   Status_Delta  Delta_Temp Status_Echo  Echo_Temp  

0 190 0 170 0 40
1 190 0 170 0 40
2 190 0 170 0 40
3 190 0 170 0 40
4 190 0 170 0 40

Mix_Gas Mix_98 Mix_95 Cost Energy
0 0 1 0 0.64 8.64
1 0 1 0 0.82 6.07
2 0 1 0 0.90 5.96
3 0 1 0 1.14 4.89
4 0 1 0 0.95 5.11

As you can see, it is a mix of discrete and continuous data (that I discretized)

erdogant commented 1 month ago

I have the feeling it has to do with the names. Maybe underscores or other special characters are replaced somewhere to prevent errors in the final results which leads to this error but I do need more information.

erdogant commented 1 month ago

I resolved the issue after several iterations. The error in the interactive plot was due to a mismatch in how node names were cleaned: they were sanitized in d3blocks but not in bnlearn's interactive plot, leading to inconsistencies. Update to the latest version and try again!

pip install -U bnlearn

Loominarty commented 1 month ago

The error does not show anymore ! Seems to be all good.