-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot.jl
295 lines (250 loc) · 10.8 KB
/
plot.jl
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
using Gadfly
using Colors
using DataFrames
using Query
Gadfly.push_theme(Theme(major_label_font="Droid Sans",
minor_label_font="Droid Sans",
major_label_font_size=18pt, minor_label_font_size=16pt,
line_width=0.8mm, key_label_font="Droid Sans",
lowlight_color=c->RGBA{Float32}(c.r, c.g, c.b, 0.2),
key_label_font_size=14pt, key_position=:right))
colors = [colorant"#1f78b4", colorant"#33a02c", colorant"#e31a1c",
colorant"#ff7f00", colorant"#6a3d9a", colorant"#a6cee3",
colorant"#b2df8a", colorant"#fb9a99", colorant"#fdbf6f",
colorant"#cab2d6"]
function std_or_z(x)
s = std(x)
if isnan(s)
s = 0.0
end
s
end
function get_res_slgym(filename::String)
names = [:ltype, :seed, :timestamp, :Problem, :Generation, :Evaluations,
:Steps, :Reward]
res = readtable(filename, separator=',', header=false, names=names)
res[:Reward][(res[:Problem] .== "Acrobot-v1").&(res[:Steps] .== 0)] .-= 500
res
end
function get_gens(res::DataFrame)
maxs = by(res, [:Problem, :Steps, :Generation, :seed],
df->DataFrame(Reward=maximum(df[:Reward])))
gens = by(maxs, [:Problem, :Steps, :Generation],
df->DataFrame(Rmean=mean(df[:Reward]), Rstd=std_or_z(df[:Reward])))
gens[:Rlow] = gens[:Rmean] .- 0.5*gens[:Rstd]
gens[:Rhigh] = gens[:Rmean] .+ 0.5*gens[:Rstd]
gens
end
function plot_gym_gens(gens::DataFrame, problem::String="Acrobot-v1",
outfile::String="acrobot.pdf";
ylabel="Reward", xlabel="Generation",
key_position=:right)
pgens = @from i in gens begin
@where i.Problem == problem
@select i; @collect DataFrame
end
xmin=minimum(pgens[:Generation])
xmax=maximum(pgens[:Generation])
ymin=minimum(pgens[:Rlow])
ymax=maximum(pgens[:Rhigh])
plt = plot(pgens, x=:Generation, y=:Rmean, ymin=:Rlow, ymax=:Rhigh,
color=:Steps, Geom.line, Geom.ribbon,
Scale.color_discrete_manual(colors...),
Guide.ylabel(ylabel), Guide.xlabel(xlabel),
Coord.cartesian(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax),
Guide.title(problem))
draw(PDF(outfile, 8inch, 6inch), plt);
plt
end
function get_sizes(filename::String)
names = [:id, :epochs, :gen, :size]
res = readtable(filename, separator=',', header=false, names=names)
stats = by(res, [:epochs, :gen], df->DataFrame(
msize=mean(df[:size]), ssize=std_or_z(df[:size])))
stats[:low_size] = stats[:msize] - 0.5 * stats[:ssize]
stats[:high_size] = stats[:msize] + 0.5 * stats[:ssize]
res, stats
end
function plot_sizes(stats::DataFrame, title::String="Boston Housing - GRN sizes",
logfile::String="boston_sizes.pdf";
xmin=minimum(stats[:gen]),
xmax=maximum(stats[:gen]),
ymin=minimum(stats[:low_size]),
ymax=maximum(stats[:high_size]),
ylabel="N",
xlabel="Generation",
key_position=:right)
plt = plot(stats, x=:gen, y=:msize, ymin=:low_size, ymax=:high_size,
color=:epochs, Geom.line, Geom.ribbon,
Scale.color_discrete_manual(colors...),
Guide.ylabel(ylabel), Guide.xlabel(xlabel),
Coord.cartesian(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax),
Guide.title(title))
draw(PDF(logfile, 8inch, 6inch), plt);
plt
end
function get_error_res(filename::String, maxgen=0)
# names = [:m, :time, :gen, :error, :accuracy, :mean, :std, :test]
names = [:id, :epochs, :time, :gen, :fit, :test]
error_res = readtable(filename, separator=',', header=false, names=names)
# error_res[:fit] = log10.(error_res[:fit])
error_res[:epochs] = string.(error_res[:epochs])
bests = by(error_res, [:id, :epochs, :gen], df->DataFrame(
error=minimum(df[:fit]), test_fit=minimum(df[:test])))
error_stats = by(bests, [:epochs, :gen], df->DataFrame(
merror=mean(df[:error]), serror=std_or_z(df[:error])))
error_stats[:low_error] = error_stats[:merror] - 0.5 * error_stats[:serror]
error_stats[:high_error] = error_stats[:merror] + 0.5 * error_stats[:serror]
error_res, error_stats
end
function get_serror_res(filename::String, maxgen=0)
# names = [:m, :time, :gen, :error, :accuracy, :mean, :std, :test]
names = [:id, :epochs, :time, :gen, :epoch, :fit, :test]
error_res = readtable(filename, separator=',', header=false, names=names)
error_res[:epochs] = string.(error_res[:epochs])
# error_res[:fit] = log10.(error_res[:fit])
bests = by(error_res, [:id, :epochs, :gen], df->DataFrame(
error=minimum(df[:fit]), test_fit=minimum(df[:test])))
error_stats = by(bests, [:epochs, :gen], df->DataFrame(
merror=mean(df[:error]), serror=std_or_z(df[:error])))
error_stats[:low_error] = error_stats[:merror] - 0.5 * error_stats[:serror]
error_stats[:high_error] = error_stats[:merror] + 0.5 * error_stats[:serror]
error_res, error_stats
end
function get_learn_res(filename::String)
all_res = readtable(filename, separator=',', header=false,
names = [:type, :id, :evo_epoch, :tgen, :time, :gen,
:epoch, :loss, :test])
not_grns = @from i in all_res begin
@where i.type != "grn"
@select i
@collect DataFrame
end;
l_grns = @from i in all_res begin
@where (i.type == "grn" && i.evo_epoch == "10" && i.tgen == 50)
@select i
@collect DataFrame
end;
res = vcat(not_grns, l_grns)
stats = by(res, [:type, :evo_epoch, :tgen, :epoch], df->DataFrame(
mloss=mean(df[:loss]), sloss=std(df[:loss]),
mtest=mean(df[:test]), stest=std(df[:test])))
stats[:low_loss] = stats[:mloss] - 0.5 * stats[:sloss]
stats[:high_loss] = stats[:mloss] + 0.5 * stats[:sloss]
stats[:low_test] = stats[:mtest] - 0.5 * stats[:stest]
stats[:high_test] = stats[:mtest] + 0.5 * stats[:stest]
res, stats
end
function get_final(filename::String)
names = [:type, :id, :epoch, :tgen, :time, :gen, :mse, :test]
final = readtable(filename, separator=',', header=false, names=names)
final[:epoch] = string.(final[:epoch])
final[:tgen][final[:tgen] .== 49] = 50
final
end
function plot_final(final::DataFrame, title::String="Boston Housing - best GRNs",
filename::String="boston_final.pdf")
grns = @from i in final begin
@where i.type == "grn"
@select i
@collect DataFrame
end;
grns[:Generation] = string.(grns[:tgen])
grns[:E] = "Train"
grns[:yax] = grns[:mse]
cgrns = deepcopy(grns)
cgrns[:yax] = cgrns[:test]
cgrns[:E] = "Test"
full = vcat(grns, cgrns)
plt = plot(full, xgroup=:epoch, ygroup=:E, x=:Generation, y=:yax, color=:epoch,
Scale.y_log10,
Guide.ylabel("Mean Squared Error"),
Guide.xlabel("Generation by number of epochs during evolution"),
Guide.title(title),
Guide.xlabel(nothing),
style(key_position=:none),
Geom.subplot_grid(Geom.boxplot),
Scale.color_discrete_manual(colors...));
draw(PDF(filename, 18inch, 8inch), plt);
plt
end
function plot_comparison(final::DataFrame, title::String="Boston Housing",
filename::String="boston_comparison.pdf", tgen=50)
not_grns = @from i in final begin
@where i.type != "grn"
@select i
@collect DataFrame
end;
l_grns = @from i in final begin
@where (i.type == "grn" && i.epoch == "10" && i.tgen == tgen)
@select i
@collect DataFrame
end;
f = vcat(not_grns, l_grns);
f[:const] = ""
plt = plot(f, xgroup=:type, x=:const, y=:test,
Scale.y_log10,
Guide.xlabel("Model"),
Guide.ylabel("Mean Squared Error"),
# Geom.boxplot,
# Scale.x_discrete(order=[4, 1, 2, 3]),
Geom.subplot_grid(Geom.boxplot),
Guide.title(title),
style(key_position=:none, default_color=colorant"#000000"),
Scale.color_discrete_manual(colors...));
draw(PDF(filename, 8inch, 6inch), plt);
plt
end
function plot_error(error_stats::DataFrame, title::String="Air Quality Evolution",
logfile::String="error.pdf";
xmin=minimum(error_stats[:gen]),
xmax=maximum(error_stats[:gen]),
ymin=log10(minimum(error_stats[:low_error])),
ymax=log10(maximum(error_stats[:high_error])),
ylabel="Mean Squared Error",
xlabel="Generation",
key_position=:right)
zerr = @from i in error_stats begin
@where ((i.epochs == "0") && (i.gen == 0))
@select [i.merror, i.low_error, i.high_error]
@collect
end
error_stats[:merror][error_stats[:gen] .== 0] = zerr[1][1].value
error_stats[:low_error][error_stats[:gen] .== 0] = zerr[1][2].value
error_stats[:high_error][error_stats[:gen] .== 0] = zerr[1][3].value
plt = plot(error_stats, x=:gen, y=:merror, ymin=:low_error, ymax=:high_error,
color=:epochs, Geom.line, Geom.ribbon,
Scale.color_discrete_manual(colors...),
Scale.y_log10,
Guide.ylabel(ylabel), Guide.xlabel(xlabel),
Coord.cartesian(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax),
Guide.title(title))
draw(PDF(logfile, 8inch, 6inch), plt);
plt
end
function plot_learn(learn_stats::DataFrame, title::String="Energy Training",
logfile::String="learn.pdf")
plt = plot(learn_stats, x=:epoch, y=:mloss, ymin=:low_loss, ymax=:high_loss,
color=:type, Geom.line, Geom.ribbon,
Scale.color_discrete_manual(colors...),
Guide.ylabel("Loss"),
Guide.title(title))
draw(PDF(logfile, 8inch, 6inch), plt);
plt
end
function plot_learn_distributions(learn_res::DataFrame,
title::String="Boston learning distributions",
logfile::String="boston_learn_dist.pdf")
downsampled = @from i in res begin
@where ((mod(i.epoch, 10) == 1) && (i.epoch <= 51))
@select i
@collect DataFrame
end;
plt = plot(downsampled, xgroup=:epoch, x=:type, y=:loss, color=:type,
Scale.y_log10,
Guide.ylabel("Loss [log10]"),
Geom.subplot_grid(Geom.boxplot), Guide.xlabel("Epoch"),
Guide.title(title), Scale.color_discrete_manual(colors...));
draw(PDF(logfile, 12inch, 6inch), plt);
plt
end