laplace_tests.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. from load_data import load_DBs
  2. from pymongo import MongoClient
  3. import datetime
  4. import random
  5. import laplace as lp
  6. import matplotlib.pyplot as plt
  7. import paper_plots as carlosplt
  8. import numpy as np
  9. import json
  10. class Mydata:
  11. def __init__(self, load):
  12. if load:
  13. (self.dsatable, self.src2dsa, self.dsa2cve, self.cvetable, self.src2month, self.src2sloccount, self.src2pop, self.src2deps, self.pkg_with_cvss) = load_DBs()
  14. else:
  15. print('no load command given')
  16. def main():
  17. vlist = []
  18. carlosplt.pre_paper_plot()
  19. fig = plt.figure()
  20. tester = calc_laplace()
  21. ax = fig.add_subplot(2,2,1)
  22. tester.laplace_php([])
  23. ax = fig.add_subplot(2,2,2)
  24. tester.laplace_openjdk([])
  25. ax = fig.add_subplot(2,2,3)
  26. tester.laplace_wheezy([], False)
  27. ax = fig.add_subplot(2,2,4)
  28. tester.laplace_wheezy([], True)
  29. carlosplt.post_paper_plot(True,True,True)
  30. plt.show()
  31. class calc_laplace:
  32. def __init__(self):
  33. i = 0
  34. def getDLAdates(self):
  35. pass
  36. def calculate(self):
  37. pass
  38. def laplace_php(self, vlist):
  39. try:
  40. with open("php5.json", "r") as fp:
  41. new_vlist = json.load(fp)
  42. except:
  43. new_vlist = vlist
  44. with open("php5.json", "w") as fp:
  45. json.dump(new_vlist,fp)
  46. print(len(new_vlist))
  47. year_start = 7
  48. self.laplace_process_list(new_vlist[12*year_start:-6], 'php5', year_start)
  49. def laplace_openjdk(self, vlist):
  50. try:
  51. with open("openjdk.json", "r") as fp:
  52. new_vlist = json.load(fp)
  53. except:
  54. new_vlist = vlist
  55. with open("openjdk.json", "w") as fp:
  56. json.dump(new_vlist,fp)
  57. print(len(new_vlist))
  58. year_start = 13
  59. self.laplace_process_list(new_vlist[12*year_start+6:], 'openjdk-7', year_start)
  60. def laplace_wheezy(self, vlist, high):
  61. (dsatable, src2dsa, dsa2cve, cvetable, src2month, src2sloccount, src2pop, src2deps, pkg_with_cvss, src2cwe) = load_DBs()
  62. print(len(src2month['linux']))
  63. if not high:
  64. with open("DLA_src2month.json", "r") as fp:
  65. dlas = json.load(fp)
  66. else:
  67. src2month = pkg_with_cvss
  68. with open("DLA_withcvss.json", "r") as fp:
  69. dlas = json.load(fp)
  70. total = [0]*len(src2month['linux'])
  71. for pkg in src2month:
  72. for month in range(len(src2month[pkg])):
  73. if high:
  74. total[month] += src2month[pkg][month][2]
  75. else:
  76. total[month] += src2month[pkg][month]
  77. total_dla = [0]*(len(dlas['linux'])*12)
  78. print(total_dla)
  79. for pkg in dlas:
  80. for year in range(len(dlas[pkg])):
  81. for month in range(len(dlas[pkg][year])):
  82. if high:
  83. total_dla[year*12+month] += dlas[pkg][year][month][2]
  84. else:
  85. total_dla[year*12+month] += dlas[pkg][year][month]
  86. print(total)
  87. print(total_dla)
  88. dsa_wheezy = total[12*13 + 4: 12*16 + 3]
  89. dla_wheezy = total_dla[-12*3 + 4:-7]
  90. print(len(dsa_wheezy))
  91. print(len(dla_wheezy))
  92. wheezy = dsa_wheezy + dla_wheezy
  93. print(len(wheezy))
  94. if not high:
  95. self.laplace_process_list(wheezy, 'wheezy', 13)
  96. else:
  97. self.laplace_process_list(wheezy, 'wheezy-high', 13)
  98. def laplace_package(self, pkg):
  99. pass
  100. def laplace_process_list(self,vlist, pkg, year):
  101. months = len(vlist)
  102. print(pkg)
  103. perhour = 24*30*months
  104. instances = []
  105. laplace_values = []
  106. i = 0
  107. print(pkg)
  108. print(vlist)
  109. for month in vlist:
  110. i += 1
  111. temp = random.sample(range(24*30*(i-1),24*30*i),month)
  112. instances += temp
  113. laplace_values.append(lp.laplace_test(instances, 24*30*i))
  114. final_laplace = lp.laplace_test(instances, 24*30*i+1)
  115. print(final_laplace)
  116. n = len(vlist)
  117. if pkg == 'wheezy':
  118. x = range(n+12)
  119. else:
  120. x = range(n)
  121. print(n)
  122. yearsx = ['\''+str(year+2000+i)[-2:] for i in range(len(vlist)//12+1)]
  123. carlosplt.post_paper_plot(True,True,True)
  124. #print(x)
  125. if pkg == 'wheezy':
  126. plt.plot(x,[None]*4+laplace_values+[None]*8)#
  127. else:
  128. plt.plot(x,laplace_values)
  129. plt.axhline(y=1.96, linestyle = ':', color = 'orange')
  130. plt.axhline(y=2.33, linestyle = ':', color = 'red')
  131. plt.axhline(y=-1.96, linestyle = ':', color = 'orange')
  132. plt.axhline(y=-2.33, linestyle = ':', color = 'red')
  133. plt.xticks(np.arange(0, n, step=12), yearsx)
  134. plt.ylabel(pkg)
  135. if __name__ == "__main__":
  136. main()