Transforming event sequence into network

25. november 2023

Reading

Event sequence is described in a CSV file. It starts with a header line. Each other line contains a quadruple (ID, s, f, S): ID - actor ID, s - starting time, f - finishing time, S - state (category). s and f determine the time interval [s,f).

tra1.csv

> wdir <- "C:/Users/vlado/docs/papers/2024/Trajectories/test"
> setwd(wdir)
> E <- read.csv("https://raw.githubusercontent.com/bavla/TQ/master/trajectories/test/tra1.csv",sep="")
> E
   ID s  f S
1  ab 1  5 P
2  ab 1  7 K
3  ab 5 10 Q
4  ab 5 11 M
5  cd 2 12 Q
6  cd 1  3 M
7  cd 9 11 P
8  ef 5  9 K
9  ef 2  7 Q
10 ef 7 12 P
11 ef 1  4 M
12 ef 9 12 K
13 gh 3  5 P
14 gh 5  8 M

Transforming

To speed up the transformation of longer sequences we reorder the data in lexicographic order of intervals.

> I <- order(E$s,E$f)
> E[I,]
   ID s  f S
6  cd 1  3 M
11 ef 1  4 M
1  ab 1  5 P
2  ab 1  7 K
9  ef 2  7 Q
5  cd 2 12 Q
13 gh 3  5 P
14 gh 5  8 M
8  ef 5  9 K
3  ab 5 10 Q
4  ab 5 11 M
10 ef 7 12 P
7  cd 9 11 P
12 ef 9 12 K

A test code with trace.

> n <- length(I); k <- 0
> for(p in 1:(n-1)){i <- I[p]; tm <- E$f[i]
+   for(q in (p+1):n){j <- I[q]; k <- k+1
+     if(E$s[j]>tm) break
+     if(E$S[i]==E$S[j]){
+       fm <- min(E$f[i],E$f[j]); sM <- max(E$s[i],E$s[j]); T <- fm - sM
+       if(T>0) cat(format(c(k,p,q,E$ID[i],E$ID[j],sM,fm,T,E$S[i]),width=3),"\n") 
+     }
+   }
+ }
1   1   2   cd  ef  1   3   2   M   
17  3   7   ab  gh  3   5   2   P   
27  4   9   ab  ef  5   7   2   K   
32  5   6   ef  cd  2   7   5   Q   
36  5   10  ef  ab  5   7   2   Q   
43  6   10  cd  ab  5   10  5   Q   
55  8   11  gh  ab  5   8   3   M   
70  12  13  ef  cd  9   11  2   P 

Creating Pajek network file

> source("https://raw.githubusercontent.com/bavla/Rnet/master/R/Pajek.R")
> n <- length(I); k <- 0; N <- data.frame(stringsAsFactors=FALSE)
> for(p in 1:(n-1)){i <- I[p]; tm <- E$f[i]
+   for(q in (p+1):n){j <- I[q]; k <- k+1
+     if(E$s[j]>tm) break
+     if(E$S[i]==E$S[j]){
+       fm <- min(E$f[i],E$f[j]); sM <- max(E$s[i],E$s[j]); T <- fm - sM
+       if(T>0) N <- rbind(N,list(u=E$ID[i],v=E$ID[j],s=sM,f=fm,rel=E$S[i])) 
+     }
+   }
+ }
> cat("density =",2*k/n/(n-1),"\n")
density = 0.7912088 
> N
   u  v s  f rel
1 cd ef 1  3   M
2 ab gh 3  5   P
3 ab ef 5  7   K
4 ef cd 2  7   Q
5 ef ab 5  7   Q
6 cd ab 5 10   Q
7 gh ab 5  8   M
8 ef cd 9 11   P
> sf <- as.matrix(N[,c("s","f")])
> uvrwt2net(N$u,N$v,w=N$f-N$s,r=N$rel,t=sf,directed=FALSE,Net="tra1.net")

tra1.net

tra1.svg

Visualization in Pajek

In Pajek

[Draw] Options/Colors/Arcs/Relation number
[Draw] Options/Colors/Edges/Relation number
[Draw] Export/2D/SVG/Multiple relations network
vlado/work/tra/net.txt · Last modified: 2023/11/27 16:28 by vlado
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki