I have a dataset not well formatted as it has the following columns
Source Target Label_Source Label_Target
E N 0.0 0.0
A B 1.0 1.0
A C 1.0 0.0
A D 1.0 0.0
A N 1.0 0.0
S G 0.0 0.0
S L 0.0 1.0
S C 0.0 0.0
Label_Source and Label_Target are nodes attributes Label_Source is a source's attribute while Label_Target is a target's attribute. Trying to replicate the following project: https://www.fatalerrors.org/a/python-networkx-learning-notes.html, I have encountered some errors, including a KeyError due to Label_Source. As explained in this answer: KeyError after re-running the (same) code, the problem seems caused by a wrong assignment in edge/node attributes, as the code is reading Label_Source as edge's attribute. I would like to replicate the project, as I said, so any format that could make it possible would be acceptable. However, I would really appreciate someone could explain (not only show) how to fix the issue as it is not clear to me what is driving it. What I have done so far is shown below:
import networkx as nx
from matplotlib import pyplot as plt
import pandas as pd
G = nx.from_pandas_edgelist(filtered, 'Source', 'Target', edge_attr=True)
df_pos = nx.spring_layout(G,k = 0.3)
nx.draw_networkx(G, df_pos)
plt.show()
node_color = [
'#1f78b4' if G.nodes[v]["Label_Source"] == 0 # actually this assignment should just Label and it should include also Target, so the whole list of nodes and their labels. A way to address this would be to select all distinct nodes in the network and their labels
else '#33a02c' for v in G]
# Iterate through all edges
for v, w in G.edges:
if G.nodes[v]["Label_Source"] == G.nodes[w]["Label_Source"]: # this should refer to all the Labels
G.edges[v, w]["internal"] = True
else:
G.edges[v, w]["internal"] = False
If you could help me to understand how to fix the issue and replicate the code it would be great. I guess the error is also in trying to iterate through strings and not indices.
from KeyError caused by a wrong attribute assignment within the network?
No comments:
Post a Comment