-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot.r
398 lines (311 loc) · 17.3 KB
/
plot.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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
library(jsonlite)
library(RSQLite)
library(ggplot2)
library(tikzDevice)
library(scales)
load.json = function(file)
{
# load experiment results from JSON file created by ./eval2json
# note: jsonlite and/or R leaks memory here; on larger data sets using load.db() is strongly recommended
# get experiments
data = fromJSON(file)
# adjustments
data$results$solved = as.logical(data$results$solved)
data$graph = as.factor(data$graph)
data$algo = as.factor(data$algo)
# calculate stat totals
data$results$totals = data.frame(matrix(unlist(lapply(data$results$counters, function(v) {return(lapply(v, sum));})), nrow = nrow(data)))
colnames(data$results$totals) = colnames(data$results$counters)
# assemble synthetic graphs into groups
data$group = as.factor(gsub("\\.n[0-9]+\\.", ".", data$graph))
data
}
load.db = function(file)
{
# load experiment results from sqlite file created by ./json2db.py
# note: does not load individual (i.e. per k) stat counters, only their total
db = dbConnect(RSQLite::SQLite(), file)
# get experiments
data = data.frame(dbGetQuery(db, 'SELECT * FROM experiment ORDER BY id ASC;'))
# recreate structure of JSON output
data$results = data.frame(time = data$time, solved = as.logical(data$solved))
data$results$solved = as.logical(data$solved)
data$time = NULL
data$solved = NULL
# resolve foreign keys
# - algorithms
data$algo = as.factor(data$algo)
levels(data$algo) = dbGetQuery(db, 'SELECT name FROM algo ORDER BY id ASC;')$name
# - graphs
data$graph = as.factor(data$graph)
levels(data$graph) = dbGetQuery(db, 'SELECT name FROM graph ORDER BY id ASC;')$name
# assemble synthetic graphs into groups
data$group = as.factor(gsub("\\.n[0-9]+\\.", ".", data$graph))
# calculate stat totals
totals = dbGetQuery(db, 'SELECT experiment, stat, SUM(value) AS value FROM stats GROUP BY experiment, stat ORDER BY experiment ASC;')
totals$stat = as.factor(totals$stat)
levels(totals$stat) = dbGetQuery(db, 'SELECT name FROM stat_names ORDER BY id ASC;')$name
totals2 = reshape(totals, idvar = "experiment", timevar = "stat", direction = "wide")
totals2$experiment = NULL
colnames(totals2) = levels(totals$stat)
data$results$totals = totals2
dbDisconnect(db)
data
}
######################################################
# plot functions
#
# general behaviour common to all plot functions:
# - if the data contains more than one set of experiments, automatically produces a legend mentioning the variant inputs
# usually these are group, algorithm and threads
# otherwise no legend is created
plot.times = function(data)
{
# plots the running time of the experiments
data = droplevels(data)
# construct label, omit non-varying factors
l = vector()
if(nlevels(data$group) > 1) {l = c(l, "Graph"); data$label = data$group}
if(nlevels(data$algo) > 1) {l = c(l, "Algorithm"); if(is.null(data$label)) {data$label = data$algo} else {data$label = paste(data$label, data$algo, sep = " / ")}}
if(min(data$threads) != max(data$threads)) {l = c(l, "Threads"); if(is.null(data$label)) {data$label = data$threads} else {data$label = paste(data$label, data$threads, sep = " / ")}}
l = paste(l, collapse = " / ")
if(is.null(data$label)) {data$label = ""}
data$label = as.factor(data$label)
# no color needed if only plotting a single group
if(l == "") {p = ggplot(data, aes(x = k, group = interaction(group, algo, threads)))}
else {p = ggplot(data, aes(x = k, group = interaction(group, algo, threads), color = label))}
p = p + scale_y_log10(labels = comma)
p = p + labs(x = "k", y = "Time [s]", color = l)
# p = p + geom_boxplot(aes(y = results$time, group = interaction(group, algo, threads, k)), alpha = 0, outlier.alpha = 1)
p = p + geom_errorbar(aes(y = results$time, group = interaction(group, algo, threads, k)), stat = "boxplot", position = "dodge")
# p = p + stat_summary(aes(y = results$time, group = interaction(group, algo, threads)), fun.y = mean, geom = "line", position = position_dodge(0.75))
p = p + stat_summary(aes(y = results$time, group = interaction(group, algo, threads)), fun.y = mean, geom = "point", size = 0.75, position = position_dodge(0.75))
p
}
plot.calls = function(data)
{
# plots the number of recursive calls of the experiments
data = droplevels(data)
# construct label, omit non-varying factors
l = vector()
if(nlevels(data$group) > 1) {l = c(l, "Graph"); data$label = data$group}
if(nlevels(data$algo) > 1) {l = c(l, "Algorithm"); if(is.null(data$label)) {data$label = data$algo} else {data$label = paste(data$label, data$algo, sep = " / ")}}
# threads should not make a difference in the number of recursive calls
#if(min(data$threads) != max(data$threads)) {l = c(l, "Threads"); if(is.null(data$label)) {data$label = data$threads} else {data$label = paste(data$label, data$threads, sep = " / ")}}
l = paste(l, collapse = " / ")
if(is.null(data$label)) {data$label = ""}
data$label = as.factor(data$label)
# no color needed if only plotting a single group
if(l == "") {p = ggplot(data, aes(x = k, group = interaction(group, algo)))}
else {p = ggplot(data, aes(x = k, group = interaction(group, algo), color = label))}
p = p + scale_y_log10(labels = comma)
p = p + labs(x = "k", y = "Recursive calls", color = l)
# p = p + geom_boxplot(aes(y = results$totals$calls, group = interaction(group, algo, k)), alpha = 0, outlier.alpha = 1)
# p = p + geom_errorbar(aes(y = results$totals$calls, group = interaction(group, algo, k)), stat = "boxplot", position = "dodge")
p = p + stat_summary(aes(y = results$totals$calls, group = interaction(group, algo)), fun.y = mean, geom = "line")
p = p + stat_summary(aes(y = results$totals$calls, group = interaction(group, algo)), fun.y = mean, geom = "point", size = 0.75)
p
}
plot.mt.efficiency = function(data)
{
# plots the efficiency of the multithreaded approach
# filter for highest k in each group present in all thread counts
data = data[as.logical(ave(data$k, data$group, data$algo, data$threads, FUN = function(x) x == max(x))),]
data = data[as.logical(ave(data$k, data$group, data$algo, FUN = function(x) x == min(x))),]
data = droplevels(data)
# construct label, omit non-varying factors
l = vector()
if(nlevels(data$group) > 1) {l = c(l, "Graph"); data$label = data$group}
if(nlevels(data$algo) > 1) {l = c(l, "Algorithm"); if(length(as.logical(data$label)) == 0) {data$label = data$algo} else {data$label = paste(data$label, data$algo, sep = " / ")}}
# threads are the x axis in this plot
#if(min(data$threads) != max(data$threads)) {l = c(l, "Threads"); if(length(as.logical(data$label)) == 0) {data$label = data$threads} else {data$label = paste(data$label, data$threads, sep = " / ")}}
l = paste(l, collapse = " / ")
if(is.null(data$label)) {data$label = ""}
data$label = as.factor(data$label)
# in each group, set results$reftime to mean(results$time) with threads == 1
refgroups = subset(data, threads == 1)
reftimes = aggregate(refgroups$results$time, by = list(group = refgroups$group, algo = refgroups$algo), FUN = mean)
data = merge(data, reftimes)
data$results$reftime = data$x
data$x = NULL
# no color needed if only plotting a single group
if(l == "") {p = ggplot(data, aes(x = threads, group = interaction(group, algo)))}
else {p = ggplot(data, aes(x = threads, group = interaction(group, algo), color = label))}
p = p + labs(x = "Threads", y = "Efficiency", color = l)
# p = p + geom_boxplot(aes(y = results$reftime / (results$time * threads), group = interaction(group, algo, threads, k)), alpha = 0, outlier.alpha = 1)
p = p + geom_errorbar(aes(y = results$reftime / (results$time * threads), group = interaction(group, algo, threads, k)), stat = "boxplot", position = "dodge")
# p = p + stat_summary(aes(y = results$reftime / (results$time * threads), group = interaction(group, algo)), fun.y = mean, geom = "line", position = position_dodge(0.75))
p = p + stat_summary(aes(y = results$reftime / (results$time * threads), group = interaction(group, algo)), fun.y = mean, geom = "point", size = 0.75, position = position_dodge(0.75))
p
}
######################################################
# plot generators
#
# produces multiple plots in the directory `dir'
plot.all = function(data, dir)
{
# create individual plots for every experiment with each of the above plot functions
# dir is the directory to which to write the plots
# see plot.eval at the end of this file for examples on how to change ggsave's output format and size
data = droplevels(data)
t = theme_get()
# reduce fontsize of title
theme_update(plot.title = element_text(size = rel(.6)))
# mangle some names
# adjust as needed
# grass_web.metis -> grassweb
levels(data$group) = gsub("grass_web\\.metis", "grassweb", levels(data$group))
# graphs/foo.graph -> foo
levels(data$group) = gsub("^graphs/", "", levels(data$group))
levels(data$group) = gsub("\\.graph$", "", levels(data$group))
# make underscores latex-safe
# commented due to pdf output
# levels(data$group) = gsub("_", "\\\\_", levels(data$group))
# levels(data$algo) = gsub("_", "\\\\_", levels(data$algo))
# for each group
for(l in levels(data$group))
{
sub_group = subset(data, group == l)
file_group = gsub("/", "_", l)
# for each algorithm
for(a in levels(sub_group$algo))
{
sub_algo = subset(sub_group, algo == a)
file_algo = gsub("/", "_", a)
# no data for this combination, skip
if(nrow(sub_algo) == 0) {next}
# did any experiment solve the graph?
s = any(sub_algo$results$solved)
# progess indicator
print(paste(l, a, s))
ggsave(paste(file_group, file_algo, s, "times.pdf", sep = "."), plot = plot.times(sub_algo) + ggtitle(paste("Graph:", l, "Algorithm:", a, "Solved:", s)), device = pdf, path = dir)
ggsave(paste(file_group, file_algo, s, "calls.pdf", sep = "."), plot = plot.calls(sub_algo) + ggtitle(paste("Graph:", l, "Algorithm:", a, "Solved:", s)), device = pdf, path = dir)
ggsave(paste(file_group, file_algo, s, "mt.efficiency.pdf", sep = "."), plot = plot.mt.efficiency(sub_algo) + ggtitle(paste("Graph:", l, "Algorithm:", a, "Solved:", s)), device = pdf, path = dir)
# plot.times with repositioned legend (see figure 3.3.a)
ggsave(paste(file_group, file_algo, s, "mt.times.pdf", sep = "."), plot = plot.times(sub_algo) + theme(legend.position = c(0.11, 0.65), legend.background = element_rect(fill = "grey92", size = 1, linetype = "solid")) + ggtitle(paste("Graph:", l, "Algorithm:", a, "Solved:", s)), device = pdf, path = dir)
}
}
# cleanup -- restore theme
theme_set(t)
}
######################################################
# other functions
#
# - for summary table in a paper the result of summary.table(filter.best(data)) might be useful
filter.best = function(data)
{
# filter the best results of each group, i.e. highest k with shortest time
data = data[as.logical(ave(data$k, data$group, FUN = function(x) x == max(x))),]
data = data[as.logical(ave(data$results$time, data$group, FUN = function(x) x == min(x))),]
data
}
summary.table = function(data)
{
# reduce the data to only include the columns needed for a summary table
data.frame(group = data$group, k = data$k, solved = data$results$solved, time = data$results$time, threads = data$threads)
}
######################################################
# onwards to uncharted err... untested territories
plot.syn = function(data)
{
# plots for synthetic graphs
data = droplevels(data)
# get graph size and number of edits targeted from file name
data$n = as.factor(as.integer(gsub("^.*components\\.([0-9]+)\\..*$", "\\1", data$group)))
data$targetk = as.factor(1.25 * as.integer(gsub("^.*\\.([0-9]+)\\.graph$", "\\1", data$group)))
# construct label, omit non-varying factors
l = vector()
if(nlevels(data$group) > 1) {l = c(l, "Graph"); data$label = data$group}
if(nlevels(data$algo) > 1) {l = c(l, "Algorithm"); if(is.null(data$label)) {data$label = data$algo} else {data$label = paste(data$label, data$algo, sep = " / ")}}
if(min(data$threads) != max(data$threads)) {l = c(l, "Threads"); if(is.null(data$label)) {data$label = data$threads} else {data$label = paste(data$label, data$threads, sep = " / ")}}
l = paste(l, collapse = " / ")
if(is.null(data$label)) {data$label = ""}
data$label = as.factor(data$label)
# no color needed if only plotting a single group
if(l == "") {p = ggplot(data, aes(x = k, group = interaction(group, algo, threads, targetk), shape = n, color = targetk))}
else {p = ggplot(data, aes(x = k, group = interaction(group, algo, threads, targetk), shape = n, color = targetk))}
p = p + scale_y_log10(labels = comma)
p = p + labs(x = "k", y = "Time [s]", shape = "Vertices", color = "Edits targeted")
# first actual plot for a proper legend
p = p + geom_point(aes(y = results$time))
# then overdraw highlight for unsolved graphs
p = p + geom_point(aes(y = results$time), data = subset(data, !results$solved), color = "black", stroke = 2.5, show.legend = FALSE)
# and redraw the first plot on top of the highlight
p = p + geom_point(aes(y = results$time))
p
}
plot.totals = function(data, no.log.scale = F)
{
# function to plot summary counters
# i doubt this one is useful
# expect this one to need work
data = droplevels(data)
# construct label, omit non-varying factors
l = vector()
if(nlevels(data$group) > 1) {l = c(l, "Graph"); data$label = data$group}
if(nlevels(data$algo) > 1) {l = c(l, "Algorithm"); if(is.null(data$label)) {data$label = data$algo} else {data$label = paste(data$label, data$algo, sep = " / ")}}
if(min(data$threads) != max(data$threads)) {l = c(l, "Threads"); if(is.null(data$label)) {data$label = data$threads} else {data$label = paste(data$label, data$threads, sep = " / ")}}
l = c(l, "Stat"); if(is.null(data$label)) {data$label = ""} else {data$label = paste(data$label, "", sep = " / ")}
l = paste(l, collapse = " / ")
if(is.null(data$label)) {data$label = ""}
data$label = as.factor(data$label)
colors = list(calls = "black", pruned = "red", fallbacks = "green", single = "blue", skipped = "yellow")
# no color needed if only plotting a single group
if(l == "") {p = ggplot(data, aes(x = k, group = interaction(group, algo, threads, k)))}
else {p = ggplot(data, aes(x = k, group = interaction(group, algo, threads, k), color = label))}
if(!no.log.scale) {p = p + scale_y_log10(labels = comma)}
p = p + labs(x = "k", y = "Count", color = l)
for(i in rev(names(data$results$totals)))
{
p = p + geom_boxplot(aes_string(y = paste0("results$totals$", i), color = "sub('$', i, label)"), alpha = 0, outlier.alpha = 1)
}
p
}
plot.eval = function(data, dir)
{
# example function to create multiple plots and write them to disk
# dir is the directory to which the plots shall be placed
# adjust as needed
data = droplevels(data)
t = theme_get()
# remove legend, instead describe it in the figure caption
theme_update(legend.position="none")
# mangle some names
# adjust as needed
# grass_web.metis -> grassweb
levels(data$group) = gsub("grass_web\\.metis", "grassweb", levels(data$group))
# graphs/foo.graph -> foo
levels(data$group) = gsub("^graphs/", "", levels(data$group))
levels(data$group) = gsub("\\.graph$", "", levels(data$group))
# make underscores latex-safe
levels(data$group) = gsub("_", "\\\\_", levels(data$group))
levels(data$algo) = gsub("_", "\\\\_", levels(data$algo))
# for each group of graphs
for(l in levels(data$group))
{
# progress indicator
print(l)
# .tex output
# use via \input{file}
# subset
sub = subset(data, group == l & algo %in% c("Basic-Center\\_Matrix-Matrix", "Redundant-Center\\_Matrix-Matrix", "LB\\_Basic-Center\\_Matrix-Matrix", "LB\\_Redundant-Center\\_Matrix-Matrix"))
# and create figures
# any "/" in the group name are replaced (no accidental directory creation fails)
# width and height are inces by default, 8 and 4 inches wide appear to be good values for spanning a full resp. half a page's width
ggsave(paste(gsub("/", "_", l), "basic.times.tex", sep = "."), plot = plot.times(sub), device = tikz, path = dir, width = 8, height = 2)
ggsave(paste(gsub("/", "_", l), "basic.calls.tex", sep = "."), plot = plot.calls(sub), device = tikz, path = dir, width = 8, height = 2)
# .pdf output
sub = subset(data, group == l & algo %in% c("Basic-Center\\_Matrix-Matrix", "Redundant-Center\\_Matrix-Matrix"))
ggsave(paste(gsub("/", "_", l), "basic.nolb.times.pdf", sep = "."), plot = plot.times(sub), device = pdf, path = dir, width = 4, height = 3)
ggsave(paste(gsub("/", "_", l), "basic.nolb.calls.pdf", sep = "."), plot = plot.calls(sub), device = pdf, path = dir, width = 4, height = 3)
# some more plots...
sub = subset(data, group == l & algo %in% c("LB\\_Basic-Center\\_Matrix-Matrix", "LB\\_Redundant-Center\\_Matrix-Matrix"))
ggsave(paste(gsub("/", "_", l), "basic.lb.times.tex", sep = "."), plot = plot.times(sub), device = tikz, path = dir, width = 4, height = 3)
ggsave(paste(gsub("/", "_", l), "basic.lb.calls.tex", sep = "."), plot = plot.calls(sub), device = tikz, path = dir, width = 4, height = 3)
sub = subset(data, group == l & algo %in% c("LB\\_Redundant-Center\\_Matrix-Matrix", "Redundant-H\\_Triangle\\_LB\\_Center\\_Matrix2-Matrix", "Redundant-LB\\_Center\\_Matrix-Matrix", "S\\_Redundant-S\\_LB\\_Center\\_Matrix-Matrix"))
ggsave(paste(gsub("/", "_", l), "subgraph.times.tex", sep = "."), plot = plot.times(sub), device = tikz, path = dir, width = 4, height = 3)
ggsave(paste(gsub("/", "_", l), "subgraph.calls.tex", sep = "."), plot = plot.calls(sub), device = tikz, path = dir, width = 4, height = 3)
}
# cleanup -- restore theme
theme_set(t)
}