From 40841f277b98d600547c2743c35b83371931fc09 Mon Sep 17 00:00:00 2001 From: JulienBretteville Date: Thu, 8 Aug 2019 16:46:13 +0200 Subject: [PATCH] =?UTF-8?q?Ajout=20d'un=20test=20Am=C3=A9lioration=20de=20?= =?UTF-8?q?plotFlowbased?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- R/graphs.R | 31 ++++++++++++++++++------ tests/testthat/test-crtlCountriesCombn.R | 18 ++++++++++++++ 2 files changed, 41 insertions(+), 8 deletions(-) create mode 100644 tests/testthat/test-crtlCountriesCombn.R diff --git a/R/graphs.R b/R/graphs.R index c678f34..63eac60 100644 --- a/R/graphs.R +++ b/R/graphs.R @@ -292,11 +292,11 @@ clusterPlot <- function(data, #' #' hubDrop = list(NL = c("BE", "DE", "FR", "AT")) #' #Plot unique polyhedron -#' plotFlowbased(PLAN, "BE", "DE", hubDrop = hubDrop, +#' plotFlowbased(PLAN, country1 = "BE", country2 = "DE", hubDrop = hubDrop, #' hours = c(2), dates = c("2018-10-02"), domainsNames = "2018-10-02", main = "") #' #' #Plot four polyhedra -#' plotFlowbased(PLAN, "BE", "DE", hubDrop = hubDrop, +#' plotFlowbased(PLAN, country1 = "BE", country2 = "DE", hubDrop = hubDrop, #' hours = c(3, 4), dates = c("2018-10-02", "2018-10-04"), domainsNames = NULL, #' main = NULL) #' @@ -306,13 +306,24 @@ clusterPlot <- function(data, #' PLAN2 <- PLAN2[Date == "2018-10-04"] #' PLAN2[, ptdfAT := NULL] #' hubDrop2 <- list("NL" = list("BE", "DE", "FR")) -#' out3 <- plotFlowbased(PLAN, PLAN2 = PLAN2, country1 = "BE", country2 = "DE", +#' plotFlowbased(PLAN, PLAN2 = PLAN2, country1 = "BE", country2 = "DE", #' hubDrop = hubDrop, hubDrop2 = hubDrop2, #' hours = c(3, 4), dates = c("2018-10-02"), #' hours2 = c(4), dates2 = c("2018-10-04"), #' domainsNames = NULL, main = NULL) #' #' +#' +#' # Plot two domains from cwe and cwe-at at the same hours +#' PLAN3 <- copy(PLAN) +#' PLAN3[, ptdfAT := NULL] +#' hubDrop2 <- list("NL" = list("BE", "DE", "FR")) +#' plotFlowbased(PLAN, PLAN2 = PLAN3, country1 = "BE", country2 = "DE", +#' hubDrop = hubDrop, hubDrop2 = hubDrop2, +#' hours = c(3, 4), dates = c("2018-10-02"), +#' hours2 = c(3, 4), dates2 = c("2018-10-02"), +#' domainsNames = NULL, main = NULL) +#' #' } #' #' @export @@ -360,7 +371,7 @@ plotFlowbased <- function(PLAN, .ctrlHubDrop(hubDrop = hubDrop, PLAN = PLAN) PLAN <- setDiffNotWantedPtdf(PLAN = PLAN, hubDrop = hubDrop) comb <- unique(PLAN[, list(Period, Date)]) - + comb[, PLAN := "PLAN"] if (!is.null(PLAN2)) { hubnames2 <- gsub("ptdf", "", colnames(PLAN2)[grep("ptdf", colnames(PLAN2))]) PLAN2 <- copy(PLAN2) @@ -368,6 +379,7 @@ plotFlowbased <- function(PLAN, .ctrlHubDrop(hubDrop = hubDrop2, PLAN = PLAN2) PLAN2 <- setDiffNotWantedPtdf(PLAN = PLAN2, hubDrop = hubDrop2) comb2 <- unique(PLAN2[, list(Period, Date)]) + comb2[, PLAN := "PLAN2"] comb <- rbindlist(list(comb, comb2)) } @@ -389,6 +401,9 @@ plotFlowbased <- function(PLAN, } if(is.null(domainsNames)){ domainsNames <- paste("Date :", comb[, Date], "Hour :", comb[, Period]) + if (length(unique(comb$PLAN) > 1)) { + domainsNames <- paste(domainsNames, "PTDF :", comb[, PLAN]) + } } VERT <- getVertices(PLAN) @@ -462,13 +477,13 @@ plotFlowbased <- function(PLAN, period <- comb[X, Period] date <- comb[X, Date] - if (nrow(VERT[Period == period & Date == date]) == 0 & - !is.null(VERT2)) { + pl <- comb[X, PLAN] + if (pl == "PLAN2") { data <- data.table(.getChull(VERT2[Period == period & Date == date], ctry1, ctry2, hubnameDiff2)) } else { - data <- data.table(.getChull(VERT[Period == period & Date == date], - ctry1, ctry2, hubnameDiff)) + data <- data.table(.getChull(VERT[Period == period & Date == date], + ctry1, ctry2, hubnameDiff)) } diff --git a/tests/testthat/test-crtlCountriesCombn.R b/tests/testthat/test-crtlCountriesCombn.R new file mode 100644 index 0000000..64b1622 --- /dev/null +++ b/tests/testthat/test-crtlCountriesCombn.R @@ -0,0 +1,18 @@ +context("Function .crtlCountriesCombn") + +test_that(".crtlCountriesCombn", { + + countries <- list(c("BE", "FR"), c("BE", "NL"), c("DE", "FR"), c("DE", "AT")) + dfcountries <- .crtlCountriesCombn(countries) + expect_true("data.frame" %in% class(dfcountries)) + expect_true(all(dim(dfcountries) == c(4, 2))) + + countries2 <- c("BE", "FR", "AT") + dfcountries2 <- .crtlCountriesCombn(countries2) + expect_true("data.frame" %in% class(dfcountries2)) + expect_true(all(dim(dfcountries2) == c(3, 2))) + + expect_error(crtlCountriesCombn(5)) + expect_error(crtlCountriesCombn(list(c("BE", "FR", "DE")))) +}) + \ No newline at end of file