plot_types.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import matplotlib.pyplot as plt
  2. import json
  3. import numpy as np
  4. import paper_plots as carlosplt
  5. import seaborn as sns
  6. from matplotlib.font_manager import FontProperties
  7. class TypePlotter:
  8. def __init__(self, data, years):
  9. self.src2dsa = data.src2dsa
  10. self.dsa2cve = data.dsa2cve
  11. self.cvetable = data.cvetable
  12. self.pkg_with_cvss = data.pkg_with_cvss
  13. self.src2cwe = data.src2cwe
  14. self.years = years
  15. self.src2monthDLA = dict()
  16. self.DLA_withcwe = dict()
  17. with open("src2month_DLA.json", "r") as fp:
  18. self.src2monthDLA = json.load(fp)
  19. with open("DLA_withcwe.json", "r") as fp:
  20. self.DLA_withcwe = json.load(fp)
  21. def plot_types(self):
  22. src2cwe_sum = []
  23. for i in range(0, 12*self.years):
  24. src2cwe_sum.append([0]*12)
  25. for pkg in self.src2cwe:
  26. for i in range(len(self.src2cwe[pkg])):
  27. for j in range(len(self.src2cwe[pkg][i])):
  28. src2cwe_sum[i][j] += self.src2cwe[pkg][i][j]
  29. src2cwe_sumofsums = [0]*12
  30. for month in src2cwe_sum:
  31. for j in range(len(month)):
  32. src2cwe_sumofsums[j] += month[j]
  33. print(src2cwe_sumofsums)
  34. cwe2month = dict()
  35. for j in range(12):
  36. cwe2month[j] = []
  37. for month in src2cwe_sum:
  38. for j in range(len(month)):
  39. cwe2month[j].append(month[j])
  40. print(cwe2month[1])
  41. print(sum(cwe2month[1]))
  42. for i in range(12):
  43. binned = []
  44. for j in range(self.years*3):
  45. binned.append(sum(cwe2month[i][4*j:4*j+4]))
  46. #plt.plot(binned)
  47. #plt.show()
  48. percent = []
  49. for i in range(self.years):
  50. temp = [0]*12
  51. for j in range(12):
  52. temp[j] = sum(cwe2month[j][12*i:12*i+12])
  53. sum_temp = sum(temp)
  54. try:
  55. temp_percent = list(map(lambda x: x / sum_temp, temp))
  56. percent.append(temp_percent)
  57. except ZeroDivisionError:
  58. percent.append(temp)
  59. print('Year ', i+1, 'is the problem')
  60. print(percent)
  61. carlosplt.pre_paper_plot()
  62. x = range(2000,2000+self.years)
  63. y = []
  64. labels_cwe = ['682', '118', '664', '691', '693', '707', '710', 'N/A']
  65. for i in range(12):
  66. if i not in [2,3,7,8]:
  67. y.append([j[i] for j in percent[8:]])
  68. pal = sns.color_palette("Paired", 12)
  69. h = plt.stackplot(x[8:], y, colors = pal, alpha=0.9, labels = labels_cwe)
  70. plt.xticks(x[8:])
  71. fontP = FontProperties()
  72. fontP.set_size('small')
  73. plt.legend(loc='upper left', handles = h[::-1], prop=fontP)
  74. carlosplt.post_paper_plot(True,True,True)
  75. plt.show()
  76. self.plot_wheezyTypes(src2cwe_sum)
  77. return 0
  78. def plot_peryearstable(self):
  79. ## Plot the proportion changes over the years (All 11 types? - top 5 types).
  80. pass
  81. def plot_wheezyTypes(self, cwe_sum):
  82. ## Plot the changes in Wheezy for the top 3 types of vulnerabilities.
  83. ## Plot for wheezy
  84. quarter_num = 4 * self.years
  85. quarter_sum = dict()
  86. quarter_sum_DLA = dict()
  87. ## DSA Q2'13-Q2'16
  88. ## DLA Q3'16-Q2'18
  89. cwe_sum_DLA = []
  90. for i in range(0, 12*self.years):
  91. cwe_sum_DLA.append([0]*12)
  92. for pkg in self.DLA_withcwe:
  93. for i in range(len(self.DLA_withcwe[pkg])):
  94. for j in range(len(self.DLA_withcwe[pkg][i])):
  95. for k in range(len(self.DLA_withcwe[pkg][i][j])):
  96. cwe_sum_DLA[i*12+j][k] += self.DLA_withcwe[pkg][i][j][k]
  97. for cwe in range(len(cwe_sum[0])):
  98. print(cwe)
  99. quarter_sum[cwe] = [0] * quarter_num
  100. quarter_sum_DLA[cwe] = [0] * quarter_num
  101. for m in range(quarter_num):
  102. quarter_sum[cwe][m] = cwe_sum[3*m][cwe] + cwe_sum[3*m+1][cwe] + cwe_sum[3*m+2][cwe]
  103. quarter_sum_DLA[cwe][m] = cwe_sum_DLA[3*m][cwe] + cwe_sum_DLA[3*m+1][cwe] + cwe_sum_DLA[3*m+2][cwe]
  104. print(quarter_sum)
  105. print(quarter_sum_DLA)
  106. quartersx = []
  107. for i in range(1,self.years+1):
  108. for j in range(1,5):
  109. if j==1:
  110. quartersx.append('Q' + str(j)+'\''+str(i).zfill(2))
  111. else:
  112. quartersx.append(' ')
  113. ## Filter only wheezy. Do it for a selection of types:
  114. root_list = ['682', '118', '330', '435', '664', '691', '693', '697', '703', '707', '710' ]
  115. fig = plt.figure()
  116. ii = 0
  117. for j in [1, 4, 6, 11]:
  118. ii += 1
  119. quarter_sum_regular = [0] * (12*4+1) + quarter_sum[j][12*4+1:12*4+9] + [0] * 12
  120. quarter_sum_errors = [0] * (12*4 + 9) + quarter_sum[j][12*4+9:12*4+9+5] + [0] * 7
  121. LTS_quarter = [0] * (15*4+2) + quarter_sum_DLA[j][15*4+2:-3-4]
  122. #print(quarter_sum_errors)
  123. cut = 12*4+1
  124. n = len(quarter_sum[j]) - cut -7
  125. x = range(quarter_num-cut-3-4)
  126. width = 1/2
  127. #print(len(LTS_quarter))
  128. print(len(x))
  129. print(len(quarter_sum_regular[cut:]))
  130. print(len(quarter_sum_errors[cut:]))
  131. print(len(LTS_quarter[cut:]))
  132. ax = fig.add_subplot(2,2,ii)
  133. bar1 = plt.bar(x, quarter_sum_regular[cut:], width, color='darkblue', label='regular', edgecolor='black')
  134. bar12 = plt.bar(x, quarter_sum_errors[cut:], width, color='darkorange', label='regular*', edgecolor='black')
  135. bar2 = plt.bar(x, LTS_quarter[cut:], width, color='darkred', label ='long-term', edgecolor='black')
  136. if ii==2:
  137. plt.legend(handles=[bar1, bar12, bar2])
  138. plt.xticks(np.arange(0,n),quartersx[cut:], rotation="vertical")
  139. try:
  140. plt.ylabel('CWE-' + root_list[j])
  141. except IndexError:
  142. plt.ylabel('N/A')
  143. plt.xlabel('Quarter')
  144. carlosplt.post_paper_plot(True,True,True)
  145. plt.show()