259 lines
11 KiB
Python
259 lines
11 KiB
Python
import helper
|
|
import csv
|
|
import dao
|
|
|
|
__author__ = 'mac'
|
|
|
|
|
|
def analysis_process(project_id, methold):
|
|
project_name = project_id
|
|
path = 'result2/' + project_name + '/'
|
|
path_analysis = path + methold + '/analysis/'
|
|
# path_data = path + 'data/'
|
|
|
|
csv_result = file(path_analysis + 'feature_precision_method.csv', 'rb')
|
|
result_reader = csv.reader(csv_result)
|
|
csv_analysis = file(path_analysis + 'feature_analysis.csv', 'wb')
|
|
writer_analysis = csv.writer(csv_analysis)
|
|
|
|
csv_analysis_before = file(path_analysis + 'analysis.csv', 'rb')
|
|
analysis_reader = csv.reader(csv_analysis_before)
|
|
# writer_classifier.writerow(['classifier infomation for project:',repr(project_name)])
|
|
|
|
|
|
break_count = 20 # cut the different, len(threshold)
|
|
change_break = 20 # cut the condition for changing class
|
|
|
|
feature_count_base = [0] * break_count
|
|
feature_pred_base = [0] * break_count
|
|
feature_right_base = [0] * break_count
|
|
feature_count_base2 = [0] * break_count
|
|
feature_pred_base2 = [0] * break_count
|
|
feature_right_base2 = [0] * break_count
|
|
|
|
feature_right_mine1 = [([0] * change_break) for i in range(break_count)]
|
|
feature_right_mine2 = [([0] * change_break) for i in range(break_count)]
|
|
feature_right_mine3 = [([0] * change_break) for i in range(break_count)]
|
|
feature_right_mine4 = [([0] * change_break) for i in range(break_count)]
|
|
|
|
feature_count_mine1 = [([0] * change_break) for i in range(break_count)]
|
|
feature_count_mine2 = [([0] * change_break) for i in range(break_count)]
|
|
feature_count_mine3 = [([0] * change_break) for i in range(break_count)]
|
|
feature_count_mine4 = [([0] * change_break) for i in range(break_count)]
|
|
|
|
result_set = [feature_count_base, feature_pred_base, feature_right_base, feature_count_base2, feature_pred_base2,
|
|
feature_right_base2, feature_right_mine1, feature_right_mine2, feature_right_mine3,
|
|
feature_right_mine4,
|
|
feature_count_mine1, feature_count_mine2, feature_count_mine3, feature_count_mine4]
|
|
|
|
status = [-1, 0]
|
|
flag = 0
|
|
for line in analysis_reader:
|
|
if line[0] == 'final threshold:':
|
|
final_threshold = int(line[1])
|
|
continue
|
|
if line[0] == 'final methold index for right count:':
|
|
flag = 1
|
|
continue
|
|
if line[0] == 'final index of methold for right count:':
|
|
flag = 2
|
|
continue
|
|
if flag == 1:
|
|
right_max_methold_index = [int(i) for i in line]
|
|
flag = 0
|
|
continue
|
|
if flag == 2:
|
|
right_max_index = [int(i) for i in line]
|
|
flag = 0
|
|
continue
|
|
|
|
# get data from line according to status
|
|
def get_data_from_line(line_info, status):
|
|
if status[0] < 6:
|
|
result_set[status[0]] = [int(each) for each in line_info]
|
|
elif status[0] >= 6:
|
|
result_set[status[0]][status[1]] = [int(each) for each in line_info]
|
|
|
|
def handle_line(line, status):
|
|
if line[0] == "base count for all:":
|
|
status[0] = 0
|
|
status[1] = 0
|
|
elif line[0] == "base right count for all:":
|
|
status[0] = 1
|
|
status[1] = 0
|
|
elif line[0] == "base pred count for all:":
|
|
status[0] = 2
|
|
status[1] = 0
|
|
elif line[0] == "base count for threshold:":
|
|
status[0] = 3
|
|
status[1] = 0
|
|
elif line[0] == "base right count for threshold:":
|
|
status[0] = 4
|
|
status[1] = 0
|
|
elif line[0] == "base pred count for threshold:":
|
|
status[0] = 5
|
|
status[1] = 0
|
|
elif line[0] == "right count of mine method 1 for each threshold:":
|
|
status[0] = 6
|
|
status[1] = 0
|
|
elif line[0] == "right count of mine method 2 for each threshold:":
|
|
status[0] = 7
|
|
status[1] = 0
|
|
elif line[0] == "right count of mine method 3 for each threshold:":
|
|
status[0] = 8
|
|
status[1] = 0
|
|
elif line[0] == "right count of mine method 4 for each threshold:":
|
|
status[0] = 9
|
|
status[1] = 0
|
|
elif line[0] == "change count of mine method 1 for each threshold:":
|
|
status[0] = 10
|
|
status[1] = 0
|
|
elif line[0] == "change count of mine method 2 for each threshold:":
|
|
status[0] = 11
|
|
status[1] = 0
|
|
elif line[0] == "change count of mine method 3 for each threshold:":
|
|
status[0] = 12
|
|
status[1] = 0
|
|
elif line[0] == "change count of mine method 4 for each threshold:":
|
|
status[0] = 13
|
|
status[1] = 0
|
|
else:
|
|
get_data_from_line(line, status)
|
|
status[1] = status[1] + 1
|
|
|
|
# read data from csv
|
|
for line in result_reader:
|
|
handle_line(line, status)
|
|
|
|
right_count = 0
|
|
change_count = 0
|
|
|
|
# change record according to get_analysis_result.py
|
|
|
|
for ind_threshold in range(int(final_threshold) + 1):
|
|
x_index = int(right_max_methold_index[ind_threshold])
|
|
y_index = int(right_max_index[ind_threshold])
|
|
feature_right_methold = [result_set[6], result_set[7], result_set[8], result_set[9]]
|
|
feature_count_methold = [result_set[10], result_set[11], result_set[12], result_set[13]]
|
|
right_count = right_count + int(feature_right_methold[x_index][ind_threshold][y_index])
|
|
change_count = change_count + int(feature_count_methold[x_index][ind_threshold][y_index])
|
|
|
|
part_improved_prf = helper.get_prec_recall_f1(right_count + result_set[1][final_threshold],
|
|
result_set[0][final_threshold],
|
|
change_count + result_set[2][final_threshold])
|
|
part_prf = helper.get_prec_recall_f1(result_set[1][final_threshold], result_set[0][final_threshold],
|
|
result_set[2][final_threshold])
|
|
improved_prf = helper.get_prec_recall_f1(right_count + result_set[1][-1], result_set[0][-1],
|
|
change_count + result_set[2][-1])
|
|
prf = helper.get_prec_recall_f1(result_set[1][-1], result_set[0][-1], result_set[2][-1])
|
|
print(final_threshold)
|
|
return [part_improved_prf, part_prf, improved_prf, prf, change_count]
|
|
|
|
# get best f1 score
|
|
|
|
# def get_f1_part(right_count, change_count,ind_threshold):
|
|
# f1_part = [[None]]*4
|
|
# for j in range(4):
|
|
# for i in range(change_break):
|
|
# f1_part[j].append(helper.get_prec_recall_f1(right_count[j][ind_threshold][i] + result_set[1][ind_threshold],
|
|
# result_set[0][ind_threshold],change_count[j][ind_threshold][i] + result_set[2][ind_threshold])[2])
|
|
# temp = [max(f1_part[0]),max(f1_part[1]),max(f1_part[2]),max(f1_part[3])]
|
|
# max_f1 = max(temp)
|
|
# x_index = temp.index(max_f1)
|
|
# y_index = f1_part[x_index].index(temp)
|
|
# return x_index,y_index,temp
|
|
#
|
|
# def get_f1(right_count, change_count,ind_threshold):
|
|
# f1 = [[],[],[],[]]
|
|
# for j in range(4):
|
|
# for i in range(change_break):
|
|
# f1[j].append(helper.get_prec_recall_f1(right_count[j][ind_threshold][i] + result_set[1][-1], result_set[0][-1],
|
|
# change_count[j][ind_threshold][i] + result_set[2][-1])[2])
|
|
# temp = [max(f1[0]),max(f1[1]),max(f1[2]),max(f1[3])]
|
|
# max_f1 = max(temp)
|
|
# x_index = temp.index(max_f1)
|
|
# y_index = f1[x_index].index(max_f1)
|
|
# return x_index,y_index
|
|
#
|
|
# def get_f1_2(right_count, change_count):
|
|
# f1 = []
|
|
# for i in range(change_break):
|
|
# f1.append(helper.get_prec_recall_f1(right_count[i] + result_set[1][-1], result_set[0][-1],
|
|
# change_count[i] + result_set[2][-1])[2])
|
|
# return f1.index(max(f1))
|
|
#
|
|
# right_method = [0]*break_count
|
|
# change_method = [0]*break_count
|
|
# x_index = [0]*break_count
|
|
# y_index = [0]*break_count
|
|
#
|
|
# diff_right = [0]*break_count
|
|
# temp_right = 0
|
|
# diff_change = [0]*break_count
|
|
# temp_change = 0
|
|
#
|
|
# for ind_threshold in range(break_count):
|
|
# x_index[ind_threshold],y_index[ind_threshold] = get_f1(result_set[6:10],result_set[10:14],ind_threshold)
|
|
# right_method[ind_threshold] = result_set[x_index[ind_threshold]+6][ind_threshold][y_index[ind_threshold]]
|
|
# change_method[ind_threshold] = result_set[x_index[ind_threshold]+10][ind_threshold][y_index[ind_threshold]]
|
|
#
|
|
# temp_right = temp_right + right_method[ind_threshold]
|
|
# diff_right[ind_threshold] = temp_right
|
|
# temp_change = temp_change + change_method[ind_threshold]
|
|
# diff_change[ind_threshold] = temp_change
|
|
#
|
|
# final_threshold = get_f1_2(diff_right,diff_change)
|
|
# right_count = diff_right[final_threshold]
|
|
# change_count = diff_change[final_threshold]
|
|
# part_improved_prf = helper.get_prec_recall_f1(right_count + result_set[1][final_threshold],
|
|
# result_set[0][final_threshold],
|
|
# change_count + result_set[2][final_threshold])
|
|
# part_prf = helper.get_prec_recall_f1(result_set[1][final_threshold], result_set[0][final_threshold],
|
|
# result_set[2][final_threshold])
|
|
# improved_prf = helper.get_prec_recall_f1(right_count + result_set[1][-1], result_set[0][-1],
|
|
# change_count + result_set[2][-1])
|
|
# prf = helper.get_prec_recall_f1(result_set[1][-1], result_set[0][-1], result_set[2][-1])
|
|
# return [part_improved_prf, part_prf, improved_prf, prf]
|
|
|
|
|
|
method = 'svm'
|
|
f_proj_id = file('proj_id.csv', 'r')
|
|
reader = csv.reader(f_proj_id)
|
|
final_path = 'final2/'
|
|
helper.mkdir(final_path)
|
|
f_all_precision = file(final_path + 'feature_precision_' + method + '.csv', 'w')
|
|
writer_final = csv.writer(f_all_precision)
|
|
precision_improve = []
|
|
precision_machine = []
|
|
p_id = []
|
|
writer_final.writerow(
|
|
['proj_id', 'issue_count', 'proj_name','change_count', 'improve_prec_all', 'mechine_prec_all', 'improve_prec', 'mechine_prec',
|
|
'improve_recall_all', 'mechine_recall_all', 'improve_recall', 'mechine_recall',
|
|
'improve_f1_all', 'mechine_f1_all', 'improve_f1', 'mechine_f1'])
|
|
|
|
# line = ['6', '500']
|
|
for line in reader:
|
|
# classifier_process(line[0])
|
|
print(line[0])
|
|
p_id.append(line[0])
|
|
temp = analysis_process(line[0], method)
|
|
precision_improve.append(temp)
|
|
# precision_machine.append(helper.get_precision(line[0]))
|
|
proj_infos = dao.get_proj_name_by_id(line[0])
|
|
for proj_info in proj_infos:
|
|
proj_name = proj_info[0]
|
|
# nb = helper.get_result_by_classifier('nb-result',line[0])
|
|
# rf = helper.get_result_by_classifier('rf-result',line[0])
|
|
# lrl1 = helper.get_result_by_classifier('lrl1-result',line[0])
|
|
# lrl2 = helper.get_result_by_classifier('lrl2-result',line[0])
|
|
# et = helper.get_result_by_classifier('nb-result',line[0])
|
|
# et_1000 = helper.get_result_by_classifier('nb-result',line[0])
|
|
# adaboost = helper.get_result_by_classifier('nb-result',line[0])
|
|
data = (line[0], line[1], proj_name,temp[4], temp[2][0] - temp[3][0], temp[3][0], temp[0][0] - temp[1][0], temp[1][0],
|
|
temp[2][1] - temp[3][1], temp[3][1], temp[0][1] - temp[1][1], temp[1][1],
|
|
temp[2][2] - temp[3][2], temp[3][2], temp[0][2] - temp[1][2], temp[1][2])
|
|
writer_final.writerow(data)
|
|
|
|
f_all_precision.close()
|
|
f_proj_id.close()
|