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 new file mode 100644 index 00000000..201dd106 --- /dev/null +++ b/R/thematic_trimming.R @@ -0,0 +1,219 @@ + +#' @title Set the thematic trimming of an Antares Study +#' +#' @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 version >= 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()){ + + # 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, + api_mode = antaresRead:::is_api_study(opts = opts)) + + # write updated file + writeIni(listData = generaldata_updated, + pathIni = "settings/generaldata", + overwrite = TRUE, + opts = opts) + + # Update simulation options object + 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) + +} + + +# 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, + api_mode = FALSE){ + # 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= 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 new file mode 100644 index 00000000..26b0df7a --- /dev/null +++ b/tests/testthat/test-thematic_trimming.R @@ -0,0 +1,196 @@ + +# for all version of study + # untar studies in temp files +setup_study_860(sourcedir860) +setup_study(studies, sourcedir) + +# v710 ---- + +test_that("set thematic trimming v710", { + # read study / load meta parameters + antaresRead::setSimulationPath(studyPath, "input") + + # list of variables + vect_select_vars <- antaresRead:::pkgEnv$thematic + + # setThematicTrimming() not available for version <800 + testthat::expect_error( + setThematicTrimming(selection_variables = vect_select_vars$variable), + regexp = "calls is only available for study version >= v8.0" + ) + + # delete study + unlink(x = studyPath, recursive = TRUE) +}) + + +# v860 ---- +## add variables ---- +test_that("set thematic trimming version >= 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") + + ## + # 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 ---- +unlink(x = study_temp_path, recursive = TRUE)