In [1]:
import networkx as nx
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from glob import glob
In [2]:
# read .gml file
graph = nx.read_gml('article0.gml')
In [3]:
# read pajek file
# graph = nx.read_pajek('article1.net')
In [4]:
# plot spring layout
plt.figure(figsize=(12,12))
nx.draw_spring(graph, arrows=True, with_labels=True)
In [5]:
info = nx.info(graph)
print info
In [6]:
plt.figure(figsize=(12,12))
nx.draw_circular(graph, arrows=True, with_labels=True)
In [7]:
# returns a list of frequencies of degrees;
# The degree values are the index in the list.
print nx.degree_histogram(graph)
In [8]:
degree_sequence=sorted(nx.degree(graph).values(),reverse=True) # degree sequence
#print "Degree sequence", degree_sequence
dmax=max(degree_sequence)
plt.loglog(degree_sequence,'b-',marker='o')
plt.title("Degree rank plot")
plt.ylabel("degree")
plt.xlabel("rank")
# draw graph in inset
plt.axes([0.45,0.45,0.45,0.45])
Gcc=sorted(nx.connected_component_subgraphs(graph), key = len, reverse=True)[0]
pos=nx.spring_layout(Gcc)
plt.axis('off')
nx.draw_networkx_nodes(Gcc,pos,node_size=20)
nx.draw_networkx_edges(Gcc,pos,alpha=0.4)
plt.show()
In [ ]:
density = nx.density(graph)
print "Density =", density
In [ ]:
# get all the values of the dictionary, this returns a list of centrality scores
# turn the list into a numpy array
# take the mean of the numpy array
deg_cen = np.array(nx.degree_centrality(graph).values()).mean()
print "Degree centrality =", deg_cen
In [ ]:
clo_cen = np.array(nx.closeness_centrality(graph).values()).mean()
print "Closeness centrality =", clo_cen
In [ ]:
nx.betweenness_centrality(graph)
bet_cen = np.array(nx.betweenness_centrality(graph).values()).mean()
print "Betweenness centrality =", bet_cen
In [ ]:
# graph must be connected
# nx.current_flow_betweenness_centrality(graph)
In [ ]:
deg_ac = nx.degree_assortativity_coefficient(graph)
print "Degree assortativity coefficient =", deg_ac
In [ ]:
deg_pcc = nx.degree_pearson_correlation_coefficient(graph)
print "Degree pearson correlation coefficient =", deg_pcc
In [ ]:
## Clustering coefficient
# (cannot be multigraph)
# nx.average_clustering(graph)
## Condensation
# nx.condensation(graph)
In [ ]:
#nx.edge_connectivity(graph)
#nx.node_connectivity(graph)
avg_node_con = nx.average_node_connectivity(graph)
print "Average node connectivity =", avg_node_con
In [ ]:
# example
G = nx.cycle_graph(3)
nx.draw(G)
nx.closeness_vitality(G)
In [ ]:
#nx.closeness_vitality(graph)
In [ ]:
# intersection_all()
# return a new graph that contains only the edges that exist in all graphs
# all supplied graphs must have the same node set
In [ ]:
print info
print "Density =", density
print "Degree centrality =", deg_cen
print "Closeness centrality =", clo_cen
print "Betweenness centrality =", bet_cen
print "Degree assortativity coefficient =", deg_ac
print "Degree pearson correlation coefficient =", deg_pcc
print "Average node connectivity =", avg_node_con
#print "Closeness vitality ="
In [ ]: