CSVReader.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import csv
  2. from CertainTrust import Opinion
  3. class CSVReader:
  4. gathered_predictions = dict()
  5. @staticmethod
  6. def read_csv_prediction_errorcompl(inputfile, vendormodel, months, f=0.5, norm_param=4):
  7. '''
  8. Converts input csv file into a dictionary of opinions,
  9. The CSV input file should contain the following structure:
  10. packageName:prediction:errorComplement:initial_expectation
  11. :param inputfile: relative path to input csv file
  12. :param vendormodel: vendor model, needed for unifying the source-name of a package
  13. :param f: initial expectation value, will be used if csv does not contain row[3]
  14. :param months: number of months
  15. :return: dictionary of package-names as key and opinions as value
  16. '''
  17. result = {}
  18. with open(inputfile, newline="") as csvfile:
  19. reader = csv.reader(csvfile, delimiter=':')
  20. for row in reader:
  21. if not len(row) == 0:
  22. package = vendormodel.unifySrcName(row [0])
  23. prediction = float(row [1])
  24. CSVReader.gathered_predictions[package]=prediction
  25. errorCompl = float(row [2])
  26. resT = 1 - prediction / (30 * months * norm_param)
  27. if len(row)==4:
  28. oldf = float(row[3])
  29. newf = 1 + ( (oldf - 1) / (norm_param / 4 ))
  30. result[package]=Opinion(resT, errorCompl, newf)
  31. else:
  32. result[package] = Opinion(resT, errorCompl, f)
  33. return result
  34. @staticmethod
  35. def read_csv_opinons(months):
  36. '''
  37. Reads a opinions from CSV file into python dictionary
  38. The CSV input file should contain the following structure:
  39. packageName:t:c:f
  40. :param months: number of months
  41. :return: dictionary of package-names as key and opinions as value
  42. '''
  43. result = {}
  44. with open("vendors/debian/models/dummy_model_"+str(months)+".csv", newline="") as csvfile:
  45. reader = csv.reader(csvfile, delimiter=':', quotechar='|')
  46. for row in reader:
  47. if not len(row) == 0:
  48. result[row[0]]=Opinion(float(row[1]),float(row[2]),float(row[3]))
  49. return result
  50. @staticmethod
  51. def read_csv_package_names(filename):
  52. '''
  53. reads package names from csv file
  54. :param filename:
  55. :return: dictionary of package names
  56. '''
  57. result = []
  58. with open(filename, newline="") as csvfile:
  59. reader = csv.reader(csvfile, delimiter=':', quotechar='|')
  60. for row in reader:
  61. if not len(row) == 0:
  62. result.append(row[0])
  63. return result
  64. '''
  65. res =CSVReader.readCSV("inputs/dummy_input.csv", 1, 3)
  66. for key in res:
  67. print(key + ":" + str(res[key].t)+ ":" + str(res[key].c)+ ":" + str(res[key].f))
  68. CSVReader.read_csv_package_names("inputs/dummy_input_package.csv")
  69. '''
  70. # /home/keks/mstar/mstar-project/vendors/debian/inputs/dummy_input_package_prediction_errorcompl.csv