[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[or-cvs] [ernie/master] Reduce graph generation time from 5.2 to 3.4 seconds.
Author: Karsten Loesing <karsten.loesing@xxxxxxx>
Date: Mon, 7 Jun 2010 18:13:48 +0200
Subject: Reduce graph generation time from 5.2 to 3.4 seconds.
Commit: 594228a0b2bb76f9f61736abe462b1eb7c86a618
ggsave() is known to be slow. Use the traditional png() and dev.off() to
store a graph to disk. This doesn't work inside functions, so get rid of
functions.
---
R/consensus.R | 59 +++++++++++++++++++-------------------------------------
1 files changed, 20 insertions(+), 39 deletions(-)
diff --git a/R/consensus.R b/R/consensus.R
index e07ffb3..6b57dc8 100755
--- a/R/consensus.R
+++ b/R/consensus.R
@@ -2,47 +2,28 @@ options(warn = -1)
suppressPackageStartupMessages(library("ggplot2"))
args <- commandArgs()
-
days <- args[4]
fname <- args[5]
-consensuses <- read.csv("/tmp/consensus-stats", header = TRUE,
+c <- read.csv("/tmp/consensus-stats", header = TRUE,
stringsAsFactors = FALSE);
-consensuses <- consensuses[1:length(consensuses$date)-1,]
-
-plot_consensus <- function(directory, filename, title, limits, rows, breaks,
- labels) {
- c <- melt(consensuses[rows], id = "date")
- ggplot(c, aes(x = as.Date(date, "%Y-%m-%d"), y = value,
- colour = variable)) + geom_line() +
- scale_x_date(name = "", limits = limits) +
- scale_y_continuous(name = "",
- limits = c(0, max(c$value, na.rm = TRUE))) +
- scale_colour_hue("", breaks = breaks, labels = labels) +
- opts(title = title)
- ggsave(filename = filename,
- width = 8, height = 5, dpi = 72)
-}
-
-plot_pastdays <- function(directory, filenamePart, titlePart, days, rows,
- breaks, labels) {
- for (day in days) {
- end <- Sys.Date()
- start <- seq(from = end, length = 2, by = paste("-", day, " days",
- sep = ""))[2]
- plot_consensus(directory, filenamePart,
- paste(titlePart, "(past", day, "days)\n"), c(start, end),
- rows, breaks, labels)
- }
-}
-
-plot_current <- function(directory, filenamePart, titlePart, rows, breaks,
- labels) {
- plot_pastdays(directory, filenamePart, titlePart, as.numeric(days), rows,
- breaks, labels)
-}
-
-plot_current("", fname,
- "Number of relays and bridges", c(1, 5, 7),
- c("running", "brunning"), c("Relays", "Bridges"))
+c <- c[1:length(c$date)-1,c("date", "running", "brunning")]
+c <- melt(c, id = "date")
+
+day <- as.numeric(days)
+end <- Sys.Date()
+start <- seq(from = end, length = 2, by = paste("-", day, " days",
+ sep = ""))[2]
+limits <- c(start, end)
+png(filename = fname, unit = "in", width = 8, height = 5, res = 72)
+ggplot(c, aes(x = as.Date(date, "%Y-%m-%d"), y = value,
+ colour = variable)) + geom_line() +
+ scale_x_date(name = "", limits = limits) +
+ scale_y_continuous(name = "",
+ limits = c(0, max(c$value, na.rm = TRUE))) +
+ scale_colour_hue("", breaks = c("running", "brunning"),
+ labels = c("Relays", "Bridges")) +
+ opts(title = paste("Number of relays and bridges (past", day,
+ "days)\n"))
+invisible(dev.off())
--
1.6.5