Skip to content

Commit

Permalink
compile creates missing directories (#119)
Browse files Browse the repository at this point in the history
* compile function creates missing directories
  • Loading branch information
gowerc authored Mar 17, 2023
1 parent f894d68 commit edd83f3
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 15 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export(SurvivalModel)
export(SurvivalWeibullPH)
export(as_stan_data)
export(as_vcov)
export(compileStanModel)
export(link_gsf_abstract)
export(link_gsf_dsld)
export(link_gsf_ttg)
Expand All @@ -36,6 +37,7 @@ export(sld)
exportMethods(as.StanModule)
exportMethods(as.character)
exportMethods(as.list)
exportMethods(compileStanModel)
exportMethods(getInits)
exportMethods(merge)
exportMethods(show)
Expand Down
5 changes: 4 additions & 1 deletion R/generics.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@


# "missing" = no argument provided
# "NULL" = explicit NULL
setClassUnion("empty", c("missing", "NULL"))

setGeneric(
name = "merge",
Expand All @@ -16,6 +18,7 @@ setGeneric(
def = function(x, file_path) standardGeneric("write_stan")
)

#' @export
setGeneric(
name = "compileStanModel",
def = function(object, exe_file) standardGeneric("compileStanModel")
Expand Down
11 changes: 3 additions & 8 deletions R/joint_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,14 @@ setMethod(
setMethod(
f = "compileStanModel",
signature = "JointModel",
definition = function(object, exe_file = NULL) {
if (is.null(exe_file)) {
exe_file = file.path(tempdir(), "model")
}
x <- cmdstanr::cmdstan_model(
stan_file = cmdstanr::write_stan_file(as.character(object)),
exe_file = exe_file
)
definition = function(object, exe_file) {
x <- compileStanModel(object@stan, exe_file)
invisible(x)
}
)



setMethod(
f = "sampleStanModel",
signature = "JointModel",
Expand Down
28 changes: 28 additions & 0 deletions R/stan_module.R
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,34 @@ setMethod(
)


#' @export
setMethod(
f = "compileStanModel",
signature = signature(object = "StanModule", exe_file = "character"),
definition = function(object, exe_file) {
if (!dir.exists(dirname(exe_file))) {
dir.create(dirname(exe_file), recursive = TRUE)
}
x <- cmdstanr::cmdstan_model(
stan_file = cmdstanr::write_stan_file(as.character(object)),
exe_file = exe_file
)
invisible(x)
}
)


#' @export
setMethod(
f = "compileStanModel",
signature = signature(object = "StanModule", exe_file = "empty"),
definition = function(object, exe_file) {
exe_file <- file.path(tempdir(), "model")
invisible(compileStanModel(object, exe_file))
}
)





Expand Down
1 change: 0 additions & 1 deletion misc/example_of_use.R
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ stan_data <- as_stan_data(dat_os, dat_lm, ~ cov_cat + cov_cont)

## Sample from JointModel

dir.create(path = file.path("local"), showWarnings = FALSE)
mp <- sampleStanModel(
jm,
data = stan_data,
Expand Down
1 change: 0 additions & 1 deletion misc/tests/gsf-no-link.R
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ stan_data$Times

## Sample from JointModel

dir.create(path = file.path("local"), showWarnings = FALSE)
mp <- sampleStanModel(
jm,
data = stan_data,
Expand Down
1 change: 0 additions & 1 deletion misc/tests/gsf-with-link.R
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ stan_data$Times

## Sample from JointModel

dir.create(path = file.path("local"), showWarnings = FALSE)
mp <- sampleStanModel(
jm,
data = stan_data,
Expand Down
4 changes: 1 addition & 3 deletions misc/tests/lm-with-link.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ jlist <- simulate_joint_data(
phi = 0.1,
.debug = TRUE
),
os_fun = sim_os_weibull(
os_fun = sim_os_exponential(
lambda = 0.00333, # 1/300
gamma = 0.97
)
)

Expand All @@ -61,7 +60,6 @@ stan_data <- as_stan_data(dat_os, dat_lm, ~ cov_cat + cov_cont)


## Sample from JointModel
dir.create(path = file.path("local"), showWarnings = FALSE)
mp <- sampleStanModel(
jm,
data = stan_data,
Expand Down
32 changes: 32 additions & 0 deletions test/test-compile.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

model <- "
data {
int n;
array[n] real x;
}
parameters {
real mu;
real sigma;
}
model {
target += normal_lpdf(x | mu, sigma);
}
"


test_that("compileStanModel doesn't error if the directory doesn't exist", {



smod <- StanModule(model)
fpath <- file.path(tempdir(), "abcd", "efg", "model")
z <- compileStanModel(smod, fpath)

expect_true(file.exists(fpath))
})




0 comments on commit edd83f3

Please sign in to comment.