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

Caching bug with function arguments #168

Open
tiffanymtang opened this issue Jul 20, 2023 · 0 comments
Open

Caching bug with function arguments #168

tiffanymtang opened this issue Jul 20, 2023 · 0 comments
Labels
bug Something isn't working

Comments

@tiffanymtang
Copy link
Collaborator

Caching does not work properly with DGPs (and Methods) that take in function arguments that explicitly contain a nested function.

Example:

rm(list = ls())
library(simChef)
library(magrittr)

n <- 300
p <- 3

dgp_fun <- function(n, p, func_arg) {
  X <- matrix(rnorm(n * p), nrow = n, ncol = p)
  y <- rnorm(n)
  return(list(X = X, y = y))
}

method_fun <- function(X, y) {
  lm_df <- cbind(data.frame(X), .y = y)
  lm_fit <- lm(.y ~ ., dat = lm_df)
  return(coef(lm_fit))
}
method <- create_method(method_fun, .name = "Method")

Example DGPs where caching works:

helper_fun <- function(x) sum(x)
working_func_arg <- function() {
  z <- apply(X, 1, FUN = helper_fun)
}
working_dgp <- create_dgp(dgp_fun, .name = "Working DGP", n = n, p = p, func_arg = working_func_arg)

experiment <- create_experiment() %>%
  add_dgp(working_dgp) %>%
  add_method(method)

out <- run_experiment(experiment, save = TRUE, use_cached = FALSE)
out <- run_experiment(experiment, save = TRUE, use_cached = TRUE)

Example DGPs where caching does not work:

buggy_func_arg1 <- function() {
  z <- apply(X, 1, FUN = function(x) sum(x))
}
buggy_dgp1 <- create_dgp(dgp_fun, .name = "Buggy DGP1", n = n, p = p, func_arg = buggy_func_arg1)

buggy_func_arg2 <- function() {
  function(x) sum(x)
  return("hi")
}
buggy_dgp2 <- create_dgp(dgp_fun, .name = "Buggy DGP2", n = n, p = p, func_arg = buggy_func_arg2)

experiment <- create_experiment() %>%
  add_dgp(buggy_dgp1) %>%
  add_dgp(buggy_dgp2) %>%
  add_method(method)

out <- run_experiment(experiment, save = TRUE, use_cached = FALSE)
out <- run_experiment(experiment, save = TRUE, use_cached = TRUE)
@tiffanymtang tiffanymtang added the bug Something isn't working label Jul 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant