-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheda.py
331 lines (310 loc) · 11.3 KB
/
eda.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
import pandas as pd
import numpy as np
import json
import os
from tqdm import tqdm
df = pd.read_csv("data/final_total.csv")
# # How many files changed between two version
# lst_num_changed_files = []
# for i in tqdm(range(len(df)), total=len(df), desc="Statistic file"):
# num_changed_files = []
# for col in [
# "java_added",
# "java_deleted",
# "java_modified",
# "java_renamed_unchanged",
# "java_renamed_modified",
# ]:
# num_changed_files.append(len(eval(df.loc[i, col])))
# lst_num_changed_files.append(num_changed_files)
# cnt = 0
# for num_changed_files in lst_num_changed_files:
# for i in range(5):
# if num_changed_files[i] != 0:
# break
# else:
# cnt += 1
# print("Num migration with no java file changed:", cnt)
# total_file_changes = sum([sum(x) for x in lst_num_changed_files])
# added, deleted, modified, renamed_unchanged, renamed_modified = np.array(
# lst_num_changed_files
# ).sum(axis=0)
# print(
# "{:<36} {:>8} ~ {:.2f}%".format(
# "Num java file added:", added, added / total_file_changes * 100
# )
# )
# print(
# "{:<36} {:>8} ~ {:.2f}%".format(
# "Num java file deleted:", deleted, deleted / total_file_changes * 100
# )
# )
# print(
# "{:<36} {:>8} ~ {:.2f}%".format(
# "Num java file modified:", modified, modified / total_file_changes * 100
# )
# )
# print(
# "{:<36} {:>8} ~ {:.2f}%".format(
# "Num java file renamed_unchanged:",
# renamed_unchanged,
# renamed_unchanged / total_file_changes * 100,
# )
# )
# print("=" * 100)
# print("++++++++++STATISTIC++++++++++")
statistic_parsed1 = []
statistic_parsed2 = []
data_storage = "/drive1/thieulvd/code-migration-total"
migrations = list(df["id"])
for migration in tqdm(migrations, total=len(migrations), desc="Statistic class"):
level1_parsed1_info = {}
level1_parsed2_info = {}
folder = os.path.join(data_storage, migration)
for x in os.listdir(folder):
if x.startswith("parsed1"):
parsed1 = os.path.join(folder, x)
flag = True
elif x.startswith("parsed2"):
parsed2 = os.path.join(folder, x)
flag = True
else:
continue
for file in os.listdir(parsed1):
with open(os.path.join(parsed1, file), "r") as f:
lst = json.load(f)
for item in lst:
level1_parsed1_info[item["class_mode"]] = (
level1_parsed1_info.get(item["class_mode"], 0) + 1
)
for file in os.listdir(parsed2):
with open(os.path.join(parsed2, file), "r") as f:
lst = json.load(f)
for item in lst:
level1_parsed2_info[item["class_mode"]] = (
level1_parsed2_info.get(item["class_mode"], 0) + 1
)
statistic_parsed1.append(level1_parsed1_info)
statistic_parsed2.append(level1_parsed2_info)
class_parsed1 = pd.DataFrame(
statistic_parsed1,
columns=[
"Modified",
"Renamed-Modified",
"Added",
"Deleted",
"Unchanged",
"Renamed-Unchanged",
],
)
for col in class_parsed1:
total = 0
print(col)
for x in class_parsed1[col]:
if not pd.isna(x):
total += x
print(total)
print("=" * 100)
class_parsed2 = pd.DataFrame(
statistic_parsed2,
columns=[
"Modified",
"Renamed-Modified",
"Added",
"Deleted",
"Unchanged",
"Renamed-Unchanged",
],
)
for col in class_parsed2:
total = 0
print(col)
for x in class_parsed2[col]:
if not pd.isna(x):
total += x
print(total)
print("=" * 100)
statistic_parsed1 = []
statistic_parsed2 = []
method_modified_pairs = []
method_added = []
method_deleted = []
for migration in tqdm(migrations, total=len(migrations), desc="Statistic method"):
level2_parsed1_info = {}
level2_parsed2_info = {}
folder = os.path.join(data_storage, migration)
for x in os.listdir(folder):
if x.startswith("parsed1"):
parsed1 = os.path.join(folder, x)
elif x.startswith("parsed2"):
parsed2 = os.path.join(folder, x)
else:
continue
# print("Parse version 1 directory:", parsed1)
# print("Parse version 2 directory:", parsed2)
# print("=" * 100)
# print("Num diff file version 1:", len(os.listdir(parsed1)))
# print("Num diff file version 2:", len(os.listdir(parsed2)))
# print("=" * 100)
cnt_methods = 0
cnt_classes = 0
ver1_method_modes = {}
ver1_class_modes = {}
for ver1_file_name in os.listdir(parsed1):
ver1_file_path = os.path.join(parsed1, ver1_file_name)
with open(ver1_file_path, "r") as f:
ver1_classes = json.load(f)
cnt_classes += len(ver1_classes)
for ver1_class in ver1_classes:
ver1_class_modes[ver1_class["class_mode"]] = (
ver1_class_modes.get(ver1_class["class_mode"], 0) + 1
)
for method in ver1_class["methods"]:
cnt_methods += 1
ver1_method_modes[method["method_mode"]] = (
ver1_method_modes.get(method["method_mode"], 0) + 1
)
# print("Total classes in version 1:", cnt_classes)
# print("Class mode info of version 1:", ver1_class_modes)
# print("Total methods in version 1:", cnt_methods)
# print("Method mode info of version 1:", ver1_method_modes)
cnt_classes = 0
cnt_methods = 0
ver2_method_modes = {}
ver2_class_modes = {}
for ver2_file_name in os.listdir(parsed2):
ver2_file_path = os.path.join(parsed2, ver2_file_name)
with open(ver2_file_path, "r") as f:
ver2_classes = json.load(f)
cnt_classes += len(ver2_classes)
for ver2_class in ver2_classes:
ver2_class_modes[ver2_class["class_mode"]] = (
ver2_class_modes.get(ver2_class["class_mode"], 0) + 1
)
for ver2_method in ver2_class["methods"]:
cnt_methods += 1
ver2_method_modes[ver2_method["method_mode"]] = (
ver2_method_modes.get(ver2_method["method_mode"], 0) + 1
)
# print("Total classes in version 2:", cnt_classes)
# print("Class mode info of version 2:", ver2_class_modes)
# print("Total methods in version 2:", cnt_methods)
# print("Method mode info of version 2:", ver2_method_modes)
# print("=" * 100)
# print("=" * 100)
statistic_parsed1.append(ver1_method_modes)
statistic_parsed2.append(ver2_method_modes)
method_parsed1 = pd.DataFrame(
statistic_parsed1,
columns=[
"Modified",
"Renamed-Modified",
"Added",
"Deleted",
"Unchanged",
"Renamed-Unchanged",
],
)
for col in method_parsed1:
total = 0
print(col)
for x in method_parsed1[col]:
if not pd.isna(x):
total += x
print(total)
print("=" * 100)
method_parsed2 = pd.DataFrame(
statistic_parsed2,
columns=[
"Modified",
"Renamed-Modified",
"Added",
"Deleted",
"Unchanged",
"Renamed-Unchanged",
],
)
for col in method_parsed2:
total = 0
print(col)
for x in method_parsed2[col]:
if not pd.isna(x):
total += x
print(total)
print("=" * 100)
data_storage = "/drive1/thieulvd/code-migration-total"
method_modified_pairs = []
method_added = []
method_deleted = []
num_modified_method = 0
for migration in tqdm(migrations, total=len(migrations), desc="Pairing method"):
folder = os.path.join(data_storage, migration)
for x in os.listdir(folder):
if x.startswith("parsed1"):
parsed1 = os.path.join(folder, x)
elif x.startswith("parsed2"):
parsed2 = os.path.join(folder, x)
else:
continue
for ver1_file_name in os.listdir(parsed1):
ver1_file_path = os.path.join(parsed1, ver1_file_name)
with open(ver1_file_path, 'r') as f:
ver1_classes = json.load(f)
for ver1_class in ver1_classes:
for ver1_method in ver1_class["methods"]:
if ver1_method["method_mode"] in ["Modified", "Renamed-Modified"]:
num_modified_method += 1
ver2_file_name = ver1_class["ver2_path"].replace("/", "--") + ".json"
ver2_file_path = os.path.join(parsed2, ver2_file_name)
with open(ver2_file_path, 'r') as f:
ver2_classes = json.load(f)
for ver2_class in ver2_classes:
if ver1_class["ver2_tree_path"] == ver2_class["ver2_tree_path"]:
for ver2_method in ver2_class["methods"]:
if ver1_method["ver2_signature"] == ver2_method["ver2_signature"]:
record = {
"migration_id": migration,
"ver1_file_path": ver1_class["ver1_path"],
"ver2_file_path": ver1_class["ver2_path"],
"ver1_tree_path": ver1_class["ver1_tree_path"],
"ver2_tree_path": ver1_class["ver2_tree_path"],
"ver1_signature": ver1_method["ver1_signature"],
"ver2_signature": ver1_method["ver2_signature"],
"method_ver1": ver1_method["definition"],
"method_ver2": ver2_method["definition"]
}
method_modified_pairs.append(record)
elif ver1_method["method_mode"] == "Deleted":
record = {
"migration_id": migration,
"ver1_file_path": ver1_class["ver1_path"],
"ver1_tree_path": ver1_class["ver1_tree_path"],
"ver1_signature": ver1_method["ver1_signature"],
"method_ver1": ver1_method["definition"]
}
method_deleted.append(record)
for ver2_file_name in os.listdir(parsed2):
ver2_file_path = os.path.join(parsed2, ver2_file_name)
with open(ver2_file_path, 'r') as f:
ver2_classes = json.load(f)
for ver2_class in ver2_classes:
for ver2_method in ver2_class["methods"]:
if ver2_method["method_mode"] == "Added":
record = {
"migration_id": migration,
"ver2_file_path": ver2_class["ver2_path"],
"ver2_tree_path": ver2_class["ver2_tree_path"],
"ver2_signature": ver2_method["ver2_signature"],
"method_ver2": ver2_method["definition"]
}
method_added.append(record)
print(num_modified_method)
print(len(method_modified_pairs))
print(len(method_added))
print(len(method_deleted))
method_modified = pd.DataFrame(method_modified_pairs)
method_added = pd.DataFrame(method_added)
method_deleted = pd.DataFrame(method_deleted)
method_modified.to_csv("data/method_modified.csv")
method_added.to_csv("data/method_added.csv")
method_deleted.to_csv("data/method_deleted.csv")