Skip to content

Commit

Permalink
Merge branch 'develop' into ant1490/bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Nekmek7 authored Apr 8, 2024
2 parents 1aacc84 + 06fdf64 commit c8e3d91
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 17 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test-coverage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ BREAKING CHANGES :
* `api_get()` has a new parameter to control JSON file parsing
* `readInputThermal()` default value when no time series in the selected clusters.
* `readInputRES()` default value when no time series in the selected clusters
* `readClusterDesc()`/ `readClusterRESDesc()` / `readClusterSTDesc()`
return empty dataTable and warning if no cluster in Antares study.

# antaresRead 2.6.0

Expand Down
46 changes: 35 additions & 11 deletions R/readClusterDesc.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ readClusterSTDesc <- function(opts = simOptions()) {
}



.readClusterDesc <- function(opts = simOptions(),
dir = "thermal/clusters") {

Expand All @@ -107,7 +106,11 @@ readClusterSTDesc <- function(opts = simOptions()) {

path <- file.path(opts$inputPath, dir)

if(opts$typeLoad == 'api'){
columns <- .generate_columns_by_type(dir = dir)
api_study <- is_api_study(opts)

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){
clusters <- rbindlist(
Expand All @@ -123,10 +126,6 @@ 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)

res <- res[, .SD, .SDcols = c("area", "name", "group", names(res)[!names(res) %in%c("area", "name", "group")])]

}else{

Expand All @@ -146,12 +145,37 @@ readClusterSTDesc <- function(opts = simOptions()) {

}

if(length(res) == 0) stop("Cannot find cluster description.", call. = FALSE)
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 <- 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)
}
40 changes: 38 additions & 2 deletions tests/testthat/test-readClusterDesc.R
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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))

})

0 comments on commit c8e3d91

Please sign in to comment.