Rev. | 37c4c3e731e4870181edcaca408bb5a68cb169b5 |
---|---|
大小 | 5,452 字节 |
时间 | 2011-02-14 20:23:36 |
作者 | lorenzo |
Log Message | I had forgotten to import some modules. |
#!/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
from sociopatterns.loader import TagMapping
import string
import scipy as s
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"
TSTART = None # 1280613600 # None
TSTOP = None #1280700000 # None
TAG_ID_MIN = 1000
TAG_ID_MAX = 2000
DELTAT = 20 # aggregation window
K_MAX = 4 # 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
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=TSTART, stop_time=TSTOP)
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)
for frame in analyzer:
frame_count += 1
adj = frame['adj']
frame_time = frame['time']
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'])
direct_indirect_seen = cum_adj.sum(0)
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:
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
g=open("graph.gml", "w")
g.write(tot)
g.close()
print "So far so good"