Tests.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import os
  2. import random
  3. import math
  4. from CertainTrust import Opinion
  5. from vendors.debian.CSVReader import CSVReader
  6. class Tests:
  7. @staticmethod
  8. def system_input_prediction_error_test(model):
  9. """
  10. Compare errors of three predictions: cummulatedAnd , cummulated separate prediction, prediction from csv file
  11. :param model: model to perform tests
  12. :return: nope
  13. """
  14. norm_param = 4
  15. months = 9
  16. model_filename = os.path.join(model.module_path, "models", "dummy_input_package_prediction_errorcompl.csv")
  17. model_set = model.gen_model_opinion_set(model_filename, 9, norm_param)
  18. system_filename = os.path.join(model.module_path, "inputs", "dummy_input_package_server.csv")
  19. system = CSVReader.read_csv_package_names(system_filename)
  20. # get system dict
  21. system_dict = dict()
  22. for key in system:
  23. new_key = model.unifySrcName(key)
  24. if new_key in model_set:
  25. system_dict[new_key] = model_set[new_key]
  26. print("Evaluating on: " + str(len(system_dict)) + " out of " + str(
  27. len(system)) + " packages, since they are not present in our model.")
  28. and_prediction = Tests.acc_AND_prediction(system_dict, months, norm_param)
  29. sep_prediction = Tests.separate_prediction(system_dict, months, norm_param)
  30. csv_prediction = Tests.read_model_prediction(system_dict)
  31. s2m_prediction = Tests.get_src2month_data(system_dict, model, months)
  32. and_error = abs(and_prediction - s2m_prediction) / s2m_prediction
  33. sep_error = abs(sep_prediction - s2m_prediction) / s2m_prediction
  34. csv_error = abs(csv_prediction - s2m_prediction) / s2m_prediction
  35. print("and_error = "+str(and_error))
  36. print("sep_error = " + str(sep_error))
  37. print("csv_error = " + str(csv_error))
  38. return
  39. @staticmethod
  40. def random_input_prediction_error_test(model):
  41. """
  42. Compare errors of three predictions: cummulatedAnd , cummulated separate prediction, prediction from model
  43. :param model: model to perform tests
  44. :return: nope
  45. """
  46. norm_param = 4
  47. months = 9
  48. model_filename = os.path.join(model.module_path, "models", "dummy_input_package_prediction_errorcompl.csv")
  49. model_set = model.gen_model_opinion_set(model_filename, 9, norm_param)
  50. errors_dict=dict()
  51. errors_dict["and"]=[]
  52. errors_dict["sep"]=[]
  53. errors_dict["csv"]=[]
  54. # compute errors for 20 subsets
  55. for i in range(0, 100):
  56. # to compute the errors, wen need a random package_list dict of size 100
  57. subset = dict()
  58. while len(subset)!=100:
  59. package = random.choice(list(model_set))
  60. subset[package] = model_set[package]
  61. and_prediction = Tests.acc_AND_prediction(subset, months, norm_param)
  62. sep_prediction = Tests.separate_prediction(subset, months, norm_param)
  63. csv_prediction = Tests.read_model_prediction(subset)
  64. s2m_prediction = Tests.get_src2month_data(subset, model, months)
  65. errors_dict["and"].append(abs(and_prediction - s2m_prediction) / s2m_prediction)
  66. errors_dict["sep"].append(abs(sep_prediction - s2m_prediction) / s2m_prediction)
  67. errors_dict["csv"].append(abs(csv_prediction - s2m_prediction) / s2m_prediction)
  68. # given error dicts , we can compute the mean errors
  69. avg_and_error_normal = sum(errors_dict["and"])/len(errors_dict["and"])
  70. avg_sep_error_normal = sum(errors_dict["sep"]) / len(errors_dict["sep"])
  71. avg_csv_error_normal = sum(errors_dict["csv"]) / len(errors_dict["csv"])
  72. print("Normal errors: " + str(avg_and_error_normal) + " : " + str(avg_sep_error_normal) + " : " + str(avg_csv_error_normal))
  73. # quadratic errors
  74. avg_and_error_quadr = math.sqrt(sum(math.pow(i, 2) for i in errors_dict["and"]) / len(errors_dict["and"]))
  75. avg_sep_error_quadr = math.sqrt(sum(math.pow(i, 2) for i in errors_dict["sep"]) / len(errors_dict["sep"]))
  76. avg_csv_error_quadr = math.sqrt(sum(math.pow(i, 2) for i in errors_dict["csv"]) / len(errors_dict["csv"]))
  77. print("Quadratic errors: " + str(avg_and_error_quadr) + " : " + str(avg_sep_error_quadr) + " : " + str(avg_csv_error_quadr))
  78. @staticmethod
  79. def relativity_of_expectations_test(model):
  80. """
  81. Compares the relativeness of predictions of two sets, to relativeness of real data
  82. :param model:
  83. :return:
  84. """
  85. norm_param = 4
  86. months = 9
  87. model_filename = os.path.join(model.module_path, "models", "dummy_input_package_prediction_errorcompl.csv")
  88. model_set = model.gen_model_opinion_set(model_filename, 9, norm_param)
  89. computetd_rel_list = []
  90. real_rel_list = []
  91. for i in range(0, 100):
  92. # get two subsets
  93. subset1 = dict()
  94. while len(subset1)!=100:
  95. package = random.choice(list(model_set))
  96. subset1[package] = model_set[package]
  97. subset2 = dict()
  98. while len(subset2)!=100:
  99. package = random.choice(list(model_set))
  100. subset2[package] = model_set[package]
  101. # for these two sets, compute relativity of their ANDed predictions and of real data
  102. computed_rel_prediction = Tests.acc_AND_prediction(subset1, months, norm_param)/Tests.acc_AND_prediction(subset2, months, norm_param)
  103. computetd_rel_list.append(computed_rel_prediction)
  104. real_rel_prediction = Tests.get_src2month_data(subset1, model, months) / Tests.get_src2month_data(subset2, model, months)
  105. real_rel_list.append(real_rel_prediction)
  106. print("Computed relativity : " + str ( computed_rel_prediction ) + " : "+ str(real_rel_prediction))
  107. # at this point we have two lists of computed relatives, lets see how similair are they
  108. similarities = []
  109. for i in range(0, 100):
  110. similarity = abs(real_rel_list[i]-computetd_rel_list[i])
  111. similarities.append(similarity)
  112. print(similarities)
  113. avg_normal_relativity = sum(similarities)/len(similarities)
  114. avg_quadratic_relativity = math.sqrt(sum(math.pow(i, 2) for i in similarities) / len(similarities))
  115. print("Average normal relativity: " + str(avg_normal_relativity))
  116. print("Average quadratic relativity: "+str(avg_quadratic_relativity))
  117. ## helper methods
  118. @staticmethod
  119. def acc_AND_prediction(package_list, months, norm_val):
  120. """
  121. Returns accumulated and prediction for a list of packages
  122. :param package_list: dictionary with package_names as keys , opinions as values
  123. :param months: months
  124. :param norm_val: normalization value
  125. :return: prediction
  126. """
  127. system_and = Opinion._cum_and(list(package_list.values()))
  128. expectation = system_and.expectation_value()
  129. AND_prediction = (1 - expectation) * (norm_val * months * 30)
  130. return AND_prediction
  131. #and_error = abs(summ - AND_prediction) / summ
  132. #print("ANDed prediction = " + str(AND_prediction) + ", ( error = " + str(and_error) + " )")
  133. @staticmethod
  134. def separate_prediction(package_list, months, norm_val):
  135. """
  136. Returns summ of separated predictions for a list of packages
  137. :param package_list: dictionary with package_names as keys , opinions as values
  138. :param months: months
  139. :param norm_val: normalization value
  140. :return: prediction
  141. """
  142. sep_pred = 0
  143. for k in package_list:
  144. sep_pred = sep_pred + ((1 - package_list[k].expectation_value()) * (norm_val * months * 30))
  145. return sep_pred
  146. @staticmethod
  147. def read_model_prediction(package_list):
  148. """
  149. :param package_list: dictionary with package_names as keys , opinions as values
  150. :return: Summ of model-predictions
  151. """
  152. summ = 0
  153. for k in package_list:
  154. summ = summ + CSVReader.gathered_predictions[k]
  155. return summ
  156. @staticmethod
  157. def get_src2month_data(package_list, vendormodel, months):
  158. summ = 0
  159. for package in package_list:
  160. unified_package = vendormodel.unifySrcName(package)
  161. src2month = vendormodel.get_src2month()
  162. if unified_package in src2month:
  163. summ = summ + sum(src2month[package][-months - 3:-3])
  164. return summ