Repository for Petra's work at ampli Jan-Feb 2019

clusterviz.R 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/usr/bin/env Rscript
  2. library(argparse)
  3. parser <- ArgumentParser(description="Create plots for aggregated cluster data")
  4. parser$add_argument("cluster_file", help = "file to visualise")
  5. parser$add_argument("-i", "--img-path", dest = "img_path", default = "../img/", help = "path to store plots in; default: ../img/")
  6. parser$add_argument("-p", "--postfix", dest = "postfix", default = "_plot", help = "postfix for files, default: _plot")
  7. parser$add_argument("--width", dest = "width", default = 40, help = "width (cm), default 40", type = "double")
  8. parser$add_argument("--height", dest = "height", default = 25, help = "height (cm), default 25", type = "double")
  9. parser$add_argument("--pkl", dest = "csv", help = "load from pickle instead of csv", action = "store_false")
  10. args <- parser$parse_args()
  11. library(ggplot2, warn.conflicts = FALSE, quietly = TRUE)
  12. library(dplyr, warn.conflicts = FALSE, quietly = TRUE)
  13. library(tidyr, warn.conflicts = FALSE, quietly = TRUE)
  14. library(TSA, warn.conflicts = FALSE, quietly = TRUE)
  15. library(forecast, warn.conflicts = FALSE, quietly = TRUE)
  16. theme_set(theme_bw())
  17. if (args$csv) {
  18. aggdf <- read.csv(args$cluster_file, header = TRUE, stringsAsFactors = FALSE)
  19. aggdf$read_time <- as.POSIXct(aggdf$read_time)
  20. } else {
  21. library(reticulate, warn.conflicts = FALSE, quietly = TRUE)
  22. p <- import("pandas")
  23. aggdf <- p$read_pickle(args$cluster_file)
  24. }
  25. aggdf$cluster <- factor(aggdf$cluster)
  26. clusters = levels(aggdf$cluster)
  27. cbp <- c('#0173b2', '#de8f05', '#029e73', '#d55e00', '#cc78bc', '#ca9161', '#fbafe4', '#949494', '#ece133', '#56b4e9', '#0173b2',
  28. '#de8f05', '#029e73', '#d55e00', '#cc78bc', '#ca9161', '#fbafe4', '#949494', '#ece133', '#56b4e9', '#0173b2', '#de8f05',
  29. '#029e73', '#d55e00', '#cc78bc', '#ca9161', '#fbafe4', '#949494', '#ece133', '#56b4e9', '#0173b2', '#de8f05', '#029e73',
  30. '#d55e00', '#cc78bc', '#ca9161', '#fbafe4', '#949494', '#ece133', '#56b4e9')[1:length(clusters)]
  31. facall <- ggplot(aggdf, aes(x = read_time, y = kwh_tot_mean, color = cluster, fill = cluster)) +
  32. geom_line(size = 1.5) + geom_ribbon(aes(ymin = kwh_tot_CI_low, ymax = kwh_tot_CI_high), alpha = 0.2, color = NA) +
  33. labs(title = "Cluster behaviour over 2017", x = "Date", y = "kwh") +
  34. scale_color_manual(values = cbp) +
  35. scale_fill_manual(values = cbp) +
  36. theme(legend.position = "none") +
  37. scale_x_datetime(date_breaks = "1 month", date_labels = "%-d %b %y")
  38. allcon <- facall + facet_grid(cluster ~ .)
  39. allfre <- facall + facet_grid(cluster ~ ., scales = "free")
  40. midjan <- filter(aggdf, read_time >= as.POSIXct("2017-01-15", tz = "UTC"), read_time <= as.POSIXct("2017-01-22", tz = "UTC"))
  41. facjan <- ggplot(midjan, aes(x = read_time, y = kwh_tot_mean, color = cluster, fill = cluster)) +
  42. geom_line(size = 1.5) + geom_ribbon(aes(ymin = kwh_tot_CI_low, ymax = kwh_tot_CI_high), alpha = 0.2, color = NA) +
  43. labs(title = "Cluster behaviour over third week of January", x = "Date", y = "kwh") +
  44. scale_color_manual(values = cbp) +
  45. scale_fill_manual(values = cbp) +
  46. theme(legend.position = "none") +
  47. scale_x_datetime(date_breaks = "1 day", date_labels = "%a, %-d %B %Y")
  48. jancon <- facjan + facet_grid(cluster ~ .)
  49. janfre <- facjan + facet_grid(cluster ~ ., scales = "free")
  50. midap <- filter(aggdf, read_time >= as.POSIXct("2017-04-16", tz = "UTC"), read_time <= as.POSIXct("2017-04-23", tz = "UTC"))
  51. facap <- ggplot(midap, aes(x = read_time, y = kwh_tot_mean, color = cluster, fill = cluster)) +
  52. geom_line(size = 1.5) + geom_ribbon(aes(ymin = kwh_tot_CI_low, ymax = kwh_tot_CI_high), alpha = 0.2, color = NA) +
  53. labs(title = "Cluster behaviour over third week of April 2017", x = "Date", y = "kwh") +
  54. scale_color_manual(values = cbp) +
  55. scale_fill_manual(values = cbp) +
  56. theme(legend.position = "none") +
  57. scale_x_datetime(date_breaks = "1 day", date_labels = "%a, %-d %B %Y")
  58. apcon <- facap + facet_grid(cluster ~ .)
  59. apfre <- facap + facet_grid(cluster ~ ., scales = "free")
  60. midjul <- filter(aggdf, read_time >= as.POSIXct("2017-07-16", tz = "UTC"), read_time <= as.POSIXct("2017-07-23", tz = "UTC"))
  61. facjul <- ggplot(midjul, aes(x = read_time, y = kwh_tot_mean, color = cluster, fill = cluster)) +
  62. geom_line(size = 1.5) + geom_ribbon(aes(ymin = kwh_tot_CI_low, ymax = kwh_tot_CI_high), alpha = 0.2, color = NA) +
  63. labs(title = "Cluster behaviour over third week of July 2017", x = "Date", y = "kwh") +
  64. scale_color_manual(values = cbp) +
  65. scale_fill_manual(values = cbp) +
  66. theme(legend.position = "none") +
  67. scale_x_datetime(date_breaks = "1 day", date_labels = "%a, %-d %B %Y")
  68. julcon <- facjul + facet_grid(cluster ~ .)
  69. julfre <- facjul + facet_grid(cluster ~ ., scales = "free")
  70. midoct <- filter(aggdf, read_time >= as.POSIXct("2017-10-15", tz = "UTC"), read_time <= as.POSIXct("2017-10-22", tz = "UTC"))
  71. facoct <- ggplot(midoct, aes(x = read_time, y = kwh_tot_mean, color = cluster, fill = cluster)) +
  72. geom_line(size = 1.5) + geom_ribbon(aes(ymin = kwh_tot_CI_low, ymax = kwh_tot_CI_high), alpha = 0.2, color = NA) +
  73. labs(title = "Cluster behaviour over third week of October 2017", x = "Date", y = "kwh") +
  74. scale_color_manual(values = cbp) +
  75. scale_fill_manual(values = cbp) +
  76. theme(legend.position = "none") +
  77. scale_x_datetime(date_breaks = "1 day", date_labels = "%a, %-d %B %Y")
  78. octcon <- facoct + facet_grid(cluster ~ .)
  79. octfre <- facoct + facet_grid(cluster ~ ., scales = "free")
  80. ggsave(paste0("all_fix", args$postfix, ".png"), allcon, path = args$img_path, dpi = "retina", width = args$width, height = args$height, units = "cm")
  81. ggsave(paste0("all_fre", args$postfix, ".png"), allfre, path = args$img_path, dpi = "retina", width = args$width, height = args$height, units = "cm")
  82. ggsave(paste0("jan_fix", args$postfix, ".png"), jancon, path = args$img_path, dpi = "retina", width = args$width, height = args$height, units = "cm")
  83. ggsave(paste0("jan_fre", args$postfix, ".png"), janfre, path = args$img_path, dpi = "retina", width = args$width, height = args$height, units = "cm")
  84. ggsave(paste0("apr_fix", args$postfix, ".png"), apcon, path = args$img_path, dpi = "retina", width = args$width, height = args$height, units = "cm")
  85. ggsave(paste0("apr_fre", args$postfix, ".png"), apfre, path = args$img_path, dpi = "retina", width = args$width, height = args$height, units = "cm")
  86. ggsave(paste0("jul_fix", args$postfix, ".png"), julcon, path = args$img_path, dpi = "retina", width = args$width, height = args$height, units = "cm")
  87. ggsave(paste0("jul_fre", args$postfix, ".png"), julfre, path = args$img_path, dpi = "retina", width = args$width, height = args$height, units = "cm")
  88. ggsave(paste0("oct_fix", args$postfix, ".png"), octcon, path = args$img_path, dpi = "retina", width = args$width, height = args$height, units = "cm")
  89. ggsave(paste0("oct_fre", args$postfix, ".png"), octfre, path = args$img_path, dpi = "retina", width = args$width, height = args$height, units = "cm")