From c14be111e655548b8a5e86d98bee2fb06f000644 Mon Sep 17 00:00:00 2001 From: Mahabd <104979479+Nekmek7@users.noreply.github.com> Date: Fri, 15 Mar 2024 14:45:25 +0100 Subject: [PATCH 1/4] Replace stop by warning readClusterDesc (#237) * change stop by warning and return same data table in local and API mode --- NEWS.md | 1 + R/readClusterDesc.R | 19 +++++++++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/NEWS.md b/NEWS.md index d22d25bf..7c2bef39 100644 --- a/NEWS.md +++ b/NEWS.md @@ -15,6 +15,7 @@ BUGFIXES : BREAKING CHANGES : * `api_get()` has a new parameter to control JSON file parsing +* `readClusterDesc()` return empty dataTable and warning if no cluster in Antares study. # antaresRead 2.6.0 diff --git a/R/readClusterDesc.R b/R/readClusterDesc.R index 4f0e9195..15bc4039 100644 --- a/R/readClusterDesc.R +++ b/R/readClusterDesc.R @@ -107,6 +107,12 @@ readClusterSTDesc <- function(opts = simOptions()) { path <- file.path(opts$inputPath, dir) + columns = c("group","enabled","must_run","unit_count","nominal_capacity", + "min_stable_power","spinning","min_up_time","min_down_time", + "co2","marginal_cost","fixed_cost","startup_cost","market_bid_cost", + "spread_cost","ts_gen","volatility_forced","volatility_planned", + "law_forced","law_planned") + if(opts$typeLoad == 'api'){ jsoncld <- read_secure_json(paste0(path, "&depth=4"), token = opts$token, timeout = opts$timeout, config = opts$httr_config) res <- rbindlist(mapply(function(X1, Y1){ @@ -123,9 +129,11 @@ readClusterSTDesc <- function(opts = simOptions()) { clusters[, .SD, .SDcols = order(names(clusters))] },jsoncld, names(jsoncld), SIMPLIFY = FALSE), fill = TRUE) - if(length(res) == 0) - stop("Cannot find cluster description.", call. = FALSE) - + if(length(res) == 0){ + warning("No cluster description available.", call. = FALSE) + res <- setNames(data.table(matrix(nrow = 0, ncol = 22)), c("area", "cluster", columns)) + } + res <- res[, .SD, .SDcols = c("area", "name", "group", names(res)[!names(res) %in%c("area", "name", "group")])] }else{ @@ -146,7 +154,10 @@ readClusterSTDesc <- function(opts = simOptions()) { } - if(length(res) == 0) stop("Cannot find cluster description.", call. = FALSE) + if(length(res) == 0){ + warning("No cluster description available.", call. = FALSE) + res <- setNames(data.table(matrix(nrow = 0, ncol = 22)), c("area", "cluster", columns)) + } res <- as.data.table(res) setnames(res, "name", "cluster") From e54b25fe6d551f6ce1fe1ad4b49d00f2ba28d14d Mon Sep 17 00:00:00 2001 From: Mahabd <104979479+Nekmek7@users.noreply.github.com> Date: Thu, 28 Mar 2024 11:52:27 +0100 Subject: [PATCH 2/4] fix bug readClusterDesc when no cluster + cover ST and RES case (#241) * fix bug readClusterDesc when no cluster + cover ST and RES case --- NEWS.md | 3 ++- R/readClusterDesc.R | 49 ++++++++++++++++++++++++++++----------------- 2 files changed, 33 insertions(+), 19 deletions(-) diff --git a/NEWS.md b/NEWS.md index 7c2bef39..4cd70501 100644 --- a/NEWS.md +++ b/NEWS.md @@ -15,7 +15,8 @@ BUGFIXES : BREAKING CHANGES : * `api_get()` has a new parameter to control JSON file parsing -* `readClusterDesc()` return empty dataTable and warning if no cluster in Antares study. +* `readClusterDesc()`/ `readClusterRESDesc()` / `readClusterSTDesc()` +return empty dataTable and warning if no cluster in Antares study. # antaresRead 2.6.0 diff --git a/R/readClusterDesc.R b/R/readClusterDesc.R index 15bc4039..71caec89 100644 --- a/R/readClusterDesc.R +++ b/R/readClusterDesc.R @@ -89,7 +89,6 @@ readClusterSTDesc <- function(opts = simOptions()) { } - .readClusterDesc <- function(opts = simOptions(), dir = "thermal/clusters") { @@ -107,13 +106,10 @@ readClusterSTDesc <- function(opts = simOptions()) { path <- file.path(opts$inputPath, dir) - columns = c("group","enabled","must_run","unit_count","nominal_capacity", - "min_stable_power","spinning","min_up_time","min_down_time", - "co2","marginal_cost","fixed_cost","startup_cost","market_bid_cost", - "spread_cost","ts_gen","volatility_forced","volatility_planned", - "law_forced","law_planned") + columns <- .generate_columns_by_type(dir = dir) if(opts$typeLoad == 'api'){ + jsoncld <- read_secure_json(paste0(path, "&depth=4"), token = opts$token, timeout = opts$timeout, config = opts$httr_config) res <- rbindlist(mapply(function(X1, Y1){ clusters <- rbindlist( @@ -131,10 +127,11 @@ readClusterSTDesc <- function(opts = simOptions()) { if(length(res) == 0){ warning("No cluster description available.", call. = FALSE) - res <- setNames(data.table(matrix(nrow = 0, ncol = 22)), c("area", "cluster", columns)) + res <- setNames(data.table(matrix(nrow = 0, ncol = 2 + length(columns))), c("area", "cluster", columns)) + }else{ + res <- res[, .SD, .SDcols = c("area", "name", "group", names(res)[!names(res) %in%c("area", "name", "group")])] } - - res <- res[, .SD, .SDcols = c("area", "name", "group", names(res)[!names(res) %in%c("area", "name", "group")])] + }else{ @@ -152,17 +149,33 @@ readClusterSTDesc <- function(opts = simOptions()) { clusters[, c(ncol(clusters), 1:(ncol(clusters) - 1))] }) + if(length(res) == 0){ + warning("No cluster description available.", call. = FALSE) + res <- setNames(data.table(matrix(nrow = 0, ncol = 2 + length(columns))), c("area", "cluster", columns)) + }else{ + res <- as.data.table(res) + setnames(res, "name", "cluster") + + res$cluster <- as.factor(tolower(res$cluster)) + } } - if(length(res) == 0){ - warning("No cluster description available.", call. = FALSE) - res <- setNames(data.table(matrix(nrow = 0, ncol = 22)), c("area", "cluster", columns)) - } - - res <- as.data.table(res) - setnames(res, "name", "cluster") + res +} + +.generate_columns_by_type <- function(dir = c("thermal/clusters", "renewables/clusters", "st-storage/clusters")) { - res$cluster <- as.factor(tolower(res$cluster)) - res + columns <- switch( + dir, + "thermal/clusters" = c("group","enabled","must_run","unit_count","nominal_capacity", + "min_stable_power","spinning","min_up_time","min_down_time", + "co2","marginal_cost","fixed_cost","startup_cost","market_bid_cost", + "spread_cost","ts_gen","volatility_forced","volatility_planned", + "law_forced","law_planned"), + + "renewables/clusters" = c("group","ts_interpretation","enabled","unit_count","nominal_capacity") + #"st-storage/clusters" = #ATTENTE DEV COTé API + ) + return(columns) } From a20a7213231c0421745aea4170194781d57e0832 Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Thu, 28 Mar 2024 15:56:28 +0100 Subject: [PATCH 3/4] update github workflow --- .github/workflows/R-CMD-check.yaml | 4 ++-- .github/workflows/test-coverage.yaml | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index 1474613d..1634e76c 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -2,9 +2,9 @@ # Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help on: push: - branches: [main, master] + branches: [main, master, develop] pull_request: - branches: [main, master] + branches: [main, master, develop] name: R-CMD-check diff --git a/.github/workflows/test-coverage.yaml b/.github/workflows/test-coverage.yaml index 3ae39f94..3cd19899 100644 --- a/.github/workflows/test-coverage.yaml +++ b/.github/workflows/test-coverage.yaml @@ -2,9 +2,9 @@ # Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help on: push: - branches: [main, master] + branches: [main, master, develop] pull_request: - branches: [main, master] + branches: [main, master, develop] name: test-coverage From 06fdf644d84538da8c990f7670c78b50c44cf484 Mon Sep 17 00:00:00 2001 From: Mahabd <104979479+Nekmek7@users.noreply.github.com> Date: Tue, 2 Apr 2024 14:44:20 +0200 Subject: [PATCH 4/4] Read cluster desc bugfix when cluster exist (#242) * fix bug readClusterDesc when no cluster + cover ST and RES case * add test (init test for thermal + res) --- R/readClusterDesc.R | 32 ++++++++++----------- tests/testthat/test-readClusterDesc.R | 40 +++++++++++++++++++++++++-- 2 files changed, 54 insertions(+), 18 deletions(-) diff --git a/R/readClusterDesc.R b/R/readClusterDesc.R index 71caec89..dad0515e 100644 --- a/R/readClusterDesc.R +++ b/R/readClusterDesc.R @@ -107,8 +107,9 @@ readClusterSTDesc <- function(opts = simOptions()) { path <- file.path(opts$inputPath, dir) columns <- .generate_columns_by_type(dir = dir) + api_study <- is_api_study(opts) - if(opts$typeLoad == 'api'){ + if(api_study){ jsoncld <- read_secure_json(paste0(path, "&depth=4"), token = opts$token, timeout = opts$timeout, config = opts$httr_config) res <- rbindlist(mapply(function(X1, Y1){ @@ -125,13 +126,6 @@ readClusterSTDesc <- function(opts = simOptions()) { clusters[, .SD, .SDcols = order(names(clusters))] },jsoncld, names(jsoncld), SIMPLIFY = FALSE), fill = TRUE) - if(length(res) == 0){ - warning("No cluster description available.", call. = FALSE) - res <- setNames(data.table(matrix(nrow = 0, ncol = 2 + length(columns))), c("area", "cluster", columns)) - }else{ - res <- res[, .SD, .SDcols = c("area", "name", "group", names(res)[!names(res) %in%c("area", "name", "group")])] - } - }else{ @@ -149,15 +143,21 @@ readClusterSTDesc <- function(opts = simOptions()) { clusters[, c(ncol(clusters), 1:(ncol(clusters) - 1))] }) - if(length(res) == 0){ - warning("No cluster description available.", call. = FALSE) - res <- setNames(data.table(matrix(nrow = 0, ncol = 2 + length(columns))), c("area", "cluster", columns)) - }else{ - res <- as.data.table(res) - setnames(res, "name", "cluster") - - res$cluster <- as.factor(tolower(res$cluster)) + } + + if(length(res) == 0){ + mandatory_cols <- c("area","cluster") + warning("No cluster description available.", call. = FALSE) + res <- setNames(data.table(matrix(nrow = 0, ncol = length(mandatory_cols) + length(columns))), c(mandatory_cols, columns)) + }else{ + if(api_study){ + mandatory_cols <- c("area", "name", "group") + additional_cols <- setdiff(colnames(res),mandatory_cols) + res <- res[, .SD, .SDcols = c(mandatory_cols, additional_cols)] } + res <- as.data.table(res) + setnames(res, "name", "cluster") + res$cluster <- as.factor(tolower(res$cluster)) } res diff --git a/tests/testthat/test-readClusterDesc.R b/tests/testthat/test-readClusterDesc.R index f62dd764..c7d14398 100644 --- a/tests/testthat/test-readClusterDesc.R +++ b/tests/testthat/test-readClusterDesc.R @@ -1,9 +1,42 @@ -## v860 ---- +# v860 ---- path_study_test <- grep(pattern = "86", x = studyPathSV8, value = TRUE) opts_study_test <- setSimulationPath(path_study_test, simulation = "input") +#minimal columns +mandatory_cols <- c("area","cluster") + +## Thermal ---- +test_that("test read cluster", { + + # function setSimulationPath() provide areas names with st-storage clusters + areas <- opts_study_test$areasWithClusters + + # read clusters informations + input <- readClusterDesc() + + # tests + testthat::expect_true("data.table" %in% class(input)) + testthat::expect_true(all(areas %in% unique(readClusterDesc()$area))) + testthat::expect_true(all(mandatory_cols %in% colnames(input))) + testthat::expect_true(nrow(input) == length(input$cluster)) + +}) + +## Renewables ---- +test_that("test read cluster renewables", { + + # function setSimulationPath() provide areas names with st-storage clusters + areas_res <- opts_study_test$areasWithRESClusters + + #Study not renewables (need activateRES() from antaresEditObject) + expect_error(readClusterResDesc(), + regexp = "readClusterDesc is available only on studies with 'renewable-generation-modelling'") + +}) + +## st-storage ---- test_that("test read cluster st-storage v860", { # function setSimulationPath() provide areas names with st-storage clusters @@ -14,5 +47,8 @@ test_that("test read cluster st-storage v860", { # tests testthat::expect_true("data.table" %in% class(input_st)) - testthat::expect_true(areas_st %in% unique(readClusterSTDesc()$area)) + testthat::expect_true(all(areas_st %in% unique(readClusterSTDesc()$area))) + testthat::expect_true(all(mandatory_cols %in% colnames(input_st))) + testthat::expect_true(nrow(input_st) == length(input_st$cluster)) + })