Revisão | b76bd139649531e0d5bd0f6a9b1b53959639e085 (tree) |
---|---|
Hora | 2011-01-25 02:01:30 |
Autor | lorenzo |
Commiter | lorenzo |
Modifications to various codes: new quantities saved in the gml networks and new functions for statistics and
visualizations in the R codes.
@@ -9,14 +9,29 @@ | ||
9 | 9 | import string |
10 | 10 | import scipy as s |
11 | 11 | |
12 | +from sociopatterns.loader import TagMapping | |
13 | + | |
14 | +M = TagMapping() | |
15 | + | |
16 | +f = open('tag_mapping_high_presence.dat') | |
17 | +for line in f: | |
18 | + (mapped_id, orig_id, t1, t2, type) = string.split(line[:-1]) | |
19 | + mapped_id = int(mapped_id) | |
20 | + orig_id = int(orig_id) | |
21 | + t1 = int(t1) | |
22 | + t2 = int(t2) | |
23 | + M.add_mapping(orig_id, mapped_id, t1, t2) | |
24 | +f.close() | |
25 | + | |
26 | + | |
12 | 27 | # ================= CONFIGURE THESE PARAMETERS ========================== |
13 | 28 | XXTEA_CRYPTO_KEY = ( 0xf6e103d4, 0x77a739f6, 0x65eecead, 0xa40543a9 ) |
14 | 29 | DECODE = 0 |
15 | 30 | EXPERIMENT = "OBG" |
16 | -TAG_ID_MIN = 1000 | |
17 | -TAG_ID_MAX = 2000 | |
18 | -DELTAT = 20 # aggregation window | |
19 | -K_MAX = 5 # filter out all nodes with degree > K_MAX (useful for filtering away clusters of tags left together) | |
31 | +TAG_ID_MIN = 10 | |
32 | +TAG_ID_MAX = +3000 | |
33 | +DELTAT = 120 # aggregation window | |
34 | +K_MAX = 24 # filter out all nodes with degree > K_MAX (useful for filtering away clusters of tags left together) | |
20 | 35 | # ======================================================================= |
21 | 36 | |
22 | 37 | TAG_ID_SPAN = TAG_ID_MAX - TAG_ID_MIN |
@@ -116,6 +131,9 @@ | ||
116 | 131 | edge_attr_list.append('%d' % (onoff[i,j] / float(cum_adj[i,j]) / 2.0) ) |
117 | 132 | edge_attr_list.append('%d' % time_first_seen[i,j]) |
118 | 133 | edge_attr_list.append('%d' % time_last_seen[i,j]) |
134 | + edge_attr_list.append('%d' % (TAG_ID_MIN+i) ) | |
135 | + edge_attr_list.append('%d' % (TAG_ID_MIN+j) ) | |
136 | + | |
119 | 137 | edge_attr_list.append("") |
120 | 138 | |
121 | 139 | edge_label_list.append("edge") |
@@ -130,6 +148,8 @@ | ||
130 | 148 | edge_label_list.append("intermittency") |
131 | 149 | edge_label_list.append("first_seen") |
132 | 150 | edge_label_list.append("last_seen") |
151 | + edge_label_list.append("node_A") | |
152 | + edge_label_list.append("node_B") | |
133 | 153 | edge_label_list.append("]") |
134 | 154 | |
135 | 155 |
@@ -181,9 +201,15 @@ | ||
181 | 201 | print ("time_grid is", time_grid ) |
182 | 202 | |
183 | 203 | 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, |
184 | - start_time=time_grid[0], stop_time=time_grid[-1]) | |
204 | + mapping=M,start_time=time_grid[0], stop_time=time_grid[-1]) | |
185 | 205 | |
186 | -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) | |
206 | +# loader = Loader(sys.argv[1:], decode=0,experiment='OBG',load_contacts=1, unique_contacts=1, load_sightings=0, mapping=M) | |
207 | + | |
208 | + | |
209 | +# 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) | |
210 | + | |
211 | +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) | |
212 | + | |
187 | 213 | |
188 | 214 | |
189 | 215 | count_day=1 |
@@ -69,6 +69,8 @@ | ||
69 | 69 | } |
70 | 70 | |
71 | 71 | |
72 | +extra_norm <- c() | |
73 | + | |
72 | 74 | original <- read.table("frequencies_extended.dat", header=TRUE) |
73 | 75 | names(original) <- c("role","freq", "norm", "count_norm") |
74 | 76 |
@@ -83,7 +85,29 @@ | ||
83 | 85 | |
84 | 86 | freq_complete <- c(original$freq,extra_freq) |
85 | 87 | |
86 | -norm_complete <- c(original$norm, extra_freq) | |
88 | + | |
89 | +n_ij <- read.table("n_ij.dat", header=TRUE) | |
90 | + | |
91 | +n_ij$role <- levels(n_ij$role)[n_ij$role] | |
92 | + | |
93 | + | |
94 | +for (i in seq(length(extra_roles))){ | |
95 | + | |
96 | +add_role <- extra_roles[i] | |
97 | + | |
98 | +sel <- which(n_ij$role==add_role) | |
99 | + | |
100 | +extra_norm <- c(extra_norm, n_ij$norm[sel]) | |
101 | + | |
102 | +} | |
103 | + | |
104 | + | |
105 | +## norm_complete <- c(original$norm, extra_freq) | |
106 | + | |
107 | +norm_complete <- c(original$norm, extra_norm) | |
108 | + | |
109 | + | |
110 | + | |
87 | 111 | |
88 | 112 | count_norm_complete <- c(original$count_norm, extra_freq) |
89 | 113 |
@@ -1237,7 +1261,7 @@ | ||
1237 | 1261 | ## scale_y_continuous( trans="log10", limits=c(.1,3500), breaks=mb, labels=my_label2## ,expand = c(0.01,0.01) |
1238 | 1262 | ## )+ |
1239 | 1263 | |
1240 | -scale_y_continuous(limits=c(0,0.0025), breaks=seq(0,0.0025, by=0.0005))+ | |
1264 | +scale_y_continuous(limits=c(0,0.026), breaks=seq(0,0.025, by=0.005))+ | |
1241 | 1265 | |
1242 | 1266 | ## scale_y_continuous( trans="log10", limits=c(.1,3500), breaks=mb, labels=my_label2)+ |
1243 | 1267 |
@@ -1256,6 +1280,8 @@ | ||
1256 | 1280 | opts(axis.text.x = theme_text(size=15, colour="black",vjust=1))+ |
1257 | 1281 | opts(axis.text.y = theme_text(size=15, colour="black", hjust=1)) |
1258 | 1282 | |
1283 | + | |
1284 | + | |
1259 | 1285 | } |
1260 | 1286 | |
1261 | 1287 |
@@ -1275,6 +1301,12 @@ | ||
1275 | 1301 | |
1276 | 1302 | } |
1277 | 1303 | |
1304 | + | |
1305 | +gpl_list[[1]] <- gpl_list[[1]] + | |
1306 | + opts(title = expression(paste("Aggregation on 5 min, 1 hour cutoff")))+ | |
1307 | + opts(plot.title = theme_text(size = 20)) | |
1308 | + | |
1309 | + | |
1278 | 1310 | fn <- paste("role-shuffle-normalized-triangles-panel.tex") |
1279 | 1311 | |
1280 | 1312 | width <- 15 |
@@ -242,6 +242,145 @@ | ||
242 | 242 | |
243 | 243 | } |
244 | 244 | |
245 | + | |
246 | + | |
247 | + | |
248 | + | |
249 | + | |
250 | + | |
251 | + | |
252 | +t_ab_bc_improved_all_triangles <- function(data_tag,tag_id){ | |
253 | + | |
254 | +## delta_t <- abs(diff(data_tag$ini_time)) | |
255 | + | |
256 | +delta_t <- c() | |
257 | +labels <- c() | |
258 | +roles <- c() | |
259 | +ternary_roles <- c() | |
260 | + | |
261 | + | |
262 | +if (length(data_tag$id1)>1){ | |
263 | + | |
264 | +sort_time_ini <- sort(data_tag$ini_time, decreasing=FALSE, index.return=TRUE) | |
265 | + | |
266 | +ms <- sort_time_ini$ix | |
267 | + | |
268 | + | |
269 | +## sort the data once for all | |
270 | + | |
271 | +data_tag <- data_tag[ms, ] | |
272 | +## print("tag_id is,") | |
273 | +## print(tag_id) | |
274 | +## print("data_tag is, ") | |
275 | +## print(data_tag) | |
276 | +for (i in seq(length(data_tag$id1)-1)){ | |
277 | +for (j in seq((i+1),length(data_tag$id1) )){ | |
278 | +if (((data_tag$id1[i]+data_tag$id2[i])!=(data_tag$id1[j]+data_tag$id2[j]))){ | |
279 | +delta_t <- c(delta_t,(data_tag$ini_time[j]-data_tag$ini_time[i])) | |
280 | +labels <- rbind(labels,cbind(data_tag$id1[i],data_tag$id2[i], | |
281 | +data_tag$id1[j],data_tag$id2[j])) | |
282 | + | |
283 | +## print("data_tag$id1[i] is, ") | |
284 | +## print(data_tag$id1[i]) | |
285 | + | |
286 | +## print("tag_id is, ") | |
287 | +## print(tag_id) | |
288 | + | |
289 | +if (data_tag$id1[i]==tag_id){ | |
290 | + tag_a <- data_tag$role_second[i] | |
291 | + tag_b <- data_tag$role_first[i] | |
292 | +}else{ | |
293 | + tag_a <- data_tag$role_first[i] | |
294 | + tag_b <- data_tag$role_second[i] | |
295 | +} | |
296 | + | |
297 | + | |
298 | +## print("i is, ") | |
299 | +## print(i) | |
300 | +## print("j is") | |
301 | +## print(j) | |
302 | + | |
303 | + | |
304 | +if (data_tag$id1[j]==tag_id){ | |
305 | + tag_c <- data_tag$role_second[j] | |
306 | +}else{ | |
307 | + tag_c <- data_tag$role_first[j] | |
308 | +} | |
309 | + | |
310 | +roles <- rbind(roles,cbind(data_tag$role_first[i],data_tag$role_second[i], | |
311 | +data_tag$role_first[j],data_tag$role_second[j])) | |
312 | +ternary_roles <- rbind(ternary_roles,cbind(tag_a,tag_b,tag_c)) | |
313 | +} | |
314 | +} | |
315 | +} | |
316 | +} | |
317 | +## labels <- cbind(data_tag$id1[1:length(data_tag$id1)-1],data_tag$id2[1:length(data_tag$id2)-1], | |
318 | +## data_tag$id1[2:length(data_tag$id1)],data_tag$id2[2:length(data_tag$id2)]) | |
319 | +return (list(delta_t, labels,roles,ternary_roles)) | |
320 | +} | |
321 | + | |
322 | + | |
323 | + | |
324 | +t_ab_bc_improved_all_triangles_stop <- function(data_tag,tag_id){ | |
325 | + | |
326 | +## delta_t <- abs(diff(data_tag$ini_time)) | |
327 | + | |
328 | +delta_t <- c() | |
329 | +labels <- c() | |
330 | +roles <- c() | |
331 | +ternary_roles <- c() | |
332 | + | |
333 | + | |
334 | +if (length(data_tag$id1)>1){ | |
335 | + | |
336 | +sort_time_ini <- sort(data_tag$ini_time, decreasing=FALSE, index.return=TRUE) | |
337 | + | |
338 | +ms <- sort_time_ini$ix | |
339 | + | |
340 | +data_tag <- data_tag[ms, ] | |
341 | +for (i in seq(length(data_tag$id1)-1)){ | |
342 | + | |
343 | + | |
344 | +j <- i+1 | |
345 | + | |
346 | +while ( (j<=length(data_tag$id1)) & ((data_tag$id1[i]+data_tag$id2[i])!=(data_tag$id1[j]+data_tag$id2[j]))){ | |
347 | + | |
348 | + delta_t <- c(delta_t,(data_tag$ini_time[j]-data_tag$ini_time[i])) | |
349 | +labels <- rbind(labels,cbind(data_tag$id1[i],data_tag$id2[i], | |
350 | +data_tag$id1[j],data_tag$id2[j])) | |
351 | + | |
352 | + | |
353 | +if (data_tag$id1[i]==tag_id){ | |
354 | + tag_a <- data_tag$role_second[i] | |
355 | + tag_b <- data_tag$role_first[i] | |
356 | +}else{ | |
357 | + tag_a <- data_tag$role_first[i] | |
358 | + tag_b <- data_tag$role_second[i] | |
359 | +} | |
360 | + | |
361 | +if (data_tag$id1[j]==tag_id){ | |
362 | + tag_c <- data_tag$role_second[j] | |
363 | +}else{ | |
364 | + tag_c <- data_tag$role_first[j] | |
365 | +} | |
366 | + | |
367 | +roles <- rbind(roles,cbind(data_tag$role_first[i],data_tag$role_second[i], | |
368 | +data_tag$role_first[j],data_tag$role_second[j])) | |
369 | +ternary_roles <- rbind(ternary_roles,cbind(tag_a,tag_b,tag_c)) | |
370 | + | |
371 | +j <- j+1 | |
372 | + | |
373 | + } | |
374 | + | |
375 | +} | |
376 | +} | |
377 | + | |
378 | +return (list(delta_t, labels,roles,ternary_roles)) | |
379 | +} | |
380 | + | |
381 | + | |
382 | + | |
383 | + | |
245 | 384 | get_frequence <- function(contact){ |
246 | 385 | |
247 | 386 | freq <- table(contact) |
@@ -284,11 +423,11 @@ | ||
284 | 423 | ####################################################################### |
285 | 424 | |
286 | 425 | |
287 | -timeslice_dur <- 20 | |
426 | +timeslice_dur <- 300 | |
288 | 427 | |
289 | -cut_off <- 900 #max interval durations | |
428 | +cut_off <- 3600*24 #max interval durations | |
290 | 429 | |
291 | -time_reversal <- 0 | |
430 | +time_reversal <- 1 | |
292 | 431 | |
293 | 432 | time_uniform <- 0 |
294 | 433 |
@@ -449,9 +588,14 @@ | ||
449 | 588 | |
450 | 589 | data_tag <- data_tot[sel, ] |
451 | 590 | |
452 | -## res_single_tag <- t_ab_bc(data_tag) | |
453 | 591 | |
454 | -res_single_tag <- t_ab_bc_improved(data_tag,tag_id) | |
592 | + | |
593 | +## res_single_tag <- t_ab_bc_improved(data_tag,tag_id) | |
594 | + | |
595 | +## res_single_tag <- t_ab_bc_improved_all_triangles(data_tag,tag_id) | |
596 | + | |
597 | +res_single_tag <- t_ab_bc_improved_all_triangles_stop(data_tag,tag_id) | |
598 | + | |
455 | 599 | |
456 | 600 | delta_t_overall <- c(delta_t_overall, res_single_tag[[1]]) |
457 | 601 | label_overall <- rbind(label_overall,res_single_tag[[2]]) |
@@ -225,6 +225,70 @@ | ||
225 | 225 | } |
226 | 226 | |
227 | 227 | |
228 | + | |
229 | +t_ab_bc_improved_all_triangles_stop <- function(data_tag,tag_id){ | |
230 | + | |
231 | +## delta_t <- abs(diff(data_tag$ini_time)) | |
232 | + | |
233 | +delta_t <- c() | |
234 | +labels <- c() | |
235 | +roles <- c() | |
236 | +ternary_roles <- c() | |
237 | + | |
238 | + | |
239 | +if (length(data_tag$id1)>1){ | |
240 | + | |
241 | +sort_time_ini <- sort(data_tag$ini_time, decreasing=FALSE, index.return=TRUE) | |
242 | + | |
243 | +ms <- sort_time_ini$ix | |
244 | + | |
245 | +data_tag <- data_tag[ms, ] | |
246 | +for (i in seq(length(data_tag$id1)-1)){ | |
247 | + | |
248 | + | |
249 | +j <- i+1 | |
250 | + | |
251 | +while ( (j<=length(data_tag$id1)) & ((data_tag$id1[i]+data_tag$id2[i])!=(data_tag$id1[j]+data_tag$id2[j]))){ | |
252 | + | |
253 | + delta_t <- c(delta_t,(data_tag$ini_time[j]-data_tag$ini_time[i])) | |
254 | +labels <- rbind(labels,cbind(data_tag$id1[i],data_tag$id2[i], | |
255 | +data_tag$id1[j],data_tag$id2[j])) | |
256 | + | |
257 | + | |
258 | +if (data_tag$id1[i]==tag_id){ | |
259 | + tag_a <- data_tag$role_second[i] | |
260 | + tag_b <- data_tag$role_first[i] | |
261 | +}else{ | |
262 | + tag_a <- data_tag$role_first[i] | |
263 | + tag_b <- data_tag$role_second[i] | |
264 | +} | |
265 | + | |
266 | +if (data_tag$id1[j]==tag_id){ | |
267 | + tag_c <- data_tag$role_second[j] | |
268 | +}else{ | |
269 | + tag_c <- data_tag$role_first[j] | |
270 | +} | |
271 | + | |
272 | +roles <- rbind(roles,cbind(data_tag$role_first[i],data_tag$role_second[i], | |
273 | +data_tag$role_first[j],data_tag$role_second[j])) | |
274 | +ternary_roles <- rbind(ternary_roles,cbind(tag_a,tag_b,tag_c)) | |
275 | + | |
276 | +j <- j+1 | |
277 | + | |
278 | + } | |
279 | + | |
280 | +} | |
281 | +} | |
282 | + | |
283 | +return (list(delta_t, labels,roles,ternary_roles)) | |
284 | +} | |
285 | + | |
286 | + | |
287 | + | |
288 | + | |
289 | + | |
290 | + | |
291 | + | |
228 | 292 | reassign_roles <- function(data_tot, tag_role_association,tag_ids_uni, select_ids, select_roles ){ |
229 | 293 | |
230 | 294 | reshuffled_unique_roles <- sample(tag_role_association) |
@@ -258,13 +322,13 @@ | ||
258 | 322 | |
259 | 323 | |
260 | 324 | |
261 | -cut_off <- 300 | |
325 | +cut_off <- 3600*24 | |
262 | 326 | |
263 | 327 | |
264 | 328 | n_iter <- 200 |
265 | 329 | |
266 | 330 | |
267 | -sample_on_times <- 1 | |
331 | +sample_on_times <- 0 | |
268 | 332 | |
269 | 333 | |
270 | 334 | data_tot <- read.table("global_data.dat", header=TRUE) |
@@ -389,7 +453,9 @@ | ||
389 | 453 | |
390 | 454 | ## res_single_tag <- t_ab_bc(data_tag) |
391 | 455 | |
392 | -res_single_tag <- t_ab_bc_improved(data_tag,tag_id) | |
456 | +## res_single_tag <- t_ab_bc_improved(data_tag,tag_id) | |
457 | + | |
458 | +res_single_tag <- t_ab_bc_improved_all_triangles_stop(data_tag,tag_id) | |
393 | 459 | |
394 | 460 | delta_t_overall <- c(delta_t_overall, res_single_tag[[1]]) |
395 | 461 | label_overall <- rbind(label_overall,res_single_tag[[2]]) |