From 7928a8c26555d2e2bcbda325d426de3bca077b0f Mon Sep 17 00:00:00 2001 From: kemihak Date: Thu, 11 Jan 2024 10:40:03 +0100 Subject: [PATCH 1/8] Add contributor and change version --- DESCRIPTION | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 2135331f..06997b20 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: antaresRead Type: Package Title: Import, Manipulate and Explore the Results of an 'Antares' Simulation -Version: 2.6.1 +Version: 2.6.2 Authors@R: c( person("Tatiana", "Vargas", email = "tatiana.vargas@rte-france.com", role = c("aut", "cre")), person("Jalal-Edine", "ZAWAM", role = "aut"), @@ -13,6 +13,7 @@ Authors@R: c( person("Etienne", "Sanchez", role = "ctb"), person("Assil", "Mansouri", role = "ctb"), person("Clement", "Berthet", role = "ctb"), + person("Kamel", "Kemiha", role = "ctb"), person("RTE", role = "cph") ) Description: Import, manipulate and explore results generated by 'Antares', a From 8dad496d6e135ee7c1ac377d64141ee4f7e35df3 Mon Sep 17 00:00:00 2001 From: kemihak Date: Thu, 11 Jan 2024 10:40:14 +0100 Subject: [PATCH 2/8] Changelog --- NEWS.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/NEWS.md b/NEWS.md index 3b41e5e8..59ec1a84 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,13 @@ > Copyright © 2016 RTE Réseau de transport d’électricité +# antaresRead 2.6.2 (devlopment) + +BUGFIXES : + +* `readAntares()` : + - returns the right column names for details-.txt and details-res-.txt + + # antaresRead 2.6.1 (devlopment) BUGFIXES : From 4fac9cce35f8a28d2472987032e31bb9daad3897 Mon Sep 17 00:00:00 2001 From: kemihak Date: Thu, 11 Jan 2024 10:40:35 +0100 Subject: [PATCH 3/8] Add unit test for the column nmaes in output table --- tests/testthat/test-readAntares.R | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/testthat/test-readAntares.R b/tests/testthat/test-readAntares.R index 611db921..a5fef918 100644 --- a/tests/testthat/test-readAntares.R +++ b/tests/testthat/test-readAntares.R @@ -28,6 +28,16 @@ sapply(studyPathS, function(studyPath){ expect_equal(nrow(clusters), 24 * 7 * nweeks * nrow(readClusterDesc())) }) + test_that("Clusters importation column names are ok", { + clusters <- readAntares(clusters = opts$areasWithClusters, + timeStep = "hourly", + mcYears = "all", + opts = opts, + showProgress = FALSE) + expect_is(clusters, "data.table") + expect_equal(setdiff(colnames(clusters),pkgEnv$idVars), c("production", "NP Cost", "NODU")) + }) + test_that("importation of different objects works", { out <- readAntares(areas = opts$areaList, links=opts$linkList, clusters=opts$areasWithClusters, showProgress= FALSE, timeStep = "annual") From 7b8c78e9f4ec589bc63f9a13184e79871faf60b0 Mon Sep 17 00:00:00 2001 From: kemihak Date: Thu, 11 Jan 2024 10:41:28 +0100 Subject: [PATCH 4/8] Add .get_value_columns_details_file() to compute the column names of the output table --- R/importOutput.R | 88 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 60 insertions(+), 28 deletions(-) diff --git a/R/importOutput.R b/R/importOutput.R index f2ae8b60..44c59418 100644 --- a/R/importOutput.R +++ b/R/importOutput.R @@ -277,6 +277,55 @@ ) } + +#' .get_value_columns_details_file +#' +#' Private function used to get the column names for the details-.txt or details-res-.txt. +#' Used in .importOutputForClusters() and importOutputForResClusters() +#' +#' @return +#' a vector +#' +#' @noRd +#' +.get_value_columns_details_file <- function(opts, type) { + + ## details part + if(type == "details") { + # Order is important. There is a correspondance between elements. + all_thematic_variables <- c("DTG by plant", "NP Cost by plant", "NODU by plant") + all_output_colnames <- c("production", "NP Cost", "NODU") + if (opts$antaresVersion >= 830){ + all_thematic_variables <- c(all_thematic_variables, "Profit by plant") + all_output_colnames <- c(all_output_colnames, "profit") + } + colNames <- all_output_colnames + if ("variables selection" %in% names(opts$parameters)) { + selection_type <- unique(names(opts$parameters$`variables selection`)) + selected_variables <- unlist(opts$parameters$`variables selection`, use.names = FALSE) + # Index of the variables found in the section "variables selection" + idx_vars <- which(all_thematic_variables %in% selected_variables) + if (length(idx_vars) > 0) { + if (selection_type == "select_var -") { + # vars to remove + colNames <- colNames[-idx_vars] + } else if (selection_type == "select_var +") { + # vars to keep + colNames <- colNames[idx_vars] + } + } + } + } + + ## details-res part + if(type == "details-res") { + colNames <- c("production") + } + + return(colNames) +} + + #' .importOutputForClusters #' #' Private function used to import the output for the thermal clusters of one area @@ -295,6 +344,7 @@ # To improve greatly the performance we use our knowledge of the position of # the columns instead of using more general functions like dcast. reshapeFun <- function(x) { + # Get cluster names n <- names(x) idx <- ! n %in% pkgEnv$idVars @@ -304,28 +354,19 @@ idVarsId <- which(!idx) idVarsNames <- n[idVarsId] - # Get final value columns - if (sum(idx) / length(clusterNames) == 4) { - colNames <- c("production", "NP Cost", "NODU", "profit") - } else if (sum(idx) / length(clusterNames) == 3) { - colNames <- c("production", "NP Cost", "NODU") - } else if (sum(idx) / length(clusterNames) == 2) { - colNames <- c("production", "NP Cost") - } else { - colNames <- c("production") - } + # Column names of the output table + colNames <- .get_value_columns_details_file(opts, "details") # Loop over clusters nclusters <- length(clusterNames) - ncols <- length(colNames) res <- llply(1:nclusters, function(i) { - dt <- x[, c(nclusters * 0:(ncols - 1) + i, idVarsId), with = FALSE] + dt <- x[, c(nclusters * 0:(length(colNames) - 1) + i, idVarsId), with = FALSE] setnames(dt, c(colNames, idVarsNames)) dt[, cluster := as.factor(clusterNames[i])] dt }) - + rbindlist(res) } @@ -436,9 +477,9 @@ res } - } + #' .importOutputForResClusters #' #' Private function used to import the output for the renewable clusters of one area @@ -457,6 +498,7 @@ # To improve greatly the performance we use our knowledge of the position of # the columns instead of using more general functions like dcast. reshapeFun <- function(x) { + # Get cluster names n <- names(x) idx <- ! n %in% pkgEnv$idVars @@ -466,23 +508,14 @@ idVarsId <- which(!idx) idVarsNames <- n[idVarsId] - # Get final value columns - # Get final value columns - # colNames <- c("resProduction") - if (sum(idx) / length(clusterNames) == 3) { - colNames <- c("production", "NP Cost", "NODU") - } else if (sum(idx) / length(clusterNames) == 2) { - colNames <- c("production", "NP Cost") - } else { - colNames <- c("production") - } + # Column names of the output table + colNames <- .get_value_columns_details_file(opts, "details-res") # Loop over clusters nclusters <- length(clusterNames) - ncols <- length(colNames) res <- llply(1:nclusters, function(i) { - dt <- x[, c(nclusters * 0:(ncols - 1) + i, idVarsId), with = FALSE] + dt <- x[, c(nclusters * 0:(length(colNames) - 1) + i, idVarsId), with = FALSE] setnames(dt, c(colNames, idVarsNames)) dt[, cluster := as.factor(clusterNames[i])] dt @@ -496,10 +529,9 @@ mcYears, showProgress, opts, reshapeFun, sameNames = FALSE, objectDisplayName = "clustersRe", parallel = parallel) ) - - } + #' .importOutputForBindingConstraints #' #' Private function used to import the output for binding constraints. From 102ee53d708b63f00372a09d285b645428b64e12 Mon Sep 17 00:00:00 2001 From: kemihak Date: Thu, 11 Jan 2024 16:45:09 +0100 Subject: [PATCH 5/8] Remove special characters to avoid warning --- NEWS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 59ec1a84..7d6a3faf 100644 --- a/NEWS.md +++ b/NEWS.md @@ -5,7 +5,7 @@ BUGFIXES : * `readAntares()` : - - returns the right column names for details-.txt and details-res-.txt + - returns the right column names for details-timeStep.txt and details-res-timeStep.txt # antaresRead 2.6.1 (devlopment) From 852f341a09b5ffd5886b3c3918b4824627de57ee Mon Sep 17 00:00:00 2001 From: kemihak Date: Thu, 11 Jan 2024 16:59:12 +0100 Subject: [PATCH 6/8] Add control on the thematic-trimming parameter --- R/importOutput.R | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/R/importOutput.R b/R/importOutput.R index 44c59418..228f3bad 100644 --- a/R/importOutput.R +++ b/R/importOutput.R @@ -46,6 +46,7 @@ #' is determined by the arguments "folder" and "file" #' - "areas", "values" => areas #' - "areas", "details" => clusters +#' - "areas", "details-res" => renewables clusters #' - "links", "values" => links #' #' @return @@ -280,7 +281,7 @@ #' .get_value_columns_details_file #' -#' Private function used to get the column names for the details-.txt or details-res-.txt. +#' Private function used to get the column names for the details-timeStep.txt or details-res-timeStep.txt. #' Used in .importOutputForClusters() and importOutputForResClusters() #' #' @return @@ -300,21 +301,30 @@ all_output_colnames <- c(all_output_colnames, "profit") } colNames <- all_output_colnames - if ("variables selection" %in% names(opts$parameters)) { - selection_type <- unique(names(opts$parameters$`variables selection`)) - selected_variables <- unlist(opts$parameters$`variables selection`, use.names = FALSE) - # Index of the variables found in the section "variables selection" - idx_vars <- which(all_thematic_variables %in% selected_variables) - if (length(idx_vars) > 0) { - if (selection_type == "select_var -") { - # vars to remove - colNames <- colNames[-idx_vars] - } else if (selection_type == "select_var +") { - # vars to keep - colNames <- colNames[idx_vars] + # With thematic-trimming enabled + if (opts$parameters$general$`thematic-trimming`) { + if ("variables selection" %in% names(opts$parameters)) { + var_selection <- opts$parameters$`variables selection` + selection_type <- unique(names(var_selection)) + allowed_selection_type <- c("select_var -", "select_var +") + # Filter the vector to avoid other properties (for example : selected_vars_reset) + selection_type <- intersect(selection_type, allowed_selection_type) + # List with a repeated name + var_selection <- var_selection[which(names(var_selection) == selection_type)] + selected_variables <- unlist(var_selection, use.names = FALSE) + # Index of the variables found in the section "variables selection" + idx_vars <- which(all_thematic_variables %in% selected_variables) + if (length(idx_vars) > 0) { + if (selection_type == "select_var -") { + # vars to remove + colNames <- colNames[-idx_vars] + } else if (selection_type == "select_var +") { + # vars to keep + colNames <- colNames[idx_vars] + } } } - } + } } ## details-res part From 3357fd3d618fffbdc450ccd482cf167efdb7599e Mon Sep 17 00:00:00 2001 From: kemihak Date: Mon, 15 Jan 2024 09:55:26 +0100 Subject: [PATCH 7/8] Factorization code --- R/importOutput.R | 59 +++++++++++++++++++++++------------------------- 1 file changed, 28 insertions(+), 31 deletions(-) diff --git a/R/importOutput.R b/R/importOutput.R index 228f3bad..f7169d4c 100644 --- a/R/importOutput.R +++ b/R/importOutput.R @@ -291,47 +291,44 @@ #' .get_value_columns_details_file <- function(opts, type) { - ## details part if(type == "details") { # Order is important. There is a correspondance between elements. all_thematic_variables <- c("DTG by plant", "NP Cost by plant", "NODU by plant") - all_output_colnames <- c("production", "NP Cost", "NODU") + colNames <- c("production", "NP Cost", "NODU") if (opts$antaresVersion >= 830){ all_thematic_variables <- c(all_thematic_variables, "Profit by plant") - all_output_colnames <- c(all_output_colnames, "profit") + colNames <- c(colNames, "profit") } - colNames <- all_output_colnames - # With thematic-trimming enabled - if (opts$parameters$general$`thematic-trimming`) { - if ("variables selection" %in% names(opts$parameters)) { - var_selection <- opts$parameters$`variables selection` - selection_type <- unique(names(var_selection)) - allowed_selection_type <- c("select_var -", "select_var +") - # Filter the vector to avoid other properties (for example : selected_vars_reset) - selection_type <- intersect(selection_type, allowed_selection_type) - # List with a repeated name - var_selection <- var_selection[which(names(var_selection) == selection_type)] - selected_variables <- unlist(var_selection, use.names = FALSE) - # Index of the variables found in the section "variables selection" - idx_vars <- which(all_thematic_variables %in% selected_variables) - if (length(idx_vars) > 0) { - if (selection_type == "select_var -") { - # vars to remove - colNames <- colNames[-idx_vars] - } else if (selection_type == "select_var +") { - # vars to keep - colNames <- colNames[idx_vars] - } + } else if(type == "details-res") { + # Order is important. There is a correspondance between elements. + all_thematic_variables <- c("RES generation by plant") + colNames <- c("production") + } + # With thematic-trimming enabled + if (opts$parameters$general$`thematic-trimming`) { + if ("variables selection" %in% names(opts$parameters)) { + var_selection <- opts$parameters$`variables selection` + selection_type <- unique(names(var_selection)) + allowed_selection_type <- c("select_var -", "select_var +") + # Filter the vector to avoid other properties (for example : selected_vars_reset) + selection_type <- intersect(selection_type, allowed_selection_type) + # List with a repeated name + var_selection <- var_selection[which(names(var_selection) == selection_type)] + selected_variables <- unlist(var_selection, use.names = FALSE) + # Index of the variables found in the section "variables selection" + idx_vars <- which(all_thematic_variables %in% selected_variables) + if (length(idx_vars) > 0) { + if (selection_type == "select_var -") { + # vars to remove + colNames <- colNames[-idx_vars] + } else if (selection_type == "select_var +") { + # vars to keep + colNames <- colNames[idx_vars] } } - } + } } - ## details-res part - if(type == "details-res") { - colNames <- c("production") - } - return(colNames) } From afe0cbcaa3c85451649ffd5135e8744d0cdc10e3 Mon Sep 17 00:00:00 2001 From: kemihak Date: Mon, 29 Jan 2024 10:08:30 +0100 Subject: [PATCH 8/8] Add some documentation for the function .get_value_columns_details_file() --- R/importOutput.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/R/importOutput.R b/R/importOutput.R index f7169d4c..2cb25a80 100644 --- a/R/importOutput.R +++ b/R/importOutput.R @@ -282,7 +282,8 @@ #' .get_value_columns_details_file #' #' Private function used to get the column names for the details-timeStep.txt or details-res-timeStep.txt. -#' Used in .importOutputForClusters() and importOutputForResClusters() +#' Used in .importOutputForClusters() and .importOutputForResClusters() +#' From the opts, we detect which outputs the user decides to take #' #' @return #' a vector