I am trying to create an undirected graph from a DataFrame formatted_unique_edges
- the 'weight'
column will purely be used for edge colouring in downstream visualisation using plotly:
source target weight
0 protein_2 protein_3 3
1 protein_2 protein_6 2
2 protein_3 protein_6 2
3 protein_2 protein_4 2
4 protein_2 protein_5 2
5 protein_3 protein_4 2
6 protein_3 protein_5 2
7 protein_4 protein_5 2
8 protein_4 protein_6 1
9 protein_5 protein_6 1
The first lines in the linked plotly example, which I am trying to emulate, is:
G = nx.random_geometric_graph(200, 0.125)
edge_x = []
edge_y = []
for edge in G.edges():
x0, y0 = G.nodes[edge[0]]['pos']
x1, y1 = G.nodes[edge[1]]['pos']
edge_x.append(x0)
edge_x.append(x1)
edge_x.append(None)
edge_y.append(y0)
edge_y.append(y1)
edge_y.append(None)
I first convert formatted_unique_edges
to a Graph, then try to emulate the code above, with some diagnostic print statements:
G = nx.from_pandas_edgelist(formatted_unique_edges,
edge_attr=True)
#also tried G = nx.random_geometric_graph(200, 0.125) as per plotly example
edge_x = []
edge_y = []
for edge in G.edges():
print(edge) #('proteinN', 'proteinM')
print(G.nodes[edge[0]]) #{}
print(G.nodes[edge[1]]) #{}
x0, y0 = G.nodes[edge[0]]['pos']
#####
#THROWS KeyError: 'pos' if G is from formatted_unique_edges
#####
#prints {'pos': [float, float]} if G is from nx.random_geometric_graph
x1, y1 = G.nodes[edge[1]]['pos']
edge_x.append(x0)
edge_x.append(x1)
edge_x.append(None)
edge_y.append(y0)
edge_y.append(y1)
edge_y.append(None)
As stated in the comments, I am getting a KeyError
from G.nodes[edge[0]]['pos']
. I had a look in the spyder variable explorer and G.nodes._nodes
from nx.random_geometric_graph
has the format:
{0 : {'pos' : [pos_float, pos_float]},
1 : {'pos' : [pos_float, pos_float]},
...
199 : {'pos' : [pos_float, pos_float]}
}
Whereas as G.nodes._nodes
from formatted_unique_edges
has the format:
{'protein_2' : {},
'protein_3' : {},
'protein_4' : {},
'protein_5' : {},
'protein_6' : {}}
This all suggests I am making my Graph
object from formatted_unique_edges
incorrectly with nx.from_pandas_edgelist
- can someone advise how I should be doing it?
Thanks! Tim