From 81628de7d86300cd63bffdc92982df8f90b7601a Mon Sep 17 00:00:00 2001 From: laresbernardo Date: Wed, 5 Oct 2022 14:55:32 -0500 Subject: [PATCH] fix: deprecated plot_folder_sub to avoid chain-issues --- R/R/checks.R | 2 +- R/R/json.R | 5 ++++- R/R/outputs.R | 38 +++++++++++++++----------------------- R/R/plots.R | 1 - R/R/refresh.R | 7 +++---- R/man/robyn_outputs.Rd | 5 ----- R/man/robyn_refresh.Rd | 9 ++++----- 7 files changed, 27 insertions(+), 40 deletions(-) diff --git a/R/R/checks.R b/R/R/checks.R index 8d5444e14..b798e572e 100644 --- a/R/R/checks.R +++ b/R/R/checks.R @@ -626,7 +626,7 @@ check_robyn_name <- function(robyn_object, quiet = FALSE) { } } -check_filedir <- function(plot_folder) { +check_dir <- function(plot_folder) { file_end <- substr(plot_folder, nchar(plot_folder) - 3, nchar(plot_folder)) if (file_end == ".RDS") { plot_folder <- dirname(plot_folder) diff --git a/R/R/json.R b/R/R/json.R index bdf561aed..efdd36fb0 100644 --- a/R/R/json.R +++ b/R/R/json.R @@ -258,6 +258,7 @@ robyn_recreate <- function(json_file, quiet = FALSE, ...) { # Import the whole chain any refresh model to init robyn_chain <- function(json_file) { json_data <- robyn_read(json_file, quiet = TRUE) + ids <- c(json_data$InputCollect$refreshChain, json_data$ExportedModel$select_model) plot_folder <- json_data$ExportedModel$plot_folder temp <- stringr::str_split(plot_folder, "/")[[1]] chain <- temp[startsWith(temp, "Robyn_")] @@ -278,6 +279,8 @@ robyn_chain <- function(json_file) { dirs <- sapply(chainData, function(x) x$ExportedModel$plot_folder) json_files <- paste0(dirs, "RobynModel-", names(dirs), ".json") attr(chainData, "json_files") <- json_files - attr(chainData, "chain") <- names(chainData) + attr(chainData, "chain") <- ids # names(chainData) + if (length(ids) != length(names(chainData))) + warning("Can't replicate chain-like results if you don't follow Robyn's chain structure") return(invisible(chainData)) } diff --git a/R/R/outputs.R b/R/R/outputs.R index d3c82b1d9..876bf3113 100644 --- a/R/R/outputs.R +++ b/R/R/outputs.R @@ -22,9 +22,6 @@ #' selection. Lower \code{calibration_constraint} increases calibration accuracy. #' @param plot_folder Character. Path for saving plots. Default #' to \code{robyn_object} and saves plot in the same directory as \code{robyn_object}. -#' @param plot_folder_sub Character. Customize sub path to save plots. The total -#' path is created with \code{dir.create(file.path(plot_folder, plot_folder_sub))}. -#' For example, plot_folder_sub = "sub_dir". #' @param plot_pareto Boolean. Set to \code{FALSE} to deactivate plotting #' and saving model one-pagers. Used when testing models. #' @param clusters Boolean. Apply \code{robyn_clusters()} to output models? @@ -43,7 +40,7 @@ robyn_outputs <- function(InputCollect, OutputModels, pareto_fronts = "auto", calibration_constraint = 0.1, - plot_folder = NULL, plot_folder_sub = NULL, + plot_folder = NULL, plot_pareto = TRUE, csv_out = "pareto", clusters = TRUE, @@ -51,7 +48,7 @@ robyn_outputs <- function(InputCollect, OutputModels, ui = FALSE, export = TRUE, quiet = FALSE, ...) { if (is.null(plot_folder)) plot_folder <- getwd() - plot_folder <- check_filedir(plot_folder) + plot_folder <- check_dir(plot_folder) # Check calibration constrains calibration_constraint <- check_calibconstr( @@ -91,6 +88,19 @@ robyn_outputs <- function(InputCollect, OutputModels, plotDataCollect = pareto_results$plotDataCollect ) + # Set folder to save outputs: legacy plot_folder_sub + if (TRUE) { + depth <- ifelse( + "refreshDepth" %in% names(InputCollect), + InputCollect$refreshDepth, + ifelse("refreshCounter" %in% names(InputCollect), + InputCollect$refreshCounter, 0 + ) + ) + folder_var <- ifelse(!as.integer(depth) > 0, "init", paste0("rf", depth)) + plot_folder_sub <- paste("Robyn", format(Sys.time(), "%Y%m%d%H%M"), folder_var, sep = "_") + } + # Final results object OutputCollect <- list( resultHypParam = filter(pareto_results$resultHypParam, .data$solID %in% allSolutions), @@ -115,24 +125,6 @@ robyn_outputs <- function(InputCollect, OutputModels, ) class(OutputCollect) <- c("robyn_outputs", class(OutputCollect)) - # Set folder to save outputs - if (is.null(plot_folder_sub)) { - refresh <- attr(OutputModels, "refresh") - depth <- ifelse( - "refreshDepth" %in% names(InputCollect), - InputCollect$refreshDepth, - ifelse("refreshCounter" %in% names(InputCollect), - InputCollect$refreshCounter, 0 - ) - ) - refresh <- as.integer(depth) > 0 - folder_var <- ifelse(!refresh, "init", paste0("rf", depth)) - plot_folder_sub <- paste("Robyn", format(Sys.time(), "%Y%m%d%H%M"), folder_var, sep = "_") - } - - plotPath <- paste0(plot_folder, "/", plot_folder_sub, "/") - OutputCollect$plot_folder <- gsub("//", "/", plotPath) - if (export) { if (!dir.exists(OutputCollect$plot_folder)) dir.create(OutputCollect$plot_folder, recursive = TRUE) tryCatch( diff --git a/R/R/plots.R b/R/R/plots.R index 195df605a..fce567d09 100644 --- a/R/R/plots.R +++ b/R/R/plots.R @@ -929,7 +929,6 @@ refresh_plots_json <- function(OutputCollectRF, json_file, export = TRUE) { group_by(.data$solID, .data$label, .data$variable) %>% summarise_all(sum) - df <- replace(df, is.na(df), 0) outputs[["pBarRF"]] <- pBarRF <- df %>% ggplot(aes(y = .data$variable)) + geom_col(aes(x = .data$decompPer)) + diff --git a/R/R/refresh.R b/R/R/refresh.R index 632e8d574..b174c342f 100644 --- a/R/R/refresh.R +++ b/R/R/refresh.R @@ -98,11 +98,11 @@ robyn_refresh <- function(json_file = NULL, robyn_object = NULL, dt_input = NULL, dt_holidays = Robyn::dt_prophet_holidays, - plot_folder_sub = NULL, refresh_steps = 4, refresh_mode = "manual", refresh_iters = 1000, refresh_trials = 3, + plot_folder = NULL, plot_pareto = TRUE, version_prompt = FALSE, export = TRUE, @@ -119,12 +119,12 @@ robyn_refresh <- function(json_file = NULL, if (!is.null(json_file)) { Robyn <- list() json <- robyn_read(json_file, step = 2, quiet = TRUE) - listInit <- robyn_recreate( + listInit <- suppressWarnings(robyn_recreate( json_file = json_file, dt_input = dt_input, dt_holidays = dt_holidays, quiet = FALSE, ... - ) + )) listInit$InputCollect$refreshSourceID <- json$ExportedModel$select_model chainData <- robyn_chain(json_file) listInit$InputCollect$refreshChain <- attr(chainData, "chain") @@ -277,7 +277,6 @@ robyn_refresh <- function(json_file = NULL, OutputCollectRF <- robyn_run( InputCollect = InputCollectRF, plot_folder = objectPath, - plot_folder_sub = plot_folder_sub, calibration_constraint = listOutputPrev[["calibration_constraint"]], add_penalty_factor = listOutputPrev[["add_penalty_factor"]], iterations = refresh_iters, diff --git a/R/man/robyn_outputs.Rd b/R/man/robyn_outputs.Rd index c615e6c70..23fcd4691 100644 --- a/R/man/robyn_outputs.Rd +++ b/R/man/robyn_outputs.Rd @@ -13,7 +13,6 @@ robyn_outputs( pareto_fronts = "auto", calibration_constraint = 0.1, plot_folder = NULL, - plot_folder_sub = NULL, plot_pareto = TRUE, csv_out = "pareto", clusters = TRUE, @@ -47,10 +46,6 @@ selection. Lower \code{calibration_constraint} increases calibration accuracy.} \item{plot_folder}{Character. Path for saving plots. Default to \code{robyn_object} and saves plot in the same directory as \code{robyn_object}.} -\item{plot_folder_sub}{Character. Customize sub path to save plots. The total -path is created with \code{dir.create(file.path(plot_folder, plot_folder_sub))}. -For example, plot_folder_sub = "sub_dir".} - \item{plot_pareto}{Boolean. Set to \code{FALSE} to deactivate plotting and saving model one-pagers. Used when testing models.} diff --git a/R/man/robyn_refresh.Rd b/R/man/robyn_refresh.Rd index 94f3583a4..901aa2e74 100644 --- a/R/man/robyn_refresh.Rd +++ b/R/man/robyn_refresh.Rd @@ -11,11 +11,11 @@ robyn_refresh( robyn_object = NULL, dt_input = NULL, dt_holidays = Robyn::dt_prophet_holidays, - plot_folder_sub = NULL, refresh_steps = 4, refresh_mode = "manual", refresh_iters = 1000, refresh_trials = 3, + plot_folder = NULL, plot_pareto = TRUE, version_prompt = FALSE, export = TRUE, @@ -40,10 +40,6 @@ data for the refresh.} \item{dt_holidays}{data.frame. Raw input holiday data. Load standard Prophet holidays using \code{data("dt_prophet_holidays")}.} -\item{plot_folder_sub}{Character. Customize sub path to save plots. The total -path is created with \code{dir.create(file.path(plot_folder, plot_folder_sub))}. -For example, plot_folder_sub = "sub_dir".} - \item{refresh_steps}{Integer. It controls how many time units the refresh model build move forward. For example, \code{refresh_steps = 4} on weekly data means the InputCollect$window_start & InputCollect$window_end move forward @@ -62,6 +58,9 @@ still needs to be investigated.} \item{refresh_trials}{Integer. Trials per refresh. Defaults to 5 trials. More reliable recommendation still needs to be investigated.} +\item{plot_folder}{Character. Path for saving plots. Default +to \code{robyn_object} and saves plot in the same directory as \code{robyn_object}.} + \item{plot_pareto}{Boolean. Set to \code{FALSE} to deactivate plotting and saving model one-pagers. Used when testing models.}