-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot.py
147 lines (111 loc) · 4.66 KB
/
plot.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import matplotlib.pyplot as plt
import seaborn as sns
import os
from scripts.script_utils import metric_labels, metric_labels_avg, metric_labels_max
cm = 1 / 2.54
FIG_SIZE = (8.48 * cm, 6 * cm)
ORDER = ["no-sharing", "dynamic-Boyd", "fully-connected", "small-world", "ring"]
def plot_intergroup_alignment(alignment, save_dir):
""" Plot inter-group alignment
alignment: Dataframe
contains infromation collected during the evaluation of the project
save_dir: str
directory in which to save the alignment plot
"""
fig, ax = plt.subplots(1, 1, figsize=FIG_SIZE)
sns.lineplot(x="train_step",
y="diff",
data=alignment,
palette="nipy_spectral",
ci="sd",
hue="pair")
ax.set(xlabel=f"Training step, $t$", ylabel="Inter-group alignment, \n $A^{\mathcal{G}_j, \mathcal{G}_j}_t$")
save_path = save_dir + "/plots"
if not os.path.exists(save_path):
os.path.makedirs(save_path)
ax.legend(loc="lower left")
ax.legend().set_title("")
plt.savefig(save_path + "/inter_total.pdf")
plt.savefig(save_path + "/inter_total.png")
plt.clf()
def plot_project(eval_info, volatilities, conformities, measure_mnemonic, project, max_step=1e10):
""" Produce all plots related to a project.
eval_info: Dataframe
contains infromation collected during the evaluation of the project
volatilities: Dataframe
contains infromation about volatility
conformities: Dataframe
contains infromation about conformity
measure_mnemonic: bool
indicates whether to plot mnemonic metrics
project: str
project directory
"""
plot_metric_with_time(eval_info, "norm_reward", project)
plot_metric_with_time(volatilities, "volatility", project)
plot_metric_with_time(conformities, "conformity", project)
if measure_mnemonic:
plot_metric_with_time(eval_info, "diversity", project)
plot_metric_with_time(eval_info, "group_diversity", project)
plot_metric_with_time(eval_info, "intragroup_alignment", project)
def plot_metric_with_time(data, metric, project):
""" Plot a specific metric with training time.
data: Dataframe
contains evaluation information
metric: str
name of metric
project: str
project directory
"""
# ----- plot average and maximum performance across population -----
if "group" in metric:
modes = ["avg"]
else:
modes = ["avg", "max"]
for mode in modes:
fig, ax = plt.subplots(1, 1, figsize=FIG_SIZE)
all_labels = []
for label, performance in data.items():
# preprocess data so that the confidence intervals are across trials
if "trial" not in performance:
print("check")
trials = list(set(performance["trial"]))
total = []
for trial_idx, trial in enumerate(trials):
trial_perf = performance.loc[performance["trial"] == trial]
if metric not in trial_perf:
print("check")
if mode == "avg":
average_perf = trial_perf.groupby('train_step', as_index=False)[metric].mean()
else:
average_perf = trial_perf.groupby('train_step', as_index=False)[metric].max()
average_perf["trial"] = trial
if len(total):
total = total.append(average_perf, ignore_index=True)
else:
total = average_perf
all_labels.append(total)
labels = list(data.keys())
for idx, el in enumerate(all_labels):
sns.lineplot(x="train_step",
y=metric,
data=all_labels[idx],
palette="nipy_spectral",
ci="sd",
label=labels[idx],
hue_order=ORDER)
plt.ticklabel_format(style='sci', axis='x', scilimits=(0, 0))
metric_label = metric_labels[metric]
if "group" not in metric and mode == "avg":
metric_label = metric_labels_avg[metric]
elif mode == "max":
metric_label = metric_labels_max[metric]
ax.set(xlabel=f"Training step, $t$", ylabel=metric_label)
ax.legend(loc="best", ncol=int(len(all_labels)/3), prop={'size': 7})
fig.tight_layout()
save_dir = project + "/plots/"
if not os.path.exists(save_dir):
os.makedirs(save_dir)
plt.savefig(save_dir + mode + "_" + metric + ".pdf")
plt.savefig(save_dir + mode + "_" + metric + ".png")
plt.clf()