Skip to content

Commit

Permalink
setPlaylist() in API mode optimization (#121)
Browse files Browse the repository at this point in the history
* Add new contributor

* Add changelog

* Add unit test for setPlaylist()

* Update setPlaylist() to minimize the number of queries in API mode

* Breaking changes in the right section

* Add elements for the changelog

* Refresh opts object at the end of the function

* Add unit test to check that opts is refreshed

---------

Co-authored-by: kemihak <kamel.kemiha@rte-france.com>
  • Loading branch information
KKamel67 and kemihak authored Jan 17, 2024
1 parent a416eaf commit e873cf0
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 9 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Authors@R: c(
person("Assil", "Mansouri", role = "ctb"),
person("Abdallah", "Mahoudi", role = "ctb"),
person("Clement", "Berthet", role = "ctb"),
person("Kamel", "Kemiha", role = "ctb"),
person("Nicolas", "Boitard", role = "ctb"),
person("RTE", role = "cph"))
Description: Edit an 'Antares' simulation before running it : create new areas, links, thermal
Expand Down
7 changes: 6 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ NEW FEATURES :

* Complete function `deleteStudy()` with new parameter `simulation` to delete a simulation in an Antares study

### Breaking changes

* `setPlaylist()` optimized for the API mode
- returned an updated list of simulation parameters returned by the function `setSimulationPath()` and `setSimulationPathAPI()`

# antaresEditObject 0.6.1

### Breaking changes

* `writeInputTS()` allows the user to set a link with the separator ' - ' (ex: 'area1 - area2')


BUGFIXES :

* Error CRAN CHECKS (fix issue #115)
Expand Down
41 changes: 33 additions & 8 deletions R/playlist.R
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ setPlaylist <- function(playlist,
weights = NULL,
opts = antaresRead::simOptions()) {

api_study <- is_api_study(opts)

version_study <- substr(opts$antaresVersion, 1, 1)
version_study <- as.numeric(version_study)

Expand Down Expand Up @@ -181,9 +183,11 @@ setPlaylist <- function(playlist,
if (length(playlist) == length(mc_years)) {
# update line to disable the playlist
generaldata$general$`user-playlist` <- FALSE
# write updated file
writeIni(listData = generaldata, pathIni = "settings/generaldata", overwrite = TRUE, opts = opts)

if (api_study) {
# To minimize the number of queries, we reduce the list to the updated items
generaldata <- generaldata[which(names(generaldata) == "general")]
generaldata$general[which(names(generaldata$general) != "user-playlist")] <- NULL
}
} else { # otherwise, set the playlist
# update line to enable the playlist
generaldata$general$`user-playlist` <- TRUE
Expand All @@ -193,7 +197,7 @@ setPlaylist <- function(playlist,

# create new playlist (+ patch double to integer)
new_playlist <- setNames(as.list(sort(as.integer(playlist - 1))), rep("playlist_year +", length(playlist)))
if (opts$typeLoad == "api"){
if (api_study){
new_playlist$sep <- ", "
new_playlist <- list("playlist_year +" = paste0("[", do.call(paste, new_playlist), "]"))
}
Expand All @@ -215,11 +219,32 @@ setPlaylist <- function(playlist,
), rep("playlist_year_weight", length(weights)))
)
}

if (api_study) {
# To minimize the number of queries, we reduce the list to the updated items
generaldata <- generaldata[which(names(generaldata) == "general")]
generaldata$general[which(names(generaldata$general) != "user-playlist")] <- NULL
}
# add new playlist to the parameters description
generaldata$playlist <- new_playlist

# write updated file
writeIni(listData = generaldata, pathIni = "settings/generaldata", overwrite = TRUE, opts = opts)
}

# write updated file
writeIni(listData = generaldata, pathIni = "settings/generaldata", overwrite = TRUE, opts = opts)

# Update simulation options object
if(api_study){
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")
)
}

return(invisible(res))
}
34 changes: 34 additions & 0 deletions tests/testthat/test-playlist.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,37 @@ sapply(studies, function(study) {
unlink(x = file.path(pathstd, "test_case"), recursive = TRUE)

})


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

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))

# Default behaviour
expect_false(opts$parameters$general$`user-playlist`)

# Set parameters
updateGeneralSettings(nbyears = 10)
suppressWarnings(opts <- setSimulationPath(opts$studyPath, simulation = "input"))
nbyears <- opts$parameters$general$nbyears
mc_years <- seq(nbyears)

# With custom playlist - user-playlist is enabled
playlist_to_write <- sort(sample(mc_years, size = nbyears/2, replace = FALSE))
opts <- setPlaylist(playlist = playlist_to_write, opts = opts)
playlist_in_file <- getPlaylist(opts = opts)

expect_true(opts$parameters$general$`user-playlist`)
playlist_opts <- opts$parameters$playlist
playlist_opts <- playlist_opts[which(names(playlist_opts) == "playlist_year +")]
expect_true(all(unlist(playlist_opts) %in% (playlist_to_write - 1)))
expect_equal(playlist_to_write, playlist_in_file)

# No playlist if length(playlist) == length(mcYear) - user-playlist is disabled
setPlaylist(playlist = mc_years, opts = opts)
suppressWarnings(opts <- setSimulationPath(opts$studyPath, simulation = "input"))

expect_false(opts$parameters$general$`user-playlist`)
})

0 comments on commit e873cf0

Please sign in to comment.