-
Notifications
You must be signed in to change notification settings - Fork 1
/
DFA_utils_tree_minerror.py
453 lines (358 loc) · 11 KB
/
DFA_utils_tree_minerror.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
import random
from GLOBAL_VARS import *
def clean_dfa(q_0, dfa, T):
"""
This code removes transitions (and nodes) that are not used.
All those transitions are forced to become self-loops
"""
not_used = set([k for k in dfa])
for tau,g in T:
q = q_0
for sigma in tau:
not_used.discard((q,sigma))
if (q,sigma) in dfa:
q = dfa[(q,sigma)]
# removing transitions that were not used
for r in not_used:
del dfa[r]
def _update_ok(g,g_pos,ok,is_q0_pos,q,t):
if q == 0:
guess = is_q0_pos
else:
guess = (q%2 == 0)
if MULTILABEL:
if g_pos in g and guess == 1:
ok[t] += 1.0
elif g_pos not in g and guess == 0:
ok[t] += 1.0
else:
if g == g_pos and guess == 1:
ok[t] += 1.0
elif g != g_pos and guess == 0:
ok[t] += 1.0
def test_binary_accuracy(q_0, dfa, T, g_pos, is_q0_pos, always_returns_no, smoothing=True):
"""
Returns binary classification statistics for a given DFA:
accuracy, precision, recall, true negative rate, false negative rate, f1 score
"""
correct = 0
total = 0
tp = 0
tn = 0
fn = 0
total_true_positive = 0
total_positive_guess = 0
fp = 0
for tau,g in T:
q = q_0
for sigma in tau:
if (q, sigma) in dfa:
q = dfa[(q, sigma)]
if q == q_0:
guess = is_q0_pos
else:
guess = (q % 2 == 0)
if MULTILABEL:
if g_pos in g and guess == 1:
correct += 1
tp += 1
elif g_pos not in g and guess == 0:
correct += 1
total += 1
if g_pos in g:
total_true_positive += 1
if g_pos not in g and guess == 1:
fp += 1
if g_pos not in g and guess == 0:
tn += 1
if g_pos in g and guess == 0:
fn += 1
if guess == 1:
total_positive_guess += 1
else:
if g == g_pos and guess == 1:
correct += 1
tp += 1
elif g != g_pos and guess == 0:
correct += 1
total += 1
if g == g_pos:
total_true_positive += 1
if g != g_pos and guess == 1:
fp += 1
if g != g_pos and guess == 0:
tn += 1
if g == g_pos and guess == 0:
fn += 1
if guess == 1:
total_positive_guess += 1
acc = float(correct)/total
if smoothing:
recall = max((float(tp)-1) / total_true_positive, 0)
tnr = max((float(tn)-1) / (total - total_true_positive), 1/2/(total - total_true_positive))
if total - total_positive_guess == 0:
fnr = 0.05
else:
fnr = max(min(float(fn) / (total - total_positive_guess), 1 - 1/(total- total_positive_guess)), 1/(total-total_positive_guess))
if total_true_positive <= 5 and recall < 0.2:
recall = 0.2
if total_positive_guess == 0 or total_positive_guess == 1:
precision = 0.5
else:
precision = max(min(float(tp)/total_positive_guess, 1-1/total_positive_guess), 1/total_positive_guess)
f1_score = 2 * (precision * recall) / (precision + recall)
else:
recall = float(tp) / total_true_positive
tnr = float(tn) / (total - total_true_positive)
fnr = float(fn) / (total - total_positive_guess) if total - total_positive_guess > 0 else 0.05
precision = float(tp)/total_positive_guess if total_positive_guess > 0 else 0.5
f1_score = 0
return (acc, precision, recall, tnr, fnr, f1_score)
def test_binary_convergence(q_0, dfa, T, g_pos, is_q0_pos):
"""
This code tests the binary classificaion performance of the automata over the set of traces T
It uses the convergence analysis (shows the performance over time while the traces are progressively shown)
"""
total = float(len(T))
ok = [0.0]*(max([len(tau) for tau,_ in T]) + 1)
for tau,g in T:
t = 0
q = q_0
for sigma in tau:
_update_ok(g,g_pos,ok,is_q0_pos,q,t)
if (q,sigma) in dfa:
# Recall that non-defined transitions are treated as self-loops
q = dfa[(q,sigma)]
t += 1
# completing the rest of the trace with the final classification
while t < len(ok):
_update_ok(g,g_pos,ok,is_q0_pos,q,t)
t+=1
# normalizing the accuracy
test_performance = [value/total for value in ok]
return test_performance
def _update_multigoal_ok_bayes(g,q,DFAs,ok,ok_prob, t, topk = 1):
"""
Uses statistics from a validation set to perform Bayesian inference over the set of possible goals.
See supplementary material from the paper for the derivation of this technique.
"""
probs = {}
sum_probs = 0
probs_sorted = []
for _g in DFAs:
if DFAs[_g].guess(q[_g]) == 1:
probs[_g] = DFAs[_g].goal_priors[_g] * DFAs[_g].recall / (1-DFAs[_g].tnr)
else:
probs[_g] = DFAs[_g].goal_priors[_g] * (1 - DFAs[_g].recall) / DFAs[_g].tnr
sum_probs += probs[_g]
probs_sorted.append((probs[_g], _g))
probs_sorted.sort()
probs_sorted.reverse()
ok_prob[t] += probs[g] / sum_probs
# Check if g is within the top k goals
for i in range(min(topk, len(probs_sorted))):
if probs_sorted[i][1] == g:
ok[t] += 1
break
def _update_multilabel_ok(gs,q,DFAs,ok, t):
"""
Only used for the multi-label experiments.
"""
assert(MULTILABEL)
probs = {}
acc = 0
for _g in DFAs:
if DFAs[_g].guess(q[_g]) == 1 and _g in gs:
acc += 1
elif DFAs[_g].guess(q[_g]) == 0 and _g not in gs:
acc += 1
ok[t] += acc / len(DFAs)
def test_multilabel_convergence(DFAs, T):
"""
Only used for the multi-label experiments.
"""
assert(MULTILABEL)
total = float(len(T))
ok = [0.0]*(max([len(tau) for tau,_ in T]) + 1)
for tau,gs in T:
t = 0
q = dict([(_g,DFAs[_g].q_0) for _g in DFAs])
for sigma in tau:
_update_multilabel_ok(gs,q,DFAs,ok, t)
for _g in DFAs:
if (q[_g],sigma) in DFAs[_g].dfa:
# Recall that non-defined transitions are treated as self-loops
q[_g] = DFAs[_g].dfa[(q[_g],sigma)]
t += 1
# completing the rest of the trace with the final classification
while t < len(ok):
_update_multilabel_ok(gs,q,DFAs,ok, t)
t+=1
# normalizing the accuracy
test_performance = [value/total for value in ok]
return test_performance
def get_prediction(q, DFAs):
"""
For a set of DFAs (one for each goal), returns the predicted goal and corresponding probability, under
the Bayesian inference method
"""
probs = {}
sum_probs = 0
probs_sorted = []
for _g in DFAs:
if DFAs[_g].guess(q[_g]) == 1:
probs[_g] = DFAs[_g].goal_priors[_g] * DFAs[_g].recall / (1-DFAs[_g].tnr)
else:
probs[_g] = DFAs[_g].goal_priors[_g] * (1 - DFAs[_g].recall) / DFAs[_g].tnr
sum_probs += probs[_g]
probs_sorted.append((probs[_g], _g))
probs_sorted.sort()
probs_sorted.reverse()
return probs_sorted[0][1], probs_sorted[0][0] / sum_probs
def test_multigoal_convergence(DFAs, T, topk = 1):
"""
This code tests the multigoal performance over the set of traces T.
It uses the convergence analysis (shows the performance over time while the traces are progressively shown).
This uses the Bayesian inference technique.
"""
total = float(len(T))
ok = [0.0]*(max([len(tau) for tau,_ in T]) + 1)
ok_prob = [0.0]*(max([len(tau) for tau,_ in T]) + 1)
for tau,g in T:
t = 0
q = dict([(_g,DFAs[_g].q_0) for _g in DFAs])
for sigma in tau:
_update_multigoal_ok_bayes(g,q,DFAs,ok,ok_prob, t, topk = topk)
for _g in DFAs:
if (q[_g],sigma) in DFAs[_g].dfa:
# Recall that non-defined transitions are treated as self-loops
q[_g] = DFAs[_g].dfa[(q[_g],sigma)]
t += 1
# completing the rest of the trace with the final classification
while t < len(ok):
_update_multigoal_ok_bayes(g,q,DFAs,ok,ok_prob, t, topk = topk)
t+=1
# normalizing the accuracy
test_performance = [value/total for value in ok]
test_performance_prob = [value/total for value in ok_prob]
return test_performance, test_performance_prob
def test_multigoal_percent_convergence(DFAs, T, topk = 1):
"""
Similar to `test_multigoal_convergence` but returns accuracy given 20%, 40%, 60%, 80%, and 100% of the full trace lengths
"""
total = float(len(T))
conv_accuracy = [0,0,0,0,0] # accuracy at 20%, 40%, 60%, 80%, 100% of traces
for tau,g in T:
t = 0
q = dict([(_g,DFAs[_g].q_0) for _g in DFAs])
ok = [0.0 for i in range(len(tau)+1)]
ok_prob = [0.0 for i in range(len(tau)+1)]
for sigma in tau:
_update_multigoal_ok_bayes(g,q,DFAs,ok,ok_prob, t, topk = topk)
for _g in DFAs:
if (q[_g],sigma) in DFAs[_g].dfa:
# Recall that non-defined transitions are treated as self-loops
q[_g] = DFAs[_g].dfa[(q[_g],sigma)]
t+=1
_update_multigoal_ok_bayes(g,q,DFAs,ok,ok_prob, t, topk = topk)
conv_accuracy[0] += ok[max(1,int(len(tau)*0.2))]
conv_accuracy[1] += ok[max(1,int(len(tau)*0.4))]
conv_accuracy[2] += ok[max(1,int(len(tau)*0.6))]
conv_accuracy[3] += ok[max(1,int(len(tau)*0.8))]
conv_accuracy[4] += ok[max(1,int(len(tau)*1))]
conv_accuracy[0] /= len(T)
conv_accuracy[1] /= len(T)
conv_accuracy[2] /= len(T)
conv_accuracy[3] /= len(T)
conv_accuracy[4] /= len(T)
return conv_accuracy
"""
The following functions are only used in the early prediction experiments
"""
def utility(t):
return max(1 - t/40, 0)
def test_early_utility(DFAs, T):
score = 0
time_percent = 0
for tau, g in T:
q = dict([(_g,DFAs[_g].q_0) for _g in DFAs])
t = 0
best_expected_utility = -1
prediction_goal = None
prediction_t = None
for sigma in tau:
for _g in DFAs:
if (q[_g],sigma) in DFAs[_g].dfa:
# Recall that non-defined transitions are treated as self-loops
q[_g] = DFAs[_g].dfa[(q[_g],sigma)]
t += 1
prediction, conf = get_prediction(q, DFAs)
expected_utility = utility(t) * conf
if expected_utility > best_expected_utility:
best_expected_utility = expected_utility
prediction_goal = prediction
prediction_t = t
if prediction_goal == g:
score += utility(prediction_t)
time_percent += prediction_t / len(tau)
print("Average score:", score / len(T))
print("Average time of prediction:", time_percent / len(T))
return score / len(T)
class DFA:
def __init__(self, q_0, dfa, confidence, g_pos, is_q0_pos, goal_priors):
self.q_0 = q_0
self.dfa = dfa
if confidence is not None:
self.accuracy = confidence[0]
self.precision = confidence[1]
self.recall = confidence[2]
self.tnr = confidence[3]
self.g_pos = g_pos
self.is_q0_pos = is_q0_pos
self.goal_priors = goal_priors
def guess(self,q):
"""
Helper function to return the binary classification decision of this DFA,
given the state
"""
if q == 0:
if self.is_q0_pos:
guess = 1
else:
guess = 0
else:
guess = (q % 2 == 0)
return guess
def get_most_common_performance(T):
"""
Accuracy given by guessing the most common goal in the data.
"""
goals = {}
for _,g in T:
if g not in goals:
goals[g] = 0
goals[g] +=1
return max([goals[g] for g in goals])/float(len(T))
def show_dfa(q_0, dfa, pos_prob):
"""
Show the DFA in a user-readable form
"""
print("\n----------- DFA -----------")
transitions = [(i,dfa[(i,sigma)],sigma) for i,sigma in dfa]
transitions.sort()
for i,j,sigma in transitions:
print(f"delta({i},{sigma}) = {j}")
print(f"q_0 = {q_0}")
# print(f"pos_prob = {pos_prob}")
def report_dfa(q_0, dfa, g):
"""
Give a complete description of the DFA as a string
"""
output = ""
output += "----------- DFA ----------- " + "Goal: " + g + " ----------- \n"
transitions = [(i,dfa[(i,sigma)],sigma) for i,sigma in dfa]
transitions.sort()
for i,j,sigma in transitions:
output += (f"delta({i},{sigma}) = {j}\n")
output += f"q_0 = {q_0}\n"
return output