Skip to content

Commit

Permalink
Column names for the details-timeStep.txt and details-res-timeStep.txt (
Browse files Browse the repository at this point in the history
#230)

* Add contributor and change version

* Changelog

* Add unit test for the column nmaes in output table

* Add .get_value_columns_details_file() to compute the column names of the output table

* Remove special characters to avoid warning

* Add control on the thematic-trimming parameter

* Factorization code

* Add some documentation for the function .get_value_columns_details_file()
  • Loading branch information
KKamel67 authored Feb 5, 2024
1 parent 7333bed commit 9901e21
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 29 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: antaresRead
Type: Package
Title: Import, Manipulate and Explore the Results of an 'Antares' Simulation
Version: 2.6.1
Version: 2.6.2
Authors@R: c(
person("Tatiana", "Vargas", email = "[email protected]", role = c("aut", "cre")),
person("Jalal-Edine", "ZAWAM", role = "aut"),
Expand All @@ -13,6 +13,7 @@ Authors@R: c(
person("Etienne", "Sanchez", role = "ctb"),
person("Assil", "Mansouri", role = "ctb"),
person("Clement", "Berthet", role = "ctb"),
person("Kamel", "Kemiha", role = "ctb"),
person("RTE", role = "cph")
)
Description: Import, manipulate and explore results generated by 'Antares', a
Expand Down
8 changes: 8 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
> Copyright © 2016 RTE Réseau de transport d’électricité
# antaresRead 2.6.2 (devlopment)

BUGFIXES :

* `readAntares()` :
- returns the right column names for details-timeStep.txt and details-res-timeStep.txt


# antaresRead 2.6.1 (devlopment)

BUGFIXES :
Expand Down
96 changes: 68 additions & 28 deletions R/importOutput.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#' is determined by the arguments "folder" and "file"
#' - "areas", "values" => areas
#' - "areas", "details" => clusters
#' - "areas", "details-res" => renewables clusters
#' - "links", "values" => links
#'
#' @return
Expand Down Expand Up @@ -277,6 +278,62 @@
)
}


#' .get_value_columns_details_file
#'
#' Private function used to get the column names for the details-timeStep.txt or details-res-timeStep.txt.
#' Used in .importOutputForClusters() and .importOutputForResClusters()
#' From the opts, we detect which outputs the user decides to take
#'
#' @return
#' a vector
#'
#' @noRd
#'
.get_value_columns_details_file <- function(opts, type) {

if(type == "details") {
# Order is important. There is a correspondance between elements.
all_thematic_variables <- c("DTG by plant", "NP Cost by plant", "NODU by plant")
colNames <- c("production", "NP Cost", "NODU")
if (opts$antaresVersion >= 830){
all_thematic_variables <- c(all_thematic_variables, "Profit by plant")
colNames <- c(colNames, "profit")
}
} else if(type == "details-res") {
# Order is important. There is a correspondance between elements.
all_thematic_variables <- c("RES generation by plant")
colNames <- c("production")
}
# With thematic-trimming enabled
if (opts$parameters$general$`thematic-trimming`) {
if ("variables selection" %in% names(opts$parameters)) {
var_selection <- opts$parameters$`variables selection`
selection_type <- unique(names(var_selection))
allowed_selection_type <- c("select_var -", "select_var +")
# Filter the vector to avoid other properties (for example : selected_vars_reset)
selection_type <- intersect(selection_type, allowed_selection_type)
# List with a repeated name
var_selection <- var_selection[which(names(var_selection) == selection_type)]
selected_variables <- unlist(var_selection, use.names = FALSE)
# Index of the variables found in the section "variables selection"
idx_vars <- which(all_thematic_variables %in% selected_variables)
if (length(idx_vars) > 0) {
if (selection_type == "select_var -") {
# vars to remove
colNames <- colNames[-idx_vars]
} else if (selection_type == "select_var +") {
# vars to keep
colNames <- colNames[idx_vars]
}
}
}
}

return(colNames)
}


