CSVReader.py 3.0 KB

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