git_issue/analysis.py

264 lines
11 KiB
Python

__author__ = 'mac'
import numpy as np
import pymysql
import csv
import helper
path = ''
def get_analysis(proj_id,method):
path = 'result/'+proj_id+'/'+method+'/data/'
# get variance/different of each results
different = []
y_test = []
pred = []
for i in range(1,11,1):
probability = np.load(path + 'probability_' + repr(i) + ".npy")
y_test_temp = np.load(path + 'y_test_' + repr(i) + ".npy")
pred_temp = np.load(path + 'pred_' + repr(i) + ".npy")
for j in range(len(probability)):
diff = probability[j:j+1,0][0]-probability[j:j+1,1][0]
different.append(diff)
y_test.append(y_test_temp[j])
pred.append(pred_temp[j])
rank_result = np.argsort([abs(i) for i in different])
acc = np.mean(helper.get_precision(proj_id,method))
hard_count = round(len(different)*(1-acc))
hard_right = 0
easy_right = 0
print(len(different))
for i, ind in enumerate(rank_result):
status = (y_test[ind] == pred[ind])
if status:
if i < hard_count:
hard_right = hard_right+1
else:
easy_right = easy_right+1
return hard_right,hard_count,easy_right,len(different)-hard_count,acc
# method = 'svm'
# f_proj_id = file('proj_id.csv', 'r')
# reader = csv.reader(f_proj_id)
# final_path = 'final2/'
# helper.mkdir(final_path)
# f_hard_tongji = file(final_path + 'hard_count_'+method+'.csv','w')
# writer_final = csv.writer(f_hard_tongji)
# writer_final.writerow(['proj_id','hard_right','hard_count','easy_right','easy_count','acc'])
# for line in reader:
# temp = get_analysis(line[0],method)
# data = (line[0],temp[0],temp[1],temp[2],temp[3],temp[4])
# writer_final.writerow(data)
# f_hard_tongji.close()
# get result of whose id = n
def get_result_of_n(n):
print("-"*100)
print("get result of issue/pr:"+repr(n))
for i in range(1,10,1):
y_test = np.load(path + 'y_test_' + repr(i) + ".npy")
pred = np.load(path + 'pred_' + repr(i) + ".npy")
variance = np.load(path + 'variance' + repr(i) + ".npy")
different = np.load(path + 'different' + repr(i) + ".npy")
probability = np.load(path + 'probability' + repr(i) + ".npy")
x_id = np.load(path + 'x_id_' + repr(i) + ".npy")
for j in range(len(x_id)):
if x_id[j:j+1] == n:
print("x_id:"+repr(x_id[j:j+1].tolist())+"\ty_test:"+repr(y_test[j:j+1].tolist())+"\tpred:"+repr(pred[j:j+1].tolist())+"\tvariance:"+repr(variance[j:j+1].tolist())+"\tdifferent:"+repr(different[j:j+1].tolist()))
print(probability[j:j+1])
# get_result_of_n(19150)
# get title and description for selected issues and write in test.csv
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123456', db='zlb_github')
def get_title_description(issue_id,y_test,pred,path):
print("path:" + path + 'csv_hard.csv')
csvfile = file(path + 'csv_hard.csv', 'wb')
writer = csv.writer(csvfile)
writer.writerow(['id', 'y_test', 'pred', 'title', 'body'])
cur = conn.cursor()
for i in range(len(issue_id)):
# sql = "select title,body from "\
# +table+" where id = " + str(issue_id[i])
sql = 'select title, body from issues where id = '+repr(issue_id[i])
cur.execute(sql)
r = cur.fetchone()
if r:
data = (issue_id[i], y_test[i], pred[i], r[0], r[1])
writer.writerow(data)
csvfile.close()
# get result by set threshold of variance
def get_result_by_variance(n):
print("-"*100)
print("get result by variance:"+repr(n))
for i in range(1,10,1):
y_test = np.load(path + 'y_test_' + repr(i) + ".npy")
pred = np.load(path + 'pred_' + repr(i) + ".npy")
variance = np.load(path + 'variance' + repr(i) + ".npy")
different = np.load(path + 'different' + repr(i) + ".npy")
probability = np.load(path + 'probability' + repr(i) + ".npy")
x_id = np.load(path + 'x_id_' + repr(i) + ".npy")
for j in range(len(x_id)):
if variance[j:j+1]<n:
print("x_id:"+repr(x_id[j:j+1].tolist())+"\ty_test:"+repr(y_test[j:j+1].tolist())+"\tpred:"+repr(pred[j:j+1].tolist())+"\tvariance:"+repr(variance[j:j+1].tolist())+"\tdifferent:"+repr(different[j:j+1].tolist()))
print(probability[j:j+1])
# get result by set threshold of different of max and second
def get_result_by_different(n, count=0, count_diff=0, count_sth=0, count_all=0):
proj_id = "6013"
path = 'result/'+proj_id+"/svm/data/"
print("-"*100)
print("get result by diff:"+repr(n))
x_id_set = []
y_test_set = []
pred_set = []
for i in range(1,10,1):
y_test = np.load(path + 'y_test_' + repr(i) + ".npy")
pred = np.load(path + 'pred_' + repr(i) + ".npy")
# variance = np.load(path + 'variance' + repr(i) + ".npy")
# different = np.load(path + 'different' + repr(i) + ".npy")
probability = np.load(path + 'probability_' + repr(i) + ".npy")
x_id = np.load(path + 'x_id_' + repr(i) + ".npy")
for j in range(len(x_id)):
count_all += 1
if pred[j:j+1] == 1:
count_sth += 1
diff = abs(probability[j:j+1][0][0] - probability[j:j+1][0][1])
if diff <= n:
count += 1
# print("x_id:"+repr(x_id[j:j+1].tolist())+"\ty_test:"+repr(y_test[j:j+1].tolist())+"\tpred:"+repr(pred[j:j+1].tolist())+"\tvariance:"+repr(variance[j:j+1].tolist())+"\tdifferent:"+repr(different[j:j+1].tolist()))
# print(probability[j:j+1])
# if y_test[j:j+1] != pred[j:j+1]:
# print("x_id:"+repr(x_id[j:j+1].tolist())+"\ty_test:"+repr(y_test[j:j+1].tolist())+"\tpred:"+repr(pred[j:j+1].tolist())+"\tvariance:"+repr(variance[j:j+1].tolist())+"\tdifferent:"+repr(different[j:j+1].tolist()))
print(probability[j:j+1])
count_diff += 1
x_id_set.append(x_id[j:j+1].tolist()[0])
y_test_set.append(y_test[j:j+1].tolist()[0])
pred_set.append(pred[j:j+1].tolist()[0])
print("different > n data count:"+repr(count))
print("different > n and wrong pred data count:"+repr(count_diff))
print("count sth:"+repr(count_sth))
print("all issue count:"+repr(count_all))
get_title_description(x_id_set,y_test_set,pred_set,path)
# get_result_by_different(0.1)
# get result by set threshold of different of max and second
def get_result_by_little_different(n, count=0, count_diff=0, count_sth=0, count_all=0):
print("-"*100)
print("get result by diff:"+repr(n))
x_id_set = []
y_test_set = []
pred_set = []
for i in range(1,10,1):
y_test = np.load(path + 'y_test_' + repr(i) + ".npy")
pred = np.load(path + 'pred_' + repr(i) + ".npy")
variance = np.load(path + 'variance' + repr(i) + ".npy")
different = np.load(path + 'different' + repr(i) + ".npy")
probability = np.load(path + 'probability' + repr(i) + ".npy")
x_id = np.load(path + 'x_id_' + repr(i) + ".npy")
for j in range(len(x_id)):
count_all += 1
if pred[j:j+1] == 1:
count_sth += 1
if different[j:j+1]<=n:
count += 1
print("x_id:"+repr(x_id[j:j+1].tolist())+"\ty_test:"+repr(y_test[j:j+1].tolist())+"\tpred:"+repr(pred[j:j+1].tolist())+"\tvariance:"+repr(variance[j:j+1].tolist())+"\tdifferent:"+repr(different[j:j+1].tolist()))
print(probability[j:j+1])
count_diff += 1
x_id_set.append(x_id[j:j+1].tolist()[0])
y_test_set.append(y_test[j:j+1].tolist()[0])
pred_set.append(pred[j:j+1].tolist()[0])
print("different < n data count:"+repr(count))
print("different < n and wrong pred data count:"+repr(count_diff))
print("count sth:"+repr(count_sth))
print("all issue count:"+repr(count_all))
get_title_description(x_id_set,y_test_set,pred_set)
# ####################################################################
def get_all_pred_analysis(count_all=0,proj_id = "0"):
path = 'result3/'+proj_id+"/svm/data/"
x_id_set = []
y_test_set = []
pred_set = []
# variance_set = []
different_set = []
probability_set = []
for i in range(1,10,1):
y_test = np.load(path + 'y_test_' + repr(i) + ".npy")
pred = np.load(path + 'pred_' + repr(i) + ".npy")
# variance = np.load(path + 'variance' + repr(i) + ".npy")
# different = np.load(path + 'different' + repr(i) + ".npy")
probability = np.load(path + 'probability_' + repr(i) + ".npy")
x_id = np.load(path + 'x_id_' + repr(i) + ".npy")
for j in range(len(x_id)):
count_all += 1
x_id_set.append(x_id[j:j+1].tolist()[0])
y_test_set.append(y_test[j:j+1].tolist()[0])
pred_set.append(pred[j:j+1].tolist()[0])
# variance_set.append(variance[j:j+1].tolist()[0])
different_set.append(abs(probability[j:j+1][0][0] - probability[j:j+1][0][1]))
probability_set.append(probability[j:j+1].tolist()[0])
print("all issue count:"+repr(count_all))
print("path:" + path + 'csv_analysis.csv')
csvfile = file(path + 'csv_analysis.csv', 'wb')
writer = csv.writer(csvfile)
# writer.writerow(['id', 'y_test', 'pred', 'variance', 'different', 'probability', 'title', 'body'])
writer.writerow(['id', 'y_test', 'pred', 'different', 'probability'])
cur = conn.cursor()
for i in range(len(x_id_set)):
# sql = "select title,body from "\
# # +table+" where id = " + str(x_id_set[i])
# cur.execute(sql)
# r = cur.fetchone()
# if r:
data = (x_id_set[i], y_test_set[i], pred_set[i], different_set[i], probability_set[i])
writer.writerow(data)
csvfile.close()
# proj_id = "11450"
# proj_id = "6013"
proj_id = "24444"
get_all_pred_analysis(0,proj_id)
# def get_pred_rate(break = 10):
# get_result_by_different(0.6)
# get_result_by_little_different(0.1)
# get_all_pred_analysis()
# y_test = np.load(path + 'y_test_' + repr(1) + ".npy")
# pred = np.load(path + 'pred_' + repr(1) + ".npy")
# variance = np.load(path + 'variance' + repr(1) + ".npy")
# different = np.load(path + 'different' + repr(1) + ".npy")
# probability = np.load(path + 'probability' + repr(1) + ".npy")
# x_id = np.load(path + 'x_id_' + repr(1) + ".npy")
# count_diff = 0
# count_all = 0
# for j in range(len(y_test)):
# if y_test[j:j+1] != pred[j:j+1]:
# print("x_id:"+repr(x_id[j:j+1].tolist())+"\ty_test:"+repr(y_test[j:j+1].tolist())+"\tpred:"+repr(pred[j:j+1].tolist())+"\tvariance:"+repr(variance[j:j+1].tolist())+"\tdifferent:"+repr(different[j:j+1].tolist()))
# print(probability[j:j+1])
# count_diff += 1
# print("total:"+repr(count_diff))
# count_diff = 0
# print("="*20)
# for j in range(len(y_test)):
# if different[j:j+1]<0.3:
# count_all += 1
# print("y_test:"+repr(y_test[j:j+1].tolist())+"\tpred:"+repr(pred[j:j+1].tolist())+"\tvariance:"+repr(variance[j:j+1].tolist())+"\tdifferent:"+repr(different[j:j+1].tolist()))
# if y_test[j:j+1] != pred[j:j+1]:
# count_diff += 1
# print("all:"+repr(count_all))
# print("different:"+repr(count_diff))
#