From 42813ee80e733d0b95a52200a548c579900ef690 Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Mon, 15 Jan 2024 11:32:43 +0100 Subject: [PATCH] readBindingConstraints reforge with private functions summary.bindingConstraints add badge "deprecated" for versions >= v2.7.0 fread_antares update to suppress warnings --- NAMESPACE | 239 +++++++++--------- NEWS.md | 7 +- R/readBindingConstraints.R | 396 ++++++++++++++++-------------- R/utils_api.R | 8 +- man/readBindingConstraints.Rd | 9 - man/summary.bindingConstraints.Rd | 19 ++ 6 files changed, 356 insertions(+), 322 deletions(-) create mode 100644 man/summary.bindingConstraints.Rd diff --git a/NAMESPACE b/NAMESPACE index 1badafdd..a3692c35 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,119 +1,120 @@ -# Generated by roxygen2: do not edit by hand - -S3method("[",antaresDataTable) -S3method(as.antaresDataList,antaresDataList) -S3method(as.antaresDataList,antaresDataTable) -S3method(as.antaresDataList,data.frame) -S3method(as.antaresDataList,default) -S3method(as.antaresDataList,list) -S3method(as.antaresDataTable,antaresDataTable) -S3method(as.antaresDataTable,data.frame) -S3method(as.antaresDataTable,default) -S3method(copyToClipboard,antaresDataList) -S3method(copyToClipboard,data.frame) -S3method(copyToClipboard,default) -S3method(copyToClipboard,matrix) -S3method(merge,antaresDataTable) -S3method(print,antaresDataList) -S3method(print,antaresDataTable) -S3method(print,simOptions) -S3method(subset,antaresDataList) -S3method(summary,bindingConstraints) -S3method(viewAntares,antaresDataList) -S3method(viewAntares,default) -export(.getOptionsH5) -export(.h5ReadAntares) -export(.writeAntaresH5Fun) -export(aggregateResult) -export(api_delete) -export(api_get) -export(api_post) -export(api_put) -export(as.antaresDataList) -export(as.antaresDataTable) -export(changeTimeStep) -export(copyToClipboard) -export(extractDataList) -export(getAlias) -export(getAreas) -export(getDistricts) -export(getGeographicTrimming) -export(getIdCols) -export(getLinks) -export(hvdcModification) -export(isH5Opts) -export(mergeDigests) -export(parAggregateMCall) -export(ponderateMcAggregation) -export(readAntares) -export(readAntaresAreas) -export(readAntaresClusters) -export(readBindingConstraints) -export(readClusterDesc) -export(readClusterResDesc) -export(readClusterSTDesc) -export(readDigestFile) -export(readIni) -export(readIniAPI) -export(readIniFile) -export(readInputRES) -export(readInputTS) -export(readInputThermal) -export(readLayout) -export(readOptimCriteria) -export(removeVirtualAreas) -export(setAlias) -export(setHvdcAreas) -export(setRam) -export(setSimulationPath) -export(setSimulationPathAPI) -export(setTimeoutAPI) -export(showAliases) -export(simOptions) -export(viewAntares) -export(writeAntaresH5) -export(writeDigest) -import(bit64) -import(data.table) -import(doParallel) -import(jsonlite) -import(parallel) -import(plyr) -importFrom(doParallel,registerDoParallel) -importFrom(grDevices,col2rgb) -importFrom(grDevices,rgb) -importFrom(httr,DELETE) -importFrom(httr,GET) -importFrom(httr,POST) -importFrom(httr,PUT) -importFrom(httr,accept_json) -importFrom(httr,add_headers) -importFrom(httr,content) -importFrom(httr,content_type_json) -importFrom(httr,http_status) -importFrom(httr,stop_for_status) -importFrom(httr,timeout) -importFrom(memuse,Sys.meminfo) -importFrom(methods,is) -importFrom(pbapply,pblapply) -importFrom(pbapply,pboptions) -importFrom(plyr,llply) -importFrom(purrr,quietly) -importFrom(shiny,getDefaultReactiveDomain) -importFrom(shiny,incProgress) -importFrom(shiny,withProgress) -importFrom(stats,as.formula) -importFrom(stats,setNames) -importFrom(stats,weighted.mean) -importFrom(stringi,stri_replace_last_fixed) -importFrom(stringr,str_match) -importFrom(stringr,str_replace) -importFrom(stringr,str_split) -importFrom(utils,URLencode) -importFrom(utils,View) -importFrom(utils,read.table) -importFrom(utils,setTxtProgressBar) -importFrom(utils,txtProgressBar) -importFrom(utils,type.convert) -importFrom(utils,untar) -importFrom(utils,write.table) +# Generated by roxygen2: do not edit by hand + +S3method("[",antaresDataTable) +S3method(as.antaresDataList,antaresDataList) +S3method(as.antaresDataList,antaresDataTable) +S3method(as.antaresDataList,data.frame) +S3method(as.antaresDataList,default) +S3method(as.antaresDataList,list) +S3method(as.antaresDataTable,antaresDataTable) +S3method(as.antaresDataTable,data.frame) +S3method(as.antaresDataTable,default) +S3method(copyToClipboard,antaresDataList) +S3method(copyToClipboard,data.frame) +S3method(copyToClipboard,default) +S3method(copyToClipboard,matrix) +S3method(merge,antaresDataTable) +S3method(print,antaresDataList) +S3method(print,antaresDataTable) +S3method(print,simOptions) +S3method(subset,antaresDataList) +S3method(summary,bindingConstraints) +S3method(viewAntares,antaresDataList) +S3method(viewAntares,default) +export(.getOptionsH5) +export(.h5ReadAntares) +export(.writeAntaresH5Fun) +export(aggregateResult) +export(api_delete) +export(api_get) +export(api_post) +export(api_put) +export(as.antaresDataList) +export(as.antaresDataTable) +export(changeTimeStep) +export(copyToClipboard) +export(extractDataList) +export(getAlias) +export(getAreas) +export(getDistricts) +export(getGeographicTrimming) +export(getIdCols) +export(getLinks) +export(hvdcModification) +export(isH5Opts) +export(mergeDigests) +export(parAggregateMCall) +export(ponderateMcAggregation) +export(readAntares) +export(readAntaresAreas) +export(readAntaresClusters) +export(readBindingConstraints) +export(readClusterDesc) +export(readClusterResDesc) +export(readClusterSTDesc) +export(readDigestFile) +export(readIni) +export(readIniAPI) +export(readIniFile) +export(readInputRES) +export(readInputTS) +export(readInputThermal) +export(readLayout) +export(readOptimCriteria) +export(removeVirtualAreas) +export(setAlias) +export(setHvdcAreas) +export(setRam) +export(setSimulationPath) +export(setSimulationPathAPI) +export(setTimeoutAPI) +export(showAliases) +export(simOptions) +export(viewAntares) +export(writeAntaresH5) +export(writeDigest) +import(bit64) +import(data.table) +import(doParallel) +import(jsonlite) +import(parallel) +import(plyr) +importFrom(doParallel,registerDoParallel) +importFrom(grDevices,col2rgb) +importFrom(grDevices,rgb) +importFrom(httr,DELETE) +importFrom(httr,GET) +importFrom(httr,POST) +importFrom(httr,PUT) +importFrom(httr,accept_json) +importFrom(httr,add_headers) +importFrom(httr,content) +importFrom(httr,content_type_json) +importFrom(httr,http_status) +importFrom(httr,stop_for_status) +importFrom(httr,timeout) +importFrom(lifecycle,deprecated) +importFrom(memuse,Sys.meminfo) +importFrom(methods,is) +importFrom(pbapply,pblapply) +importFrom(pbapply,pboptions) +importFrom(plyr,llply) +importFrom(purrr,quietly) +importFrom(shiny,getDefaultReactiveDomain) +importFrom(shiny,incProgress) +importFrom(shiny,withProgress) +importFrom(stats,as.formula) +importFrom(stats,setNames) +importFrom(stats,weighted.mean) +importFrom(stringi,stri_replace_last_fixed) +importFrom(stringr,str_match) +importFrom(stringr,str_replace) +importFrom(stringr,str_split) +importFrom(utils,URLencode) +importFrom(utils,View) +importFrom(utils,read.table) +importFrom(utils,setTxtProgressBar) +importFrom(utils,txtProgressBar) +importFrom(utils,type.convert) +importFrom(utils,untar) +importFrom(utils,write.table) diff --git a/NEWS.md b/NEWS.md index 992a76e1..fece1c36 100644 --- a/NEWS.md +++ b/NEWS.md @@ -4,7 +4,12 @@ ### Breaking changes (Antares v8.7) : -* Existing function `readBindingConstraints()` (cf. Antares v8.7 changelog) +* `readBindingConstraints()` read now Scenarized RHS for binding constraints (cf. Antares v8.7 changelog) + - returns a new list structure + +BUGFIXES : + +* `readBindingConstraints()` read well study >= v8.3.2 # antaresRead 2.6.1 (devlopment) diff --git a/R/readBindingConstraints.R b/R/readBindingConstraints.R index ac76099f..ac2ee782 100644 --- a/R/readBindingConstraints.R +++ b/R/readBindingConstraints.R @@ -21,7 +21,6 @@ #' \item{values}{values used by the constraint. It contains one line per time step #' and three columns "less", "greater" and "equal"} #' -#' The \code{summary} method returns a data.frame with one line per constraint. #' #' #' @examples @@ -35,7 +34,7 @@ #' #' @export readBindingConstraints <- function(opts = simOptions()) { -if(isH5Opts(opts)){ + if(isH5Opts(opts)){ if(.requireRhdf5_Antares(stopP = FALSE)){ return(h5ReadBindingConstraints(opts)) } else { @@ -48,11 +47,17 @@ if(isH5Opts(opts)){ ## if(opts$typeLoad == 'api'){ - bindingConstraints <- read_secure_json(file.path(opts$inputPath, "bindingconstraints", "bindingconstraints"), - opts$token, timeout = opts$timeout, config = opts$httr_config) + bindingConstraints <- read_secure_json(file.path(opts$inputPath, + "bindingconstraints", + "bindingconstraints"), + opts$token, + timeout = opts$timeout, + config = opts$httr_config) }else{ - path <- file.path(opts$inputPath, "bindingconstraints/bindingconstraints.ini") - bindingConstraints <- readIniFile(path, stringsAsFactors = FALSE) + path <- file.path(opts$inputPath, + "bindingconstraints/bindingconstraints.ini") + bindingConstraints <- readIniFile(path, + stringsAsFactors = FALSE) } ## @@ -65,128 +70,11 @@ if(isH5Opts(opts)){ } ## - # read txt files + # read values txt files ## - for (i in 1:length(bindingConstraints)) { - - # dimension according to parameter "type" to return default value (TS file) - nrows <- switch(bindingConstraints[[i]]$type, - hourly = 24*7*52, - daily = 7 * 52, - weekly = 52, - monthly = 12, - annual = 1) - - # v870 - if(opts$antaresVersion>=870){ - - parse_type <- switch(bindingConstraints[[i]]$operator, - less = "lt", - greater = "gt", - equal = "eq", - both = c("lt", "gt")) # "both" case ? - - path_file_value <- file.path(opts$inputPath, - sprintf("bindingconstraints/%s.txt", - paste0(bindingConstraints[[i]]$id, - "_", - parse_type))) - - # check if "both" case - both_case <- bindingConstraints[[i]]$operator %in% "both" - - # check path file - # multiple path for "both" case - if(!all(file.exists(path_file_value))) - stop("Time series file for binding constraint ", - bindingConstraints[[i]]$id, - " not exist", - call. = FALSE) - - # Read files - # both case - if(both_case){ - tmp_values <- lapply(path_file_value, - fread_antares, - opts = opts) - names(tmp_values) <- c("less", "greater") - } - else - tmp_values <- fread_antares(opts = opts, - file = path_file_value) - - - # check empty values to return default values - # both case - default_scenarised_values <- as.data.table( - matrix(0L, nrow = nrows, ncol = 1)) - - if(both_case){ - check_nrow <- unlist(lapply(tmp_values, nrow)) - if(any(check_nrow %in% 0)){ - warning("Time series files for binding constraint ", - bindingConstraints[[i]]$id, - " are empty", - call. = FALSE) - - tmp_values[["less"]] <- default_scenarised_values - tmp_values[["greater"]] <- default_scenarised_values - # tmp_values <- lapply(tmp_values, - # function(x){ - # x[["less"]] <- default_scenarised_values - # x[["greater"]] <- default_scenarised_values - # }) - } - } - else - if(nrow(tmp_values)==0){ - warning("Time series file for binding constraint ", - bindingConstraints[[i]]$id, - " is empty", - call. = FALSE) - tmp_values <- default_scenarised_values - } - - # # [return] default values - # if(is.null(tmp_values)){ - # default_scenarised_values <- as.data.table( - # matrix(0L, nrow = nrows, ncol = 1)) - # tmp_values <- default_scenarised_values - # } - - # return - bindingConstraints[[i]]$values <- tmp_values - - }else{ # <870 (legacy) - path <- file.path(opts$inputPath, - sprintf("bindingconstraints/%s.txt", - bindingConstraints[[i]]$id)) - - # why return 0 if file.size(path) == 0 ? - if(opts$typeLoad != "api" && file.size(path) == 0){ - bindingConstraints[[i]]$values <- as.data.table( - matrix(0L, nrow = nrows, 3)) - setnames(bindingConstraints[[i]]$values, - names(bindingConstraints[[i]]$values), - c("less", "greater", "equal")) - } - else{ - # bindingConstraints[[i]]$values <- fread(path) - tmp_values <- fread_antares(opts = opts, file = path) - - # this test do nothing => tmp_values never NULL - # return 0 row/col for empty file or error if file does not exist - if(is.null(tmp_values)){ - tmp_values <- as.data.table(matrix(0L, nrow = nrows, 3)) - } - - bindingConstraints[[i]]$values <- tmp_values - setnames(bindingConstraints[[i]]$values, - names(bindingConstraints[[i]]$values), - c("less", "greater", "equal")) - } - } - } + bindingConstraints <- lapply(bindingConstraints, + .read_binding_values, + opts = opts) ## # manage full list object @@ -195,93 +83,219 @@ if(isH5Opts(opts)){ # to return named list constraintNames <- sapply(bindingConstraints, `[[`, - "name") + "id") # re structure list ($properties, $coefs, $values) # [breaking changes] add "$properties" for all version - bindingConstraints <- lapply(bindingConstraints, function(x) { - # default names of parameters (core parameters) - names_elements <- c("name", "id", "enabled", "type", "operator", "values") + bindingConstraints <- lapply(bindingConstraints, + .manage_list_structure, + opts = opts) + + names(bindingConstraints) <- constraintNames + class(bindingConstraints) <- "bindingConstraints" + bindingConstraints +} + +# read values files for every binding of study +.read_binding_values <- function(binding_object, + opts = simOptions()){ + # dimension according to parameter "type" to return default value (TS file) + nrows <- switch(binding_object$type, + hourly = 24*7*52, + daily = 7 * 52, + weekly = 52, + monthly = 12, + annual = 1) + + # v870 + if(opts$antaresVersion>=870){ - # get links information from list - coefs_elements <- setdiff(names(x), names_elements) - coefs_values <- x[which(names(x)%in%coefs_elements)] + parse_type <- switch(binding_object$operator, + less = "lt", + greater = "gt", + equal = "eq", + both = c("lt", "gt")) # "both" case ? - ## - # manage properties with version (filter) - ## - # filter on parameters to keep only links information + path_file_value <- file.path(opts$inputPath, + sprintf("bindingconstraints/%s.txt", + paste0(binding_object$id, + "_", + parse_type))) - # v832 - if (opts$antaresVersion>=832){ - names_elements_832 <- c("filter-year-by-year", - "filter-synthesis") - elements_832 <- x[which(names(x)%in%names_elements_832)] - - coefs_values[names_elements_832] <- NULL + # check if "both" case + both_case <- binding_object$operator %in% "both" + + # check path file + # multiple path for "both" case + if(!all(file.exists(path_file_value))) + stop("Time series file for binding constraint ", + binding_object$id, + " not exist", + call. = FALSE) + + # Read files + # both case + if(both_case){ + tmp_values <- lapply(path_file_value, + fread_antares, + opts = opts) + names(tmp_values) <- c("less", "greater") } + else + tmp_values <- fread_antares(opts = opts, + file = path_file_value) - # v870 - if(opts$antaresVersion>=870){ - names_elements_870 <- "group" - elements_870 <- x[which(names(x)%in%names_elements_870)] - - coefs_values[names_elements_870] <- NULL + + # check empty values to return default values + # both case + default_scenarised_values <- as.data.table( + matrix(0L, nrow = nrows, ncol = 1)) + + if(both_case){ + check_nrow <- unlist(lapply(tmp_values, nrow)) + if(any(check_nrow %in% 0)){ + warning("Time series files for binding constraint ", + binding_object$id, + " are empty", + call. = FALSE) + + tmp_values[["less"]] <- default_scenarised_values + tmp_values[["greater"]] <- default_scenarised_values + } } - - ## - # update list - ## + else + if(nrow(tmp_values)==0){ + warning("Time series file for binding constraint ", + binding_object$id, + " is empty", + call. = FALSE) + tmp_values <- default_scenarised_values + } - # core elements list - core_list <- list( - properties = list( - name = x$name, - id = x$id, - enabled = x$enabled, - timeStep = x$type, - operator = x$operator), - coefs = unlist(coefs_values), - values = x$values) + # return + binding_object$values <- tmp_values + return(binding_object) - # add properties according to version - # decreasing approach + }else{ # <870 (legacy) + path <- file.path(opts$inputPath, + sprintf("bindingconstraints/%s.txt", + binding_object$id)) - # v870 - if(opts$antaresVersion>=870){ - list_870 <- list() - list_870$properties = append(core_list$properties, - c( - unlist(elements_832), - unlist(elements_870))) - list_870 <- append(list_870, - core_list[c(2,3)]) - return(list_870) + # why return 0 if file.size(path) == 0 ? + if(opts$typeLoad != "api" && file.size(path) == 0){ + binding_object$values <- as.data.table( + matrix(0L, nrow = nrows, 3)) + setnames(binding_object$values, + names(binding_object$values), + c("less", "greater", "equal")) + return(binding_object) } - # v832 - if(opts$antaresVersion>=832){ - list_832 <- list() - list_832$properties = append(core_list$properties, - unlist(elements_832)) - list_832 <- append(list_832, - core_list[c(2,3)]) - return(list_832) + else{ + # binding_object$values <- fread(path) + tmp_values <- fread_antares(opts = opts, file = path) + + # this test do nothing => tmp_values never NULL + # return 0 row/col for empty file or error if file does not exist + if(is.null(tmp_values)){ + tmp_values <- as.data.table(matrix(0L, nrow = nrows, 3)) + } + + binding_object$values <- tmp_values + setnames(binding_object$values, + names(binding_object$values), + c("less", "greater", "equal")) + return(binding_object) } + } +} - return(core_list) - }) +# build list structure according to antares version +.manage_list_structure <- function(binding_object, + opts = simOptions()){ + # default names of parameters (core parameters) + names_elements <- c("name", "id", "enabled", "type", "operator", "values") - names(bindingConstraints) <- constraintNames - class(bindingConstraints) <- "bindingConstraints" - bindingConstraints + # get links information from list + coefs_elements <- setdiff(names(binding_object), + names_elements) + coefs_values <- binding_object[which(names(binding_object) %in% + coefs_elements)] + + ## + # manage properties with version (filter) + ## + # filter on parameters to keep only links information + + # v832 + if (opts$antaresVersion>=832){ + names_elements_832 <- c("filter-year-by-year", + "filter-synthesis") + elements_832 <- binding_object[which(names(binding_object) %in% + names_elements_832)] + + coefs_values[names_elements_832] <- NULL + } + + # v870 + if(opts$antaresVersion>=870){ + names_elements_870 <- "group" + elements_870 <- binding_object[which(names(binding_object) %in% + names_elements_870)] + + coefs_values[names_elements_870] <- NULL + } + + ## + # update list + ## + + # core elements list + core_list <- list( + properties = list( + name = binding_object$name, + id = binding_object$id, + enabled = binding_object$enabled, + timeStep = binding_object$type, + operator = binding_object$operator), + coefs = unlist(coefs_values), + values = binding_object$values) + + # add properties according to version + # decreasing approach + + # v870 + if(opts$antaresVersion>=870){ + list_870 <- list() + list_870$properties = append(core_list$properties, + c( + unlist(elements_832), + unlist(elements_870))) + list_870 <- append(list_870, + core_list[c(2,3)]) + return(list_870) + } + # v832 + if(opts$antaresVersion>=832){ + list_832 <- list() + list_832$properties = append(core_list$properties, + unlist(elements_832)) + list_832 <- append(list_832, + core_list[c(2,3)]) + return(list_832) + } + + return(core_list) } +#' `r lifecycle::badge("deprecated")` +#' @title Display equation of binding constraint #' @param object Object returned by readBindingConstraints #' @param ... Unused #' +#' @return A data.frame with one line per constraint. #' @export -#' @rdname readBindingConstraints summary.bindingConstraints <- function(object, ...) { + lifecycle::deprecate_warn(">= 2.7.0", "antaresRead::summary.bindingConstraints()") equations <- vapply(object, FUN.VALUE = character(1), function(x) { coefs <- sprintf( "%s %s x %s", diff --git a/R/utils_api.R b/R/utils_api.R index 921e2715..40284089 100644 --- a/R/utils_api.R +++ b/R/utils_api.R @@ -7,9 +7,13 @@ fread_antares <- function(opts, file, ...) { endpoint = I(file), query = list(formatted = FALSE) ) - tryCatch(fread(response, ...), error = function(e) {message(file); message(e)}) + suppressWarnings( + tryCatch(fread(response, ...), error = function(e){ + message(file); message(e) + })) } else { - fread(file, ...) + suppressWarnings( + fread(file, ...)) } } diff --git a/man/readBindingConstraints.Rd b/man/readBindingConstraints.Rd index af6e6928..51c8052e 100644 --- a/man/readBindingConstraints.Rd +++ b/man/readBindingConstraints.Rd @@ -2,20 +2,13 @@ % Please edit documentation in R/readBindingConstraints.R \name{readBindingConstraints} \alias{readBindingConstraints} -\alias{summary.bindingConstraints} \title{Read binding constraints} \usage{ readBindingConstraints(opts = simOptions()) - -\method{summary}{bindingConstraints}(object, ...) } \arguments{ \item{opts}{list of simulation parameters returned by the function \code{\link{setSimulationPath}}} - -\item{object}{Object returned by readBindingConstraints} - -\item{...}{Unused} } \value{ \code{readBindingConstraints} returns an object of class \code{bindingConstraints}. @@ -27,8 +20,6 @@ a list with the following elements: \item{coefficients}{elements containing the coefficients used by the constraint} \item{values}{values used by the constraint. It contains one line per time step and three columns "less", "greater" and "equal"} - -The \code{summary} method returns a data.frame with one line per constraint. } \description{ This function reads the binding constraints of an Antares project. diff --git a/man/summary.bindingConstraints.Rd b/man/summary.bindingConstraints.Rd new file mode 100644 index 00000000..f00ec913 --- /dev/null +++ b/man/summary.bindingConstraints.Rd @@ -0,0 +1,19 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/readBindingConstraints.R +\name{summary.bindingConstraints} +\alias{summary.bindingConstraints} +\title{Display equation of binding constraint} +\usage{ +\method{summary}{bindingConstraints}(object, ...) +} +\arguments{ +\item{object}{Object returned by readBindingConstraints} + +\item{...}{Unused} +} +\value{ +A data.frame with one line per constraint. +} +\description{ +\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#deprecated}{\figure{lifecycle-deprecated.svg}{options: alt='[Deprecated]'}}}{\strong{[Deprecated]}} +}