From 8df09b5073ec2671f25edb6059274fbc27451a57 Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Mon, 9 Oct 2023 17:38:35 +0200 Subject: [PATCH 1/3] initial commit to add function setThematicTrimming + tests --- R/thematic_trimming.R | 154 ++++++++++++++++++++++++ tests/testthat/test-thematic_trimming.R | 135 +++++++++++++++++++++ 2 files changed, 289 insertions(+) create mode 100644 R/thematic_trimming.R create mode 100644 tests/testthat/test-thematic_trimming.R diff --git a/R/thematic_trimming.R b/R/thematic_trimming.R new file mode 100644 index 00000000..d73ef586 --- /dev/null +++ b/R/thematic_trimming.R @@ -0,0 +1,154 @@ + +#' @title Set the thematic trimming of an Antares Study +#' +#' @description Put only variables names you want to keep in study output +#' +setThematicTrimming <- function(selection_variables, + type_select = c("add", "suppr"), + opts = simOptions()){ + + # available only for study version >= 800 + if(opts$antaresVersion<800) + stop("'setThematicTrimming()' calls is only available for study version >= v8.0", + call. = FALSE) + + # basics parameters checks + assertthat::assert_that(inherits(opts, "simOptions")) + assertthat::assert_that(inherits(selection_variables, "character")) + type_select <- match.arg(type_select) + + # checks whether the variables agree with the version + # at least one variable must be present in the list + variables_package_list <- antaresRead:::pkgEnv$thematic + + # filter with version + variables_package_list <- variables_package_list[ + variables_package_list$version<=opts$antaresVersion, variable] + + is_selection_ok <- any( + selection_variables %in% + variables_package_list + ) + + if(!is_selection_ok) + stop(paste0("Put only variables according too version of study : "), + opts$antaresVersion, + call. = FALSE) + + # read general data parameters + generaldata <- readIni("settings/generaldata", + opts = opts) + + # update [general] (used to custom variable selection in Antares UI) + generaldata$general$`thematic-trimming` <- TRUE + + # manage writing general data + generaldata_updated <- .set_thematic(type_select = type_select, + pkg_var_version = variables_package_list, + list_var_selection = selection_variables, + general_data = generaldata) + + # write updated file + writeIni(listData = generaldata_updated, + pathIni = "settings/generaldata", + overwrite = TRUE, + opts = opts) + + # Update simulation options object + suppressWarnings({ + res <- antaresRead::setSimulationPath(path = opts$studyPath, + simulation = "input") + }) + + invisible(res) + +} + +# according to UI or API + # write in generaldata file only the fewest variables + # Ex : you want ADD 90% of variables => write to SUPPR 10% +.set_thematic <- function(type_select, + pkg_var_version, + list_var_selection, + general_data){ + # reset [variables selection] + general_data$`variables selection` <- NULL + + # count variables to write + nb_var_version <- length(pkg_var_version) + index_var_selection <- pkg_var_version %in% + list_var_selection + nb_right_var_selection <- sum(index_var_selection) + + # case with all variables selected + if(nb_var_version==nb_right_var_selection){ + if(type_select %in% "add"){ + cat("All variables are selected, by defaut it's not required to edit 'generaldata.ini'") + # general_data$`variables selection` <- NULL + return(general_data) + } + else{ + cat("All variables will be skiped") + general_data$`variables selection`$selected_vars_reset <- FALSE + return(general_data) + } + } + + # write your selection + if(nb_right_var_selection= 800 (ADD VAR)", { + # read study / load meta parameters + antaresRead::setSimulationPath(study_temp_path, "input") + + # list of variables + vect_select_vars <- antaresRead:::pkgEnv$thematic + + ## + # set all variables + ## + setThematicTrimming(selection_variables = vect_select_vars$variable) + + # check variables names according to antares version + opts_study_test <- simOptions() + antares_version <- opts_study_test$antaresVersion + filter_vars_version <- vect_select_vars[version<=antares_version,] + + res_read <- getThematicTrimming() + + # test if variables are all in output + testthat::expect_true(all(filter_vars_version$variable%in% + res_read$variables)) + # test status values + testthat::expect_equal(object = unique(res_read$status_selection), + expected = "active") + + ## + # set few variables + ## + setThematicTrimming(selection_variables = vect_select_vars$variable[1:10]) + res_read <- getThematicTrimming() + + # test if vars are activated + res_read_active <- res_read[res_read$status_selection %in% "active",] + testthat::expect_true(all( + vect_select_vars$variable[1:10]%in%res_read_active$variables + )) + + # test opts updated + opts_study <- simOptions() + thematic_values <- opts_study$parameters$`variables selection` + testthat::expect_true(!is.null(thematic_values)) + testthat::expect_true(all( + c("selected_vars_reset", "select_var +") %in% + names(thematic_values) + )) + thematic_values <- unlist(thematic_values, use.names = FALSE) + testthat::expect_true(all( + c("FALSE", vect_select_vars$variable[1:10])%in% + thematic_values + )) + + # set more than 50% of variables + # Opposite case with ADD columns but write suppression columns + nb_vars <- length(vect_select_vars$variable) + setThematicTrimming(selection_variables = vect_select_vars$variable[1:(nb_vars-10)]) + + res_read <- getThematicTrimming() + + # test if vars are activated + res_read_active <- res_read[res_read$status_selection %in% "active",] + testthat::expect_true(all( + vect_select_vars$variable[1:(nb_vars-10)]%in%res_read_active$variables + )) + + # test opts updated + opts_study <- simOptions() + thematic_values <- opts_study$parameters$`variables selection` + testthat::expect_true(!is.null(thematic_values)) + testthat::expect_true(all( + c("selected_vars_reset", "select_var -") %in% + names(thematic_values) + )) + # control values + thematic_values <- unlist(thematic_values, use.names = FALSE) + res_read_skip <- res_read[res_read$status_selection %in% "skip",] + testthat::expect_true(all( + c("TRUE", res_read_skip$variables)%in% + thematic_values + )) + +}) + +## suppr variables ---- +test_that("set thematic trimming version >= 800 (SUPPR VAR)", { + # read study / load meta parameters + antaresRead::setSimulationPath(study_temp_path, "input") + + # list of variables + vect_select_vars <- antaresRead:::pkgEnv$thematic + + ## + # set all variables + ## + setThematicTrimming(selection_variables = vect_select_vars$variable, + type_select = "suppr") + + # read + res_read <- getThematicTrimming() + + # test status values + testthat::expect_equal(object = unique(res_read$status_selection), + expected = "skip") +}) + +# delete study ---- +unlink(x = study_temp_path, recursive = TRUE) From 913896f07f80c394fb45100c9026456b2fd2752e Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Tue, 10 Oct 2023 14:15:22 +0200 Subject: [PATCH 2/3] setThematicTrimming update with suppression part + tests + doc --- NAMESPACE | 1 + R/thematic_trimming.R | 51 +++++++++++++++++++-- man/setThematicTrimming.Rd | 50 ++++++++++++++++++++ tests/testthat/test-thematic_trimming.R | 61 +++++++++++++++++++++++++ 4 files changed, 159 insertions(+), 4 deletions(-) create mode 100644 man/setThematicTrimming.Rd diff --git a/NAMESPACE b/NAMESPACE index d135f18a..c37cfbc6 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -72,6 +72,7 @@ export(searchStudy) export(setAPImode) export(setPlaylist) export(setSolverPath) +export(setThematicTrimming) export(storage_values_default) export(updateAdequacySettings) export(updateGeneralSettings) diff --git a/R/thematic_trimming.R b/R/thematic_trimming.R index d73ef586..1224206d 100644 --- a/R/thematic_trimming.R +++ b/R/thematic_trimming.R @@ -1,8 +1,36 @@ #' @title Set the thematic trimming of an Antares Study #' -#' @description Put only variables names you want to keep in study output +#' @description Put only variables names you want to keep in study output. +#' +#' you can add or remove variables. #' +#' @param selection_variables `character` of variables to add or remove. +#' @param type_select `character` select mode to add or remove (default add mode). +#' @template opts +#' +#' @note You can put only variables according to version of study +#' +#' @export +#' +#' @examples +#' \dontrun{ +#' +#' # list of variables (from version v8.0 to v8.6) +#' vect_select_vars <- antaresRead:::pkgEnv$thematic +#' +#' ## +#' # add all variables +#' ## +#' setThematicTrimming(selection_variables = vect_select_vars$variable) +#' +#' ## +#' # remove all variables +#' ## +#' setThematicTrimming(selection_variables = vect_select_vars$variable, +#' type_select = "suppr") +#' +#' } setThematicTrimming <- function(selection_variables, type_select = c("add", "suppr"), opts = simOptions()){ @@ -64,6 +92,7 @@ setThematicTrimming <- function(selection_variables, } + # according to UI or API # write in generaldata file only the fewest variables # Ex : you want ADD 90% of variables => write to SUPPR 10% @@ -100,6 +129,12 @@ setThematicTrimming <- function(selection_variables, var_select_bloc <- .make_thematic_list(var_selection = list_var_selection) general_data$`variables selection` <- var_select_bloc return(general_data) + }else{ + var_select_bloc <- .make_thematic_list(var_selection = list_var_selection, + pattern_list = "select_var -", + type_select = "suppr") + general_data$`variables selection` <- var_select_bloc + return(general_data) } # write the opposite }else{ @@ -111,6 +146,14 @@ setThematicTrimming <- function(selection_variables, type_select = "suppr") general_data$`variables selection` <- var_select_bloc return(general_data) + }else{ + # diff + list_var_selection <- setdiff(pkg_var_version, list_var_selection) + var_select_bloc <- .make_thematic_list(var_selection = list_var_selection, + pattern_list = "select_var +", + type_select = "add") + general_data$`variables selection` <- var_select_bloc + return(general_data) } } @@ -121,6 +164,9 @@ setThematicTrimming <- function(selection_variables, } + +# list construction (section [variables selection] + # build list with pattern and type .make_thematic_list <- function(pattern_list = "select_var +", type_select = "add", var_selection){ @@ -148,7 +194,4 @@ setThematicTrimming <- function(selection_variables, bloc_list) return(bloc_list) - - - } diff --git a/man/setThematicTrimming.Rd b/man/setThematicTrimming.Rd new file mode 100644 index 00000000..efdfce0d --- /dev/null +++ b/man/setThematicTrimming.Rd @@ -0,0 +1,50 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/thematic_trimming.R +\name{setThematicTrimming} +\alias{setThematicTrimming} +\title{Set the thematic trimming of an Antares Study} +\usage{ +setThematicTrimming( + selection_variables, + type_select = c("add", "suppr"), + opts = simOptions() +) +} +\arguments{ +\item{selection_variables}{\code{character} of variables to add or remove.} + +\item{type_select}{\code{character} select mode to add or remove (default add mode).} + +\item{opts}{List of simulation parameters returned by the function +\code{\link[antaresRead:setSimulationPath]{antaresRead::setSimulationPath()}}} +} +\value{ +An updated list containing various information about the simulation. +} +\description{ +Put only variables names you want to keep in study output. + +you can add or remove variables. +} +\note{ +You can put only variables according to version of study +} +\examples{ +\dontrun{ + +# list of variables (from version v8.0 to v8.6) +vect_select_vars <- antaresRead:::pkgEnv$thematic + +## +# add all variables +## +setThematicTrimming(selection_variables = vect_select_vars$variable) + +## +# remove all variables +## +setThematicTrimming(selection_variables = vect_select_vars$variable, + type_select = "suppr") + +} +} diff --git a/tests/testthat/test-thematic_trimming.R b/tests/testthat/test-thematic_trimming.R index 7d3cf2db..26b0df7a 100644 --- a/tests/testthat/test-thematic_trimming.R +++ b/tests/testthat/test-thematic_trimming.R @@ -129,6 +129,67 @@ test_that("set thematic trimming version >= 800 (SUPPR VAR)", { # test status values testthat::expect_equal(object = unique(res_read$status_selection), expected = "skip") + + ## + # set few variables + ## + setThematicTrimming(selection_variables = vect_select_vars$variable[1:10], + type_select = "suppr") + + # read + res_read <- getThematicTrimming() + + # test if vars are activated + res_read_active <- res_read[res_read$status_selection %in% "skip",] + testthat::expect_true(all( + vect_select_vars$variable[1:10]%in%res_read_active$variables + )) + + # test opts updated + opts_study <- simOptions() + thematic_values <- opts_study$parameters$`variables selection` + testthat::expect_true(!is.null(thematic_values)) + testthat::expect_true(all( + c("selected_vars_reset", "select_var -") %in% + names(thematic_values) + )) + + thematic_values <- unlist(thematic_values, use.names = FALSE) + testthat::expect_true(all( + c("TRUE", vect_select_vars$variable[1:10])%in% + thematic_values + )) + + # set more than 50% of variables + # Opposite case with "suppr" columns but write "add" columns + nb_vars <- length(vect_select_vars$variable) + setThematicTrimming(selection_variables = vect_select_vars$variable[1:(nb_vars-10)], + type_select = "suppr") + + res_read <- getThematicTrimming() + + # test if vars are skiped + res_read_skip <- res_read[res_read$status_selection %in% "skip",] + testthat::expect_true(all( + vect_select_vars$variable[1:(nb_vars-10)]%in% + res_read_skip$variables + )) + + # test opts updated + opts_study <- simOptions() + thematic_values <- opts_study$parameters$`variables selection` + testthat::expect_true(!is.null(thematic_values)) + testthat::expect_true(all( + c("selected_vars_reset", "select_var +") %in% + names(thematic_values) + )) + # control values + thematic_values <- unlist(thematic_values, use.names = FALSE) + res_read_active <- res_read[res_read$status_selection %in% "active",] + testthat::expect_true(all( + c("FALSE", res_read_active$variables)%in% + thematic_values + )) }) # delete study ---- From 17f9554e7aff0936fb5ca1d710cc6513b3d59a1d Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Fri, 10 Nov 2023 16:54:43 +0100 Subject: [PATCH 3/3] setThematicTrimming add api part code (use command with writeIni "update config") --- R/thematic_trimming.R | 72 +++++++++++++++++++++++++------------- man/setThematicTrimming.Rd | 2 +- 2 files changed, 48 insertions(+), 26 deletions(-) diff --git a/R/thematic_trimming.R b/R/thematic_trimming.R index 1224206d..201dd106 100644 --- a/R/thematic_trimming.R +++ b/R/thematic_trimming.R @@ -16,7 +16,7 @@ #' @examples #' \dontrun{ #' -#' # list of variables (from version v8.0 to v8.6) +#' # list of variables (from version v8.0 to version >= v8.6) #' vect_select_vars <- antaresRead:::pkgEnv$thematic #' #' ## @@ -34,7 +34,7 @@ setThematicTrimming <- function(selection_variables, type_select = c("add", "suppr"), opts = simOptions()){ - + # available only for study version >= 800 if(opts$antaresVersion<800) stop("'setThematicTrimming()' calls is only available for study version >= v8.0", @@ -74,7 +74,8 @@ setThematicTrimming <- function(selection_variables, generaldata_updated <- .set_thematic(type_select = type_select, pkg_var_version = variables_package_list, list_var_selection = selection_variables, - general_data = generaldata) + general_data = generaldata, + api_mode = antaresRead:::is_api_study(opts = opts)) # write updated file writeIni(listData = generaldata_updated, @@ -83,11 +84,20 @@ setThematicTrimming <- function(selection_variables, opts = opts) # Update simulation options object - suppressWarnings({ - res <- antaresRead::setSimulationPath(path = opts$studyPath, - simulation = "input") - }) - + if(antaresRead:::is_api_study(opts = opts)){ + suppressWarnings( + res <- antaresRead::setSimulationPathAPI(host = opts$host, + study_id = opts$study_id, + token = opts$token, + simulation = "input") + ) + }else{ + suppressWarnings( + res <- antaresRead::setSimulationPath(path = opts$studyPath, + simulation = "input") + ) + } + invisible(res) } @@ -99,7 +109,8 @@ setThematicTrimming <- function(selection_variables, .set_thematic <- function(type_select, pkg_var_version, list_var_selection, - general_data){ + general_data, + api_mode = FALSE){ # reset [variables selection] general_data$`variables selection` <- NULL @@ -126,32 +137,34 @@ setThematicTrimming <- function(selection_variables, # write your selection if(nb_right_var_selection= v8.6) vect_select_vars <- antaresRead:::pkgEnv$thematic ##