2

I am in a very wired situation. So, I am trying to visualize a graph using Bokeh. The graph has nodes as nodes IDs, which are connected by sticks that have length and also IDs. For example, I have the following DataFrame of sticks:

import pandas as pd
import networkx as nx

d = {'Node 1': [56416, 56416], 'Node 2': [76449, 76448], 'len': [45, 146], 'Stick ID': [25788, 25789]}
df = pd.DataFrame(data=d)    

which looks like:

df
Out[82]: 
   Node 1  Node 2  len  Stick ID
0   56416   76449   45     25788
1   56416   76448  146     25789

Next, I'm buliding a graph which I want to visualize using Bokeh.

G_1=nx.from_pandas_edgelist(df, 'Node 1', 'Node 2', ['Stick ID', 'len']) # Length and Stick ID are wights for the edges

from bokeh.io import output_file, show
from bokeh.plotting import figure, from_networkx

plot = figure(title="Networkx Integration Demonstration", x_range=(-10.1,10.1), y_range=(-10.1,10.1),
              tools="", toolbar_location=None)

graph = from_networkx(G_1, nx.spring_layout, scale=2, center=(0,0))
plot.renderers.append(graph)

output_file("networkx_graph.html")
show(plot)

And it works, I'm getting the following picture enter image description here

However! When I try to visualize a DataFrame with all my data, I'm getting the following error when calling from_network:

ValueError: failed to validate StaticLayoutProvider(id='5319', ...).graph_layout: expected an element of Dict(Either(String, Int), Seq(Any)), got {55331.0: array([0.63049136, 0.20157234]), 76996.0: array([0.74836386, 0.17916439]),..... and so on the whole dictionary of my data

The DataFrame with the data has the exactly identical structure!

df.head()
Out[88]: 
    Node 1   Node 2        len  Stick ID
0  55331.0  76996.0  15.269983   25594.0
1  55844.0  60447.0   4.457858   25595.0
2  55624.0  76989.0  21.560522   25736.0
3  54578.0  76990.0  28.195875   25737.0
4  56416.0  76491.0  54.661070   25788.0

When I'm just plotting my Data with matplotlib. I have no problem:

pos = graphviz_layout(G_1, prog='neato')
fig, ax = plt.subplots()
nx.draw_networkx(G_1, pos=pos)
fig.tight_layout()
fig.show()

enter image description here

Can someone help to figure out what wrong I'm doing with Bokeh?

There was a similar question before, but there is no solution posted.

Network graph error: failed to validate StaticLayoutProvider(id='1058', ...).graph_layout:

1 Answers1

0

It's a speculation only since there is no reproducible code, but one potential source of the error is the float node IDs. This could happen because of a missing value somewhere in the data frame. A quick fix would be to run:

df = df.dropna().astype({'Node 1': 'int', 'Node 2': 'int'})
# the rest of your code, starting with `nx.from_pandas_edgelist` line

Note that for confidence in results one would want to investigate where the missing values originated from and make appropriate adjustments.

SultanOrazbayev
  • 14,900
  • 3
  • 16
  • 46
  • 1
    I removed all the missining values before in the data. But converting nodes IDs to int format helped!! Thank you man. But can you maybe elaborate what is the problem with float? – errenmike1806 Nov 01 '22 at 12:12
  • Well, in a few words, it's because we are assigning numeric labels to nodes, so we are basically counting (node 1, node 2, node 3, ...). – SultanOrazbayev Nov 01 '22 at 12:19