Skip to content

Instantly share code, notes, and snippets.

@tpoisot
Created February 13, 2013 04:02
Show Gist options
  • Save tpoisot/4942162 to your computer and use it in GitHub Desktop.
Save tpoisot/4942162 to your computer and use it in GitHub Desktop.
Node contribution to network modularity
import networkx as nx
import scipy as sp
import numpy as np
import matplotlib.pyplot as plt
import community
def removeNode(web,node):
tempWeb = nx.DiGraph()
tempWeb.add_nodes_from(web.nodes())
tempWeb.add_edges_from(web.edges())
tempWeb.remove_nodes_from([node])
return tempWeb
def getModularity(web):
D = web.to_undirected()
dendo = community.generate_dendogram(D, None)
partition = community.partition_at_level(dendo,len(dendo)-1)
return community.modularity(partition, D)
G = nx.read_edgelist('network.txt')
orig_mod = getModularity(G)
print orig_mod
delta_mod = [orig_mod-getModularity(removeNode(G,n)) for n in G]
print delta_mod
plt.plot(np.sort(delta_mod),'ok')
plt.show()
plt.axis('off')
pos = nx.spring_layout(G,iterations=5000)
nx.draw_networkx_edges(G,pos,alpha=0.07)
nodes = nx.draw_networkx_nodes(G,pos,node_size=20,node_color=delta_mod,cmap=plt.cm.Spectral,linewidths=0.0,weight=None)
plt.sci(nodes)
plt.colorbar()
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment