-
Notifications
You must be signed in to change notification settings - Fork 0
/
spectrum_analysis.py
87 lines (64 loc) · 3 KB
/
spectrum_analysis.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
from keras import backend as K
from collections import defaultdict
import numpy as np
from utils import get_layer_outs
import math
'''
[1] Empirical Evaulation of the Tarantaul automatic fault localization technique
[2] Zoogeographic studies on the soleoids fishes found in Japan and its
neighbourings regions.
[3] The Dstar Method for Effective Fault Localization
'''
def scores_with_foo(trainable_layers, scores, num_cf, num_uf, num_cs, num_us, suspicious_num, foo):
for i in range(len(scores)):
for j in range(len(scores[i])):
score = foo(i, j)
if np.isnan(score):
score = 0
scores[i][j] = score
flat_scores = [float(item) for sublist in scores for item in sublist if not math.isnan(float(item))]
# grab the indexes of the highest suspicious_num scores
if suspicious_num == -1 or suspicious_num >= len(flat_scores):
flat_indexes = range(len(flat_scores))
else:
flat_indexes = np.argpartition(flat_scores, -suspicious_num)[-suspicious_num:]
suspicious_neuron_idx = []
for idx in flat_indexes:
# unflatten idx
i = 0
accum = idx
while accum >= len(scores[i]):
accum -= len(scores[i])
i += 1
j = accum
if trainable_layers is None:
suspicious_neuron_idx.append([i, j, flat_scores[idx], num_cf[i][j], num_uf[i][j], num_cs[i][j], num_us[i][j]])
else:
suspicious_neuron_idx.append([trainable_layers[i], j, flat_scores[idx], num_cf[i][j], num_uf[i][j], num_cs[i][j], num_us[i][j]])
suspicious_neuron_idx.sort(key=lambda x: x[2], reverse=True)
return suspicious_neuron_idx
def tarantula_analysis(trainable_layers, scores, num_cf, num_uf, num_cs, num_us, suspicious_num):
"""
More information on Tarantula fault localization technique can be found in
[1]
"""
def tarantula(i, j):
return float(float(num_cf[i][j]) / (num_cf[i][j] + num_uf[i][j])) / \
(float(num_cf[i][j]) / (num_cf[i][j] + num_uf[i][j]) + float(num_cs[i][j]) / (num_cs[i][j] + num_us[i][j]))
return scores_with_foo(trainable_layers, scores, num_cf, num_uf, num_cs, num_us, suspicious_num, tarantula)
def ochiai_analysis(trainable_layers, scores, num_cf, num_uf, num_cs, num_us, suspicious_num):
"""
More information on Ochiai fault localization technique can be found in
[2]
"""
def ochiai(i, j):
return float(num_cf[i][j]) / (((num_cf[i][j] + num_uf[i][j]) * (num_cf[i][j] + num_cs[i][j])) ** .5)
return scores_with_foo(trainable_layers, scores, num_cf, num_uf, num_cs, num_us, suspicious_num, ochiai)
def dstar_analysis(trainable_layers, scores, num_cf, num_uf, num_cs, num_us, suspicious_num, star):
"""
More information on DStar fault localization technique can be found in
[3]
"""
def dstar(i, j):
return float(num_cf[i][j]**star) / (num_cs[i][j] + num_uf[i][j])
return scores_with_foo(trainable_layers, scores, num_cf, num_uf, num_cs, num_us, suspicious_num, dstar)