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

clusterviz.R 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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("-v", "--virtualenv", dest = "virtualenv", default = "../venv/", help = "path of virtualenv; default: ../venv/; '-' for none")
  8. parser$add_argument("--width", dest = "width", default = 40, help = "width (cm), default 40", type = "double")
  9. parser$add_argument("--height", dest = "height", default = 25, help = "height (cm), default 25", type = "double")
  10. args <- parser$parse_args()
  11. library(reticulate)
  12. library(ggplot2)
  13. library(dplyr)
  14. library(tidyr)
  15. library(TSA)
  16. library(forecast)
  17. theme_set(theme_bw())
  18. if (args$virtualenv != "-") {
  19. print(args$virtualenv)
  20. use_virtualenv(args$virtualenv)
  21. }
  22. p <- import("pandas")
  23. sns <- import("seaborn")
  24. cbp <- as.character(p$Series(sns$color_palette("colorblind", as.integer(9))$as_hex()))
  25. aggdf <- p$read_pickle(args$cluster_file)
  26. # aggdf <- as.data.frame(aggdf)
  27. aggdf$cluster <- factor(aggdf$cluster)
  28. str(aggdf)
  29. clusters = levels(aggdf$cluster)
  30. ggplot(aggdf, aes(y = kwh_tot_mean, x = cluster)) + geom_boxplot()
  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")