Skip to content
Open
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
22 changes: 18 additions & 4 deletions R/model_from_api.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,25 @@ model_from_api <- function(url,
## Add a parameter and initialization code for setting the initial concentration based on a TDM value
def <- define_tdm_init_model(def)
}
if(!is.null(nonmem) && !is.null(def$implementations$nonmem)) {
nonmem <- paste(readLines(nonmem), collapse="\n")
if(!is.null(nonmem)) {
if(!is.null(def$implementations$nonmem)) {
nonmem <- paste(readLines(nonmem), collapse="\n")
} else {
if(verbose) {
message("- Model definition does not list a NONMEM implementation; ignoring `nonmem`.")
}
nonmem <- NULL
}
}
if(!is.null(ferx) && !is.null(def$implementations$ferx)) {
ferx <- paste(readLines(ferx), collapse="\n")
if(!is.null(ferx)) {
if(!is.null(def$implementations$ferx)) {
ferx <- paste(readLines(ferx), collapse="\n")
} else {
if(verbose) {
message("- Model definition does not list a FeRx implementation; ignoring `ferx`.")
}
ferx <- NULL
}
}
if(is.null(def$comments)) def$comments <- ""
mod <- NULL
Expand Down
67 changes: 67 additions & 0 deletions tests/testthat/test_ferx.R
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,70 @@ test_that("nonmem code is stored as attribute on directly compiled models", {
expect_equal(attr(mod, "nonmem"), nonmem_code)
expect_null(attr(mod_1cmt_iv, "nonmem"))
})

test_that("model_from_api resets nonmem and ferx to NULL when definition lacks implementation", {
captured <- NULL
local_mocked_bindings(
new_ode_model = function(...) {
captured <<- list(...)
"model"
}
)

model_from_api(
url = test_path("sample_json", "test_1cmt_iv.json5"),
model = "test_1cmt_iv",
nonmem = "path/to/model.mod",
ferx = "path/to/model.ferx",
verbose = FALSE,
force = TRUE
)
expect_null(captured$nonmem)
expect_null(captured$ferx)

expect_message(
expect_message(
model_from_api(
url = test_path("sample_json", "test_1cmt_iv.json5"),
model = "test_1cmt_iv",
nonmem = "path/to/model.mod",
ferx = "path/to/model.ferx",
verbose = TRUE,
force = TRUE
),
"NONMEM"
),
"FeRx"
) |> suppressMessages()
expect_null(captured$nonmem)
expect_null(captured$ferx)
})

test_that("model_from_api reads listed implementation and resets unlisted one to NULL", {
def_file <- tempfile(fileext = ".json5")
nonmem_file <- tempfile(fileext = ".mod")
on.exit(unlink(c(def_file, nonmem_file)), add = TRUE)
writeLines(c("$PROB test", "$PK"), nonmem_file)
def <- read_model_json(test_path("sample_json", "test_1cmt_iv.json5"))
def$implementations <- list(nonmem = TRUE)
writeLines(jsonlite::toJSON(def, auto_unbox = TRUE), def_file)

captured <- NULL
local_mocked_bindings(
new_ode_model = function(...) {
captured <<- list(...)
"model"
}
)

model_from_api(
url = def_file,
model = "test_1cmt_iv",
nonmem = nonmem_file,
ferx = "path/to/model.ferx",
verbose = FALSE,
force = TRUE
)
expect_equal(captured$nonmem, "$PROB test\n$PK")
expect_null(captured$ferx)
})