-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruis.py
334 lines (259 loc) · 9.83 KB
/
ruis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as CM
import matplotlib.patches as mpatches
import numbers
import ctypes
import sys
import csv
import sqlite3
import pandas
import os
import math
class Analysis:
def __init__(self,
token_name=None,
rui_fname=None,
events_fname=None,
expertise=0,
offset=0,
debug=False):
##
## Initalize input parameters
##
self.token_name = token_name
self.rui_fname = rui_fname
self.events_fname = events_fname
self.expertise = expertise
self.offset = offset
self.data_is_good = False
self.events = []
self.rui_t0=None
self.events_t0=None
self.cuts = []
self.tags_times = {}
self.tags_colours = {}
##
## fill in the rui events and find the syncing token
##
if self.fill_rui_and_sync() == True and \
self.fill_events_and_sync() == True:
self.data_is_good = True
else:
return
self.t0_diff = (float(self.events_t0)+self.offset) - float(self.rui_t0)
self.start_time = 0
self.stop_time = self.events[len(self.events)-1].time
if(debug):
print "Time shift: ",self.t0_diff
print "Start time: ",self.start_time
print "Stop time : ",self.stop_time
print "offset: ", self.offset
def fill_rui_and_sync(self):
if self.rui_fname == '':
return False
print "RUI filename: ",self.rui_fname
f = open(self.rui_fname,'r')
self.events = []
for line in f:
self.events.append( Event(line, self.offset) )
for index, e in enumerate(self.events):
if self.events[index+0].spt[2] == '/' and \
self.events[index+1].spt[2] == 'l' and \
self.events[index+2].spt[2] == 'o' and \
self.events[index+3].spt[2] == 'g' and \
self.events[index+4].spt[2] == "RETURN":
self.rui_t0 = self.events[index+4].time
break
print "RUI log activated: ", self.rui_t0
if self.rui_t0 <> None:
return True
else:
return False
def fill_events_and_sync(self):
if self.events_fname == '':
return False
print "events filename: ",self.events_fname
csv_reader = pandas.read_csv(open(self.events_fname,'r'), sep='\t');
self.Behavior = csv_reader['Behavior']
self.Event_Type = csv_reader['Event_Type']
self.Time_Relative_sf = csv_reader['Time_Relative_sf']
for index, row in enumerate(self.Behavior):
if row == "Activate chatlog":
self.events_t0=self.Time_Relative_sf[index]
break
print "Activate chatlog: ",self.events_t0
if self.events_t0 <> None:
return True
else:
return False
def add_tags(self, tag_name, tag_colour):
start_times = []
stop_times = []
for index, row in enumerate(self.Behavior):
if row == tag_name:
if self.Event_Type[index] == "State start":
start_times.append( float(self.Time_Relative_sf[index]) - self.t0_diff + self.offset - eps )
elif self.Event_Type[index] == "State stop":
stop_times.append( float(self.Time_Relative_sf[index]) - self.t0_diff + self.offset + eps )
if len(start_times) <> len(stop_times):
sys.exit("tags length mismatch")
self.tags_times[tag_name] = zip(start_times,stop_times)
self.tags_colours[tag_name] = tag_colour
def add_point_tag(self, tag_name, tag_colour):
start_times = []
stop_times = []
for index, row in enumerate(self.Behavior):
if row == tag_name:
if self.Event_Type[index] == "Point":
start_times.append( float(self.Time_Relative_sf[index]) - self.t0_diff + self.offset )
stop_times.append( float(self.Time_Relative_sf[index]) - self.t0_diff + self.offset + 1 )
if len(start_times) <> len(stop_times):
sys.exit("tags length mismatch")
self.tags_times[tag_name] = zip(start_times,stop_times)
self.tags_colours[tag_name] = tag_colour
def add_cut( self, cut ):
assert isinstance(cut, tuple)
self.cuts.append( cut )
def print_cuts( self ):
print self.cuts
def cut_by_tags(self, tag_name):
tags = self.tags_times[tag_name]
for tag in tags:
self.cuts.append(tag)
def cut_all_but(self,
tag_name,
start_time=None,
stop_time=None):
if start_time == None:
start_time = self.start_time
if stop_time == None:
stop_time = self.stop_time
tags = self.tags_times[tag_name]
if len(tags)==0 : return
self.cuts.append( (start_time, tags[0][0] ) )
for index, tag in enumerate(tags):
try:
self.cuts.append( (tags[index][1], tags[index+1][0]) )
except:
self.cuts.append( (tags[index][1], stop_time) )
def apply_cuts(self):
for e in self.events:
for c in self.cuts:
if e.time>c[0] and e.time <c[1]:
e.cut = True
def calc_play_time( self,
start_time=None,
stop_time=None ):
if start_time == None:
start_time = self.start_time
if stop_time == None:
stop_time = self.stop_time
time = stop_time - start_time
for cut in self.cuts:
time = time - (cut[1] - cut[0])
return time
def make_keys_hist(self):
keys={}
for i in accepted:
keys[i]=0
for i in range(len(self.events)-1):
e = self.events[i]
if e.cut == True:
continue
if e.spt[1] == 'KEY':
key = e.spt[2]
if key not in accepted:
s = key.split()
if s[0] == 'SHIFT':
try:
keys[key] = keys[key] + 1
except:
keys[key] = 1
continue
else:
print "unknown: ",key
continue
try:
keys[key] = keys[key] + 1
except:
keys[key] = 1
return keys
def make_time_keys(self):
x = []
y = []
for i in range(len(self.events)-1):
e = self.events[i]
if e.cut == True:
continue
if e.spt[1] == 'KEY':
key = e.spt[2]
if key not in accepted:
s = key.split()
if s[0] == 'SHIFT':
try:
y.append(accepted.index(s[2]))
x.append(e.time)
except: continue
continue
else:
print "unknown: ",key
continue
y.append(accepted.index(key))
x.append(e.time)
if e.spt[1] == 'Pressed' and e.spt[2] == 'Left':
x.append(e.time)
y.append(accepted.index('MOUSE Left'))
if e.spt[1] == 'Pressed' and e.spt[2] == 'Right':
x.append(e.time)
y.append(accepted.index('MOUSE Right'))
if e.spt[1] == 'Moved':
x.append(e.time)
y.append(accepted.index('MOUSE Moved'))
return x,y
def make_clicks_hist(self):
hx = []
hy = []
for i in range(len(self.events)-1):
e = self.events[i]
next_e = self.events[i+1]
x = -1
y = -1
if e.spt[1] == 'Pressed' and e.spt[2] == 'Left' and next_e.spt[1] == 'Released' and next_e.spt[2] == 'Left':
j = i - 1
while True:
prev_e = self.events[j]
#print j, prev_e.spt
if prev_e.spt[1] == 'Moved':
x = int(prev_e.spt[2])
y = int(prev_e.spt[3])
break
else:
j = j - 1
#apply cuts if necessary
if e.cut == False:
hx.append(x)
hy.append(y)
return hx,hy
class Event:
def __init__(self):
return
def __init__(self, line, offset):
self.data = line.rstrip()
self.spt = self.data.split('\t')
self.time = float(self.spt[0]) + offset
self.cut = False
def Print(self):
if self.spt[1] == 'Pressed':
print self.spt
accepted = ['LEFT','RIGHT','UP','DOWN','0','1','2','3','4','5','6','7','8','9','RETURN','SPACE','ESCAPE','ENTER','DELETE','TAB','.',',','-','/',';','=','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','MOUSE Moved','MOUSE Left','MOUSE Right']
accepted.reverse()
accepted_no_mouse = ['LEFT','RIGHT','UP','DOWN','0','1','2','3','4','5','6','7','8','9','RETURN','SPACE','ESCAPE','ENTER','DELETE','TAB','.',',','-','/',';','=','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
eps=5 #seconds
def get_colour(expertise):
return {
0 : "grey", #NA
1 : "blue", #Novice
2 : "red", #Intermediate
3 : "black" #Expert
}[expertise]