Reputation: 75
quick question. I have used numpy to generate a matrix from an excel file that was changed into a csv. I then used this matrix G to calculate some graph metrics. At first I tried eigenvector centrality which worked absolutely fine but I haven't had such success with percolation centrality. I am getting KeyError: 0 when I attempt to do so. Checking the networkx documentation, I don't think I am making any obvious errors but please do help.
import networkx as nx
import numpy as np
mydata = np.genfromtxt('brain10.csv', delimiter=',')
G = nx.from_numpy_matrix(mydata)
centrality = nx.percolation_centrality(G, weight="weight")
import parcellation_dictionary from parcellation_dictionary.py
result = {k:centrality[v] for v,k in parcellation_dictionary.items()}
print(result)
sorted_by_value = sorted(result.items(), reverse= True, key=lambda kv: kv[1])
print(sorted_by_value)
The error I am getting:
"C:\Program Files (x86)\Python37-32\python.exe" "C:/Users/bob/PycharmProjects/EVC trial/evc trial no 138.py"
Traceback (most recent call last):
File "C:/Users/bob/PycharmProjects/EVC trial/evc trial no 138.py", line 9, in <module>
centrality = nx.percolation_centrality(G, weight="weight")
File "C:\Program Files (x86)\Python37-32\lib\site-packages\networkx\algorithms\centrality\percolation.py", line 109, in percolation_centrality
states, p_sigma_x_t)
File "C:\Program Files (x86)\Python37-32\lib\site-packages\networkx\algorithms\centrality\percolation.py", line 129, in _accumulate_percolation
pw_s_w = states[s] / (p_sigma_x_t - states[w])
KeyError: 0
Process finished with exit code 1
Upvotes: 3
Views: 1044
Reputation: 35
You need to set the node attributes first. Try this approach.
nx.set_node_attributes(G, 0.1, 'percolation')
percolation_cent_dict = nx.percolation_centrality(
G=G,
attribute='percolation',
)
Upvotes: 2