Skip to content

Commit

Permalink
readClusterSTDesc() reforge return function and api part + conversion…
Browse files Browse the repository at this point in the history
… list to date.frame changing columns names + test
  • Loading branch information
berthetclement committed Jun 19, 2024
1 parent e80d1cd commit 183d171
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 60 deletions.
75 changes: 27 additions & 48 deletions R/readClusterDesc.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,6 @@ readClusterSTDesc <- function(opts = simOptions()) {
.readClusterDesc <- function(opts = simOptions(),
dir = "thermal/clusters") {

if(isH5Opts(opts)){
if(dir %in% "thermal/clusters"){
if(.requireRhdf5_Antares(stopP = FALSE)){
return(h5ReadClusterDesc(opts))
} else {
stop(rhdf5_message, call. = FALSE)
}
} else {
stop("Read cluster Description from '", dir, "' not available using .h5", call. = FALSE)
}
}

path <- file.path(opts$inputPath, dir)
columns <- .generate_columns_by_type(dir = dir)
api_study <- is_api_study(opts)
Expand All @@ -117,55 +105,46 @@ readClusterSTDesc <- function(opts = simOptions()) {
"st-storage/clusters" = "st-storages"
)

# api request with all columns
list_clusters = api_get(
opts = opts,
endpoint = paste0(opts$study_id, "/table-mode/",table_type),
endpoint = paste0(opts$study_id, "/table-mode/", table_type),
query = list(
columns = ""
)
)
if(length(list_clusters) == 0){
mandatory_cols <- c("area","name")
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{
clusters <- rbindlist(list_clusters, idcol = "name")
newcol <- data.table()
newcol <- newcol[, c("area", "name") := tstrsplit(clusters$name, " / ",
fixed = TRUE,
keep = 1:2)]
res <- data.table(newcol,clusters[,-"name"])

}
}else{
areas <- list.files(path)
res <- ldply(areas, function(x) {
clusters <- readIniFile(file.path(path, x, "list.ini"))
if (length(clusters) == 0) return(NULL)
clusters <- ldply(clusters, as.data.frame)
clusters$.id <- NULL
clusters$area <- x
clusters[, c(ncol(clusters), 1:(ncol(clusters) - 1))]
})

return(list_clusters)
}

# "text" mode
areas <- list.files(path)

# read properties for each area
res <- llply(areas, function(x) {
clusters <- readIniFile(file.path(path, x, "list.ini"))
if (length(clusters) == 0)
return(NULL)
clusters <- ldply(clusters, data.frame) # check.names = FALSE (too many side effects)
clusters$.id <- NULL
clusters$area <- x
clusters[, c(ncol(clusters), 1:(ncol(clusters) - 1))]
})

res <- rbindlist(l = res, fill = TRUE)

if(length(res) == 0){
mandatory_cols <- c("area","name")
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)]
}
}
warning("No properties found",
call. = FALSE)
return(NULL)
}

res <- as.data.table(res)
setnames(res, "name", "cluster")
res$cluster <- as.factor(tolower(res$cluster))
res
}

.generate_columns_by_type <- function(dir = c("thermal/clusters", "renewables/clusters", "st-storage/clusters")) {

columns <- switch(
Expand Down
35 changes: 23 additions & 12 deletions tests/testthat/test-readClusterDesc.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
# read study ----
# latest version
path_study_test <- grep(pattern = "87", x = studyPathSV8, value = TRUE)
opts_study_test <- setSimulationPath(path_study_test, simulation = "input")

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

## Thermal ----
test_that("test read cluster", {
path_study_test <- studyPathS
opts_study_test <- setSimulationPath(path_study_test, simulation = "input")

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

# function setSimulationPath() provide areas names with st-storage clusters
areas <- opts_study_test$areasWithClusters

Expand All @@ -17,13 +16,19 @@ test_that("test read cluster", {

# tests
testthat::expect_true("data.table" %in% class(input))
testthat::expect_true(all(areas %in% unique(readClusterDesc()$area)))
testthat::expect_true(all(areas %in% unique(input$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", {
path_study_test <- grep(pattern = "87", x = studyPathSV8, value = TRUE)
opts_study_test <- setSimulationPath(path_study_test, simulation = "input")

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

# function setSimulationPath() provide areas names with st-storage clusters
areas_res <- opts_study_test$areasWithResClusters

Expand All @@ -40,6 +45,12 @@ test_that("test read cluster renewables", {
# v860 ----
## st-storage ----
test_that("test read cluster st-storage v860", {
path_study_test <- grep(pattern = "87", x = studyPathSV8, value = TRUE)
opts_study_test <- setSimulationPath(path_study_test, simulation = "input")

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

# function setSimulationPath() provide areas names with st-storage clusters
areas_st <- opts_study_test$areasWithSTClusters

Expand All @@ -56,9 +67,9 @@ test_that("test read cluster st-storage v860", {
})

# read empty study ----
path_empty_study <- setup_study_empty(sourcedir_empty_study)
opts_study_test <- setSimulationPath(path_empty_study, simulation = "input")

test_that("test when study has no cluster (empty)", {
path_empty_study <- setup_study_empty(sourcedir_empty_study)
opts_study_test <- setSimulationPath(path_empty_study, simulation = "input")

readClusterDesc()
})

0 comments on commit 183d171

Please sign in to comment.