In [286]:
%pylab inline
import networkx as nx
from pprint import pprint


Populating the interactive namespace from numpy and matplotlib

In [318]:
class TemporalEdge:
        def __init__(self,node1,node2):
            self.node1 = node1
            self.node2 = node2
            self.occurences = dict()
        
        def add(self,weight,time):
            if time not in self.occurences:
                self.occurences[time] = weight
            else:
                 self.occurences[time] += weight
        
        @property
        def nodes(self):
            return (self.node1,self.node2)
        
        def weight(self,currentTime):
            curDict = {time : weight for time,weight in self.occurences.items() if time <= currentTime}
            if curDict:
                t = max(curDict.keys())
                w = curDict[t]
                t = currentTime - t
                w *= 0.9**t
                return w
            else:
                return 0
            
        def __eq__(self,other):
            if isinstance(other,type(self)):
                return other.node1 == self.node1 and other.node2 == self.node2 and other.occurences == self.occurences
            elif isinstance(other,tuple):
                if len(other) == 2:
                    return self.node1 == other[0] and self.node2 == other[1]
            return False
        
        def __repr__(self):
            return "%s -> %s %s" % (self.node1,self.node2,self.occurences)

In [337]:
class TemporalCoAuthorNetwork:
    
    def __init__(self):
        self.edges = []
            
    def __repr__(self):
        return str(self.edges)
    
    def addEdge(self,node1,node2,weight,time):
        node1, node2 = min(node1,node2),max(node1,node2)
        if (node1,node2) not in self.edges:
            edge = TemporalEdge(node1,node2)
            self.edges.append(edge)
        i = self.edges.index((node1,node2))
        self.edges[i].add(weight,time)
    
    def networkAtTime(self,time):
        N = nx.Graph()
        for edge in self.edges:
            w = edge.weight(time)
            if w:
                print edge.nodes, w
                N.add_edge(*edge.nodes,weight = w)
        return N

Temporal Co-author Networks

First create the Co-author network.


In [338]:
N = TemporalCoAuthorNetwork()

Add edges according to the following format:

Author 1, Author 2, Amount of papers, Year


In [339]:
N.addEdge('Paul','Luka',5,10)
N.addEdge('Paul','Peter',1,1)
N.addEdge('Paul','Peter',2,3)
N.addEdge('Paul','Peter',3,5)
N.addEdge('Paul','Peter',1,10)
N.addEdge('Jacob','Paul',2,1)
N.addEdge('Jacob','Paul',3,2)
N.addEdge('Jacob','Peter',2,4)

Now to show the network get the network at a certain time point


In [340]:
for timePoint in [1,5,10]:
    networkTime = N.networkAtTime(timePoint)
    nx.draw(networkTime)
    plt.show()



In [326]:


In [ ]: