Analysis


When you vizualize your network, you also want to analize the network. In this section, you can learn basic analysis methods to network. The methods used in this section are prepared in the Python's libraries, igraph, networkx and so on. So, we will also use them to analyze network and if you want to get more detail about this examples or find more information, please look at these documentations.

Further Information about igraph


Table of contents


  • Global Network Analysis

    • Density
    • Transivity
    • community detection
  • Node Analysis

    • Closeness
    • Degree
    • PageRank
    • community detection
  • Edge Analysis

    • EdgeBetweenness
    • community detection

Network Data Preparation


To prepare network analysis, let's load network data.


In [52]:
# import data from url
from py2cytoscape.data.cyrest_client import CyRestClient
from IPython.display import Image

# Create REST client for Cytoscape
cy = CyRestClient()

# Reset current session for fresh start
cy.session.delete()

# Load a sample network
network = cy.network.create_from('http://chianti.ucsd.edu/~kono/data/galFiltered.sif')

# Apply layout to the cytoscape network object
cy.layout.apply(network = network)

# Show it!!
network_png = network.get_png()
Image(network_png)


Out[52]:

Global Network Analysis


In this example, we will use "igraph" to analyze global network propety. "igraph" are the very popular and useful package and you can analyze your own network by this.

And py2cytoscape can convert their own network to igraph object. So, first, we have to convert cytoscape network object to igraph object.


In [55]:
# covert cytoscape network object to igraph object
import igraph as ig
import py2cytoscape.util.util_igraph as util_ig

# convert cytoscape object to igraph object
g = util_ig.to_igraph(network.to_json())

Density


Calculates the density of the graph.

Further information : http://igraph.org/python/doc/igraph.GraphBase-class.html#density


In [57]:
density = g.density()
print(density)


0.0066282156916598

Transitivity


There are three methods to calculate the transitivity in igraph. In the following methods, we will use "transitivity_undirected" method.

Method : transitivity_avglocal_undirected

Calculates the average of the vertex transitivities of the graph.

http://igraph.org/python/doc/igraph.GraphBase-class.html#transitivity_avglocal_undirected

Method : transitivity_local_undirected

Calculates the local transitivity (clustering coefficient) of the given vertices in the graph.

http://igraph.org/python/doc/igraph.GraphBase-class.html#transitivity_local_undirected

Method : transitivity_undirected

Calculates the global transitivity (clustering coefficient) of the graph.

http://igraph.org/python/doc/igraph.GraphBase-class.html#transitivity_undirected


In [58]:
transitivity  = g.transitivity_undirected()
print(transitivity)


0.078

community detection


Finds the community structure of the graph according to the algorithm of Clauset et al based on the greedy optimization of modularity. http://igraph.org/python/doc/igraph.GraphBase-class.html#community_fastgreedy


In [72]:
# If you want to use this method, you have to use the non-multiple-edges graph object.
#community_fastgreedy = g.community_fastgreedy()
#print(community_fastgreedy)

Node Analysis


Closeness


Calculates the closeness centralities of given vertices in a graph.

http://igraph.org/python/doc/igraph.GraphBase-class.html#closeness


In [75]:
closeness = g.closeness()

# Show 10 results of node closeness
print(closeness[0:9])


[0.010857048856719856, 0.01094599973464243, 0.0030394856821803246, 0.011159204653050182, 0.011251662177367111, 0.011173562673528814, 0.0030486959895790026, 0.01122334455667789, 0.011220291727584918]

Degree



In [74]:
indegree = g.indegree()
outdegree = g.outdegree()
# Show 10 results of node degree
print(indegree[0:9])
print(outdegree[0:9])


[1, 2, 1, 1, 2, 3, 1, 1, 1]
[1, 2, 1, 1, 2, 3, 1, 1, 1]

PageRank


Calculates the Google PageRank values of a graph. http://igraph.org/python/doc/igraph.Graph-class.html#pagerank


In [73]:
pagerank = g.pagerank()

# Show 10 results of node degree
print(pagerank[0:9])


[0.001669256848836194, 0.0028613756315192377, 0.002327100514415226, 0.0017009589539318975, 0.0025390848664674755, 0.003684617815271894, 0.0021201038850943766, 0.0016683223460983874, 0.0015002738267074773]

community detection


Finds the community structure of the graph according to the algorithm of Clauset et al based on the greedy optimization of modularity. http://igraph.org/python/doc/igraph.GraphBase-class.html#community_fastgreedy


In [ ]:
# If you want to use this method, you have to use the non-multiple-edges graph object.
#community_fastgreedy = g.community_fastgreedy()
#print(community_fastgreedy)

Edge Analysis


EdgeBetweenness


Calculates or estimates the edge betweennesses in a graph. http://igraph.org/python/doc/igraph.GraphBase-class.html#edge_betweenness


In [68]:
edge_betweenness = g.edge_betweenness()
print(edge_betweenness[0:9])


[248.0, 494.0, 248.0, 1373.0, 1587.0, 1481.0, 2618.0, 3.0, 248.0]

community detection


Community structure detection based on the betweenness of the edges in the network. http://igraph.org/python/doc/igraph.GraphBase-class.html#community_edge_betweenness


In [70]:
community_edge_betweenness_detection = g.community_edge_betweenness()
print(community_edge_betweenness_detection)


Dendrogram, 331 elements, 305 merges