forked from karpathy/ulogme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
legacy_split_events.py
103 lines (87 loc) · 2.94 KB
/
legacy_split_events.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
# convert old type events to new type events, in case you used legacy ulogme code
# where all events were written to one file based on type. In new version these are
# split also by date.
import time
import datetime
import json
import os
import os.path
import sys
mint = -1
maxt = -1
ROOT = ''
RENDER_ROOT = os.path.join(ROOT, 'render')
def loadEvents(fname):
"""
Reads a file that consists of first column of unix timestamps
followed by arbitrary string, one per line. Outputs as dictionary.
Also keeps track of min and max time seen in global mint,maxt
"""
global mint, maxt # not proud of this, okay?
events = []
try:
ws = open(fname, 'r').read().splitlines()
events = []
for w in ws:
ix = w.find(' ') # find first space, that's where stamp ends
stamp = int(w[:ix])
str = w[ix+1:]
events.append({'t':stamp, 's':str})
if stamp < mint or mint==-1: mint = stamp
if stamp > maxt or maxt==-1: maxt = stamp
except Exception, e:
print 'could not load %s. Setting empty events list.' % (fname, )
print '(this is probably OKAY by the way, just letting you know)'
print e
events = []
return events
# load all window events
active_window_file = os.path.join(ROOT, 'logs/activewin.txt')
print 'loading windows events...'
wevents = loadEvents(active_window_file)
# load all keypress events
keyfreq_file = os.path.join(ROOT, 'logs/keyfreq.txt')
print 'loading key frequencies...'
kevents = loadEvents(keyfreq_file)
for k in kevents: # convert the key frequency to just be an int, not string
k['s'] = int(k['s'])
print 'loading notes...'
notes_file = os.path.join(ROOT, 'logs/notes.txt')
nevents = loadEvents(notes_file)
# rewind time to 7am on earliest data collection day
dfirst = datetime.datetime.fromtimestamp(mint)
dfirst = datetime.datetime(dfirst.year, dfirst.month, dfirst.day, 7) # set hour to 7am
curtime = int(dfirst.strftime("%s"))
out_list = []
while curtime < maxt:
t0 = curtime
t1 = curtime + 60*60*24 # one day later
# this will break if there are leap seconds... sigh :D
# filter events
e1 = [x for x in wevents if x['t'] >= t0 and x['t'] < t1]
e2 = [x for x in kevents if x['t'] >= t0 and x['t'] < t1]
e3 = [x for x in nevents if x['t'] >= t0 and x['t'] < t1]
# sort by time just in case
e1.sort(key = lambda x: x['t'])
e2.sort(key = lambda x: x['t'])
e3.sort(key = lambda x: x['t'])
# write out log files split up
if e3:
fout = 'logs/notes_%d.txt' % (t0, )
f = open(fout, 'w')
f.write(''.join( ['%d %s\n' % (x['t'], x['s']) for x in e3] ))
f.close()
print 'wrote ' + fout
if e2:
fout = 'logs/keyfreq_%d.txt' % (t0, )
f = open(fout, 'w')
f.write(''.join( ['%d %s\n' % (x['t'], x['s']) for x in e2] ))
f.close()
print 'wrote ' + fout
if e1:
fout = 'logs/window_%d.txt' % (t0, )
f = open(fout, 'w')
f.write(''.join( ['%d %s\n' % (x['t'], x['s']) for x in e1] ))
f.close()
print 'wrote ' + fout
curtime += 60*60*24