Neutral network:
In [15]:
import networkx as nx
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
pd.options.display.max_rows = 20
In [2]:
# read multigraph M
M = nx.read_gml('../output/network/u_Gc_neutral2.gml')
# convert multigraph M to simple graph G (remove parallel edges)
G = nx.Graph()
for u,v,data in M.edges_iter(data=True):
w = data['weight'] if 'weight' in data else 1.0
if G.has_edge(u,v):
G[u][v]['weight'] += w
else:
G.add_edge(u, v, weight=w)
#print G.edges(data=True)
print nx.info(M), '\n'
print nx.info(G)
In [3]:
## jaccard coefficient
jc = nx.jaccard_coefficient(G)
jc = list(jc)
for u, v, p in jc:
'(%s, %s) -> %.4f' % (u, v, p)
In [16]:
df = pd.DataFrame(jc, columns=['u', 'v', 'jaccard'])
subset_df = df.ix[df['jaccard'] != 0, :]
subset_df.sort_values('jaccard', ascending=False)
Out[16]:
In [5]:
# save jaccard
#subset_df.to_csv('jaccard_neutral.csv')
In [6]:
## adamic adar index
aa = nx.adamic_adar_index(G)
aa = list(aa)
for u, v, p in aa:
'(%s, %s) -> %.8f' % (u, v, p)
In [17]:
df = pd.DataFrame(aa, columns=['u', 'v', 'adamic_adar'])
subset_df = df.ix[df['adamic_adar'] != 0, :]
subset_df.sort_values('adamic_adar', ascending=False)
Out[17]:
In [8]:
# save adamic idar
#subset_df.to_csv('adamic_neutral.csv')
In [18]:
# closeness vitality
# of a node is the change in the sum of distances between all node pairs when excluding that node
cv = nx.closeness_vitality(M)
cv_df = pd.DataFrame.from_dict(cv, orient = 'index')
cv_df.columns = ['closeness vitality']
cv_df.sort_values(by = ['closeness vitality'], ascending = False)
Out[18]:
In [10]:
# save closeness vitality
#cv_df.to_csv('cv_neutral.csv')
In [19]:
# link analysis: page rank
# PageRank computes a ranking of nodes based on structure of incoming links
pr = nx.pagerank_numpy(M)
pr_df = pd.DataFrame.from_dict(pr, orient = 'index')
pr_df.columns = ['page rank']
pr_df.sort_values(by = ['page rank'], ascending = False)
Out[19]:
In [12]:
# save page rank
#pr_df.to_csv('pr_neutral.csv')
In [ ]: