CSVReader.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import csv
  2. from CertainTrust import Opinion
  3. class CSVReader:
  4. prediction_read = 0;
  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. summ = 0
  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. summ = summ + 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. CSVReader.prediction_read = summ
  34. return result
  35. @staticmethod
  36. def read_csv_opinons(months):
  37. '''
  38. Reads a opinions from CSV file into python dictionary
  39. The CSV input file should contain the following structure:
  40. packageName:t:c:f
  41. :param months: number of months
  42. :return: dictionary of package-names as key and opinions as value
  43. '''
  44. result = {}
  45. with open("vendors/debian/models/dummy_model_"+str(months)+".csv", newline="") as csvfile:
  46. reader = csv.reader(csvfile, delimiter=':', quotechar='|')
  47. for row in reader:
  48. if not len(row) == 0:
  49. result[row[0]]=Opinion(float(row[1]),float(row[2]),float(row[3]))
  50. return result
  51. @staticmethod
  52. def read_csv_package_names(filename):
  53. '''
  54. reads package names from csv file
  55. :param filename:
  56. :return: dictionary of package names
  57. '''
  58. result = []
  59. with open(filename, newline="") as csvfile:
  60. reader = csv.reader(csvfile, delimiter=':', quotechar='|')
  61. for row in reader:
  62. if not len(row) == 0:
  63. result.append(row[0])
  64. return result
  65. '''
  66. res =CSVReader.readCSV("inputs/dummy_input.csv", 1, 3)
  67. for key in res:
  68. print(key + ":" + str(res[key].t)+ ":" + str(res[key].c)+ ":" + str(res[key].f))
  69. CSVReader.read_csv_package_names("inputs/dummy_input_package.csv")
  70. '''
  71. # /home/keks/mstar/mstar-project/vendors/debian/inputs/dummy_input_package_prediction_errorcompl.csv