Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to use percolation_centrality from networkx

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

this isn't working and I actually have to copy paste the whole dictionary

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
like image 362
Python programmer Avatar asked Dec 09 '25 19:12

Python programmer


1 Answers

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',
)
like image 120
DRG Avatar answered Dec 11 '25 07:12

DRG



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!