Skip to content

Instantly share code, notes, and snippets.

@fnielsen
Created November 25, 2011 01:29
Show Gist options
  • Save fnielsen/1392607 to your computer and use it in GitHub Desktop.
Save fnielsen/1392607 to your computer and use it in GitHub Desktop.
Computation of co-author distance in the Brede Wiki co-author graph
# wget http://neuro.imm.dtu.dk/services/bredewiki/download/bredewiki-templates.sqlite3
import matplotlib.pyplot as plt
import networkx as nx
from pysqlite2 import dbapi2
connection = dbapi2.Connection('bredewiki-templates.sqlite3')
sql = "SELECT DISTINCT tid FROM brede WHERE (template='paper' OR template='conference_paper');"
cursor = connection.cursor()
cursor.execute(sql)
tids = [ row[0] for row in cursor.fetchall() ]
g = nx.Graph()
sql = "SELECT value FROM brede WHERE tid=? AND field='author'";
for tid in tids:
cursor.execute(sql, (tid,))
authors = [ row[0] for row in cursor.fetchall() ]
for n in range(len(authors)):
for m in range(len(authors)):
g.add_edge(authors[n], authors[m])
# Biggest connected component
g1 = nx.connected_component_subgraphs(g)[0]
# Shortest path
spl = nx.shortest_path_length(g1)
authors = spl.keys()
distances = [ spl[authors[n]][authors[m]] for n in range(len(authors))
for m in range(len(authors)) if m < n ]
# Maximum shortest path
ecc = nx.eccentricity(g1).values()
from pylab import *
subplot(2,1,1)
hist(distances, bins=arange(1,17)-0.5)
xlabel('Distance')
ylabel('Absolute frequency')
title('Histogram of distances')
text(11, 30000, 'Mean distance = %.2f' % mean(distances))
text(11, 25000, 'Distance variance = %.2f' % var(distances))
text(11, 20000, 'var/mean (distance)= %.2f' % (var(distances)/mean(distances)))
subplot(2,1,2)
hist(ecc, bins=arange(1,17)-0.5)
xlabel('Eccentricity')
ylabel('Absolute frequency')
title('Histogram of eccentricities')
text(11, 200, 'Mean eccentricity = %.2f' % mean(ecc))
text(11, 180, 'Eccentricity variance = %.2f' % var(ecc))
text(11, 160, 'var/mean (eccentricity)= %.2f' % (var(ecc)/mean(ecc)))
show()
# savefig('bredewiki-coauthor.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment