-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbox_plots.py
79 lines (61 loc) · 2.95 KB
/
box_plots.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import matplotlib.pyplot as plt
import os
import seaborn as sns
import numpy as np
import pandas as pd
from ast import literal_eval
import scipy.stats as stats
# group of enemies trained on
ENEMIES_TRAINING = [2, 5, 8]
EA_DIRS = ["ea1", "ea2"]
RUNS_DIR = "runs"
FILE_NAME = "games_played.csv"
PLOTS_DIR = "plots"
PLOT_RESULT_NAME = "box_plot_enemy_"
def enemies_dir_name(enemies):
"""
Converts a list of enemies into a string
"""
dir_name = ""
for enemy in enemies:
dir_name += str(enemy) + "_"
return dir_name[:-1]
def statistics_across_generations(data):
"""
Aggregate statistics across a given list of dataframes and compute the mean and the standard deviation of the
'avg_fitness' and 'max_fitness'. Return a dataframe.
"""
aggregated_data = pd.concat(data)
df_avg = aggregated_data.groupby(aggregated_data.index).agg(mean_avg_fitness=('avg_fitness', 'mean'), std_avg_fitness=('avg_fitness', 'std'))
df_max = aggregated_data.groupby(aggregated_data.index).agg(mean_max_fitness=('max_fitness', 'mean'), std_max_fitness=('max_fitness', 'std'))
return pd.concat([df_avg, df_max], axis=1)
def main():
# read the csv file containing the results of played games
ea1_file_path = os.path.join(EA_DIRS[0], RUNS_DIR, "enemy_" + enemies_dir_name(ENEMIES_TRAINING), FILE_NAME)
ea2_file_path = os.path.join(EA_DIRS[1], RUNS_DIR, "enemy_" + enemies_dir_name(ENEMIES_TRAINING), FILE_NAME)
ea1_df_games = pd.read_csv(ea1_file_path, sep=";")
ea2_df_games = pd.read_csv(ea2_file_path, sep=";")
# convert the element inside the column 'individual_gains' to an array
ea1_df_games['individual_gains'] = ea1_df_games['individual_gains'].apply(literal_eval)
ea2_df_games['individual_gains'] = ea2_df_games['individual_gains'].apply(literal_eval)
# compute the mean of the individual_gains for each run
ea1_df_games['individual_gains'] = ea1_df_games['individual_gains'].map(lambda x: np.array(x).mean())
ea2_df_games['individual_gains'] = ea2_df_games['individual_gains'].map(lambda x: np.array(x).mean())
df = pd.concat([ea1_df_games['individual_gains'], ea2_df_games['individual_gains']]
, axis=1, keys=['Approach1', 'Approach2'])
# compute statistic test
t_statistic = stats.ttest_ind(df['Approach1'], df['Approach2'])
# show box plot
sns.set(rc={'figure.figsize': (5,5)})
palette = {"Approach1": "blue", "Approach2": "red"}
ax = sns.boxplot(data = df, palette=palette)
ax.set(ylabel='Individual gain'
, title=f'Enemies {ENEMIES_TRAINING} (T-statistic = {t_statistic.statistic:.2f}, p-value = {t_statistic.pvalue:.2f})')
plt.show()
# save plot
plot_file_name = os.path.join(PLOTS_DIR, PLOT_RESULT_NAME + enemies_dir_name(ENEMIES_TRAINING))
os.makedirs(PLOTS_DIR, exist_ok=True)
ax.get_figure().savefig(plot_file_name, bbox_inches='tight')
print(f"Plot saved in {plot_file_name}")
if __name__ == "__main__":
main()