Skip to content

Commit

Permalink
resolve conflicts from "develop" + maj news.md
Browse files Browse the repository at this point in the history
Merge branch 'develop' of https://github.com/rte-antares-rpackage/antaresRead into release/v8.7.0

# Conflicts:
#	tests/testthat/test-readClusterDesc.R
  • Loading branch information
BERTHET Clement (Externe) committed Apr 4, 2024
2 parents b1aa89c + 06fdf64 commit db474a5
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 28 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
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* `readBindingConstraints()` read now Scenarized RHS for binding constraints (cf. Antares v8.7 changelog)
- function returns a new list structure
* Private function `fread_antares()` no longer returns warnings
* `api_put()/api_delete()` return a server error message

BUGFIXES :

Expand Down Expand Up @@ -34,7 +35,11 @@ BUGFIXES :
- returns the right column names for details-timeStep.txt and details-res-timeStep.txt
* Correction in `.formatlist()`, read N-level list instead of 2.

BREAKING CHANGES :

* `api_get()` has a new parameter to control JSON file parsing
* `readClusterDesc()`/ `readClusterRESDesc()` / `readClusterSTDesc()`
return empty dataTable and warning if no cluster in Antares study.

# antaresRead 2.6.0

Expand Down
21 changes: 15 additions & 6 deletions R/API-methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#' Can be a full URL (by wrapping ìn [I()]), in that case `default_endpoint` is ignored.
#' @param ... Additional arguments passed to API method.
#' @param default_endpoint Default endpoint to use.
#' @param parse_result `character` options for parameter `as` of function [httr::content()]
#' @param opts Antares simulation options or a `list` with an `host = ` slot.
#'
#' @return Response from the API.
Expand All @@ -18,13 +19,21 @@
#' \dontrun{
#'
#' # List studies with local API
#' api_get(
#' opts = list(host = "http://0.0.0.0:8080"),
#' endpoint = NULL
#' )
#' # default result content in R object (auto parsed)
#' api_get(opts = list(host = "http://0.0.0.0:8080"),
#' endpoint = NULL,
#' parse_result = NULL)
#'
#' # you can force parse options as text
#' api_get(opts = list(host = "http://0.0.0.0:8080"),
#' endpoint = NULL,
#' parse_result = "text")
#'
#' }
api_get <- function(opts, endpoint, ..., default_endpoint = "v1/studies") {
api_get <- function(opts,
endpoint, ...,
default_endpoint = "v1/studies",
parse_result = NULL) {
if (inherits(endpoint, "AsIs")) {
opts$host <- endpoint
endpoint <- NULL
Expand Down Expand Up @@ -65,7 +74,7 @@ api_get <- function(opts, endpoint, ..., default_endpoint = "v1/studies") {
stop_for_status(result, task = mess_error)
}else
warn_for_status(result)
content(result)
content(result, as = parse_result)
}

#' @export
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)
}
23 changes: 18 additions & 5 deletions man/API-methods.Rd

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

40 changes: 38 additions & 2 deletions tests/testthat/test-readClusterDesc.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,43 @@
path_study_test <- grep(pattern = "87", x = studyPathSV8, value = TRUE)
opts_study_test <- setSimulationPath(path_study_test, simulation = "input")

## v860 ----
# all version ----
#minimal columns
mandatory_cols <- c("area","cluster")

test_that("test read cluster st-storage v860", {
## 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

#read
input <- readClusterResDesc()

# tests
testthat::expect_true("data.table" %in% class(input))
testthat::expect_true(all(areas_res %in% unique(input$area)))
testthat::expect_true(all(mandatory_cols %in% colnames(input)))
testthat::expect_true(nrow(input) == length(input$cluster))
})

# v860 ----
## st-storage ----
test_that("test read cluster st-storage v860", {
# function setSimulationPath() provide areas names with st-storage clusters
areas_st <- opts_study_test$areasWithSTClusters

Expand All @@ -17,4 +50,7 @@ test_that("test read cluster st-storage v860", {
testthat::expect_true("data.table" %in% class(input_st))
testthat::expect_true(all(
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 db474a5

Please sign in to comment.