-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalysis.py
144 lines (113 loc) · 4.22 KB
/
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
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
from scipy.stats import ttest_ind, mannwhitneyu, tmean
import math
def get_distance(p1, p2):
dist = 0
for i in range(0, len(p1)):
dist += (p1[i] - p2[i]) ** 2
dist = math.sqrt(dist)
return dist
# perform bootstrapping to check noteworthiness
def bootstrap(pos, neg, pval):
better = 0
for pv in pos:
for nv in neg:
if pv < nv:
better += 1
ratio = better / (len(pos) * len(neg))
if ratio >= pval:
return True
else:
return False
# perform Welch's t-test to check noteworthiness
def welch_t(pos, neg, cl):
stat, pvalue = ttest_ind(pos, neg, equal_var=False)
if stat < 0 and pvalue < (1 - cl):
return True
else:
return False
# perform Mann-Whitney U test to check noteworthiness
def u_test(pos, neg, cl):
stat, pvalue = mannwhitneyu(pos, neg, alternative='less')
if pvalue < (1 - cl):
return True
else:
return False
# find noteworthy features from benchmarked samples
def get_noteworthy(features_, measurements_, obj_, goal=()):
_noteworthy = list()
common = list()
if len(measurements_) > 1:
if len(goal) != 0:
# sort by goal distance
sortedlist = sorted(measurements_, key=lambda x: x[2], reverse=False)
mindist = 0
second = list()
for i in range(1, len(sortedlist)):
dist = get_distance(sortedlist[0][1], sortedlist[i][1])
if i == 1:
mindist = dist
second = sortedlist[i][0]
elif dist < mindist:
mindist = dist
second = sortedlist[i][0]
# find common features from best two measurements
common = list(set(sortedlist[0][0]).intersection(second))
else:
# sort by objective
sortedlist = sorted(measurements_, key=lambda x: x[1][obj_], reverse=False)
# find common features from best two measurements
common = list(set(sortedlist[0][0]).intersection(sortedlist[1][0]))
# for c in common:
# if c > 0:
# print(features_[abs(c)-1][1], end=",")
# else:
# print('-' + features_[abs(c) - 1][1], end=",")
# print()
# check noteworthiness of common features
for c in common:
in_measure = list()
ex_measure = list()
for m in measurements_:
if len(goal) != 0:
if c in m[0]:
in_measure.append(m[2])
else:
ex_measure.append(m[2])
else:
if c in m[0]:
in_measure.append(m[1][obj_])
else:
ex_measure.append(m[1][obj_])
if len(in_measure) > 1 and len(ex_measure) > 1:
if tmean(in_measure) < tmean(ex_measure):
#res = welch_t(in_measure, ex_measure, 0.95)
#res = bootstrap(in_measure, ex_measure, 0.95)
res = u_test(in_measure, ex_measure, 0.95)
if res:
found = list()
found.append(c)
_noteworthy.append(found)
# for c in _noteworthy:
# if c[0] > 0:
# print(features_[abs(c[0])-1][1], end=",")
# else:
# print('-' + features_[abs(c[0]) - 1][1], end=",")
# print()
# filter selection of alternative features
filtered = list()
for ntw in _noteworthy:
if features_[abs(ntw[0]) - 1][2] == 'bool':
if len(ntw) == 1:
if len(features_[abs(ntw[0])-1]) > 2:
if features_[abs(ntw[0])-1][2] == 'choice_bool' or features_[abs(ntw[0])-1][2] == 'alt':
if ntw[0] < 0:
filtered.append(ntw)
else:
filtered.append(ntw)
else:
if ntw[0] < 0 and not features_[abs(ntw[0])-1][1].startswith('_X'):
filtered.append(ntw)
# print(noteworthy, end=';')
return filtered
if __name__ == "__main__":
file = ""