-
Notifications
You must be signed in to change notification settings - Fork 0
/
generateresults_rq3.py
74 lines (59 loc) · 2.64 KB
/
generateresults_rq3.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
import os
import numpy as np
import matplotlib.pyplot as plt
import json
from properties import dbpath, resultspath
from libs.dbmanager import DBManager
""" Generate diagram for RQ-3 """
# Connect to database
dbmanager = DBManager(dbpath)
db = dbmanager.db
# Create a folder to store the results if it doesn't exist
results_folder = resultspath
os.makedirs(results_folder, exist_ok=True)
# Load annotations from file
with open('annotations.txt', 'r') as file:
# Load the JSON data from the file into a dictionary
annotations = json.load(file)
before_after_diff = []
# Get commits
for commit in db["commits"].find({"AnalysisFeatures": {"$exists": True}}):
# Keep only the entries of class `improve this code` (2)
if annotations[commit['URL']] == "2":
for commited_file in commit['AnalysisFeatures']['FileAnalysis']:
if isinstance(commited_file['QualityAnalysis'], dict):
# Keep only the entries where previous version exists
if commited_file['QualityAnalysis']['PreviousContent']:
diff = commited_file['QualityAnalysis']['Current'] - commited_file['QualityAnalysis']['Previous']
before_after_diff.append(diff)
# Create a list of differences with zeros removed
before_after_diff_nz = [a for a in before_after_diff if a]
# Create the bar chart of differences
fig, ax = plt.subplots(figsize=(4.85, 3))
# Adjust the white space around the figure
plt.subplots_adjust(bottom=0.15)
before_after_diff_nz = np.array(before_after_diff_nz)
# Plotting the data with two different colors
added_violations = np.where(before_after_diff_nz > 0, before_after_diff_nz, 0)
removed_violations = np.where(before_after_diff_nz < 0, before_after_diff_nz, 0)
bars_added = ax.barh(range(len(before_after_diff_nz)), added_violations, color='green', label='Violations\nIncreased')
bars_removed = ax.barh(range(len(before_after_diff_nz)), removed_violations, color='red', label='Violations\nDecreased')
# Set labels
ax.set_xlabel('Change in Violations', fontsize=13)
ax.set_ylabel('Case Index', fontsize=13)
# Calculate new x-axis limits based on the data
data_max = np.max(np.abs(before_after_diff_nz))
rounded_max = 5 * round((data_max + 5) / 5)
# Set ticks and labels based on the rounded values
ticks = np.arange(-rounded_max, rounded_max + 1, 5)
ax.set_xticks(ticks)
ax.set_xticklabels([str(t) for t in ticks])
# Extend the x-axis range a little bit from the right
current_xlim = ax.get_xlim()
new_xlim = (current_xlim[0] - 2, current_xlim[1] + 2)
ax.set_xlim(new_xlim)
plt.legend()
plt.tight_layout()
plt.savefig(os.path.join(results_folder, 'RQ3ViolationDifference.eps'), format='eps')
plt.savefig(os.path.join(results_folder, 'RQ3ViolationDifference.pdf'), format='pdf')
# plt.show()