diff --git a/DESCRIPTION b/DESCRIPTION index e30b375..d1e7eb4 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: treepplr Title: R Interface to TreePPL -Version: 0.14.0 +Version: 0.14.1 Authors@R: person("Mariana", "P Braga", , "mpiresbr@gmail.com", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-1253-2536")) diff --git a/R/data.R b/R/data.R index 50d1c28..a1aef81 100644 --- a/R/data.R +++ b/R/data.R @@ -2,6 +2,7 @@ + #' Import data for TreePPL program #' #' @description diff --git a/R/model.R b/R/model.R index 176955b..48f60a5 100644 --- a/R/model.R +++ b/R/model.R @@ -19,9 +19,21 @@ tpplcCompileOptions <- c( #' Options that can be passed to TreePPL compiler #' -#' @returns A data frame with the output from the compiler's help +#' @returns A data frame with some outputs from the compiler's help #' tp_compile_options <- function() { + tp_find_options("Compile options:", "Runtime options:") +} + +#' Options that can be passed to TreePPL run +#' +#' @returns A data frame with with some outputs from compiler's help +#' +tp_runtime_options <- function() { + tp_find_options("Runtime options:", "Inference methods:") +} + +tp_find_options <- function(str_begin, str_end) { tpplc_path <- tp_installing_treeppl() # treeppl options cmd_opt <- system2( @@ -33,10 +45,11 @@ tp_compile_options <- function() { # Preparing the output # - # find the line containing "Options:" - x <- which(cmd_opt == "Options:") + # find the line between str_begin and str_end + x <- which(cmd_opt == str_begin) + y <- which(cmd_opt == str_end) # extract everything after that line - cmd_opt <- cmd_opt[(x + 1):length(cmd_opt)] + cmd_opt <- cmd_opt[(x + 1):(y - 2)] cmd_opt <- trimws(cmd_opt) cmd_opt <- strsplit(cmd_opt, " {2,}", perl = TRUE) @@ -79,13 +92,12 @@ options_to_string <- function(options) { args_str <- c() if (length(options) != 0) { vec <- c() - args_vec <- unlist(options) - for (i in seq_along(args_vec)) { - if (!is.logical(args_vec[[i]])) { - str <- paste0("--", names(args_vec[i]), " ", args_vec[[i]]) + for (i in seq_along(options)) { + if (!is.logical(options[[i]])) { + str <- paste0("--", names(options[i]), " ", options[[i]]) } else { - if (args_vec[[i]]) { - str <- paste0("--", names(args_vec[i])) + if (options[[i]]) { + str <- paste0("--", names(options[i])) } } vec <- c(vec, str) @@ -98,15 +110,15 @@ options_to_string <- function(options) { #' TreePPL model template #' #' @description -#' `tp_modelT` template for TreePPL code carrying all the informations necessary +#' `sampler_T` template for TreePPL code carrying all the informations necessary #' for compiling and running this model efficently -compiled_model_Template <- +sampler_T <- setRefClass( - "compiled_model_Template", + "sampler_T", fields = list( exe_path = "character", - path = "character", + model_path = "character", compile_options = "list" ) ) @@ -140,7 +152,7 @@ compilation <- function(path, args_str) { dir_path <- tp_tempdir() # output - output_path <- paste0(dir_path, digest::digest(paste(path,args_str), "sha256"), ".exe") + output_path <- paste0(dir_path, digest::digest(paste(path, args_str), "sha256"), ".exe") options <- paste("--output", output_path, args_str) @@ -167,7 +179,6 @@ compilation <- function(path, args_str) { #' @export #' tp_write_model <- function(model, model_file_name = "tmp_model_file") { - path <- paste0(tp_tempdir(), model_file_name, ".tppl") cat(model, file = path) @@ -177,7 +188,7 @@ tp_write_model <- function(model, model_file_name = "tmp_model_file") { #' Create a TreePPL model #' #' @description -#' `tp_compile` takes TreePPL model and prepares it to be used by +#' `tp_compile` takes TreePPL model and create a sampler to be used by #' [treepplr::tp_run()]. #' #' @param model One of tree options: @@ -186,7 +197,7 @@ tp_write_model <- function(model, model_file_name = "tmp_model_file") { #' (see [treepplr::tp_model_library()]), OR #' * A string containing the entire TreePPL code. #' -#' @return compiled_model from a compiled_model_Template +#' @return sampler from a sampler_T #' @export tp_compile <- function(model, method = "mcmc", ...) { @@ -212,12 +223,12 @@ tp_compile <- function(model, method = "mcmc", ...) { model_path <- tp_write_model(model) } } - m <- new("compiled_model_Template", path = model_path) + sampler <- new("sampler_T", model_path = model_path) user_list <- append(tp_list(...), list(method = method)) tmp <- list_to_options(user_list) - m$compile_options <- tmp[["compile"]] + sampler$compile_options <- tmp[["compile"]] full_options = append(tmp[["compile"]], tmp[["runtime"]]) - m$exe_path <- compilation(m$path, options_to_string(full_options)) - return(m) + sampler$exe_path <- compilation(sampler$model_path, options_to_string(full_options)) + return(sampler) } diff --git a/R/post_treatment.R b/R/post_treatment.R index 0811318..03631bc 100644 --- a/R/post_treatment.R +++ b/R/post_treatment.R @@ -106,7 +106,7 @@ tp_parse_mcmc <- function(treeppl_out) { #' @param treeppl_out a character vector giving the TreePPL json output #' produced by [tp_run]. #' -#' @return A list (n = n_runs) of data frames with the output from inference +#' @return A list (n = sweeps) of data frames with the output from inference #' in TreePPL under the host repertoire evolution model. #' @export diff --git a/R/run.R b/R/run.R index c14d1f2..838de95 100644 --- a/R/run.R +++ b/R/run.R @@ -3,8 +3,7 @@ #' @description #' Run TreePPL and return output. #' -#' @param compiled_model a [base::character] with the full path to the compiled model -#' outputted by [treepplr::tp_compile]. +#' @param sampler a [treepplr::sampler_T] outputted by [treepplr::tp_compile]. #' @param data a [base::character] with the full path to the data file in TreePPL #' JSON format (as outputted by [treepplr::tp_data]). #' @param dir a [base::character] with the full path to the directory where you @@ -38,10 +37,10 @@ #' data_path <- tp_data(data_input = "coin") #' #' # run TreePPL -#' result <- tp_run(exe_path, data_path, n_runs = 2) +#' result <- tp_run(exe_path, data_path) #' } -tp_run <- function(compiled_model, +tp_run <- function(sampler, data, dir = NULL, out_file_name = "out", @@ -62,7 +61,7 @@ tp_run <- function(compiled_model, # So the user list have priority options <- list_to_options(tp_list(...)) - if(length(options[["compile"]]) != 0) { + if (length(options[["compile"]]) != 0) { stop("Can't give compile time options here") } @@ -70,7 +69,7 @@ tp_run <- function(compiled_model, # due to conflict with internal env from treeppl self container command <- paste( "LD_LIBRARY_PATH= ", - compiled_model$exe_path, + sampler$exe_path, data, options_to_string(options[["runtime"]]), paste(">", output_path) diff --git a/R/utils.R b/R/utils.R index 67bd4d0..dd4bdd8 100644 --- a/R/utils.R +++ b/R/utils.R @@ -153,6 +153,7 @@ sep <- function() { #' @return A list of model names. #' @export tp_model_library <- function() { + tp_installing_treeppl() # make sure you get the appropriate version if you have more than one treeppl folder in the tmp fd <- list.files("/tmp", pattern = paste0("treeppl-", TPPLC_VERSION), @@ -194,6 +195,7 @@ tp_find <- function(model_name, ext) { full.names = TRUE ) } else { + tp_installing_treeppl() fd <- list.files("/tmp", pattern = paste0("treeppl-", TPPLC_VERSION), full.names = TRUE) diff --git a/man/compiled_model_Template-class.Rd b/man/sampler_T-class.Rd similarity index 55% rename from man/compiled_model_Template-class.Rd rename to man/sampler_T-class.Rd index f2ff692..60efa3e 100644 --- a/man/compiled_model_Template-class.Rd +++ b/man/sampler_T-class.Rd @@ -1,12 +1,12 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/model.R \docType{class} -\name{compiled_model_Template-class} -\alias{compiled_model_Template-class} -\alias{compiled_model_Template} +\name{sampler_T-class} +\alias{sampler_T-class} +\alias{sampler_T} \title{TreePPL model template} \description{ -\code{tp_modelT} template for TreePPL code carrying all the informations necessary +\code{sampler_T} template for TreePPL code carrying all the informations necessary for compiling and running this model efficently } diff --git a/man/tp_compile.Rd b/man/tp_compile.Rd index 1799a58..29bc30e 100644 --- a/man/tp_compile.Rd +++ b/man/tp_compile.Rd @@ -16,9 +16,9 @@ tp_compile(model, method = "mcmc", ...) }} } \value{ -compiled_model from a compiled_model_Template +sampler from a sampler_T } \description{ -\code{tp_compile} takes TreePPL model and prepares it to be used by +\code{tp_compile} takes TreePPL model and create a sampler to be used by \code{\link[=tp_run]{tp_run()}}. } diff --git a/man/tp_compile_options.Rd b/man/tp_compile_options.Rd index 78c691f..dd41a36 100644 --- a/man/tp_compile_options.Rd +++ b/man/tp_compile_options.Rd @@ -7,7 +7,7 @@ tp_compile_options() } \value{ -A data frame with the output from the compiler's help +A data frame with some outputs from the compiler's help } \description{ Options that can be passed to TreePPL compiler diff --git a/man/tp_parse_host_rep.Rd b/man/tp_parse_host_rep.Rd index 66f7df7..e96f2b8 100644 --- a/man/tp_parse_host_rep.Rd +++ b/man/tp_parse_host_rep.Rd @@ -11,7 +11,7 @@ tp_parse_host_rep(treeppl_out) produced by \link{tp_run}.} } \value{ -A list (n = n_runs) of data frames with the output from inference +A list (n = sweeps) of data frames with the output from inference in TreePPL under the host repertoire evolution model. } \description{ diff --git a/man/tp_run.Rd b/man/tp_run.Rd index 38dffbf..0c06f8c 100644 --- a/man/tp_run.Rd +++ b/man/tp_run.Rd @@ -4,11 +4,10 @@ \alias{tp_run} \title{Run a TreePPL program} \usage{ -tp_run(compiled_model, data, dir = NULL, out_file_name = "out", ...) +tp_run(sampler, data, dir = NULL, out_file_name = "out", ...) } \arguments{ -\item{compiled_model}{a \link[base:character]{base::character} with the full path to the compiled model -outputted by \link{tp_compile}.} +\item{sampler}{a \link{sampler_T} outputted by \link{tp_compile}.} \item{data}{a \link[base:character]{base::character} with the full path to the data file in TreePPL JSON format (as outputted by \link{tp_data}).} @@ -48,6 +47,6 @@ exe_path <- tp_compile(model = "coin", method = "mcmc-naive", iterations = 2000) data_path <- tp_data(data_input = "coin") # run TreePPL -result <- tp_run(exe_path, data_path, n_runs = 2) +result <- tp_run(exe_path, data_path) } } diff --git a/man/tp_runtime_options.Rd b/man/tp_runtime_options.Rd new file mode 100644 index 0000000..e2cafc7 --- /dev/null +++ b/man/tp_runtime_options.Rd @@ -0,0 +1,14 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/model.R +\name{tp_runtime_options} +\alias{tp_runtime_options} +\title{Options that can be passed to TreePPL run} +\usage{ +tp_runtime_options() +} +\value{ +A data frame with with some outputs from compiler's help +} +\description{ +Options that can be passed to TreePPL run +} diff --git a/tests/testthat/test-model.R b/tests/testthat/test-model.R index caafdb0..a2cd77f 100644 --- a/tests/testthat/test-model.R +++ b/tests/testthat/test-model.R @@ -10,24 +10,24 @@ cat(crayon::yellow("\nTest-model : Compilation and modification of the options.\ test_that("Test-model_1a : tp_compile MCMC", { cat("\tTest-model_1a : tp_compile MCMC \n") - model <- treepplr::tp_compile("crbd") + sampler <- treepplr::tp_compile("crbd") - expect_no_error(readBin(model$exe_path, "raw", 10e6)) + expect_no_error(readBin(sampler$exe_path, "raw", 10e6)) }) test_that("Test-model_1b : tp_compile method SMC", { cat("\tTest-model_1b : tp_compile method SMC \n") - model <- treepplr::tp_compile("crbd", method = "smc-bpf") + sampler <- treepplr::tp_compile("crbd", method = "smc-bpf") - expect_no_error(readBin(model$exe_path, "raw", 10e6)) + expect_no_error(readBin(sampler$exe_path, "raw", 10e6)) }) test_that("Test-model_2a : tp_compile model name", { cat("\tTest-model_2a : tp_compile\n") - model <- treepplr::tp_compile("crbd") + sampler <- treepplr::tp_compile("crbd") version <- list.files("/tmp", pattern = paste0("treeppl-", TPPLC_VERSION), @@ -35,7 +35,7 @@ test_that("Test-model_2a : tp_compile model name", { model_right = system(paste0("find ", version, " -name crbd.tppl"), intern = T) - expect_equal(readr::read_file(model$path), readr::read_file(model_right)) + expect_equal(readr::read_file(sampler$model_path), readr::read_file(model_right)) }) test_that("Test-model_2b : tp_compile model path ", { @@ -46,9 +46,9 @@ test_that("Test-model_2b : tp_compile model path ", { full.names = TRUE) model_right = system(paste0("find ", version, " -name crbd.tppl"), intern = T) - model <- treepplr::tp_compile(model_right) + sampler <- treepplr::tp_compile(model_right) - expect_equal(model$path, model_right) + expect_equal(sampler$model_path, model_right) }) test_that("Test-model_3a : tp_write path", { @@ -75,8 +75,8 @@ test_that("Test-model_4 : tp_compile model string ", { cat("\tTest-model_4 : tp_compile\n") model_string <- "model function blo() => Int {let blo = 3; return blo;}" - model <- treepplr::tp_compile(model_string) + sampler <- treepplr::tp_compile(model_string) model_right <- paste0(temp_dir, "tmp_model_file.tppl") - expect_equal(model$path, model_right) + expect_equal(sampler$model_path, model_right) }) diff --git a/vignettes/coin-example.Rmd b/vignettes/coin-example.Rmd index 7a0198f..41faa82 100644 --- a/vignettes/coin-example.Rmd +++ b/vignettes/coin-example.Rmd @@ -45,11 +45,11 @@ can find all available models and some information about them [here](https://tre If you want to look at the TreePPL code here in R, you can use the following functions: ```{r, eval = FALSE} -model_path <- tp_compile("coin") +sampler <- tp_compile("coin") ``` ```{r, eval=FALSE} -readr::read_file(model_path$path) +readr::read_file(sampler$model_path) ``` The main part of the model is defined in a function called `coinModel`: diff --git a/vignettes/treepplr.Rmd b/vignettes/treepplr.Rmd index 212267f..fcb598c 100644 --- a/vignettes/treepplr.Rmd +++ b/vignettes/treepplr.Rmd @@ -103,10 +103,10 @@ to run the chosen inference method. ```{r} # Using a model from the library and a Sequential Monte Carlo method -exe_path <- tp_compile(model = "crbd", method = "smc-apf", particles = 10000) +sampler <- tp_compile(model = "crbd", method = "smc-apf", particles = 10000) # Using a custom model and a Markov chain Monte Carlo method -exe_path <- tp_compile(model = model_path, method = "mcmc-lightweight", +sampler <- tp_compile(model = model_path, method = "mcmc-lightweight", iterations = 10000) ``` @@ -117,7 +117,7 @@ Now you are ready to run your analysis. All you have to do is to pass your data to the compiled executable and choose how many independent runs you want to do. ```{r} -output <- tp_run(compiled_model = exe_path, data = data_path, n_runs = 4) +output <- tp_run(compiled_model = sampler, data = data_path) ```