From 4f6a815a227839c10d7bb27b6f5899e934f6b0d8 Mon Sep 17 00:00:00 2001 From: KKamel67 <58913912+KKamel67@users.noreply.github.com> Date: Thu, 26 Sep 2024 16:59:02 +0200 Subject: [PATCH] Avoid to send NULL value in API mode if weights are not provided (#189) --- NEWS.md | 1 + R/playlist.R | 78 +++++++++++++++++------------- man/dot-format_playlist_weights.Rd | 19 ++++++++ tests/testthat/test-playlist.R | 15 ++++++ 4 files changed, 79 insertions(+), 34 deletions(-) create mode 100644 man/dot-format_playlist_weights.Rd diff --git a/NEWS.md b/NEWS.md index 5c146da5..d613f742 100644 --- a/NEWS.md +++ b/NEWS.md @@ -28,6 +28,7 @@ BUGFIXES : * Enable control of matrix dimension in `.check_bulk_object_dim()` even if the values are not in first position in the list * `editLink()` : avoid *NULL* value (default) for arguments *filter_synthesis* and *filter_year_by_year* to write an empty string * `updateOutputSettings()` : in API mode, allow the user to edit the desired property +* `setPlaylist()`: do not send NULL value if the weights are not provided in argument OTHER UPDATES : diff --git a/R/playlist.R b/R/playlist.R index 4e4c9b79..b3b9b224 100644 --- a/R/playlist.R +++ b/R/playlist.R @@ -160,7 +160,7 @@ getPlaylist <- function(opts = antaresRead::simOptions()) { #' * `setPlaylist` does not return anything. It is used to modify the input of an Antares study. #' #' @importFrom assertthat assert_that -#' @importFrom antaresRead simOptions +#' @importFrom antaresRead simOptions readIni #' @export #' #' @rdname playlist @@ -171,9 +171,8 @@ setPlaylist <- function(playlist, api_study <- is_api_study(opts) version_study <- substr(opts$antaresVersion, 1, 1) - version_study <- as.numeric(version_study) - if (version_study < 8 & !is.null(weights)) { + if (as.numeric(version_study) < 8 & !is.null(weights)) { stop("weights can be use only for antares > V8, please convert your studie before") } @@ -192,9 +191,9 @@ setPlaylist <- function(playlist, playlist <- sort(playlist) playlist <- unique(playlist) - # read general data parameters generaldata <- readIni("settings/generaldata", opts = opts) + existing_playlist_weight <- "playlist_year_weight" %in% names(generaldata$playlist) # if all mc_years must be simulated, desactive playlist if (length(playlist) == length(mc_years)) { @@ -208,41 +207,27 @@ setPlaylist <- function(playlist, } else { # otherwise, set the playlist # update line to enable the playlist generaldata$general$`user-playlist` <- TRUE - # delete lines with current playlist generaldata$playlist <- NULL # create new playlist (+ patch double to integer) new_playlist <- setNames(as.list(sort(as.integer(playlist - 1))), rep("playlist_year +", length(playlist))) - if (api_study){ - + playlist_weights <- .format_playlist_weights(weights = weights, api_mode = api_study) + + if (api_study) { new_playlist$sep <- ", " new_playlist <- list("playlist_year +" = paste0("[", do.call(paste, new_playlist), "]")) - - couple_value <- paste(weights$mcYears - 1, weights$weights, sep = ",") - element_list <- paste("\'", couple_value, "\'", - collapse = ",", sep = "") - element_list <- paste("[", element_list, "]", sep = "") - new_playlist$playlist_year_weight <- element_list + if (existing_playlist_weight & is.null(playlist_weights)) { + weights <- data.table("mcYears" = playlist, "weights" = rep(1, length(playlist))) # 1 is the default weight + playlist_weights <- .format_playlist_weights(weights = weights, api_mode = TRUE) } - else if (!is.null(weights)) { - - new_playlist <- c( - new_playlist, - setNames(apply( - X = weights, - MARGIN = 1, - FUN = function(X) { - paste0( - X[1] - 1, - ",", - format(round(X[2], 6), nsmall = 6) - ) - } - ), rep("playlist_year_weight", length(weights$weights))) - ) + new_playlist[["playlist_year_weight"]] <- playlist_weights + } else { + new_playlist <- c(new_playlist, playlist_weights) } - new_playlist <- c(list(playlist_reset = FALSE), new_playlist) + + new_playlist <- c(list("playlist_reset" = FALSE), new_playlist) + if (api_study) { # To minimize the number of queries, we reduce the list to the updated items generaldata <- generaldata[which(names(generaldata) == "general")] @@ -252,18 +237,16 @@ setPlaylist <- function(playlist, generaldata$playlist <- new_playlist } - # write updated file writeIni(listData = generaldata, pathIni = "settings/generaldata", overwrite = TRUE, opts = opts) - # Update simulation options object - if(api_study){ + if (api_study) { suppressWarnings( res <- antaresRead::setSimulationPathAPI(host = opts$host, study_id = opts$study_id, token = opts$token, simulation = "input") ) - }else{ + } else { suppressWarnings( res <- antaresRead::setSimulationPath(path = opts$studyPath, simulation = "input") @@ -272,3 +255,30 @@ setPlaylist <- function(playlist, return(invisible(res)) } + + +#' Generate playlist_year_weight section in the appropriate format. +#' +#' @param weights +#' data.table, 2 columns : mcYears and weights. Only with after antares V8 +#' @param api_mode Boolean to identify an api study +#' +#' @return The playlist_year_weight section formatted. +#' +.format_playlist_weights <- function(weights, api_mode) { + + playlist_year_weight <- NULL + + if (!is.null(weights)) { + if (api_mode) { + playlist_year_weight <- paste0(weights$mcYears - 1, ",", weights$weights) + playlist_year_weight <- paste("\'", playlist_year_weight, "\'", collapse = ",", sep = "") + playlist_year_weight <- paste("[", playlist_year_weight, "]", sep = "") + } else { + playlist_year_weight <- paste0(weights$mcYears - 1, ",", format(round(weights$weights, 6), nsmall = 6)) + playlist_year_weight <- setNames(playlist_year_weight, rep("playlist_year_weight", length(playlist_year_weight))) + } + } + + return(playlist_year_weight) +} diff --git a/man/dot-format_playlist_weights.Rd b/man/dot-format_playlist_weights.Rd new file mode 100644 index 00000000..e33e101e --- /dev/null +++ b/man/dot-format_playlist_weights.Rd @@ -0,0 +1,19 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/playlist.R +\name{.format_playlist_weights} +\alias{.format_playlist_weights} +\title{Generate playlist_year_weight section in the appropriate format.} +\usage{ +.format_playlist_weights(weights, api_mode) +} +\arguments{ +\item{weights}{data.table, 2 columns : mcYears and weights. Only with after antares V8} + +\item{api_mode}{Boolean to identify an api study} +} +\value{ +The playlist_year_weight section formatted. +} +\description{ +Generate playlist_year_weight section in the appropriate format. +} diff --git a/tests/testthat/test-playlist.R b/tests/testthat/test-playlist.R index f2cbaa82..84292c27 100644 --- a/tests/testthat/test-playlist.R +++ b/tests/testthat/test-playlist.R @@ -84,3 +84,18 @@ test_that("Check if setPlaylist() is disabled when playlist = mcYear and enabled expect_false(opts$parameters$general$`user-playlist`) }) + + +test_that("Check the general behaviour of .format_playlist_weights()", { + + weights <- data.table("mcYears" = c(14,27,28), "weights" = c(1,2,3)) + + res <- .format_playlist_weights(weights = NULL, api_mode = TRUE) + expect_null(res) + res <- .format_playlist_weights(weights = NULL, api_mode = FALSE) + expect_null(res) + res <- .format_playlist_weights(weights = weights, api_mode = TRUE) + expect_equal(res, "['13,1','26,2','27,3']") + res <- .format_playlist_weights(weights = weights, api_mode = FALSE) + expect_equal(res, c("playlist_year_weight" = "13,1.000000", "playlist_year_weight" = "26,2.000000","playlist_year_weight" = "27,3.000000")) +})