#' .importOutputForClusters
#'
#' Private function used to import the output for the thermal clusters of one area
Expand All @@ -295,6 +352,7 @@
# To improve greatly the performance we use our knowledge of the position of
# the columns instead of using more general functions like dcast.
reshapeFun <- function(x) {

# Get cluster names
n <- names(x)
idx <- ! n %in% pkgEnv$idVars
Expand All @@ -304,28 +362,19 @@
idVarsId <- which(!idx)
idVarsNames <- n[idVarsId]

# Get final value columns
if (sum(idx) / length(clusterNames) == 4) {
colNames <- c("production", "NP Cost", "NODU", "profit")
} else if (sum(idx) / length(clusterNames) == 3) {
colNames <- c("production", "NP Cost", "NODU")
} else if (sum(idx) / length(clusterNames) == 2) {
colNames <- c("production", "NP Cost")
} else {
colNames <- c("production")
}
# Column names of the output table
colNames <- .get_value_columns_details_file(opts, "details")

# Loop over clusters
nclusters <- length(clusterNames)
ncols <- length(colNames)

res <- llply(1:nclusters, function(i) {
dt <- x[, c(nclusters * 0:(ncols - 1) + i, idVarsId), with = FALSE]
dt <- x[, c(nclusters * 0:(length(colNames) - 1) + i, idVarsId), with = FALSE]
setnames(dt, c(colNames, idVarsNames))
dt[, cluster := as.factor(clusterNames[i])]
dt
})

rbindlist(res)
}

Expand Down Expand Up @@ -436,9 +485,9 @@

res
}

}


#' .importOutputForResClusters
#'
#' Private function used to import the output for the renewable clusters of one area
Expand All @@ -457,6 +506,7 @@
# To improve greatly the performance we use our knowledge of the position of
# the columns instead of using more general functions like dcast.
reshapeFun <- function(x) {

# Get cluster names
n <- names(x)
idx <- ! n %in% pkgEnv$idVars
Expand All @@ -466,23 +516,14 @@
idVarsId <- which(!idx)
idVarsNames <- n[idVarsId]

# Get final value columns
# Get final value columns
# colNames <- c("resProduction")
if (sum(idx) / length(clusterNames) == 3) {
colNames <- c("production", "NP Cost", "NODU")
} else if (sum(idx) / length(clusterNames) == 2) {
colNames <- c("production", "NP Cost")
} else {
colNames <- c("production")
}
# Column names of the output table
colNames <- .get_value_columns_details_file(opts, "details-res")

# Loop over clusters
nclusters <- length(clusterNames)
ncols <- length(colNames)

res <- llply(1:nclusters, function(i) {
dt <- x[, c(nclusters * 0:(ncols - 1) + i, idVarsId), with = FALSE]
dt <- x[, c(nclusters * 0:(length(colNames) - 1) + i, idVarsId), with = FALSE]
setnames(dt, c(colNames, idVarsNames))
dt[, cluster := as.factor(clusterNames[i])]
dt
Expand All @@ -496,10 +537,9 @@
mcYears, showProgress, opts, reshapeFun, sameNames = FALSE,
objectDisplayName = "clustersRe", parallel = parallel)
)


}


#' .importOutputForBindingConstraints
#'
#' Private function used to import the output for binding constraints.
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/test-readAntares.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ sapply(studyPathS, function(studyPath){
expect_equal(nrow(clusters), 24 * 7 * nweeks * nrow(readClusterDesc()))
})

test_that("Clusters importation column names are ok", {
clusters <- readAntares(clusters = opts$areasWithClusters,
timeStep = "hourly",
mcYears = "all",
opts = opts,
showProgress = FALSE)
expect_is(clusters, "data.table")
expect_equal(setdiff(colnames(clusters),pkgEnv$idVars), c("production", "NP Cost", "NODU"))
})

test_that("importation of different objects works", {
out <- readAntares(areas = opts$areaList, links=opts$linkList,
clusters=opts$areasWithClusters, showProgress= FALSE, timeStep = "annual")
Expand Down

0 comments on commit 9901e21

Please sign in to comment.