Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug Setplaylist #130

Merged
merged 4 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ NEW FEATURES :
BUGFIXES :

* Fix `filter_synthesis` and `filter_year_by_year` parameters of `editLink()` in API mode
* Fix `setPlaylist()` works in API and local mode with weights.
* Fix `getPlaylist()` works in API and local mode with weights.
* Fix `createDSR()` in API mode : daily binding constraint takes 366 rows


Expand Down
78 changes: 51 additions & 27 deletions R/playlist.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@
#' setPlaylist(c(3, 5, 7))
#' }
getPlaylist <- function(opts = antaresRead::simOptions()) {
assertthat::assert_that(inherits(opts, "simOptions"))

assertthat::assert_that(inherits(opts, "simOptions"))
api_study <- is_api_study(opts)
# read general parameters
parameters <- readIni("settings/generaldata", opts = opts)

Expand All @@ -58,18 +59,29 @@
# otherwise, update the vector of mc_years by removing disabled years
playlist_update_type <- names(parameters$playlist)
playlist_update_value <- parameters$playlist

for (type in playlist_update_type){
no_uptdate <- grepl(pattern = "reset", type)
if (!no_uptdate){
transform_values <- gsub(pattern = "\\[|\\]",
x = playlist_update_value[[type]],
replacement = "")
transform_values <- unlist(strsplit(x = transform_values, split = "\\,"))

playlist_update_value[[type]] <- as.integer(transform_values)
}
if (api_study){

for (type in playlist_update_type){
no_uptdate <- grepl(pattern = "reset", type)
if (!no_uptdate){
if (type == "playlist_year +"){
transform_values <- gsub(pattern = "\\[|\\]",
x = playlist_update_value[[type]],
replacement = "")
transform_values <- unlist(strsplit(x = transform_values, split = "\\,"))

Check warning on line 71 in R/playlist.R

View check run for this annotation

Codecov / codecov/patch

R/playlist.R#L64-L71

Added lines #L64 - L71 were not covered by tests
}
else if (type == "playlist_year_weight"){
transform_values <- gsub(pattern = "\\['|'|'\\]",
x = playlist_update_value[[type]],
replacement = "")
transform_values <- unlist(strsplit(x = transform_values, split = "\\,"))

Check warning on line 77 in R/playlist.R

View check run for this annotation

Codecov / codecov/patch

R/playlist.R#L73-L77

Added lines #L73 - L77 were not covered by tests
}


playlist_update_value[[type]] <- as.numeric(transform_values)

Check warning on line 81 in R/playlist.R

View check run for this annotation

Codecov / codecov/patch

R/playlist.R#L81

Added line #L81 was not covered by tests
}

}
}

# untouched playlist - no modification have been made
Expand Down Expand Up @@ -112,14 +124,19 @@
return(activate_mc)
} else{
vect_value_weigth = unlist(playlist_update_value[names(playlist_update_value) == "playlist_year_weight"])
mat_play_list <-
data.table(t(cbind.data.frame(strsplit(
vect_value_weigth, ","
))))
mat_play_list$V1 <- as.numeric(mat_play_list$V1) + 1
mat_play_list$V2 <- as.numeric(mat_play_list$V2)
setnames(mat_play_list, "V1", "mcYears")
setnames(mat_play_list, "V2", "weights")
if(api_study){
mcYears <- vect_value_weigth[seq(1, length(vect_value_weigth), by = 2)] + 1
weights <- vect_value_weigth[seq(2, length(vect_value_weigth), by = 2)]
mat_play_list <- data.table(mcYears, weights)

Check warning on line 130 in R/playlist.R

View check run for this annotation

Codecov / codecov/patch

R/playlist.R#L128-L130

Added lines #L128 - L130 were not covered by tests
}
else {
mat_play_list <- data.table(t(cbind.data.frame(
strsplit(vect_value_weigth, ","))))
mat_play_list$V1 <- as.numeric(mat_play_list$V1) + 1
mat_play_list$V2 <- as.numeric(mat_play_list$V2)
setnames(mat_play_list, "V1", "mcYears")
setnames(mat_play_list, "V2", "weights")
}
return(list(activate_mc = activate_mc, weights = mat_play_list))
}
}
Expand Down Expand Up @@ -198,16 +215,22 @@
# 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){

new_playlist$sep <- ", "
new_playlist <- list("playlist_year +" = paste0("[", do.call(paste, new_playlist), "]"))
}
new_playlist <- c(list(playlist_reset = FALSE), new_playlist)

if (!is.null(weights)) {

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

Check warning on line 226 in R/playlist.R

View check run for this annotation

Codecov / codecov/patch

R/playlist.R#L222-L226

Added lines #L222 - L226 were not covered by tests
}
else if (!is.null(weights)) {

new_playlist <- c(
new_playlist,
new_playlist,
setNames(apply(
X = weights,
X = weights,
MARGIN = 1,
FUN = function(X) {
paste0(
Expand All @@ -216,9 +239,10 @@
format(round(X[2], 6), nsmall = 6)
)
}
), rep("playlist_year_weight", length(weights)))
), rep("playlist_year_weight", length(weights$weights)))
)
}
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 Down
22 changes: 22 additions & 0 deletions tests/testthat/test-playlist.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,28 @@ sapply(studies, function(study) {
})


test_that("Update playlist with weight", {

ant_version <- "8.2.0"
st_test <- paste0("my_study_820_", paste0(sample(letters,5),collapse = ""))
suppressWarnings(opts <- createStudy(path = pathstd, study_name = st_test, antares_version = ant_version))

updateGeneralSettings(nbyears = 10)
suppressWarnings(opts <- setSimulationPath(opts$studyPath, simulation = "input"))

mcYears = sample(1:5,3)
weights = c(0.1,0.4,0.5)
dfw = data.frame(mcYears, weights)

setPlaylist(playlist = mcYears, weights = dfw, opts = opts)
expect_length(getPlaylist(opts = opts), 2)

expect_equal(getPlaylist(opts = opts)$activate_mc, sort(mcYears))
expect_equal(getPlaylist(opts = opts)$weights$mcYears, mcYears)
expect_equal(getPlaylist(opts = opts)$weights$weights, weights)

})

test_that("Check if setPlaylist() is disabled when playlist = mcYear and enabled when playlist is customized", {

ant_version <- "8.2.0"
Expand Down
Loading