From 3410a01216c3b33ed3ee1dd50a417d3983f48b46 Mon Sep 17 00:00:00 2001 From: Martin Petr Date: Fri, 11 Oct 2024 07:41:19 +0000 Subject: [PATCH] Adapt run_iteration and simulate_abc to latest changes --- R/run_iteration.R | 15 ++++++++++----- R/simulate_abc.R | 23 ++++++++++++----------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/R/run_iteration.R b/R/run_iteration.R index 701d852..4860662 100644 --- a/R/run_iteration.R +++ b/R/run_iteration.R @@ -4,7 +4,7 @@ run_iteration <- function(it, sequence_length, recombination_rate, mutation_rate, data, format, engine, model_args, engine_args, model_name, attempts) { - if (format == "files" && missing(data_funs)) + if (format == "files" && missing(data)) stop("Models which generate custom files require a list of data function(s)\n", "which will process them for computation of summary statistics.", call. = FALSE) @@ -32,10 +32,6 @@ run_iteration <- function(it, if (mutation_rate != 0) result_data <- slendr::ts_mutate(result_data, mutation_rate = mutation_rate) - # clean up if needed - if (!is.null(attr(result_data, "path"))) - unlink(attr(result_data, "path")) - result_data <- list(ts = result_data) } @@ -45,6 +41,15 @@ run_iteration <- function(it, result_data <- evaluate_functions(data_expr, env) } + # clean up if needed + if (format == "ts") + result_path <- attr(result_data, "path") + else + result_path <- result$data + + if (!is.null(result_path)) + unlink(result_path, recursive = TRUE) + # collect data for a downstream ABC inference: # 1. compute summary statistics using user-defined tree-sequence functions simulated_stats <- summarise_data(result_data, functions) diff --git a/R/simulate_abc.R b/R/simulate_abc.R index 4953775..9e64f2a 100644 --- a/R/simulate_abc.R +++ b/R/simulate_abc.R @@ -60,6 +60,12 @@ simulate_abc <- function( "statistics, and the number of iterations must be provided (check\n", "that the variables that you provided really do contain what you think)", call. = FALSE) + # unless a tree-sequence is supposed to be returned directly, create a + # temporary directory where a simulation script can store output files + format <- match.arg(format) + if (!format %in% c("ts", "files")) + stop("Unknown output format type '", format, "'. Valid values are 'ts' or 'files'.", call. = FALSE) + if (mutation_rate < 0) stop("Mutation rate must be a non-negative number", call. = FALSE) @@ -67,7 +73,7 @@ simulate_abc <- function( utils::capture.output(validate_abc( model, priors, functions, observed, sequence_length = sequence_length, recombination_rate = recombination_rate, - mutation_rate = mutation_rate, + mutation_rate = mutation_rate, format = format, data = data, engine = engine, model_args = model_args, engine_args = engine_args )) @@ -90,17 +96,11 @@ simulate_abc <- function( its <- seq_len(iterations) p <- progressr::progressor(along = its) - # unless a tree-sequence is supposed to be returned directly, create a - # temporary directory where a simulation script can store output files - format <- match.arg(format) - if (!format %in% c("ts", "files")) - stop("Unknown output format type '", format, "'. Valid values are 'ts' or 'files'.", call. = FALSE) - if (format == "files" && is.null(data)) stop("Models which generate custom files must provide a list of function(s)\n", "which will convert them for computing summary statistics.", call. = FALSE) - if (engine == "msprime" && format != "ts") + if (!is.null(engine) && engine == "msprime" && format != "ts") stop("When using the slendr msprime engine, \"ts\" is the only valid data format", call. = FALSE) @@ -182,14 +182,15 @@ simulate_abc <- function( ) opts <- list( - sequence_length = sequence_length, - recombination_rate = recombination_rate, - mutation_rate = mutation_rate, engine = engine, model_args = model_args, engine_args = engine_args, packages = packages ) + if (!missing(sequence_length)) opts$sequence_length <- sequence_length + if (!missing(recombination_rate)) opts$recombination_rate <- recombination_rate + if (!missing(mutation_rate)) opts$mutation_rate <- mutation_rate + attr(result, "options") <- opts class(result) <- "demografr_abc_sims"