123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- import glob
- import os
- import pandas as pd
- import matplotlib.pyplot as plt
- import time
- import numpy as np
- import seaborn as sns
- import math
- path = os.getcwd()
- def get_average(records):
- """
- 平均值
- """
- return sum(records) / len(records)
- def get_variance(records):
- """
- 方差 反映一个数据集的离散程度
- """
- average = get_average(records)
- return sum([(x - average) ** 2 for x in records]) / len(records)
- def get_standard_deviation(records):
- """
- 标准差 == 均方差 反映一个数据集的离散程度
- """
- variance = get_variance(records)
- return math.sqrt(variance)
- def get_rms(records):
- """
- 均方根值 反映的是有效值而不是平均值
- """
- return math.sqrt(sum([x ** 2 for x in records]) / len(records))
- def get_mse(records_real, records_predict):
- """
- 均方误差 估计值与真值 偏差
- """
- if len(records_real) == len(records_predict):
- return sum([(x - y) ** 2 for x, y in zip(records_real, records_predict)]) / len(records_real)
- else:
- return None
- def get_rmse(records_real, records_predict):
- """
- 均方根误差:是均方误差的算术平方根
- """
- mse = get_mse(records_real, records_predict)
- if mse:
- return math.sqrt(mse)
- else:
- return None
- def get_mae(records_real, records_predict):
- """
- 平均绝对误差
- """
- if len(records_real) == len(records_predict):
- return sum([abs(x - y) for x, y in zip(records_real, records_predict)]) / len(records_real)
- else:
- return None
- def writeSDCSV(filename):
- file = pd.read_csv("Mean.csv")
- conditions = file['condition']
-
- dict = {}
- dict['conditon'] = conditions
- for scale in scales:
- temp = []
- for condition in conditions:
- col = df_merged.groupby('condition').get_group(condition)
- col = col[scale]
- temp.append(get_standard_deviation(col))
- dict[scale] = temp
- df = pd.DataFrame(dict)
- df.to_csv(filename)
- def draw(scale):
- conditions = file['condition']
- result = file[scale]
- plt.figure(figsize=(9, 6), dpi=100)
- sd = pd.read_csv(SD)
- std_err = sd[scale]
- error_params=dict(elinewidth=1,ecolor='black',capsize=5)
- plt.bar(conditions, result, width=0.35, color=colors,alpha=a,yerr=std_err,error_kw=error_params)
- plt.title(scale,fontsize=15)
- plt.ylabel('score')
- plt.grid(alpha=0, linestyle=':')
- plt.savefig(scale, dpi=300)
- #plt.show()
- def drawTogether():
- scales = ["mental-demand","physical-demand","temporal-demand","performance", "effort","frustration"]
- plt.figure(figsize=(15,7))
- x = np.arange(len(scales))
- total_width, n = 0.8, 4
- width = total_width / n
-
- for i in range(0,4):
- result = []
- for scale in scales:
- result.append(file.iloc[i][scale])
- plt.bar(x+width*(i-1),result,width=width,color=colors[i],label=file.iloc[i]["condition"],alpha=a)
- plt.legend()
- plt.title("TLX Average",fontsize=15)
- plt.xticks(x+width/2,scales)
- #plt.show()
-
- plt.savefig("summary.jpg",dpi=300)
- # Merge all the .csv file start with "HectorVR", and
- all_files = glob.glob(os.path.join(path, "HectorVR*.csv"))
- df_from_each_file = (pd.read_csv(f, sep=',') for f in all_files)
- df_merged = pd.concat(df_from_each_file, ignore_index=True)
- # Save the file to Merged.csv in the same folder
- df_merged.to_csv( "Merged.csv")
- # save the results in csv
- file = df_merged.groupby(["condition"]).mean()
- file.to_csv("Mean.csv")
- scales = ["mental-demand","physical-demand","temporal-demand","performance", "effort","frustration","total"]
- SD = "standard_deviation.csv"
- writeSDCSV(SD)
-
- file = pd.read_csv("Mean.csv")
- colors = sns.color_palette()
- a = 0.6
- for scale in scales:
- draw(scale)
- drawTogether()
|