Bearloga has submitted this change and it was merged.

Change subject: Smarter log-scaling + smoothing
......................................................................


Smarter log-scaling + smoothing

Change-Id: I6be3c80f6bfcfa47600526b4f2a4685fa0a7969c
---
M server.R
M ui.R
M utils.R
3 files changed, 28 insertions(+), 5 deletions(-)

Approvals:
  Bearloga: Verified; Looks good to me, approved



diff --git a/server.R b/server.R
index ee64151..faa43e9 100644
--- a/server.R
+++ b/server.R
@@ -16,10 +16,16 @@
   
   output$wdqs_usage_plot <- renderDygraph(
     wdqs_usage %>%
-      polloi::smoother(smooth_level = 
polloi::smooth_switch(input$smoothing_global, input$smoothing_usage), rename = 
FALSE) %>%
-      polloi::subset_by_date_range(time_frame_range(input$usage_timeframe, 
input$usage_timeframe_daterange)) %>%
       spider_subset(val = input$include_automata) %>%
+      # The next few lines make for better smoothing because the data is first 
log-transformed:
+      conditional_transform(input$usage_logscale && 
polloi::smooth_switch(input$smoothing_global, input$smoothing_usage) != "day", 
log10) %>%
+      # ...THEN smoothed:
+      polloi::smoother(smooth_level = 
polloi::smooth_switch(input$smoothing_global, input$smoothing_usage), rename = 
FALSE) %>%
+      # ...and then exp-transformed back to the original scale:
+      conditional_transform(input$usage_logscale && 
polloi::smooth_switch(input$smoothing_global, input$smoothing_usage) != "day", 
exp) %>%
+      polloi::subset_by_date_range(time_frame_range(input$usage_timeframe, 
input$usage_timeframe_daterange)) %>%
       polloi::make_dygraph(xlab = "Date", ylab = "Events", title = "Daily WDQS 
Homepage usage", group = "usage") %>%
+      # ...because we're using dygraphs' native log-scaling:
       dyAxis("y", logscale = input$usage_logscale) %>%
       dyLegend(labelsDiv = "usage_legend") %>%
       dyEvent(as.Date("2015-09-07"), "A (Announcement)", labelLoc = "bottom")
@@ -27,9 +33,12 @@
   
   output$sparql_usage_plot <- renderDygraph(
     sparql_usage %>%
-      polloi::smoother(smooth_level = 
polloi::smooth_switch(input$smoothing_global, input$smoothing_usage), rename = 
FALSE) %>%
-      polloi::subset_by_date_range(time_frame_range(input$usage_timeframe, 
input$usage_timeframe_daterange)) %>%
       spider_subset(val = input$include_automata) %>%
+      # See above for why we're conditional_transform'ing here.
+      conditional_transform(input$usage_logscale && 
polloi::smooth_switch(input$smoothing_global, input$smoothing_usage) != "day", 
log10) %>%
+      polloi::smoother(smooth_level = 
polloi::smooth_switch(input$smoothing_global, input$smoothing_usage), rename = 
FALSE) %>%
+      conditional_transform(input$usage_logscale && 
polloi::smooth_switch(input$smoothing_global, input$smoothing_usage) != "day", 
exp) %>%
+      polloi::subset_by_date_range(time_frame_range(input$usage_timeframe, 
input$usage_timeframe_daterange)) %>%
       polloi::make_dygraph(xlab = "Date", ylab = "Events", title = "Daily 
SPARQL usage", group = "usage") %>%
       dyLegend(labelsDiv = "usage_legend") %>%
       dyAxis("y", logscale = input$usage_logscale) %>%
diff --git a/ui.R b/ui.R
index 9cbfe5c..c3afb75 100644
--- a/ui.R
+++ b/ui.R
@@ -36,7 +36,7 @@
               column(polloi::smooth_select("smoothing_usage"), width = 3),
               column(polloi::timeframe_select("usage_timeframe"), width = 3),
               column(polloi::timeframe_daterange("usage_timeframe"), width = 
3),
-              column(checkboxInput("usage_logscale", "Use Log scale", FALSE), 
width = 3)),
+              column(checkboxInput("usage_logscale", "Use Log scale", TRUE), 
width = 3)),
             spider_checkbox("include_automata"),
             dygraphOutput("wdqs_usage_plot", height = "200px"),
             dygraphOutput("sparql_usage_plot", height = "200px"),
diff --git a/utils.R b/utils.R
index 86356e3..b3680f9 100644
--- a/utils.R
+++ b/utils.R
@@ -33,3 +33,17 @@
       summarise(events = sum(events))
   })
 }
+
+conditional_transform <- function(x, cond, .f, ...) {
+  if (cond) {
+    return(data_transform(x, .f, ...))
+  } else {
+    return(x)
+  }
+}
+
+data_transform <- function(x, .f = identity, ...) {
+  col_names <- setdiff(colnames(x), c("Date", "date", "timestamp"))
+  x[, col_names] <- .f(x[, col_names], ...)
+  return(x)
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/303606
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I6be3c80f6bfcfa47600526b4f2a4685fa0a7969c
Gerrit-PatchSet: 1
Gerrit-Project: wikimedia/discovery/twilightsparql
Gerrit-Branch: master
Gerrit-Owner: Bearloga <[email protected]>
Gerrit-Reviewer: Bearloga <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to