Tests.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import os
  2. from CertainTrust import Opinion
  3. from vendors.debian.CSVReader import CSVReader
  4. class Tests:
  5. @staticmethod
  6. def input_prediction_error_test(model):
  7. norm_param = 4
  8. model_filename = os.path.join(model.module_path, "models", "dummy_input_package_prediction_errorcompl.csv")
  9. model_set = model.gen_model_opinion_set(model_filename, 9, norm_param)
  10. system_filename = os.path.join(model.module_path, "inputs", "dummy_input_package_server.csv")
  11. system = CSVReader.read_csv_package_names(system_filename)
  12. # get system dict
  13. system_dict = dict()
  14. for key in system:
  15. print(key)
  16. new_key = model.unifySrcName(key)
  17. if new_key in model_set:
  18. system_dict[new_key] = model_set[new_key]
  19. print("Evaluating on: " + str(len(system_dict)) + " out of " + str(
  20. len(system)) + " packages, since they are not present in our model.")
  21. Tests.evaluate_expectation(system_dict, model, 9, norm_param)
  22. @staticmethod
  23. def evaluate_expectation(package_list, vendormodel, months, norm_val):
  24. """
  25. TODO:
  26. :param package_list: dictionary of opinions with names as key and opinion as value
  27. :param vendormodel: the vendor-model to compare the prediction with
  28. :return: TODO:
  29. """
  30. print("For normalization value = " + str(norm_val))
  31. # ANDed prediction
  32. system_and = Opinion._cum_and(list(package_list.values()))
  33. expectation = system_and.expectation_value()
  34. AND_prediction = (1 - expectation) * (norm_val * months * 30)
  35. # separate prediction
  36. sep_pred = 0
  37. for k in package_list:
  38. sep_pred = sep_pred + ((1 - package_list[k].expectation_value()) * (norm_val * months * 30))
  39. # src2month prediction gathering
  40. summ = 0
  41. for package in package_list:
  42. unified_package = vendormodel.unifySrcName(package)
  43. src2month = vendormodel.get_src2month()
  44. if unified_package in src2month:
  45. summ = summ + sum(src2month[package][-months - 3:-3])
  46. file_error = abs(summ - CSVReader.prediction_read) / summ
  47. print("Input file prediction = " + str(CSVReader.prediction_read) + ", ( error = " + str(file_error) + " )")
  48. sep_error = abs(summ - sep_pred) / summ
  49. print("Separate prediction = " + str(sep_pred) + ", ( error = " + str(sep_error) + " )")
  50. and_error = abs(summ - AND_prediction) / summ
  51. print("ANDed prediction = " + str(AND_prediction) + ", ( error = " + str(and_error) + " )")
  52. print("Src2month data = " + str(summ))