Skip to content

Commit

Permalink
Avoid to send NULL value in API mode if weights are not provided (#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
KKamel67 authored Sep 26, 2024
1 parent 92f5927 commit 4f6a815
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 34 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 :
Expand Down
78 changes: 44 additions & 34 deletions R/playlist.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
}

Expand All @@ -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)) {
Expand All @@ -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")]
Expand All @@ -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")
Expand All @@ -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)
}
19 changes: 19 additions & 0 deletions man/dot-format_playlist_weights.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions tests/testthat/test-playlist.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
})

0 comments on commit 4f6a815

Please sign in to comment.