Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Column names for the details-timeStep.txt and details-res-timeStep.txt #230

Merged
merged 8 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")

Check warning on line 301 in R/importOutput.R

View check run for this annotation

Codecov / codecov/patch

R/importOutput.R#L300-L301

Added lines #L300 - L301 were not covered by tests
}
} else if(type == "details-res") {

Check warning on line 303 in R/importOutput.R

View check run for this annotation

Codecov / codecov/patch

R/importOutput.R#L303

Added line #L303 was not covered by tests
# Order is important. There is a correspondance between elements.
all_thematic_variables <- c("RES generation by plant")
colNames <- c("production")

Check warning on line 306 in R/importOutput.R

View check run for this annotation

Codecov / codecov/patch

R/importOutput.R#L305-L306

Added lines #L305 - L306 were not covered by tests
}
# 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 +")

Check warning on line 313 in R/importOutput.R

View check run for this annotation

Codecov / codecov/patch

R/importOutput.R#L310-L313

Added lines #L310 - L313 were not covered by tests
# Filter the vector to avoid other properties (for example : selected_vars_reset)
selection_type <- intersect(selection_type, allowed_selection_type)

Check warning on line 315 in R/importOutput.R

View check run for this annotation

Codecov / codecov/patch

R/importOutput.R#L315

Added line #L315 was not covered by tests
# List with a repeated name
var_selection <- var_selection[which(names(var_selection) == selection_type)]
selected_variables <- unlist(var_selection, use.names = FALSE)

Check warning on line 318 in R/importOutput.R

View check run for this annotation

Codecov / codecov/patch

R/importOutput.R#L317-L318

Added lines #L317 - L318 were not covered by tests
# 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 -") {

Check warning on line 322 in R/importOutput.R

View check run for this annotation

Codecov / codecov/patch

R/importOutput.R#L320-L322

Added lines #L320 - L322 were not covered by tests
# vars to remove
colNames <- colNames[-idx_vars]
} else if (selection_type == "select_var +") {

Check warning on line 325 in R/importOutput.R

View check run for this annotation

Codecov / codecov/patch

R/importOutput.R#L324-L325

Added lines #L324 - L325 were not covered by tests
# vars to keep
colNames <- colNames[idx_vars]

Check warning on line 327 in R/importOutput.R

View check run for this annotation

Codecov / codecov/patch

R/importOutput.R#L327

Added line #L327 was not covered by tests
}
}
}
}

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")

Check warning on line 520 in R/importOutput.R

View check run for this annotation

Codecov / codecov/patch

R/importOutput.R#L520

Added line #L520 was not covered by tests

# 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]

Check warning on line 526 in R/importOutput.R

View check run for this annotation

Codecov / codecov/patch

R/importOutput.R#L526

Added line #L526 was not covered by tests
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
Loading