Skip to content

Commit

Permalink
Adapt run_iteration and simulate_abc to latest changes
Browse files Browse the repository at this point in the history
  • Loading branch information
bodkan committed Oct 11, 2024
1 parent da8bbdc commit 3410a01
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
15 changes: 10 additions & 5 deletions R/run_iteration.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
}

Expand All @@ -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)
Expand Down
23 changes: 12 additions & 11 deletions R/simulate_abc.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,20 @@ 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)

# validate the ABC setup
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
))
Expand All @@ -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)

Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 3410a01

Please sign in to comment.