Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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"))
Expand Down
1 change: 1 addition & 0 deletions R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@




#' Import data for TreePPL program
#'
#' @description
Expand Down
55 changes: 33 additions & 22 deletions R/model.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 <tpplc --help>
#' @returns A data frame with some outputs from the compiler's help <tpplc --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 <tpplc --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(
Expand All @@ -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)

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

Expand All @@ -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)

Expand All @@ -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:
Expand All @@ -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", ...) {
Expand All @@ -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)
}
2 changes: 1 addition & 1 deletion R/post_treatment.R
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 5 additions & 6 deletions R/run.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand All @@ -62,15 +61,15 @@ 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")
}

# Empty LD_LIBRARY_PATH from R_env for this command specifically
# 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)
Expand Down
2 changes: 2 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/tp_compile.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/tp_compile_options.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/tp_parse_host_rep.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions man/tp_run.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions man/tp_runtime_options.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 10 additions & 10 deletions tests/testthat/test-model.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,32 @@ 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),
full.names = TRUE)

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 ", {
Expand All @@ -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", {
Expand All @@ -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)
})
4 changes: 2 additions & 2 deletions vignettes/coin-example.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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`:
Expand Down
6 changes: 3 additions & 3 deletions vignettes/treepplr.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```

Expand All @@ -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)
```


Expand Down
Loading