In [1]:
%pylab inline
%load_ext autoreload
%autoreload 2
import networkx as nx
from matplotlib import pyplot as plt
from temporal_coauthor_network import TemporalCoAuthorNetwork
import process_pubmed
import vincent
vincent.core.initialize_notebook()
import pandas as pd
import csv
import itertools
import re
First create the Co-author network.
Add edges according to the following format:
Author1, Author2, Weight, Time
In [2]:
N = TemporalCoAuthorNetwork()
with open('sample.csv', 'r') as f:
nodes = []
for line in f:
line = line.split(",")
a1 = line[0].strip()
a2 = line[1].strip()
if a1 not in nodes:
nodes.append(a1)
if a2 not in nodes:
nodes.append(a2)
N.addEdge(a1,a2,int(line[2]),int(line[3]))
In [3]:
process_pubmed.byAuthor('Adamson, J W')
In [2]:
N = TemporalCoAuthorNetwork()
with open('processed_pubmed_Adamson,JW.csv') as f:
nodes = []
for line in f:
line = line.strip()
line = line.split("\t")
a1 = line[0].strip()
a2 = line[1].strip()
if a1 not in nodes:
nodes.append(a1)
if a2 not in nodes:
nodes.append(a2)
N.addEdge(a1,a2,int(line[2]),int(line[3]))
To show the network we need to use a layout function, of which an example is shown here:
In [3]:
def fixed_node_layout(network):
pos = nx.spring_layout(network,pos={'Adamson, J W' : (0.5,0.5)},fixed=['Adamson, J W'])
return pos
Now to show the network at a certain time point
In [4]:
all_edge_times = [time for edge in N.edges for time in edge.occurences.keys()]
max_time = max(all_edge_times)
min_time = min(all_edge_times)
timeSeries = range(min_time,max_time,1)
eigenvector_centrality = dict()
clustering_coefficient = dict()
for timePoint in timeSeries:
network = N.networkAtTime(timePoint)
if not network.edges():
continue;
pos = fixed_node_layout(network)
print pos['Adamson, J W']
nx.draw(network, pos = nx.spring_layout(network,pos={'Adamson, J W' : (0.5,0.5)},fixed=['Adamson, J W']))
plt.show()
centrality = nx.eigenvector_centrality_numpy(network)
clustering = nx.clustering(network)
for author in ['Adamson, J W']:
if author not in eigenvector_centrality:
eigenvector_centrality[author] = []
clustering_coefficient[author] = []
if author not in centrality:
eigenvector_centrality[author].append(0)
clustering_coefficient[author].append(0)
else:
eigenvector_centrality[author].append(centrality[author])
clustering_coefficient[author].append(clustering[author])
In [5]:
eigen_data = pd.DataFrame.from_dict(eigenvector_centrality)
cluster_data = pd.DataFrame.from_dict(clustering_coefficient)
vincent.Line(eigen_data).display()
vincent.Line(cluster_data).display()
In [ ]: