Rev. | b76bd139649531e0d5bd0f6a9b1b53959639e085 |
---|---|
Tamanho | 7,296 bytes |
Hora | 2011-01-25 02:01:30 |
Autor | lorenzo |
Mensagem de Log | Modifications to various codes: new quantities saved in the gml networks and new functions for statistics and
|
#!/usr/bin/env python
import sys, time
import os
from numpy import *
import sociopatterns
from sociopatterns.loader import Loader
from sociopatterns.analysis import ContactGraphAnalyzer
from xml.dom import minidom
import string
import scipy as s
from sociopatterns.loader import TagMapping
M = TagMapping()
f = open('tag_mapping_high_presence.dat')
for line in f:
(mapped_id, orig_id, t1, t2, type) = string.split(line[:-1])
mapped_id = int(mapped_id)
orig_id = int(orig_id)
t1 = int(t1)
t2 = int(t2)
M.add_mapping(orig_id, mapped_id, t1, t2)
f.close()
# ================= CONFIGURE THESE PARAMETERS ==========================
XXTEA_CRYPTO_KEY = ( 0xf6e103d4, 0x77a739f6, 0x65eecead, 0xa40543a9 )
DECODE = 0
EXPERIMENT = "OBG"
TAG_ID_MIN = 10
TAG_ID_MAX = +3000
DELTAT = 120 # aggregation window
K_MAX = 24 # filter out all nodes with degree > K_MAX (useful for filtering away clusters of tags left together)
# =======================================================================
TAG_ID_SPAN = TAG_ID_MAX - TAG_ID_MIN
cum_adj = zeros((TAG_ID_SPAN, TAG_ID_SPAN), uint32)
onoff = zeros((TAG_ID_SPAN, TAG_ID_SPAN), uint32)
adj_prev = zeros((TAG_ID_SPAN, TAG_ID_SPAN), uint32)
time_first_seen = zeros((TAG_ID_SPAN, TAG_ID_SPAN), uint32)
time_last_seen = zeros((TAG_ID_SPAN, TAG_ID_SPAN), uint32)
frame_count = 0
####################################################################################
def collect_node_and_edge_properties(TAG_ID_SPAN,direct_indirect_seen,TAG_ID_MIN,cum_adj,onoff,time_first_seen, time_last_seen):
node_attr_list=[]
node_label_list=[]
edge_attr_list=[]
edge_label_list=[]
for i in range(TAG_ID_SPAN):
if not direct_indirect_seen[i]:
continue
node_attr_list.append("[")
node_attr_list.append('%d' % i)
node_attr_list.append('"%d"' % (TAG_ID_MIN+i))
node_label_list.append("node")
node_label_list.append("id")
node_label_list.append("label")
if cum_adj[i, i]:
node_attr_list.append('%d' % cum_adj[i,i])
node_attr_list.append('%d' % onoff[i,i])
node_attr_list.append('%e' % (onoff[i,i] / float(cum_adj[i,i]) / 2.0) )
node_attr_list.append('%d' % time_first_seen[i,i])
node_attr_list.append('%d' % time_last_seen[i,i])
node_attr_list.append("1")
node_attr_list.append("]")
node_label_list.append("weight")
node_label_list.append("count")
node_label_list.append("intermittency")
node_label_list.append("first_seen")
node_label_list.append("last_seen")
node_label_list.append("direct")
node_label_list.append("")
else:
# nodeElem.appendChild(new_attribute('integer', 'direct', '0'))
node_attr_list.append('0')
node_attr_list.append("]")
node_label_list.append("direct")
node_label_list.append("")
for i in range(TAG_ID_SPAN):
for j in range(i+1,TAG_ID_SPAN):
if not cum_adj[i, j]:
continue
edge_attr_list.append("[")
edge_attr_list.append('%d' % i)
edge_attr_list.append('%d' % j)
edge_attr_list.append('"%d (pp) %d"' % (TAG_ID_MIN+i, TAG_ID_MIN+j) )
edge_attr_list.append('%d' % cum_adj[i,j])
edge_attr_list.append('%d' % onoff[i,j])
edge_attr_list.append('%d' % (onoff[i,j] / float(cum_adj[i,j]) / 2.0) )
edge_attr_list.append('%d' % time_first_seen[i,j])
edge_attr_list.append('%d' % time_last_seen[i,j])
edge_attr_list.append('%d' % (TAG_ID_MIN+i) )
edge_attr_list.append('%d' % (TAG_ID_MIN+j) )
edge_attr_list.append("")
edge_label_list.append("edge")
edge_label_list.append("source")
edge_label_list.append("target")
edge_label_list.append("label")
edge_label_list.append("weight")
edge_label_list.append("count")
edge_label_list.append("intermittency")
edge_label_list.append("first_seen")
edge_label_list.append("last_seen")
edge_label_list.append("node_A")
edge_label_list.append("node_B")
edge_label_list.append("]")
# -----------------------------------
nodes_all_together=zip(node_label_list,node_attr_list)
n2=["%s %s" %(x,y) for x,y in nodes_all_together]
n3="\n".join(n2)
edges_all_together=zip(edge_label_list,edge_attr_list)
e2=["%s %s" %(x,y) for x,y in edges_all_together]
e3="\n".join(e2)
print e3
tot=n3+e3
#if needed, change the value of "directed"
ini="\n".join(["graph [", "directed 0",""])
end="\n".join(["","]" ])
tot=ini+tot+end
return tot
#################################################################################################################
f = open("time_grid.dat")
time_grid = [map(int, string.split(line)) for line in f.readlines()]
f.close()
time_grid = s.array(time_grid, dtype="int64")
print ("time_grid is", time_grid )
loader = Loader(sys.argv[1:-1], experiment=EXPERIMENT, decode=DECODE, xxtea_crypto_key=XXTEA_CRYPTO_KEY, load_contacts=1, unique_contacts=1, load_sightings=0,
mapping=M,start_time=time_grid[0], stop_time=time_grid[-1])
# loader = Loader(sys.argv[1:], decode=0,experiment='OBG',load_contacts=1, unique_contacts=1, load_sightings=0, mapping=M)
# analyzer = ContactGraphAnalyzer(loader, DELTAT, get_graph=False, get_adj=True, tag_id_min=TAG_ID_MIN, tag_id_max=TAG_ID_MAX, isolated_nodes=True, weighted=False)
analyzer = ContactGraphAnalyzer(loader, DELTAT, get_graph=False, get_adj=True, tag_id_min=TAG_ID_MIN, tag_id_max=TAG_ID_MAX, isolated_nodes=True, weighted=True)
count_day=1
for frame in analyzer:
frame_time = frame['time']
if (frame_time > time_grid[count_day]):
direct_indirect_seen = cum_adj.sum(0)
total=collect_node_and_edge_properties(TAG_ID_SPAN,direct_indirect_seen,\
TAG_ID_MIN,cum_adj,onoff,time_first_seen, time_last_seen)
filename="graph_%01d"%(count_day)
filename=filename+"_.gml"
g=open(filename, "w")
g.write(total)
g.close()
cum_adj = zeros((TAG_ID_SPAN, TAG_ID_SPAN), uint32)
onoff = zeros((TAG_ID_SPAN, TAG_ID_SPAN), uint32)
adj_prev = zeros((TAG_ID_SPAN, TAG_ID_SPAN), uint32)
time_first_seen = zeros((TAG_ID_SPAN, TAG_ID_SPAN), uint32)
time_last_seen = zeros((TAG_ID_SPAN, TAG_ID_SPAN), uint32)
frame_count = 0
count_day=count_day+1
frame_count += 1
adj = frame['adj']
tags_high_k = set(where(adj.sum(0) > K_MAX)[0])
tags_ignore = list(tags_high_k)
adj[tags_ignore,:] = 0
adj[:,tags_ignore] = 0
cum_adj += adj
onoff += where(adj_prev != adj, 1, 0)
time_first_seen = where(onoff == 1, frame_time, time_first_seen)
time_last_seen = where(adj > 0, frame_time, time_last_seen)
adj_prev = adj
print '#%d' % frame_count, time.ctime(frame['time'])
print "So far so good"