'How to calculate local and global efficiency on igraph using Python and edges/nodes from CSV file

I have generated a graph through OSMNX and now I have one list of edges and one list of nodes, both in CSV

nodes.csv contain the columns 'id', 'x_coordinate' and 'y_coordinate' edges.csv contain the columns 'u', 'v, and 'length'; where 'u' and 'v' refer to the connected nodes

Now I am trying to use igraph to calculate the efficiency of each node (nodal efficiency) and also the global efficiency

Here's what I tried so far, but results are not what I expected.

import numpy as np
import igraph
import pandas as pd
np.seterr(divide='ignore')
edgefields = ['u','v','length']
edgelist = pd.read_csv('edges.csv', skipinitialspace=True, usecols=edgefields)
g = igraph.Graph.DataFrame(edgelist)
    
weight = edgelist["length"][:]

def nodal_eff(g):

   weights = weight
   sp = (1.0 / np.array(g.shortest_paths_dijkstra(weights=weights)))
   np.fill_diagonal(sp,0)
   N=sp.shape[0]
   ne= (1.0/(N-1)) * np.apply_along_axis(sum,0,sp)
   return ne

eff = nodal_eff(g)
print(eff)
print(np.mean(eff))


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source