-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest.py
360 lines (313 loc) · 11.4 KB
/
test.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
import json
import rebucket
from rebucket import Stack
from rebucket import Frame
import time
def read_dataset(json_path):
with open(json_path) as json_file:
dataset_dict = json.load(json_file)
all_stacks = []
for stack_dict in dataset_dict:
stack_id = stack_dict['stack_id']
duplicated_stack = stack_dict['duplicated_stack']
frame_arr = []
for fram_dict in stack_dict['stack_arr']:
frame = Frame(fram_dict)
frame_arr.append(frame)
all_stacks.append(Stack(stack_id, frame_arr, duplicated_stack))
return all_stacks
def generate_realbuckets(stacks):
real_buckets = []
for stack in stacks:
found = False
for bucket in real_buckets:
if stack.id in bucket and stack.duplicated_stack in bucket:
found = True
break
if stack.id not in bucket and stack.duplicated_stack not in bucket:
continue
# real_buckets.append([stack.id])
if stack.id in bucket:
for d_stack in stacks:
if d_stack.id == stack.duplicated_stack:
bucket.append(d_stack.id)
else:
bucket.append(stack.id)
found = True
if not found:
real_buckets.append([stack.id])
for d_stack in stacks:
if d_stack.id == stack.duplicated_stack:
real_buckets[-1].append(d_stack.id)
return real_buckets
def purity(real_buckets, BUCKETS, flag = False):
buckets = []
if not flag:
for bucket in BUCKETS:
tmp_bucket = []
for stack in bucket:
tmp_bucket.append(stack.id)
buckets.append(tmp_bucket)
else:
buckets = BUCKETS
N = 0.0
for bucket in real_buckets:
N += len(bucket)
purity = 0.0
for j in range(0, len(buckets)):
pur = []
for i in range(0, len(real_buckets)):
Li_Cj = len(list(set(real_buckets[i]).intersection(set(buckets[j]))))
precision = float(Li_Cj)/float(len(buckets[j]))
pur.append(precision)
purity += float(len(buckets[j])) * float(max(pur)) / float(N)
return purity
def inverse_purity(real_buckets, BUCKETS, flag = False):
buckets = []
if not flag:
for bucket in BUCKETS:
tmp_bucket = []
for stack in bucket:
tmp_bucket.append(stack.id)
buckets.append(tmp_bucket)
else:
buckets = BUCKETS
N = 0.0
for bucket in real_buckets:
N += len(bucket)
inverse_purity = 0.0
for i in range(0, len(real_buckets)):
inverse_pur = []
for j in range(0, len(buckets)):
Li_Cj = len(list(set(real_buckets[i]).intersection(set(buckets[j]))))
recall = float(Li_Cj)/float(len(real_buckets[i]))
inverse_pur.append(recall)
inverse_purity += float(len(real_buckets[i])) * float(max(inverse_pur)) / float(N)
return inverse_purity
def wrong(real_buckets, BUCKETS, flag = False):
buckets = []
if not flag:
for bucket in BUCKETS:
tmp_bucket = []
for stack in bucket:
tmp_bucket.append(stack.id)
buckets.append(tmp_bucket)
else:
buckets = BUCKETS
N = 0.0
for bucket in real_buckets:
N += len(bucket)
wrong_set = []
for j in range(0, len(buckets)):
found = False
for i in range(0, len(real_buckets)):
if set(buckets[j]).issubset(set(real_buckets[i])):
found = True
if not found:
wrong_set.append(buckets[j])
real_set = []
for bucket in wrong_set:
for stack in bucket:
for real_bucket in real_buckets:
if stack in real_bucket:
if real_bucket not in real_set:
real_set.append(real_bucket)
debug = False
if debug:
for bucket in wrong_set:
for stack in bucket:
for real_bucket in real_buckets:
if stack in real_bucket:
print "Real bucket is " + str(real_bucket)
print "Wrong bucket is " + str(bucket)
return len(real_set) - len(wrong_set)
def meature_result(real_buckets, BUCKETS, flag = False):
buckets = []
if not flag:
for bucket in BUCKETS:
tmp_bucket = []
for stack in bucket:
tmp_bucket.append(stack.id)
buckets.append(tmp_bucket)
else:
buckets = BUCKETS
N = 0.0
for bucket in real_buckets:
N += len(bucket)
fmeasure = 0.0
for i in range(0, len(real_buckets)):
f = []
for j in range(0, len(buckets)):
Li_Cj = len(list(set(real_buckets[i]).intersection(set(buckets[j]))))
precision = float(Li_Cj)/float(len(buckets[j]))
recall = float(Li_Cj)/float(len(real_buckets[i]))
if precision == 0 or recall == 0:
f.append(0)
else:
f.append(float((2 * precision * recall) / (precision + recall)) )
fmeasure += float(len(real_buckets[i])) * float(max(f)) / float(N)
return fmeasure
def train(dataset):
#---Training---
c = 0.0
c_best = 0.04
c_max = 2.0
dist = 0.0
dist_best = 0.06
dist_max = 1.0
o = 0.0
o_best = 0.13
o_max = 2.0
s = 0.01
real_buckets = generate_realbuckets(dataset)
fm_max = 0.0
while dist < dist_max:
rebuckets = rebucket.clustering(dataset, c, o ,dist)
f_m = meature_result(real_buckets, rebuckets)
if f_m > fm_max:
fm_max = f_m
dist_best = dist
dist += s
print "best dist is " + str(dist_best)
fm_max = 0.0
while o < o_max:
rebuckets = rebucket.clustering(dataset, c, o ,dist_best)
f_m = meature_result(real_buckets, rebuckets)
if f_m > fm_max:
fm_max = f_m
o_best = o
o += s
print "best o is " + str(o_best)
fm_max = 0.0
while c < c_max:
rebuckets = rebucket.clustering(dataset, c, o_best ,dist_best)
f_m = meature_result(real_buckets, rebuckets)
if f_m > fm_max:
fm_max = f_m
c_best = c
c += s
print "best c is " + str(c_best)
#---Training End---
return [c_best, o_best, dist_best]
def profile(json_path, para, test_num):
profile_num = 5000
c_best = para[0]
o_best = para[1]
dist_best = para[2]
all_stacks = read_dataset(json_path)
if test_num > len(all_stacks):
test_num = len(all_stacks)
real_buckets = generate_realbuckets(all_stacks[0:test_num])
print "testing"
print meature_result(real_buckets, real_buckets, True)
print "Wrong = " + str(wrong(real_buckets, real_buckets, True))
print "end testing"
start = time.time()
count = 0
for stack in all_stacks:
count += 1
if count == profile_num:
t_start = time.time()
rebucket.single_pass_clustering(stack, c_best, o_best ,dist_best)
if count == profile_num:
t_end = time.time()
print "t_end = " + str(t_end - t_start)
if count % 100 == 0:
print count
if count == test_num:
break
print "-----------"
print "Buckets = " + str(len(rebucket.BUCKETS))
print "F = " + str(meature_result(real_buckets, rebucket.BUCKETS))
print "purity = " + str(purity(real_buckets, rebucket.BUCKETS))
print "inverse_purity = " + str(inverse_purity(real_buckets, rebucket.BUCKETS))
print "Wrong = " + str(wrong(real_buckets, rebucket.BUCKETS))
rebucket.BUCKETS = []
end = time.time()
print "Time = " + str(end - start)
start = time.time()
count = 0
for stack in all_stacks:
count += 1
if count == profile_num:
t_start = time.time()
rebucket.single_pass_clustering_4(stack, c_best, o_best ,dist_best)
if count == profile_num:
t_end = time.time()
print "t_end = " + str(t_end - t_start)
if count % 100 == 0:
print count
if count == test_num:
break
print "-----------"
print "Buckets = " + str(len(rebucket.BUCKETS))
print "F = " + str(meature_result(real_buckets, rebucket.BUCKETS))
print "purity = " + str(purity(real_buckets, rebucket.BUCKETS))
print "inverse_purity = " + str(inverse_purity(real_buckets, rebucket.BUCKETS))
print "Wrong = " + str(wrong(real_buckets, rebucket.BUCKETS))
rebucket.BUCKETS = []
end = time.time()
print "Time = " + str(end - start)
print "Test num " + str(test_num)
print "real buckets " + str(len(real_buckets))
def test(json_path, para):
c_best = para[0]
o_best = para[1]
dist_best = para[2]
all_stacks = read_dataset(json_path)
test_num = 2000
if test_num > len(all_stacks):
test_num = len(all_stacks)
real_buckets = generate_realbuckets(all_stacks[0:test_num])
print "testing"
print meature_result(real_buckets, real_buckets, True)
print "Wrong = " + str(wrong(real_buckets, real_buckets, True))
print "end testing"
print "-----------"
print "rebucket.clustering..."
rebuckets = rebucket.clustering(all_stacks[0:test_num], c_best, o_best ,dist_best)
print "Buckets = " + str(len(rebuckets))
print "F = " + str(meature_result(real_buckets, rebuckets))
print "purity = " + str(purity(real_buckets, rebuckets))
print "inverse_purity = " + str(inverse_purity(real_buckets, rebuckets))
print "Wrong = " + str(wrong(real_buckets, rebuckets))
rebucket.BUCKETS = []
print "-----------"
print "prefix match..."
prefix_buckets = rebucket.prefix_match(all_stacks[0:test_num])
print "Buckets = " + str(len(prefix_buckets))
print "F = " + str(meature_result(real_buckets, prefix_buckets))
print "purity = " + str(purity(real_buckets, prefix_buckets))
print "inverse_purity = " + str(inverse_purity(real_buckets, prefix_buckets))
print "Wrong = " + str(wrong(real_buckets, prefix_buckets))
rebucket.BUCKETS = []
print "Test num "+str(test_num)
print "real buckets " + str(len(real_buckets))
def main():
json_path = 'dataset/eclipse/df_eclipse.json'
#json_path = 'dataset/JDT/df_eclipse_jdt.json'
all_stacks = read_dataset(json_path)
need_train = False
c_best = 0.04
o_best = 0.13
dist_best = 0.06
# dist_best = 0.06
para = [c_best, o_best, dist_best]
if need_train:
print "training...."
para = train(all_stacks[0:400])
print "Result is " + str(para)
json_arr = ["dataset/Firefox/df_mozilla_firefox.json","dataset/mozilla_core/df_mozilla_core.json",
"dataset/eclipse/df_eclipse.json","dataset/JDT/df_eclipse_jdt.json"]
need_test = True
if need_test:
for json_path in json_arr:
print "------------------------"
print json_arr
test(json_path, para)
print "------------------------"
need_profile = False
if need_profile:
profile(json_arr[2], para, 5001)
if __name__ == "__main__":
main()