statistic.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import glob
  2. import os
  3. import pandas as pd
  4. import matplotlib.pyplot as plt
  5. import time
  6. import numpy as np
  7. import seaborn as sns
  8. import math
  9. path = os.getcwd()
  10. def get_average(records):
  11. return sum(records) / len(records)
  12. def get_variance(records):
  13. average = get_average(records)
  14. return sum([(x - average) ** 2 for x in records]) / len(records)
  15. def get_standard_deviation(records):
  16. variance = get_variance(records)
  17. return math.sqrt(variance)
  18. def get_rms(records):
  19. return math.sqrt(sum([x ** 2 for x in records]) / len(records))
  20. def get_mse(records_real, records_predict):
  21. if len(records_real) == len(records_predict):
  22. return sum([(x - y) ** 2 for x, y in zip(records_real, records_predict)]) / len(records_real)
  23. else:
  24. return None
  25. def get_rmse(records_real, records_predict):
  26. mse = get_mse(records_real, records_predict)
  27. if mse:
  28. return math.sqrt(mse)
  29. else:
  30. return None
  31. def get_mae(records_real, records_predict):
  32. if len(records_real) == len(records_predict):
  33. return sum([abs(x - y) for x, y in zip(records_real, records_predict)]) / len(records_real)
  34. else:
  35. return None
  36. def writeSDCSV(filename):
  37. file = pd.read_csv("Mean.csv")
  38. conditions = file['condition']
  39. dict = {}
  40. dict['conditon'] = conditions
  41. for scale in scales:
  42. temp = []
  43. for condition in conditions:
  44. col = df_merged.groupby('condition').get_group(condition)
  45. col = col[scale]
  46. temp.append(get_standard_deviation(col))
  47. dict[scale] = temp
  48. df = pd.DataFrame(dict)
  49. df.to_csv(filename)
  50. def draw(scale):
  51. conditions = file['condition']
  52. result = file[scale]
  53. plt.figure(figsize=(9, 6), dpi=100)
  54. sd = pd.read_csv(SD)
  55. std_err = sd[scale]
  56. error_params=dict(elinewidth=1,ecolor='black',capsize=5)
  57. plt.bar(conditions, result, width=0.35, color=colors,alpha=a,yerr=std_err,error_kw=error_params)
  58. plt.title(scale,fontsize=15)
  59. plt.ylabel('score')
  60. plt.grid(alpha=0, linestyle=':')
  61. plt.savefig(scale, dpi=300)
  62. #plt.show()
  63. def drawTogether():
  64. scales = ["mental-demand","physical-demand","temporal-demand","performance", "effort","frustration","total"]
  65. scales = ["total","mental-demand","physical-demand","frustration"]
  66. plt.figure(figsize=(15,7))
  67. x = np.arange(len(scales))
  68. total_width, n = 0.8, 4
  69. width = total_width / n
  70. for i in range(0,4):
  71. result = []
  72. std_err = []
  73. sd = pd.read_csv(SD)
  74. for scale in scales:
  75. result.append(file.iloc[i][scale])
  76. std_err.append(sd.iloc[i][scale])
  77. error_params=dict(elinewidth=1,ecolor='black',capsize=5)
  78. plt.bar(x+width*(i-1),result,width=width,color=colors[i],label=file.iloc[i]["condition"],alpha=a,yerr=std_err,error_kw=error_params)
  79. plt.legend()
  80. # plt.title("TLX Average",fontsize=15)
  81. plt.xticks(x+width/2,scales)
  82. #plt.show()
  83. plt.ylabel('score')
  84. plt.savefig("summary.jpg",dpi=300)
  85. # Merge all the .csv file start with "HectorVR", and
  86. all_files = glob.glob(os.path.join(path, "HectorVR*.csv"))
  87. df_from_each_file = (pd.read_csv(f, sep=',') for f in all_files)
  88. df_merged = pd.concat(df_from_each_file, ignore_index=True)
  89. # Save the file to Merged.csv in the same folder
  90. df_merged.to_csv( "Merged.csv")
  91. # save the results in csv
  92. file = df_merged.groupby(["condition"]).mean()
  93. file.to_csv("Mean.csv")
  94. scales = ["mental-demand","physical-demand","temporal-demand","performance", "effort","frustration","total"]
  95. SD = "standard_deviation.csv"
  96. writeSDCSV(SD)
  97. file = pd.read_csv("Mean.csv")
  98. colors = sns.color_palette()
  99. a = 0.6
  100. for scale in scales:
  101. draw(scale)
  102. drawTogether()