|
| 1 | +#!/usr/bin/env Rscript |
| 2 | +library(ggplot2); |
| 3 | +library(plyr); |
| 4 | + |
| 5 | +# get __dirname and load ./_cli.R |
| 6 | +args = commandArgs(trailingOnly = F); |
| 7 | +dirname = dirname(sub("--file=", "", args[grep("--file", args)])); |
| 8 | +source(paste0(dirname, '/_cli.R'), chdir=T); |
| 9 | + |
| 10 | +if (is.null(args.options$xaxis) || is.null(args.options$category) || |
| 11 | + (!is.null(args.options$plot) && args.options$plot == TRUE)) { |
| 12 | + stop("usage: cat file.csv | Rscript scatter.R [variable=value ...] |
| 13 | + --xaxis variable variable name to use as xaxis (required) |
| 14 | + --category variable variable name to use as colored category (required) |
| 15 | + --plot filename save plot to filename |
| 16 | + --log use a log-2 scale for xaxis in the plot"); |
| 17 | +} |
| 18 | + |
| 19 | +plot.filename = args.options$plot; |
| 20 | + |
| 21 | +# parse options |
| 22 | +x.axis.name = args.options$xaxis; |
| 23 | +category.name = args.options$category; |
| 24 | +use.log2 = !is.null(args.options$log); |
| 25 | + |
| 26 | +# parse data |
| 27 | +dat = read.csv(file('stdin'), strip.white=TRUE); |
| 28 | +dat = data.frame(dat); |
| 29 | + |
| 30 | +# List of aggregated variables |
| 31 | +aggregate = names(dat); |
| 32 | +aggregate = aggregate[ |
| 33 | + ! aggregate %in% c('rate', 'time', 'filename', x.axis.name, category.name) |
| 34 | +]; |
| 35 | +# Variables that don't change aren't aggregated |
| 36 | +for (aggregate.key in aggregate) { |
| 37 | + if (length(unique(dat[[aggregate.key]])) == 1) { |
| 38 | + aggregate = aggregate[aggregate != aggregate.key]; |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +# Print out aggregated variables |
| 43 | +for (aggregate.variable in aggregate) { |
| 44 | + cat(sprintf('aggregating variable: %s\n', aggregate.variable)); |
| 45 | +} |
| 46 | +if (length(aggregate) > 0) { |
| 47 | + cat('\n'); |
| 48 | +} |
| 49 | + |
| 50 | +# Calculate statistics |
| 51 | +stats = ddply(dat, c(x.axis.name, category.name), function(subdat) { |
| 52 | + rate = subdat$rate; |
| 53 | + |
| 54 | + # calculate standard error of the mean |
| 55 | + se = sqrt(var(rate)/length(rate)); |
| 56 | + |
| 57 | + # calculate mean and 95 % confidence interval |
| 58 | + r = list( |
| 59 | + rate = mean(rate), |
| 60 | + confidence.interval = se * qt(0.975, length(rate) - 1) |
| 61 | + ); |
| 62 | + |
| 63 | + return(data.frame(r)); |
| 64 | +}); |
| 65 | + |
| 66 | +print(stats, row.names=F); |
| 67 | + |
| 68 | +if (!is.null(plot.filename)) { |
| 69 | + p = ggplot(stats, aes_string(x=x.axis.name, y='mean', colour=category.name)); |
| 70 | + if (use.log2) { |
| 71 | + p = p + scale_x_continuous(trans='log2'); |
| 72 | + } |
| 73 | + p = p + geom_errorbar(aes(ymin=mean-confidence.interval, ymax=mean+confidence.interval), width=.1); |
| 74 | + p = p + geom_point(); |
| 75 | + p = p + ylab("rate of operations (higher is better)"); |
| 76 | + p = p + ggtitle(dat[1, 1]); |
| 77 | + ggsave(plot.filename, p); |
| 78 | +} |
0 commit comments