-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProduct+Process.py
More file actions
227 lines (189 loc) · 6.5 KB
/
Copy pathProduct+Process.py
File metadata and controls
227 lines (189 loc) · 6.5 KB
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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import pandas as pd
import numpy as np
import math
import pickle
from scipy import stats
import scipy.io
from scipy.spatial.distance import pdist
from scipy.linalg import cholesky
from scipy.io import loadmat
import matlab.engine as engi
import matlab as mat
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC
from sklearn.metrics import classification_report,roc_auc_score,recall_score,precision_score
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
from sklearn.ensemble import RandomForestClassifier
from sklearn.decomposition import PCA
from pyearth import Earth
from src import SMOTE
from src import CFS
from src import metrices_V2 as metrices
import platform
from os import listdir
from os.path import isfile, join
from glob import glob
from pathlib import Path
import sys
import os
import copy
import traceback
from pathlib import Path
import matplotlib.pyplot as plt
# In[6]:
def load_data(project):
understand_path = 'data/understand_files_all/' + project + '_understand.csv'
commit_guru_path = 'data/commit_guru/' + project + '.csv'
understand_df = pd.read_csv(understand_path)
understand_df = understand_df.dropna(axis = 1,how='all')
cols_list = understand_df.columns.values.tolist()
for item in ['Kind', 'Name','commit_hash', 'Bugs']:
if item in cols_list:
cols_list.remove(item)
cols_list.insert(0,item)
understand_df = understand_df[cols_list]
commit_guru_df = pd.read_csv(commit_guru_path)
cols = understand_df.columns.tolist()
commit_guru_df = commit_guru_df.drop(labels = ['parent_hashes','author_name','author_name',
'author_email','fileschanged','author_date',
'author_date_unix_timestamp', 'commit_message',
'classification', 'fix', 'contains_bug','fixes',],axis=1)
understand_df = understand_df.drop_duplicates(cols[4:len(cols)])
df = understand_df.merge(commit_guru_df,on='commit_hash')
cols = df.columns.tolist()
cols = cols[1:] + [cols[0]]
df = df[cols]
for item in ['Kind', 'Name','commit_hash']:
if item in cols:
df = df.drop(labels = [item],axis=1)
df.dropna(inplace=True)
df.reset_index(drop=True, inplace=True)
df.to_csv('data/converted/'+ project + '_understand.csv',index=False)
y = df.Bugs
X = df.drop('Bugs',axis = 1)
cols = X.columns
scaler = MinMaxScaler()
X = scaler.fit_transform(X)
X = pd.DataFrame(X,columns = cols)
return X,y
def apply_smote(df):
cols = df.columns
smt = SMOTE.smote(df)
df = smt.run()
df.columns = cols
return df
def apply_cfs(df):
y = df.Bugs.values
X = df.drop(labels = ['Bugs'],axis = 1)
X = X.values
selected_cols = CFS.cfs(X,y)
cols = df.columns[[selected_cols]].tolist()
cols.append('Bugs')
return df[cols],cols
# In[7]:
def run_self(project):
X,y = load_data(project)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.40, random_state=18)
loc = X_test.CountLineCode
df_smote = pd.concat([X_train,y_train],axis = 1)
df_smote = apply_smote(df_smote)
y_train = df_smote.Bugs
X_train = df_smote.drop('Bugs',axis = 1)
clf = RandomForestClassifier()
clf.fit(X_train,y_train)
importance = clf.feature_importances_
predicted = clf.predict(X_test)
abcd = metrices.measures(y_test,predicted,loc)
pf = abcd.get_pf()
recall = abcd.calculate_recall()
precision = abcd.calculate_precision()
f1 = abcd.calculate_f1_score()
g_score = abcd.get_g_score()
pci_20 = abcd.get_pci_20()
ifa = abcd.get_ifa()
try:
auc = roc_auc_score(y_test, predicted)
except:
auc = 0
print(classification_report(y_test, predicted))
return recall,precision,pf,f1,g_score,auc,pci_20,ifa,importance
# In[8]:
def run_self_CFS(project):
X,y = load_data(project)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.40, random_state=18)
loc = X_test.CountLineCode
df_smote = pd.concat([X_train,y_train],axis = 1)
df_smote = apply_smote(df_smote)
df_smote,cols = apply_cfs(df_smote)
y_train = df_smote.Bugs
X_train = df_smote.drop('Bugs',axis = 1)
clf = RandomForestClassifier()
clf.fit(X_train,y_train)
importance = clf.feature_importances_
predicted = clf.predict(X_test[cols[:-1]])
abcd = metrices.measures(y_test,predicted,loc)
pf = abcd.get_pf()
recall = abcd.calculate_recall()
precision = abcd.calculate_precision()
f1 = abcd.calculate_f1_score()
g_score = abcd.get_g_score()
pci_20 = abcd.get_pci_20()
ifa = abcd.get_ifa()
try:
auc = roc_auc_score(y_test, predicted)
except:
auc = 0
print(classification_report(y_test, predicted))
return recall,precision,pf,f1,g_score,auc,pci_20,ifa,importance
# In[9]:
proj_df = pd.read_csv('projects.csv')
projects = proj_df.repo_name.tolist()
# In[ ]:
precision_list = {}
recall_list = {}
pf_list = {}
f1_list = {}
g_list = {}
auc_list = {}
pci_20_list = {}
ifa_list = {}
featue_importance = {}
for project in projects:
try:
if project == '.DS_Store':
continue
# if project != 'org.alloytools.alloy':
# continue
# if project != 'guice':
# continue
print("+++++++++++++++++ " + project + " +++++++++++++++++")
recall,precision,pf,f1,g_score,auc,pci_20,ifa,importance = run_self(project)
recall_list[project] = recall
precision_list[project] = precision
pf_list[project] = pf
f1_list[project] = f1
g_list[project] = g_score
auc_list[project] = auc
pci_20_list[project] = pci_20
ifa_list[project] = ifa
featue_importance[project] = importance
except Exception as e:
print(e)
continue
final_result = {}
final_result['precision'] = precision_list
final_result['recall'] = recall_list
final_result['pf'] = pf_list
final_result['f1'] = f1_list
final_result['g'] = g_list
final_result['auc'] = auc_list
final_result['pci_20'] = pci_20_list
final_result['ifa'] = ifa_list
with open('results/Performance/process+product_CFS.pkl', 'wb') as handle:
pickle.dump(final_result, handle, protocol=pickle.HIGHEST_PROTOCOL)