-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPlotResultsAndComputeStats.py
364 lines (310 loc) · 12.7 KB
/
PlotResultsAndComputeStats.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#!/usr/bin/python3
from collections import Counter
import json
import re
import matplotlib.pyplot as plt
import config
#from script_AnalyzeRepos import results_base_dir, projects
import pandas as pd
from os import path, scandir
import glob, os
font_size = 15
plt.rc('pdf',fonttype = 42)
plots_base_dir = config.ROOT_DIR + "/Resources/Output_typeErrors/"
ignored_warning_kinds = [
"Undefined import [21]",
"Undefined or invalid type [11]",
"Undefined name [18]",
"Undefined attribute [16]",
]
def find_all_projects():
projects = []
for e in scandir(plots_base_dir):
if '.json' in e.name:
#print(e.name.replace('.json', '').replace('history_',''))
projects.append(e.name.replace('.json', '').replace('history_',''))
return projects
# "history_"
projects = find_all_projects()
def read_results(name):
with open(plots_base_dir+name+".json") as fp:
r = json.load(fp)
return r
def get_results():
project_to_history = {}
latest_results = []
for p in projects:
try:
p_results = read_results("history_"+p)
except Exception as e:
print(
f"WARNING: Couldn't read results for {p}. Skipping this project.")
continue
project_to_history[p] = p_results
latest_p_result = p_results[0]
latest_p_result["project"] = p
latest_results.append(latest_p_result)
print(f"Read all project histories. Computing the plots...")
return project_to_history, pd.DataFrame(latest_results)
def count_filtered_warnings(kind_to_nb):
total = 0
for kind, nb in kind_to_nb.items():
if kind not in ignored_warning_kinds:
total += nb
return total
def compute_more_columns(results):
warnings = []
for kind_to_nb in results["kind_to_nb"]:
nb_warnings = count_filtered_warnings(kind_to_nb)
warnings.append(nb_warnings)
results["nb_filtered_warnings"] = warnings
results["nb_types"] = results["nb_param_types"] + \
results["nb_return_types"] + results["nb_variable_types"]
def plot_warnings_loc_evolution(p):
results = read_results("history_"+p)
dates = []
locs = []
warnings = []
for r in reversed(results):
date = r["commit_date"]
date = date.split(" ")[0]
dates.append(date)
locs.append(r["loc"])
nb_warnings = count_filtered_warnings(r["kind_to_nb"])
warnings.append(nb_warnings)
print(dates)
print(warnings)
print(locs)
plt.rcParams.update({'font.size': font_size})
plt.rc('pdf',fonttype = 42)
fig, ax1 = plt.subplots()
ax1.set_xlabel("Date")
plt.xticks(rotation=90)
ax1.set_ylabel("Type warnings")
ax1.plot(dates, warnings, label="Type warnings", marker="o", color=(0.2, 0.4, 0.6, 0.6))
ax1.legend()
ax1.set_ylim(bottom=0)
ax2 = ax1.twinx()
ax2.set_ylabel("Lines of code")
ax2.plot(dates, locs, label="Lines of code",
linestyle="dashed", marker="x", color='lightsalmon')
ax2.legend()
ax2.set_ylim(bottom=0)
fig.tight_layout()
ax1.set_title(f"Evolution of {p}")
plt.savefig(plots_base_dir+"warnings_log_evolution_"+p+".pdf")
plt.close()
def plot_kinds_of_errors(results):
total_kind_to_nb = Counter()
for kind_to_nb in results["kind_to_nb"]:
total_kind_to_nb.update(Counter(kind_to_nb))
kinds = []
nbs = []
kind_matcher = re.compile(r" \[.*")
for kind, nb in total_kind_to_nb.most_common():
if kind not in ignored_warning_kinds:
kind = kind_matcher.sub("", kind)
kinds.append(kind)
nbs.append(nb)
plt.rcParams.update({'font.size': 15})
plt.rc('pdf',fonttype = 42)
y_pos = range(5)
numlist = sum(nbs)
plt.bar(y_pos[:5], nbs[:5], align='center', alpha=0.5, color=(0.2, 0.4, 0.6, 0.6))
plt.xticks(y_pos[:5], kinds[:5])
plt.xticks(rotation=30, horizontalalignment='right')
#plt.setp(ax.get_xticklabels(), rotation=30, horizontalalignment='right')
plt.ylabel("Number of warnings")
#plt.title("Top-5 kinds of type warnings")
plt.tight_layout()
plt.savefig(plots_base_dir+"kinds_of_warnings.pdf")
plt.close()
def plot_errors_vs_loc(results):
plt.rcParams.update({'font.size': font_size})
plt.rc('pdf',fonttype = 42)
plt.plot(results["loc"], results["nb_filtered_warnings"], "o", color=(0.2, 0.4, 0.6, 0.6))
plt.xscale("log")
plt.yscale("log")
plt.xlabel("Lines of code")
plt.ylabel("Number of warnings")
#plt.title("Type errors vs. code size")
plt.tight_layout()
#plt.savefig(plots_base_dir+"errors_vs_loc.pdf")
plt.close()
def plot_evolution_of_errors_vs_loc(project_to_history):
plt.rcParams.update({'font.size': font_size})
plt.rc('pdf',fonttype = 42)
for p, h in project_to_history.items():
errors_per_loc = []
for r in h:
loc = r["loc"]
nb_warnings = count_filtered_warnings(r["kind_to_nb"])
if loc > 0:
errors_per_loc.append(nb_warnings / loc)
else:
errors_per_loc.append(0)
plt.plot(errors_per_loc, color=(0.2, 0.4, 0.6, 0.6))
plt.xlabel("Time steps during version history")
plt.ylabel("Type errors per lines of code")
plt.tight_layout()
#plt.savefig(plots_base_dir+"errors_vs_loc_evolution.pdf")
plt.close()
def plot_evolution_of_errors(project_to_history):
plt.rcParams.update({'font.size': font_size})
plt.rc('pdf',fonttype = 42)
for p, h in project_to_history.items():
errors_per_loc = []
for r in h:
loc = r["loc"]
nb_warnings = count_filtered_warnings(r["kind_to_nb"])
errors_per_loc.append(nb_warnings)
plt.plot(errors_per_loc, color=(0.2, 0.4, 0.6, 0.6))
plt.xlabel("Time steps during version history")
plt.ylabel("Type errors")
plt.tight_layout()
#plt.savefig(plots_base_dir+"errors_evolution.pdf")
plt.close()
def plot_errors_vs_annotations(results):
plt.rcParams.update({'font.size': font_size})
plt.rc('pdf',fonttype = 42)
plt.plot(results["nb_types"], results["nb_filtered_warnings"], "o", color=(0.2, 0.4, 0.6, 0.6))
plt.xscale("log")
plt.yscale("log")
plt.xlabel("Type annotations (log scale)")
plt.ylabel("Number of type errors (log scale)")
#plt.title("Type errors vs. annotations")
plt.tight_layout()
plt.savefig(plots_base_dir+"errors_vs_annotations.pdf")
plt.close()
def plot_evolution_of_errors_vs_annotations(project_to_history):
plt.rcParams.update({'font.size': font_size})
plt.rc('pdf',fonttype = 42)
for p, h in project_to_history.items():
errors_per_annotations = []
for r in h:
nb_annotations = r["nb_param_types"] + \
r["nb_return_types"] + r["nb_variable_types"]
nb_warnings = count_filtered_warnings(r["kind_to_nb"])
if nb_annotations > 0:
errors_per_annotations.append(nb_warnings / nb_annotations)
else:
errors_per_annotations.append(0)
plt.plot(errors_per_annotations, color=(0.2, 0.4, 0.6, 0.6))
plt.xlabel("Time steps during version history")
plt.ylabel("Type errors per annotation")
plt.ylim(0, 50)
plt.tight_layout()
#plt.savefig(plots_base_dir+"errors_vs_annotations_evolution.pdf")
plt.close()
def plot_evolution_of_avg_errors_vs_annotations(project_to_history):
plt.rcParams.update({'font.size': font_size})
plt.rc('pdf',fonttype = 42)
sum_at_step = [0.0] * 10
for p, h in project_to_history.items():
for step, r in enumerate(h[:10]):
nb_annotations = r["nb_param_types"] + \
r["nb_return_types"] + r["nb_variable_types"]
nb_warnings = count_filtered_warnings(r["kind_to_nb"])
if nb_annotations > 0:
sum_at_step[step] += nb_warnings / nb_annotations
avg_at_step = [s / len(project_to_history) for s in sum_at_step]
plt.plot(avg_at_step, color=(0.2, 0.4, 0.6, 0.6))
plt.xlabel("Time steps during version history")
plt.ylabel("Type errors per annotation\n(avg. across projects)")
plt.tight_layout()
#plt.savefig(plots_base_dir+"errors_vs_annotations_evolution_avg.pdf")
plt.close()
def plot_per_project_evolution(project_to_history):
plt.rcParams.update({'font.size': font_size})
plt.rcParams.update({'font.size': 20})
plt.rc('pdf',fonttype = 42)
for p, h in project_to_history.items():
annotation_evol = []
loc_evol = []
for r in reversed(h[:10]):
annotation_evol.append(r["nb_param_types"] +
r["nb_return_types"] + r["nb_variable_types"])
loc_evol.append(r["loc"])
fig, ax1 = plt.subplots()
plt.xlabel(f"Time steps during version\nhistory of {p}")
ax1.set_ylim(bottom=0, top=max(loc_evol)*1.05)
line1 = ax1.plot(loc_evol, label="Lines of code (left)",
marker="o", color=(0.2, 0.4, 0.6, 0.6))
ax2 = ax1.twinx()
ax2.set_ylim(bottom=0, top=max(max(annotation_evol)*1.05, 20))
line2 = ax2.plot(
annotation_evol, label="Type annotations (right)", marker="x", color='lightsalmon')
lines = line1 + line2
labels = [l.get_label() for l in lines]
fig.legend(lines, labels,loc='upper center', bbox_to_anchor=(0.52, 1.1))
plt.savefig(f"{plots_base_dir}per_project/{p}.pdf", bbox_inches='tight')
plt.close()
def plot_error_per_project_evolution(project_to_history):
plt.rcParams.update({'font.size': font_size})
plt.rcParams.update({'font.size': 20})
plt.rc('pdf',fonttype = 42)
for p, h in project_to_history.items():
annotation_evol = []
loc_evol = []
for r in reversed(h[:10]):
annotation_evol.append(count_filtered_warnings(r["kind_to_nb"]))
loc_evol.append(r["loc"])
fig, ax1 = plt.subplots()
plt.xlabel(f"Time steps during version\nhistory of {p}")
ax1.set_ylim(bottom=0, top=max(loc_evol)*1.05)
line1 = ax1.plot(loc_evol, label="Lines of code (left)",
marker="o", color=(0.2, 0.4, 0.6, 0.6))
ax2 = ax1.twinx()
ax2.set_ylim(bottom=0, top=max(max(annotation_evol)*1.05, 20))
line2 = ax2.plot(
annotation_evol, label="Type errors (right)", marker="x", color='lightsalmon')
lines = line1 + line2
labels = [l.get_label() for l in lines]
fig.legend(lines, labels,loc='upper center', bbox_to_anchor=(0.52, 1.1))
plt.savefig(f"{plots_base_dir}error_per_project/{p}.pdf", bbox_inches='tight')
plt.close()
def compute_stats_on_histories(project_to_history):
print("\n------------------")
print("Statistics on histories:")
commits_total = 0
commits_with_errors = 0
for p, h in project_to_history.items():
for r in h:
commits_total += 1
nb_warnings = count_filtered_warnings(r["kind_to_nb"])
if nb_warnings > 0:
commits_with_errors += 1
print(f" {commits_total} total commits")
print(f" {commits_with_errors} commits with errors ({round(commits_with_errors*100 / commits_total, 1)}%)")
def compute_stats_on_latest(results):
print("\n------------------")
print("Statistics on latest versions:")
loc_total = results["loc"].sum()
errors_total = results["nb_filtered_warnings"].sum()
print(f" {loc_total} total LoC in latest versions")
print(f" {errors_total} total errors in latest versions")
print(f" {round(errors_total / loc_total, 3)} errors per LoC, on average")
corr_loc_errors = results["loc"].corr(results["nb_filtered_warnings"])
print(f" Correlation between errors and LoC: {corr_loc_errors}")
corr_annotations_errors = results["nb_types"].corr(
results["nb_filtered_warnings"])
print(
f" Correlation between errors and annotations: {corr_annotations_errors}")
#Pure commits: Run on all
#Evolution of type errors vs type annotations: Run on 1/20 or 1/50
#Store raw output of mypy for later reuse
if __name__ == "__main__":
project_to_history, latest_results = get_results()
compute_more_columns(latest_results)
#plot_kinds_of_errors(latest_results)
#plot_errors_vs_loc(latest_results)
plot_errors_vs_annotations(latest_results)
plot_per_project_evolution(project_to_history)
#plot_error_per_project_evolution(project_to_history)
#plot_evolution_of_errors_vs_annotations(project_to_history)
#plot_evolution_of_avg_errors_vs_annotations(project_to_history)
#plot_evolution_of_errors_vs_loc(project_to_history)
#plot_evolution_of_errors(project_to_history)
#compute_stats_on_latest(latest_results)
#compute_stats_on_histories(project_to_history)
# plot_warnings_loc_evolution(p)