CSVReader.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import csv
  2. from CertainTrust import Opinion
  3. class CSVReader:
  4. @staticmethod
  5. def readCSV(inputfile, f, months):
  6. '''
  7. Converts input csv file into a dictionary of opinions,
  8. The CSV input file should contain the following structure:
  9. packageName:prediction:errorComplement
  10. :param inputfile: relative path to input csv file
  11. :param f: initial expectation value
  12. :param months: number of months
  13. :return: dictionary of package-names as key and opinions as value
  14. '''
  15. result = {}
  16. with open(inputfile, newline="") as csvfile:
  17. reader = csv.reader(csvfile, delimiter=':', quotechar='|')
  18. for row in reader:
  19. if not len(row) == 0:
  20. package = row [0]
  21. prediction = row [1]
  22. errorCompl = float(row [2])
  23. # FIXME: HARDCODED MONTHS
  24. resT = 1 - int(prediction) / (30 * months)
  25. result[package]=Opinion(resT , errorCompl, f)
  26. return result
  27. @staticmethod
  28. def readCSVModel(months):
  29. '''
  30. Reads a model from CSV file into python dictionary
  31. :param months: number of months
  32. :return: dictionary of package-names as key and opinions as value
  33. '''
  34. result = {}
  35. with open("models/dummy_model_"+str(months)+".csv", newline="") as csvfile:
  36. reader = csv.reader(csvfile, delimiter=':', quotechar='|')
  37. for row in reader:
  38. if not len(row) == 0:
  39. result[row[0]]=Opinion(float(row[1]),float(row[2]),float(row[3]))
  40. return result
  41. @staticmethod
  42. def readCSVPackages(filename):
  43. '''
  44. reads package names from csv file
  45. :param filename:
  46. :return: dictionary of package names
  47. '''
  48. result = []
  49. with open(filename, newline="") as csvfile:
  50. reader = csv.reader(csvfile, delimiter=':', quotechar='|')
  51. for row in reader:
  52. if not len(row) == 0:
  53. result.append(row[0])
  54. return result
  55. '''
  56. res =CSVReader.readCSV("inputs/dummy_input.csv", 1)
  57. for key in res:
  58. print(key + ":" + str(res[key].t)+ ":" + str(res[key].c)+ ":" + str(res[key].f))
  59. CSVReader.readCSVPackages("inputs/dummy_input_package.csv")
  60. '''