Plot graph and basic analysis

  • MultiGraph
  • normal template for reading gml file
  • for undirected multigraph

article1.gml


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


Name: 
Type: MultiDiGraph
Number of nodes: 140
Number of edges: 147
Average in degree:   1.0500
Average out degree:   1.0500

In [6]:
plt.figure(figsize=(12,12))
nx.draw_circular(graph, arrows=True, with_labels=True)


Degree histogram

Return a list of the frequency of each degree value


In [7]:
# returns a list of frequencies of degrees;
# The degree values are the index in the list.
print nx.degree_histogram(graph)


[0, 77, 32, 7, 11, 6, 3, 1, 0, 1, 1, 0, 0, 0, 1]

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()


---------------------------------------------------------------------------
NetworkXNotImplemented                    Traceback (most recent call last)
<ipython-input-8-2a8db047430c> in <module>()
     10 # draw graph in inset
     11 plt.axes([0.45,0.45,0.45,0.45])
---> 12 Gcc=sorted(nx.connected_component_subgraphs(graph), key = len, reverse=True)[0]
     13 pos=nx.spring_layout(Gcc)
     14 plt.axis('off')

<decorator-gen-158> in connected_component_subgraphs(G, copy)

/usr/local/lib/python2.7/site-packages/networkx/utils/decorators.pyc in _not_implemented_for(f, *args, **kwargs)
     64         if match:
     65             raise nx.NetworkXNotImplemented('not implemented for %s type'%
---> 66                                             ' '.join(graph_types))
     67         else:
     68             return f(*args,**kwargs)

NetworkXNotImplemented: not implemented for directed type

Density

Notes: The density is 0 for a graph without edges and 1 for a complete graph. The density of multigraphs can be higher than 1. Self loops are counted in the total number of edges so graphs with self loops can have density higher than 1.


In [ ]:
density = nx.density(graph)
print "Density =", density

Degree centrality

Degree centrality for a node v is the fraction of nodes it is connected to


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

Closeness centrality

  • Closeness centrality of a node u is the reciprocal of the sum of the shortest path distances from u to all n-1 other nodes. Since the sum of distances depends on the number of nodes in the graph, closeness is normalized by the sum of minimum possible distances n-1
  • Higher values of closeness indicate higher centrality

In [ ]:
clo_cen = np.array(nx.closeness_centrality(graph).values()).mean()
print "Closeness centrality =", clo_cen

Betweenness centrality

Betweenness centrality of a node v is the sum of the fraction of all pairs shortest paths that pass through v

  • Compute the shortest-path betweenness centrality for nodes

In [ ]:
nx.betweenness_centrality(graph)
bet_cen = np.array(nx.betweenness_centrality(graph).values()).mean()
print "Betweenness centrality =", bet_cen

Current-flow betweenness centrality

  • Current-flow betweenness centrality uses an electrical current model for information spreading in contrast to betweenness centrality which uses shortest paths.
  • Current-flow betweenness centrality is also known as random-walk betweenness centrality

In [ ]:
# graph must be connected
# nx.current_flow_betweenness_centrality(graph)

Degree assortativity coefficient


In [ ]:
deg_ac = nx.degree_assortativity_coefficient(graph)
print "Degree assortativity coefficient =", deg_ac

Degree pearson correlation coefficient

Assortativity measures the similarity of connections in the graph with respect to the node negree

  • Returns r -- Assortativity of graph by degree

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)

Average node connectivity

The average connectivity \bar{\kappa} of a graph G is the average of local node connectivity over all pairs of nodes of G


In [ ]:
#nx.edge_connectivity(graph)
#nx.node_connectivity(graph)
avg_node_con = nx.average_node_connectivity(graph)
print "Average node connectivity =", avg_node_con

Closeness vitality

Compute closeness vitality for nodes. Closeness vitality of a node is the change in the sum of distances between all node pairs when excluding that node.


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

Summary


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 [ ]: