# -*- coding:utf8 -*- # Given a link stream on stdin, a value of delta and the last timestamp of the stream, outputs the delta-density for each link. # Date : December 2013 # Author : jvd # Notes to self : # t_init is 0 (datafile needs to start at 0 too) import sys import pdb import math links = set() delta = float(sys.argv[1]) links_dict = {} t_init = 0 init_set = 0 tspan = float(sys.argv[2]) nodes = set() def update_density(delta, t, link): """ Updates the value of delta density with information from the latest apparition @param delta : float The fixed value for delta @param t : float The timestamp of the latest link in stream @param link : tuple The latest two nodes (u,v) in the stream """ d = 0 num = 0 if links_dict[link][2] == 1: # The link appears for the first time d = max((t - t_init)-delta, 0) else: # The link appears, but it is not the first time intercontact_t = float(t) - float(links_dict[link][0]) d = max(intercontact_t - delta, 0) # Update link info links_dict[link][2] += 1 links_dict[link][1] += d links_dict[link][0] = t for line in sys.stdin: # Get info from line contents = line.split(" ") t = int(contents[0].strip()) u = int(contents[1].strip()) v = int(contents[2].strip()) # Add info to data structure nodes.add(u) nodes.add(v) link = frozenset([u,v]) links.add(link) # Append ts, density (0), number of links apparitions if links_dict.get(link) is None: # 0 : intercontact sequence # 1 : Current value of deltadensity # 2 : Number of occurrences links_dict[link] = [] links_dict[link].append(t_init) links_dict[link].append(0) links_dict[link].append(1) update_density(delta, t, link) sys.stdout.write("%d\n" % len(nodes)) for link in links_dict: # Update with last term of sum if tspan - links_dict[link][0] != 0: links_dict[link][1] += max((tspan - float(links_dict[link][0]) - delta),0) # Print nodes ids if command-line arg has been stated if len(sys.argv) == 4 and sys.argv[3] == "--print-nodes": sys.stdout.write("%d %d" % (list(link)[0], list(link)[1])) # Print values of delta densities for each pair of nodes if links_dict[link][1] != 0: sys.stdout.write("%10f\n" % (1 - (links_dict[link][1]/(tspan - delta)))) else: sys.stdout.write("%10f\n" % (1))