statistic.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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(filename):
  51. conditions = file['condition']
  52. result = file[filename]
  53. plt.figure(figsize=(9, 6), dpi=100)
  54. plt.bar(conditions, result, width=0.35, color=colors,alpha=a)
  55. plt.title(filename,fontsize=20)
  56. plt.ylabel('score')
  57. plt.grid(alpha=0, linestyle=':')
  58. plt.savefig(filename + ".jpg", dpi=300)
  59. #plt.show()
  60. def drawRobotPerformance():
  61. plt.figure(figsize=(20,8))
  62. conditions = file['condition']
  63. sd = pd.read_csv(SD)
  64. error_params=dict(elinewidth=1,ecolor='black',capsize=5)
  65. plt.suptitle("Robot Performance",fontsize=20)
  66. plt.subplot(221)
  67. plt.title("Collision",fontsize=15)
  68. std_err = sd["Collision"]
  69. plt.ylabel('Times')
  70. plt.bar(conditions, file["Collision"], width=0.35, color=colors,alpha=a,yerr=std_err,error_kw=error_params)
  71. plt.subplot(222)
  72. plt.title("Drive Distance",fontsize=15)
  73. std_err = sd["Drive Distance"]
  74. plt.ylabel('Distance(m)')
  75. plt.bar(conditions, file["Drive Distance"], width=0.35, color=colors,alpha=a,yerr=std_err,error_kw=error_params)
  76. plt.subplot(223)
  77. plt.title("Total driving time",fontsize=15)
  78. std_err = sd["Total driving time"]
  79. plt.ylabel('Time(s)')
  80. plt.bar(conditions, file["Total driving time"], width=0.35, color=colors,alpha=a,yerr=std_err,error_kw=error_params)
  81. plt.subplot(224)
  82. plt.title("Adverage speed",fontsize=15)
  83. std_err = sd["Adverage speed"]
  84. plt.ylabel('Speed(m/s)')
  85. plt.bar(conditions, file["Adverage speed"], width=0.35, color=colors,alpha=a,yerr=std_err,error_kw=error_params)
  86. plt.savefig("Robot Performance",dpi=300)
  87. def drawRescue():
  88. plt.figure(figsize =(7,7))
  89. x = np.arange(len(scales))
  90. total_width, n = 0.8, 4
  91. width = total_width / n
  92. sd = pd.read_csv(SD)
  93. error_params=dict(elinewidth=1,ecolor='black',capsize=5)
  94. # set range
  95. plt.ylim(0, 10.5)
  96. for i in range(0,4):
  97. result = []
  98. std_err = []
  99. for scale in scales:
  100. result.append(file.iloc[i][scale])
  101. std_err.append(sd.iloc[i][scale])
  102. 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)
  103. plt.legend()
  104. plt.title("Rescue situation",fontsize=15)
  105. plt.xticks(x+width/2,scales)
  106. plt.ylabel('Person')
  107. #plt.show()
  108. plt.savefig("Rescue situation",dpi=300)
  109. # Merge all the .csv file
  110. all_files = glob.glob(os.path.join(path, "*.csv"))
  111. df_from_each_file = (pd.read_csv(f, sep=',') for f in all_files)
  112. df_merged = pd.concat(df_from_each_file, ignore_index=True)
  113. # Save the file to Merged.csv in the same folder
  114. # df_merged.to_csv( "Merged.csv")
  115. # save the results in csv
  116. df_merged["condition"] = df_merged["condition"].apply(lambda x: x.replace("Test",""))
  117. file = df_merged.groupby(["condition"]).mean()
  118. file.to_csv( "Mean.csv")
  119. scales = ["Collision","Drive Distance","Total driving time","Adverage speed","Rescued Target", "Remained Visible Target","Remained Unvisible Target"]
  120. SD = "standard_deviation.csv"
  121. writeSDCSV(SD)
  122. file = pd.read_csv("Mean.csv")
  123. colors = sns.color_palette()
  124. a = 0.6
  125. # for scale in scales:
  126. # draw(scale)
  127. #scales = ["Rescued Target", "Remained Visible Target","Remained Unvisible Target"]
  128. scales = ["Rescued Target", "Remained Visible Target"]
  129. drawRescue()
  130. scales = ["Collision","Drive Distance","Total driving time","Adverage speed"]
  131. drawRobotPerformance()
  132. os.remove("Mean.csv")
  133. os.remove(SD)