-
Notifications
You must be signed in to change notification settings - Fork 0
/
time_statistics.py
179 lines (140 loc) · 6.13 KB
/
time_statistics.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
#!/usr/bin/env python3
from argparse import ArgumentParser
import log_reader
import datetime
import math
import collections
def speed_filter(strokes, speed_activate, speed_deactivate, sample_duration):
strokes_buffer = []
active_periods = []
activate_time = None
strokes_i = 0
while strokes_i < len(strokes):
popped_stroke = None
if len(strokes_buffer) > 0 and \
strokes[strokes_i].time - strokes_buffer[0].time > sample_duration:
popped_stroke = strokes_buffer[0]
del strokes_buffer[0]
else:
strokes_buffer.append(strokes[strokes_i])
strokes_i += 1
speed = len(strokes_buffer)/sample_duration.total_seconds()
if speed >= speed_activate and activate_time is None:
activate_time = strokes_buffer[-1].time
elif speed < speed_deactivate and not activate_time is None:
deactivate_time = popped_stroke.time
active_periods.append((activate_time, deactivate_time))
activate_time = None
if not activate_time is None:
deactivate_time = strokes[-1].time
active_periods.append((activate_time, deactivate_time))
activate_time = None
return active_periods
def intersection(a1, a2, b1, b2):
return (max(a1, b1), min(a2, b2))
class LogStat:
def __init__(self, period):
self.period = period
self.stroke_count = 0
self.character_count = 0
self.undo_stroke_count = 0
self.undo_character_count = 0
self.active_time = datetime.timedelta(0)
def add_period(self, period):
period_intersection = intersection(
self.period[0], self.period[1],
period[0], period[1])
period_intersection_delta = period_intersection[1] - period_intersection[0]
if period_intersection_delta > datetime.timedelta(0):
self.active_time += period_intersection_delta
def add_stroke(self, stroke):
if stroke.stroke == "*":
self.undo_stroke_count += 1
else:
self.stroke_count += 1
for translation in stroke.undo_translations:
self.undo_character_count += len(translation.text)
for translation in stroke.do_translations:
self.character_count += len(translation.text)
def to_csv_row(self, row, add_derived):
row = str(row)
return self.period[0].isoformat() + "," \
+ str(self.stroke_count) + "," \
+ str(self.character_count) + "," \
+ str(self.undo_stroke_count) + "," \
+ str(self.undo_character_count) + "," \
+ str(self.active_time.total_seconds()) + "," \
+ (("=B" + row + " + D" + row + "," \
+ "=B" + row + " - D" + row + "," \
+ "=C" + row + " - E" + row + "," \
+ "=I" + row + "/5," \
+ "=G" + row + " / J" + row + "," \
+ "=H" + row + " / J" + row + "," \
+ "=G" + row + " * 60 / F" + row + "," \
+ "=H" + row + " * 60 / F" + row + "," \
+ "=J" + row + " * 60 / F" + row + ","
) if add_derived else "")
@staticmethod
def csv_header(add_derived):
return "time," \
+ "strokes," \
+ "characters," \
+ "undo strokes," \
+ "undo characters," \
+ "active time (seconds)," \
+ (("total strokes," \
+ "net strokes," \
+ "net characters," \
+ "net words (characters/5)," \
+ "stroke/word," \
+ "net stroke/word," \
+ "stroke/min," \
+ "net stroke/min," \
+ "net word/min,"
) if add_derived else "")
arg_parser = ArgumentParser(description="Measure statistics over time in Plover logs. Outputs as CSV to standard out.")
arg_parser.add_argument("logs", nargs="+", help="log file paths")
arg_parser.add_argument("-r", "--resume", help="start recording after encountering this translation")
arg_parser.add_argument("-s", "--suspend", help="stop recording when encountering this translation")
arg_parser.add_argument("-sa", "--speed_activation", nargs=3, type=float, help="speed to start recording on (stroke/second), speed to stop recording on (stroke/second), length of window to check speed in (seconds)")
arg_parser.add_argument("-w", "--sample-window", required=True, type=float, help="duration of time (seconds) to sample for each discrete statistic")
arg_parser.add_argument("--raw", action="store_true", help="raw statistics only, no derived")
args = arg_parser.parse_args()
log = []
for log_file in args.logs:
with open(log_file) as data_file:
log += data_file.readlines()
log_strokes = log_reader.process_log(log, args.resume, args.suspend)
sample_duration = datetime.timedelta(seconds = args.sample_window)
active_periods = [(log_strokes[0].time, log_strokes[-1].time)]
if not args.speed_activation is None:
active_periods = speed_filter(
log_strokes,
args.speed_activation[0],
args.speed_activation[1],
datetime.timedelta(seconds = args.speed_activation[2]))
log_stats = []
log_stats.append(LogStat((log_strokes[0].time, log_strokes[0].time + sample_duration)))
stroke_i = 0
for active_period in active_periods:
while active_period[0] >= log_stats[-1].period[1]:
log_stats.append(LogStat((
log_stats[-1].period[1],
log_stats[-1].period[1] + sample_duration)))
log_stats[-1].add_period(active_period)
while log_strokes[stroke_i].time < active_period[0]:
stroke_i += 1
while log_strokes[stroke_i].time < active_period[1]:
stroke = log_strokes[stroke_i]
while stroke.time >= log_stats[-1].period[1]:
log_stats.append(LogStat((
log_stats[-1].period[1],
log_stats[-1].period[1] + sample_duration)))
log_stats[-1].add_period(active_period)
log_stats[-1].add_stroke(stroke)
stroke_i += 1
print(LogStat.csv_header(not args.raw))
row = 2
for log_stat in log_stats:
print(log_stat.to_csv_row(row, not args.raw))
row += 1