Skip to content

Commit

Permalink
Update function run_treeppl
Browse files Browse the repository at this point in the history
  • Loading branch information
maribraga committed Jan 9, 2024
1 parent 4c23e26 commit 71f21d7
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 18 deletions.
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ Imports:
dplyr,
jsonlite,
magrittr,
rlang
rlang,
strex,
tidyr
Suggests:
ggplot2,
knitr,
Expand Down
22 changes: 6 additions & 16 deletions R/run_treeppl.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#' @param dir The directory that contains the TreePPL code and input file. The executable and the output files will be written here.
#' @param source Name of file with TreePPL program.
#' @param method Inference method to be used.
#' @param data Name of object with input data.
#' @param data_path Path to JSON file with input data.
#' @param samples The number of samples during inference.
#'
#' @return A data frame with sampled values, log weights, normalized weights, and the normalizing constant for all samples.
Expand All @@ -20,39 +20,29 @@
#' ggplot2::theme_bw()
#' }
#' }
run_treeppl <- function(dir = NULL, source = NULL, method = "smc-bpf", data = NULL, samples = 1000) { # smc-apf
run_treeppl <- function(dir = NULL, source = NULL, method = "smc-bpf", data_path = NULL, samples = 1000) { # smc-apf

# check inputs

if(method == "smc-apf") samples <- samples + 1

# Compile program
system2(command = "tpplc", args = c(paste0(dir,"/", source),
paste0("-m ", method),
paste0("--output ", dir,"/out")))
# which arguments are necessary other than method?
# should the executable go to a temporary folder and be delete afterwards?

# write json with input data
input_json <- jsonlite::toJSON(data, dataframe = "columns")
write(input_json, paste0(dir,"/input.json"))

# run
system2(command = paste0(dir,"/out"),
args = c(paste0(dir,"/input.json"),
args = c(data_path,
paste0(samples, " 1")),
stdout = paste0(dir,"/stdout.json")
)

# read output
output <- jsonlite::fromJSON(paste0(dir,"/stdout.json")) %>%
as.data.frame() %>%
dplyr::mutate(nweights = norm_weights(.data$weights))
output <- read_treeppl_output(paste0(dir,"/stdout.json"))

return(output)
}

# Function to get normalize weights from log weights
norm_weights <- function(weights_vector) { # input log weights from TreePPL
lweights <- as.numeric(weights_vector) # making sure weights are numeric vector
weights <- exp(lweights - max(lweights)) # exponentiate the difference between the weight and the maximum of the weights
weights / sum(weights) # normalising weights
}
23 changes: 23 additions & 0 deletions R/treeppl_input.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#' Prepare input for inference with TreePPL
#'
#' This function takes R objects and writes out an JSON file to be used by `run_treeppl()`.
#'
#' @param object_list A list of R objects to be included in the JSON input file.
#' @param file_path The complete path to where the JSON file is to be written.
#'
#' @return Path to a JSON file
#' @export
#'
#' @examples
treeppl_input <- function(object_list, file_path){

# process one element at a time?

# figure out the class and if anything special is necessary

# write json with input data
input_json <- jsonlite::toJSON(data, dataframe = "columns")
write(input_json, paste0(dir,"/input.json"))


}
4 changes: 3 additions & 1 deletion vignettes/coin-example.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ Run the `coin.tppl` TreePPL program using the created input data.
```{r, eval=FALSE}
coinflips <- tibble(coinflips = sample(c(TRUE, FALSE), 20, replace = TRUE))
input <- treeppl_input(coinflips)
output <- run_treeppl(dir = system.file("extdata", package = "treepplr"),
source = "coin.tppl", data = coinflips, samples = 100)
source = "coin.tppl", data = input, samples = 100)
```

Expand Down

0 comments on commit 71f21d7

Please sign in to comment.