Skip to content

Commit

Permalink
In API mode, convert list to data.table (#255)
Browse files Browse the repository at this point in the history
* Return data.table in API mode for .readClusterDesc()

* Define function .convert_list_clusterDesc_to_datatable() and call it in .readClusterDesc()
  • Loading branch information
KKamel67 authored Aug 13, 2024
1 parent bdb77fe commit 1036461
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ BUGFIXES :

* `readInputThermal()` return data from file data.txt with `thermalData` parameter
* `setSimulationPath()` has also the parameter **areasWithSTClusters** in 'output' mode

* `readClusterDesc()` / `readClusterResDesc()` / `readClusterSTDesc()` return a data.table in API mode

# antaresRead 2.7.0

Expand Down
41 changes: 33 additions & 8 deletions R/readClusterDesc.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,17 @@ readClusterSTDesc <- function(opts = simOptions()) {
"st-storage/clusters" = "st-storages"
)

if(api_study){

if (api_study) {
# api request with all columns
list_clusters = api_get(
list_clusters <- api_get(
opts = opts,
endpoint = paste0(opts$study_id, "/table-mode/", table_type),
query = list(
columns = ""
)
query = list(columns = "")
)

return(list_clusters)
dt_clusters <- .convert_list_clusterDesc_to_datatable(list_clusters)

return(dt_clusters)
}

# "text" mode
Expand All @@ -124,7 +123,7 @@ readClusterSTDesc <- function(opts = simOptions()) {
# READ cluster properties
properties <- get_input_cluster_properties(table_type = table_type,
opts = opts)

# read properties for each area
res <- plyr::llply(areas, function(x, prop_ref=properties) {
clusters <- readIniFile(file.path(path, x, "list.ini"))
Expand Down Expand Up @@ -225,3 +224,29 @@ get_input_cluster_properties <- function(table_type, opts){

return(wide_ref)
}


.convert_list_clusterDesc_to_datatable <- function(list_clusters) {

if (length(list_clusters) == 0) {
return(data.table())
}

rows_cluster <- lapply(names(list_clusters), FUN = function(cl_name) {

row_cluster <- as.data.frame(list_clusters[[cl_name]])
row_cluster[,c("area", "cluster")] <- unlist(strsplit(cl_name, split = " / "))

return(row_cluster)
}
)

df_clusters <- do.call("rbind", rows_cluster)
id_cols <- intersect(c("area", "cluster", "group"), colnames(df_clusters))
additional_cols <- setdiff(colnames(df_clusters), id_cols)
df_clusters <- df_clusters[,c(id_cols, additional_cols)]
df_clusters$cluster <- as.factor(tolower(df_clusters$cluster))
colnames(df_clusters) <- tolower(colnames(df_clusters))

return(as.data.table(df_clusters))
}

0 comments on commit 1036461

Please sign in to comment.