/
WeightCalculate.py
319 lines (260 loc) · 13.7 KB
/
WeightCalculate.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
import math
import os
import time
from datetime import datetime
from source.config.projectConfig import projectConfig
from source.scikit.FPS.FPSAlgorithm import FPSAlgorithm
from source.utils.pandas.pandasHelper import pandasHelper
class WeightCalculate:
@staticmethod
def loadLocalPrDistance(project):
path = projectConfig.getPullRequestDistancePath()
prDisDf_LCP = pandasHelper.readTSVFile(path + os.sep +
f"pr_distance_{project}_FPS.tsv",
header=pandasHelper.INT_READ_FILE_WITH_HEAD)
DisMapLCP = {}
DisMapLCS = {}
DisMapLCSubseq = {}
DisMapLCSubstr = {}
for row in prDisDf_LCP.itertuples(index=False, name='Pandas'):
p1 = row[0]
p2 = row[1]
dis = row[2]
DisMapLCP[(p1, p2)] = dis
DisMapLCP[(p2, p1)] = dis
return [DisMapLCS, DisMapLCP, DisMapLCSubseq, DisMapLCSubstr]
@staticmethod
def getTrainDataPrDistance(train_data, K, pathDict, date, prCreatedTimeMap):
trainPrDis = {}
start_time = time.strptime(str(date[0]) + "-" + str(date[1]) + "-" + "01 00:00:00", "%Y-%m-%d %H:%M:%S")
start_time = int(time.mktime(start_time) - 86400)
end_time = time.strptime(str(date[2]) + "-" + str(date[3]) + "-" + "01 00:00:00", "%Y-%m-%d %H:%M:%S")
end_time = int(time.mktime(end_time) - 1)
print(train_data.shape)
data = train_data[['pr_number', 'filename']].copy(deep=True)
data.drop_duplicates(inplace=True)
data.reset_index(inplace=True, drop=True)
prList = list(set(data['pr_number']))
prList.sort()
scoreMap = {}
for index, p1 in enumerate(prList):
scores = {}
print("now pr:", index, " all:", prList.__len__())
for p2 in prList:
if p1 < p2:
score = 0
paths1 = list(pathDict[p1]['filename'])
paths2 = list(pathDict[p2]['filename'])
score = 0
for filename1 in paths1:
for filename2 in paths2:
score += FPSAlgorithm.LCP_2(filename1, filename2)
score /= paths1.__len__() * paths2.__len__()
t1 = prCreatedTimeMap[p1]
t2 = prCreatedTimeMap[p2]
t = math.fabs(t1 - t2) / (end_time - start_time)
score = score * math.exp(-t)
scores[p2] = score
scoreMap[(p1, p2)] = score
scoreMap[(p2, p1)] = score
elif p1 > p2:
score = scoreMap[(p1, p2)]
scores[p2] = score
KNN = [x[0] for x in sorted(scores.items(), key=lambda d: d[1], reverse=True)[0:K]]
for p2 in KNN:
trainPrDis[(p1, p2)] = scores[p2]
return trainPrDis
@staticmethod
def buildPrToRevRelation(train_data):
print("start building request -> reviewer relations....")
start = datetime.now()
pr_created_time_data = train_data['pr_created_at'].apply(
lambda x: time.mktime(time.strptime(x, "%Y-%m-%d %H:%M:%S")))
start_time = min(pr_created_time_data.to_list())
pr_created_time_data = train_data['pr_created_at'].apply(
lambda x: time.mktime(time.strptime(x, "%Y-%m-%d %H:%M:%S")))
end_time = max(pr_created_time_data.to_list())
prToRevMat = {}
grouped_train_data = train_data.groupby([train_data['pr_number'], train_data['review_user_login']])
max_weight = 0
for relation, group in grouped_train_data:
group.reset_index(drop=True, inplace=True)
weight = WeightCalculate.caculateRevToPrWeight(group, start_time, end_time)
max_weight = max(weight, max_weight)
if not prToRevMat.__contains__(relation[0]):
prToRevMat[relation[0]] = {}
prToRevMat[relation[0]][relation[1]] = weight
for pr, relations in prToRevMat.items():
for rev, weight in relations.items():
prToRevMat[pr][rev] = weight / max_weight
return prToRevMat
@staticmethod
def caculateRevToPrWeight(comment_records, start_time, end_time):
weight_lambda = 0.8
weight = 0
comment_records = comment_records.copy(deep=True)
comment_records.drop(columns=['filename'], inplace=True)
comment_records.drop_duplicates(inplace=True)
comment_records.reset_index(inplace=True, drop=True)
for cm_idx, cm_row in comment_records.iterrows():
cm_timestamp = time.strptime(cm_row['review_created_at'], "%Y-%m-%d %H:%M:%S")
cm_timestamp = int(time.mktime(cm_timestamp))
t = (cm_timestamp - start_time) / (end_time - start_time)
cm_weight = math.pow(weight_lambda, cm_idx) * math.exp(t - 1)
weight += cm_weight
return weight
@staticmethod
def buildAuthToPrRelation(train_data, date):
start = datetime.now()
start_time = time.strptime(str(date[0]) + "-" + str(date[1]) + "-" + "01 00:00:00", "%Y-%m-%d %H:%M:%S")
start_time = int(time.mktime(start_time) - 86400)
end_time = time.strptime(str(date[2]) + "-" + str(date[3]) + "-" + "01 00:00:00", "%Y-%m-%d %H:%M:%S")
end_time = int(time.mktime(end_time) - 1)
authToRrMat = {}
grouped_train_data = train_data.groupby([train_data['author_user_login'], train_data['pr_number']])
max_weight = 0
for relation, group in grouped_train_data:
group.reset_index(drop=True, inplace=True)
weight = WeightCalculate.caculateAuthToPrWeight(group, start_time, end_time)
max_weight = max(weight, max_weight)
if not authToRrMat.__contains__(relation[0]):
authToRrMat[relation[0]] = {}
authToRrMat[relation[0]][relation[1]] = weight
for auth, relations in authToRrMat.items():
for rev, weight in relations.items():
authToRrMat[auth][rev] = weight / max_weight
return authToRrMat
@staticmethod
def caculateAuthToPrWeight(comment_records, start_time, end_time):
weight_lambda = 0.8
weight = 0
comment_records = comment_records.copy(deep=True)
comment_records.drop(columns=['filename'], inplace=True)
comment_records.drop_duplicates(inplace=True)
comment_records.reset_index(inplace=True, drop=True)
for cm_idx, cm_row in comment_records.iterrows():
cm_timestamp = time.strptime(cm_row['pr_created_at'], "%Y-%m-%d %H:%M:%S")
cm_timestamp = int(time.mktime(cm_timestamp))
t = (cm_timestamp - start_time) / (end_time - start_time)
cm_weight = math.pow(weight_lambda, cm_idx) * t
weight += cm_weight
if weight > 1:
print("some thing errors....")
break
return weight
@staticmethod
def buildCommitToPrRelation(train_data_commit, date):
start = datetime.now()
start_time = time.strptime(str(date[0]) + "-" + str(date[1]) + "-" + "01 00:00:00", "%Y-%m-%d %H:%M:%S")
start_time = int(time.mktime(start_time) - 86400)
end_time = time.strptime(str(date[2]) + "-" + str(date[3]) + "-" + "01 00:00:00", "%Y-%m-%d %H:%M:%S")
end_time = int(time.mktime(end_time) - 1)
commitToRrMat = {}
grouped_train_data = train_data_commit.groupby(
[train_data_commit['pr_number'], train_data_commit['commit_user_login']])
max_weight = 0
for relation, group in grouped_train_data:
group.reset_index(drop=True, inplace=True)
weight = WeightCalculate.caculateCommitToPrWeight(group, start_time, end_time)
max_weight = max(weight, max_weight)
if not commitToRrMat.__contains__(relation[0]):
commitToRrMat[relation[0]] = {}
commitToRrMat[relation[0]][relation[1]] = weight
for auth, relations in commitToRrMat.items():
for rev, weight in relations.items():
commitToRrMat[auth][rev] = weight / max_weight
return commitToRrMat
@staticmethod
def caculateCommitToPrWeight(commit_records, start_time, end_time):
weight_lambda = 0.8
weight = 0
commit_records = commit_records.copy(deep=True)
commit_records.drop_duplicates(inplace=True)
commit_records.reset_index(inplace=True, drop=True)
for cm_idx, cm_row in commit_records.iterrows():
cm_timestamp = time.strptime(cm_row['commit_created_at'], "%Y-%m-%d %H:%M:%S")
cm_timestamp = int(time.mktime(cm_timestamp))
code_lines_total = int(cm_row['commit_status_total'])
code_lines_total = max(1, code_lines_total)
code_line_canshu = 1 / (1 + math.exp(-code_lines_total * 0.01))
t = math.fabs(cm_timestamp - start_time) / (end_time - start_time)
cm_weight = math.pow(weight_lambda, cm_idx) * t * code_line_canshu
weight += cm_weight
return weight
@staticmethod
def buildPrToIssueCommentRelation(train_data_issue_comment):
start = datetime.now()
pr_created_time_data = train_data_issue_comment['pr_created_at'].apply(
lambda x: time.mktime(time.strptime(x, "%Y-%m-%d %H:%M:%S")))
start_time = min(pr_created_time_data.to_list())
pr_created_time_data = train_data_issue_comment['pr_created_at'].apply(
lambda x: time.mktime(time.strptime(x, "%Y-%m-%d %H:%M:%S")))
end_time = max(pr_created_time_data.to_list())
commentToPrMat = {}
train_data = train_data_issue_comment.loc[train_data_issue_comment['comment_user_login'] != ''].copy(deep=True)
grouped_train_data = train_data.groupby([train_data['pr_number'], train_data['comment_user_login']])
max_weight = 0
for relation, group in grouped_train_data:
group.reset_index(drop=True, inplace=True)
weight = WeightCalculate.caculateIssueCommentToPrWeight(group, start_time, end_time)
max_weight = max(weight, max_weight)
if not commentToPrMat.__contains__(relation[0]):
commentToPrMat[relation[0]] = {}
commentToPrMat[relation[0]][relation[1]] = weight
for pr, relations in commentToPrMat.items():
for rev, weight in relations.items():
commentToPrMat[pr][rev] = weight / max_weight
return commentToPrMat
@staticmethod
def caculateIssueCommentToPrWeight(comment_records, start_time, end_time):
weight_lambda = 0.8
weight = 0
comment_records = comment_records.copy(deep=True)
comment_records.drop_duplicates(inplace=True)
comment_records.reset_index(inplace=True, drop=True)
for cm_idx, cm_row in comment_records.iterrows():
cm_timestamp = time.strptime(cm_row['comment_created_at'], "%Y-%m-%d %H:%M:%S")
cm_timestamp = int(time.mktime(cm_timestamp))
t = (cm_timestamp - start_time) / (end_time - start_time)
cm_weight = math.pow(weight_lambda, cm_idx) * math.exp(t - 1)
weight += cm_weight
return weight
@staticmethod
def buildPrToReviewCommentRelation(train_data_review_comment):
start = datetime.now()
pr_created_time_data = train_data_review_comment['pr_created_at'].apply(
lambda x: time.mktime(time.strptime(x, "%Y-%m-%d %H:%M:%S")))
start_time = min(pr_created_time_data.to_list())
pr_created_time_data = train_data_review_comment['pr_created_at'].apply(
lambda x: time.mktime(time.strptime(x, "%Y-%m-%d %H:%M:%S")))
end_time = max(pr_created_time_data.to_list())
commentToPrMat = {}
train_data = train_data_review_comment.loc[train_data_review_comment['review_comment_user_login'] != ''].copy(
deep=True)
grouped_train_data = train_data.groupby([train_data['pr_number'], train_data['review_comment_user_login']])
max_weight = 0
for relation, group in grouped_train_data:
group.reset_index(drop=True, inplace=True)
weight = WeightCalculate.caculateReviewCommentToPrWeight(group, start_time, end_time)
max_weight = max(weight, max_weight)
if not commentToPrMat.__contains__(relation[0]):
commentToPrMat[relation[0]] = {}
commentToPrMat[relation[0]][relation[1]] = weight
for pr, relations in commentToPrMat.items():
for rev, weight in relations.items():
commentToPrMat[pr][rev] = weight / max_weight
return commentToPrMat
@staticmethod
def caculateReviewCommentToPrWeight(comment_records, start_time, end_time):
weight_lambda = 0.8
weight = 0
comment_records = comment_records.copy(deep=True)
comment_records.drop_duplicates(inplace=True)
comment_records.reset_index(inplace=True, drop=True)
for cm_idx, cm_row in comment_records.iterrows():
cm_timestamp = time.strptime(cm_row['review_comment_created_at'], "%Y-%m-%d %H:%M:%S")
cm_timestamp = int(time.mktime(cm_timestamp))
t = (cm_timestamp - start_time) / (end_time - start_time)
cm_weight = math.pow(weight_lambda, cm_idx) * math.exp(t - 1)
weight += cm_weight
return weight