-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplotting.R
245 lines (188 loc) · 8.71 KB
/
plotting.R
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
library(data.table)
source("R_utils/plotting_functions.R")
#### DATA ####
timestep_data_task1 = fread("processed_data/timestep_data_task1.csv")
timestep_data_task4 = fread("processed_data/timestep_data_task4.csv")
# Combined data
averaged_data = fread("processed_data/full_average_data.csv")
time_series_data = fread("processed_data/time_series_surprisal_Phi_combined.csv")
# Goal prior
goal_prior_task4 = fread("processed_data/goal_prior_distribution_task4.csv", colClasses = c('sensory_state'='character'))
# Fitness data
fitness_task1 = fread("processed_data/fitness_task1.csv")
fitness_task4 = fread("processed_data/fitness_task4.csv")
#### AVERAGE LOD PLOTS ####
# convert task names
averaged_data[.(task = c("Task 1", "Task 4", "Task 4 - Perfect"),
to = c("Easy task", "Hard task", "Hard task - Perfect")),
on = "task", task := i.to]
# Average LOD plot
ggsave(
"plots/plots_for_publication/fig3.tiff",
ggpubr::ggarrange(
make_LOD_plot(averaged_data, "fitness", y_label = "Average fitness", seperator = "task"),
make_LOD_plot(averaged_data, "Phi", y_label = "Average Phi", seperator = "task"),
make_LOD_plot(averaged_data, "surprisal", y_label = "Average surprisal", seperator = "task"),
labels = "AUTO",
ncol = 3, common.legend = T
), width = 7.5, height = 3, device='tiff', dpi=300
)
#Constructing correlation profile data
neg_runs = time_series_data[task == "Task 4" & lag == 0, mean(cor), by = run][V1<(-0.1), run]
pos_runs = time_series_data[task == "Task 4" & lag == 0, mean(cor), by = run][V1>0.1, run]
neu_runs = time_series_data[task == "Task 4" & lag == 0, mean(cor), by = run][V1<0.1 & V1>(-.1), run]
averaged_data_cor_profile = timestep_data_task4[,.(Phi_mean = mean(Phi), surprisal_mean = mean(surprisal)), by = .(run,agent)][,lapply(.SD, smooth), by = run]
averaged_data_cor_profile$profile = NA
averaged_data_cor_profile$profile[averaged_data_cor_profile$run %in% neg_runs] = "Negative"
averaged_data_cor_profile$profile[averaged_data_cor_profile$run %in% pos_runs] = "Positive"
averaged_data_cor_profile$profile[averaged_data_cor_profile$run %in% neu_runs] = "Neutral"
averaged_data_cor_profile = averaged_data_cor_profile[complete.cases(averaged_data_cor_profile)]
averaged_data_cor_profile = merge(averaged_data_cor_profile, fitness_task4, by = c("run", "agent"))[
,.(Phi = mean(Phi_mean),
Phi_se = sd(Phi_mean)/sqrt(.N),
fitness = mean(fitness),
fitness_se = sd(fitness)/sqrt(.N),
surprisal = mean(surprisal_mean),
surprisal_se = sd(surprisal_mean)/sqrt(.N)
), by = .(agent, profile)
]
# Average LOD plot (hard task split by correlation profiles)
ggsave(
"plots/plots_for_publication/fig7.tiff",
ggpubr::ggarrange(
make_LOD_plot(averaged_data_cor_profile, "fitness", y_label = "Average fitness", seperator = "profile"),
make_LOD_plot(averaged_data_cor_profile, "Phi", y_label = "Average Phi", seperator = "profile"),
make_LOD_plot(averaged_data_cor_profile, "surprisal", y_label = "Average surprisal", seperator = "profile"),
labels = "AUTO",
ncol = 3, common.legend = T
), width = 7.5, height = 3, device='tiff', dpi=300
)
# MEDIAN
median_data1 = timestep_data_task1[,.(Phi = median(Phi),surprisal = median(surprisal)), by = agent]
median_data1$task = "Easy task"
median_data4 = timestep_data_task4[,.(Phi = median(Phi),surprisal = median(surprisal)), by = agent]
median_data4$task = "Hard task"
perfect_runs = unique(fitness_task4[fitness == 1 & agent == 120,run])
median_data4_perfect = timestep_data_task4[run %in% perfect_runs,.(Phi = median(Phi),surprisal = median(surprisal)), by = agent]
median_data4_perfect$task = "Hard task - Perfect"
median_data = rbind(median_data1,median_data4,median_data4_perfect)
median_data$Phi_se = 0
median_data$surprisal_se = 0
ggsave(
"plots/average_LOD_plot_median.jpg",
ggpubr::ggarrange(
make_LOD_plot(median_data, "Phi", y_label = "Median Phi", seperator = "task"),
make_LOD_plot(median_data, "surprisal", y_label = "Median surprisal", seperator = "task"),
labels = "AUTO",
ncol = 2, common.legend = T
), width = 7.5, height = 3
)
# Average LOD plot different Phi aggregates
ggsave(
"plots/average_LOD_plot_Phi_dif.jpg",
ggpubr::ggarrange(
make_LOD_plot(averaged_data, "Phi", y_label = "Average Phi", seperator = "task"),
make_LOD_plot(averaged_data, "Phi_median", y_label = "Average median of Phi", seperator = "task"),
make_LOD_plot(averaged_data, "Phi_binary", y_label = "Average % of non-zero Phi", seperator = "task"),
#labels = "AUTO",
ncol = 3, common.legend = T
), width = 7.5, height = 3
)
#### TIMESTEP PLOTS ####
# Example selection plot
ggsave("plots/plots_for_publication/fig5.tiff",
make_timestep_multi_plot(data = timestep_data_task4,
fitness_data = fitness_task4,
time_series_data = time_series_data,
trial_list = list(
c(run = 26, agent = 120, trial = 55),
c(run = 6, agent = 120, trial = 55),
c(run = 3, agent = 120, trial = 103),
c(run = 14, agent = 120, trial = 103),
c(run = 36, agent = 120, trial = 87),
c(run = 1, agent = 120, trial = 23)
)
), width = 7, height = 8, device='tiff', dpi=300, units="in"
)
#### TIME SERIES PLOTS ####
# Convert task names
time_series_data[.(task = c("Task 1", "Task 4"),
to = c("Easy task", "Hard task")),
on = "task", task := i.to]
ggsave(
"plots/time_series_density_-16_16.jpg",
make_time_series_plot(time_series_data, range = -16:16, seperator = "task"),
width = 7.5, height = 6
)
ggsave(
"plots/time_series_density_-6_5.jpg",
make_time_series_plot(time_series_data, range = -6:5, seperator = "task"),
width = 7.5, height = 6
)
# Add "Hard task - Perfect"
perfects = fitness_task4[agent==120 & fitness==1, run]
time_series_data_perfect_task4 = time_series_data[run %in% perfects & task == "Hard task"]
time_series_data_perfect_task4$task = "Hard task - Perfect"
time_series_data_perfect_task4 = rbind(time_series_data, time_series_data_perfect_task4)
ggsave(
"plots/plots_for_publication/fig6.tiff",
make_time_series_plot(time_series_data_perfect_task4, range = -6:5, seperator = "task"),
width = 7.5, height = 7, device='tiff', dpi=300, units="in"
)
#### GOAL PRIOR PLOTS ####
ggsave(
"plots/plots_for_publication/fig10.tiff",
make_goal_prior_plot(goal_prior_task4),
width = 7.5, height = 8.75, device='tiff', dpi=300, units="in"
)
#### AVERAGE TRIAL PLOTS ####
# Reload data to avoid code break if data have been changed.
time_series_data = fread("processed_data/time_series_surprisal_Phi_combined.csv")
# Construct plots
average_trial_plot_list = make_average_trial_plot(timestep_data_task1, timestep_data_task4, fitness_task1, fitness_task4, time_series_data)
# Save plots
ggsave(
"plots/plots_for_publication/fig4.tiff",
average_trial_plot_list[[1]],
width = 7.5, height = 3.75, device='tiff', dpi=300, units="in"
)
ggsave(
"plots/plots_for_publication/fig8.tiff",
average_trial_plot_list[[2]],
width = 7.5, height = 3.75, device='tiff', dpi=300, units="in"
)
# Plot that shows all runs across different groupings
ggsave(
"plots/average_trial_all_runs_plot.jpg",
average_trial_all_runs_plot(timestep_data_task1, timestep_data_task4, time_series_data, fitness_task4),
width = 8, height = 8
)
# Plots showing all LODs
average_trial_plot_LOD = make_average_trial_plot_LOD(timestep_data_task4, fitness_task4, time_series_data)
ggsave(
"plots/average_trial_all_runs_plot_profiles_Phi.jpg",
average_trial_plot_LOD[[1]],
width = 8, height = 12
)
ggsave(
"plots/average_trial_all_runs_plot_profiles_surprisal.jpg",
average_trial_plot_LOD[[2]],
width = 8, height = 12
)
# MEDIAN plots
average_trial_plot_median_list = make_average_trial_plot(timestep_data_task1, timestep_data_task4, fitness_task1, fitness_task4, time_series_data, T)
ggsave(
"plots/average_trial_plot_median.jpg",
average_trial_plot_median_list[[1]],
width = 10, height = 5
)
ggsave(
"plots/average_trial_plot_profile_split_median.jpg",
average_trial_plot_median_list[[2]],
width = 10, height = 5
)
#### DISTRIBUTION PLOT ####
ggsave("plots/plots_for_publication/fig2.tiff",
distribution_plot(timestep_data_task1, timestep_data_task4, fitness_task4),
width = 6, height = 4, device='tiff', dpi=300, units="in"
)