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

Ant843 bis #228

Closed
wants to merge 4 commits into from
Closed
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
84 changes: 59 additions & 25 deletions R/importOutput.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
colname
}


#' .importOutput
#'
#' Private function used to import the results of a simulation. The type of result
Expand Down Expand Up @@ -277,6 +278,55 @@
)
}


#' .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()
#'
#' @return
#' a vector
#'
#' @noRd
#'
.get_value_columns_details_file <- function(opts, type) {

## details part
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")
all_output_colnames <- c("production", "NP Cost", "NODU")
if (opts$antaresVersion >= 830){
all_thematic_variables <- c(all_thematic_variables, "Profit by plant")
all_output_colnames <- c(all_output_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
}
colNames <- all_output_colnames
if ("variables selection" %in% names(opts$parameters)) {
selection_type <- unique(names(opts$parameters$`variables selection`))
selected_variables <- unlist(opts$parameters$`variables selection`, use.names = FALSE)

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
# 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 310 in R/importOutput.R

View check run for this annotation

Codecov / codecov/patch

R/importOutput.R#L308-L310

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

Check warning on line 313 in R/importOutput.R

View check run for this annotation

Codecov / codecov/patch

R/importOutput.R#L312-L313

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

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
}
}
}
}

## details-res part
if(type == "details-res") {
colNames <- c("production")

Check warning on line 323 in R/importOutput.R

View check run for this annotation

Codecov / codecov/patch

R/importOutput.R#L323

Added line #L323 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 +345,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 +355,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 @@ -457,6 +499,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 +509,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 513 in R/importOutput.R

View check run for this annotation

Codecov / codecov/patch

R/importOutput.R#L513

Added line #L513 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 519 in R/importOutput.R

View check run for this annotation

Codecov / codecov/patch

R/importOutput.R#L519

Added line #L519 was not covered by tests
setnames(dt, c(colNames, idVarsNames))
dt[, cluster := as.factor(clusterNames[i])]
dt
Expand Down
6 changes: 6 additions & 0 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ utils::globalVariables(
"MRG. PRICE", "H. LEV", "V2", "V1")
)

# Correspondance between output column name and data table column name
pkgEnv$output_correspondance <- read.table(system.file("format_output/referentiel_output_name_column.csv",
package = "antaresRead"),
sep = ";",
header = TRUE)

#----------------------------- HDF5 ------------------------------------#


Expand Down
6 changes: 6 additions & 0 deletions inst/format_output/referentiel_output_name_column.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ANTARES_OUTPUT_TYPE;ANTARES_OUTPUT_FILE_COLUMN_NAME;ANTARES_OUTPUT_R_VARIABLE;ANTARES_OUTPUT_ORDINAL_POSITION
clusters;MWh;production;1
clusters;NP Cost - Euro;NP Cost;2
clusters;NODU;NODU;3
clusters;Profit - Euro;profit;4
res_clusters;MWh;production;1
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