-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransition_aggregator.py
More file actions
268 lines (227 loc) · 9.27 KB
/
transition_aggregator.py
File metadata and controls
268 lines (227 loc) · 9.27 KB
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
import log
from collections import namedtuple, defaultdict
from statistics import mean, median, quantiles
KeyStat = namedtuple("KeyStat", ["key", "time"])
class TransitionAggregator:
def wpm(self, key_time_in_seconds):
return 60 / key_time_in_seconds / 5
def stages(self, transitions):
return sum([1 for t in transitions if t.start == "START"])
def errors(self, transitions):
return len(self.last_errors(transitions))
def adjusted_key_stats(self, transitions):
"""
Gives the total time in seconds it took to write the given character correctly. Accounts for error correction.
"""
result = defaultdict(list)
current_time = 0
skipped_letters = ""
for t in transitions:
if t.start == "START":
skipped_letters += t.end
continue
if t.state != "CORRECT":
current_time += t.time / 1000
else:
result[t.end].append(current_time + t.time / 1000)
current_time = 0
log.debug("Skipped letters: {}".format("".join(sorted(skipped_letters))))
return result
def calculate_for_adjusted_keys(self, adjusted_key_stats, function):
return {
k: function(v)
for k, v in sorted(
adjusted_key_stats.items(), key=lambda x: function(x[1]),
)
}
def calculate_for_transitions(self, transitions, function):
return self.calculate_for_adjusted_keys(
self.adjusted_key_stats(transitions), function
)
def total_time_for_keys(self, transitions):
return self.calculate_for_transitions(transitions, sum)
def count_for_keys(self, transitions):
return self.calculate_for_transitions(transitions, len)
def mean_for_keys(self, transitions):
return self.calculate_for_transitions(
transitions, function=lambda x: self.wpm(mean(x))
)
def median_for_keys(self, transitions):
return self.calculate_for_transitions(
transitions, function=lambda x: self.wpm(median(x))
)
def p95_for_keys(self, transitions):
stats = {
k: v for k, v in self.adjusted_key_stats(transitions).items() if len(v) > 9
}
result = {}
for k, v in stats.items():
result[k] = quantiles(v, n=20, method="inclusive")[-1]
log.debug("P95: {}".format(result))
return {
k: self.wpm(v)
for k, v in sorted(result.items(), key=lambda x: x[1], reverse=True)
if v > 0
}
def time_accuracy_for_keys(self, transitions):
total = self.total_time_for_keys(transitions)
# TODO needs fixing. use normal error counting method without limit based on adjusted key stats?
error = self.total_error_time_for_keys(transitions)
log.debug(
"Counting time accuracy 1 - error/total: 1 - {}/{}".format(error, total)
)
return {k: (v - error[k]) / v if k in error else 1 for k, v in total.items()}
def total_error_time_for_keys(self, transitions):
# TODO rewrite this method, it should not take limit into account anymore with new transition system
limit = 100
result = defaultdict(int)
keypresses = defaultdict(int)
last_correct_key = None
for t in reversed(transitions):
if t.state == "CORRECT" and (keypresses[t.end] <= limit or limit == 0):
keypresses[t.end] += 1
if t.state == "CORRECT":
last_correct_key = t.end
if (
keypresses[last_correct_key] > 0
and (keypresses[last_correct_key] <= limit or limit == 0)
and t.state != "CORRECT"
and len(last_correct_key) == 1
and last_correct_key is not None
):
# TODO unify everything to seconds
result[last_correct_key] += t.time / 1000
return {
k: v for k, v in sorted(result.items(), key=lambda x: x[1], reverse=True)
}
def last_errors(self, transitions, max_errors=None):
"""
Gives back sorted string of length max_errors (or all of it if ommited).
Error means the character that the user wanted to type but mistyped.
"""
original_error = False
result = ""
for t in transitions:
if t.state == "ERROR" and not original_error:
original_error = True
if t.state == "CORRECT" and original_error:
original_error = False
result += t.end
if max_errors is not None:
limit = min(max_errors, len(result))
else:
limit = len(result)
return result[:limit]
def key_presses(self, transitions):
return len(transitions)
def correct(self, transitions):
return sum([1 for t in transitions if t.state == "CORRECT"])
def erases(self, transitions):
return sum([1 for t in transitions if t.state == "ERASE"])
def total_time(self, transitions):
return sum([t.time for t in transitions]) / 1000
def time_fixing_errors(self, transitions):
return (
sum(
[
t.time
for t in transitions
if t.state == "ERROR" or t.state == "ERASE"
]
)
/ 1000
)
def time_if_without_errors(self, transitions):
return self.total_time(transitions) - self.time_fixing_errors(transitions)
def accuracy(self, transitions):
return (
100
* self.correct(transitions)
/ (self.correct(transitions) + self.errors(transitions))
)
def key_stats(self, transitions, filter_function=lambda x: x.start != "START"):
d = defaultdict(list)
for t in transitions:
if filter_function(t):
d[t.end].append(t.time)
return d
def calculate_stats(self, stats, aggregate):
result = {}
for k, v in stats.items():
result[k] = aggregate(v)
return result
def total_wpm(self, transitions):
return self.correct(transitions) / self.total_time(transitions) * 60 / 5
def format_dict(self, dictonary, max_entries, format_string="'{}' {:.1f}s "):
result = ""
entries = 0
for k, v in dictonary.items():
result += format_string.format(k, v)
entries += 1
if entries == max_entries:
return result
return result
def summary(self, transitions):
def round_dict(d):
result = {}
for k, v in d.items():
result[k] = round(v, 1)
return result
errors = self.last_errors(transitions)
return """Aggregations:
Stages: {}
Key presses: {}
Correct: {}
Errors: {}
Erases: {}
WPM: {:.1f}
WPM if w/o error: {:.1f} (if you typed at your current speed but without errors)
Least perfect WPM: {:.1f} (if you typed without error, but slower, this will be enough to reach your speed)
Total time: {:.1f}s
Time fixing errors: {:.1f}s
Time w/o error: {:.1f}s
Accuracy: {:.1f}%
Time Accuracy: {:.1f}% (% time spent on correct chars)
Last Errors: {}
Keys:
Counts: {}
Total Times: {}
Mean [WPM]: {}
Median [WPM]: {}
P95 [WPM]: {}
Errors: {}
""".format(
self.stages(transitions),
self.key_presses(transitions),
self.correct(transitions),
len(self.last_errors(transitions)),
self.erases(transitions),
self.total_wpm(transitions),
self.correct(transitions)
/ self.time_if_without_errors(transitions)
* 60
/ 5,
self.time_if_without_errors(transitions)
/ self.total_time(transitions)
* (self.correct(transitions) / self.total_time(transitions) * 60 / 5),
self.total_time(transitions),
self.time_fixing_errors(transitions),
self.time_if_without_errors(transitions),
self.accuracy(transitions),
self.time_if_without_errors(transitions)
/ self.total_time(transitions)
* 100,
"".join(
sorted(
sorted(errors.replace(" ", "")),
key=lambda l: errors.count(l),
reverse=True,
)
),
self.format_dict(self.count_for_keys(transitions), 25, "'{}' {} "),
self.format_dict(self.total_time_for_keys(transitions), 15),
self.format_dict(self.mean_for_keys(transitions), 15, "'{}' {:.1f} "),
self.format_dict(self.median_for_keys(transitions), 15, "'{}' {:.1f} "),
self.format_dict(self.p95_for_keys(transitions), 15, "'{}' {:.1f} "),
self.format_dict(self.total_error_time_for_keys(transitions), 15),
)