diff --git a/NAMESPACE b/NAMESPACE index 314a32e8..6c09f664 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -76,6 +76,7 @@ S3method(print,bbi_nmboot_summary) S3method(print,bbi_nonmem_summary) S3method(print,bbi_process) S3method(print,model_tree_static) +S3method(print,nmtran_process) S3method(print_model_files,default) S3method(submit_model,bbi_base_model) S3method(submit_model,bbi_nmboot_model) @@ -194,6 +195,7 @@ export(replace_model_field) export(replace_note) export(replace_tag) export(run_log) +export(run_nmtran) export(setup_bootstrap_run) export(submit_model) export(submit_models) diff --git a/man/execute_nmtran.Rd b/man/execute_nmtran.Rd new file mode 100644 index 00000000..50d578df --- /dev/null +++ b/man/execute_nmtran.Rd @@ -0,0 +1,19 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/run-nmtran.R +\name{execute_nmtran} +\alias{execute_nmtran} +\title{Execute NMTRAN in a given directory} +\usage{ +execute_nmtran(nmtran_exe, mod_path, dir = NULL) +} +\arguments{ +\item{nmtran_exe}{Path to \code{NMTRAN} executable.} + +\item{mod_path}{Path of a model to evaluate. Should be relative to \code{dir}.} + +\item{dir}{Directory in which to execute the command.} +} +\description{ +Execute NMTRAN in a given directory +} +\keyword{internal} diff --git a/man/locate_nmtran.Rd b/man/locate_nmtran.Rd new file mode 100644 index 00000000..a1aeb357 --- /dev/null +++ b/man/locate_nmtran.Rd @@ -0,0 +1,23 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/run-nmtran.R +\name{locate_nmtran} +\alias{locate_nmtran} +\title{Search for and validate existence of an \code{NMTRAN} executable} +\usage{ +locate_nmtran(.mod = NULL, .config_path = NULL, nmtran_exe = NULL) +} +\arguments{ +\item{.mod}{a \code{bbr} model object} + +\item{.config_path}{Path to a bbi configuration file. If \code{NULL}, the +default, will attempt to use a \code{bbi.yaml} in the same directory as the +model.} + +\item{nmtran_exe}{Path to an \code{NMTRAN} executable. If \code{NULL}, will look for a +\code{bbi.yaml} file in the same directory as the model.} +} +\description{ +If \code{nmtran_exe = NULL}, this will look for a \code{bbi.yaml} file in the same +directory as the model. +} +\keyword{internal} diff --git a/man/print_bbi.Rd b/man/print_bbi.Rd index bc312230..50ee3fe8 100644 --- a/man/print_bbi.Rd +++ b/man/print_bbi.Rd @@ -6,6 +6,7 @@ \alias{print.bbi_model} \alias{print.bbi_nonmem_summary} \alias{print.bbi_nmboot_summary} +\alias{print.nmtran_process} \alias{print.model_tree_static} \title{Print methods for bbr objects} \usage{ @@ -17,6 +18,8 @@ \method{print}{bbi_nmboot_summary}(x, .digits = 3, .nrow = 10, ...) +\method{print}{nmtran_process}(x, ...) + \method{print}{model_tree_static}(x, newpage = is.null(vp), vp = NULL, ...) } \arguments{ @@ -59,6 +62,8 @@ will make for prettier formatting, especially of table outputs. \item \code{print(bbi_nmboot_summary)}: Prints a high level summary of a model from a \code{bbi_nmboot_summary} object +\item \code{print(nmtran_process)}: Prints the \code{NMTRAN} evaluation of a \code{bbi_model} model + \item \code{print(model_tree_static)}: Draw model tree as a static plot }} diff --git a/man/run_nmtran.Rd b/man/run_nmtran.Rd new file mode 100644 index 00000000..f6e71ea7 --- /dev/null +++ b/man/run_nmtran.Rd @@ -0,0 +1,34 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/run-nmtran.R +\name{run_nmtran} +\alias{run_nmtran} +\title{Run NMTRAN on a model object} +\usage{ +run_nmtran(.mod, .config_path = NULL, nmtran_exe = NULL, delete_on_exit = TRUE) +} +\arguments{ +\item{.mod}{a \code{bbr} model object} + +\item{.config_path}{Path to a bbi configuration file. If \code{NULL}, the +default, will attempt to use a \code{bbi.yaml} in the same directory as the +model.} + +\item{nmtran_exe}{Path to an \code{NMTRAN} executable. If \code{NULL}, will look for a +\code{bbi.yaml} file in the same directory as the model.} + +\item{delete_on_exit}{Logical. If \code{FALSE}, don't delete the temporary folder +containing the \code{NMTRAN} run.} +} +\description{ +Run NMTRAN on a model object +} +\examples{ +\dontrun{ +mod <- read_model(file.path(MODEL_DIR, 1)) +run_nmtran(mod) + +# Set the path to an NMTRAN executable +run_nmtran(mod, nmtran_exe = "/opt/NONMEM/nm75/tr/NMTRAN.exe") +} + +} diff --git a/tests/testthat/test-workflow-bbi.R b/tests/testthat/test-workflow-bbi.R index 9ab8f683..afb31811 100644 --- a/tests/testthat/test-workflow-bbi.R +++ b/tests/testthat/test-workflow-bbi.R @@ -305,5 +305,103 @@ withr::with_options(list( run_times <- model_summaries(mods) %>% check_run_times(.wait = FALSE) %>% suppressWarnings() expect_equal(dim(run_times), c(3, 3)) }) + + describe("run_nmtran", { + it("locate_nmtran", { + mod1 <- read_model(file.path(MODEL_DIR_BBI, "1")) + # Using model object, looks for bbi.yaml + nmtran_exe <- locate_nmtran(mod1) + # Confirm executable + expect_equal(as.character(nmtran_exe), "/opt/NONMEM/nm74gf/tr/NMTRAN.exe") + # Confirm NONMEM version + expect_equal(attr(nmtran_exe, "nonmem_version"), "nm74gf") + + # Passed executable + nmtran_exe <- locate_nmtran(mod1, nmtran_exe = "/opt/NONMEM/nm74gf/tr/NMTRAN.exe") + # Confirm executable + expect_equal(as.character(nmtran_exe), "/opt/NONMEM/nm74gf/tr/NMTRAN.exe") + # Confirm NONMEM version + expect_true(is.null(attr(nmtran_exe, "nonmem_version"))) + + # Passed config_path + nmtran_exe <- locate_nmtran(.config_path = file.path(MODEL_DIR_BBI, "bbi.yaml")) + # Confirm executable + expect_equal(as.character(nmtran_exe), "/opt/NONMEM/nm74gf/tr/NMTRAN.exe") + # Confirm NONMEM version + expect_equal(attr(nmtran_exe, "nonmem_version"), "nm74gf") + + # Wrong nmtran_exe path passed + expect_error( + locate_nmtran(mod1, nmtran_exe = "/opt/NONMEM/nm74gf/tr/NMTRAN2.exe"), + "Could not find an NMTRAN executable" + ) + + # no configuration file found + expect_error( + locate_nmtran(.config_path = file.path(tempdir(), "bbi.yaml")), + "No bbi configuration was found" + ) + }) + + it("execute_nmtran", { + # Execute in subdirectory to avoid messing with other tests + nmtran_dir <- file.path(MODEL_DIR_BBI, "nmtran") + fs::dir_create(nmtran_dir) + on.exit(fs::dir_delete(nmtran_dir), add = TRUE) + + # Copy model file into new model dir + fs::file_copy(CTL_TEST_FILE, nmtran_dir, overwrite = TRUE) + mod1 <- new_model(file.path(nmtran_dir, "1"), .overwrite = TRUE) + + # create new bbi.yaml + bbi_init( + nmtran_dir, + .nonmem_dir = Sys.getenv("BBR_TESTS_NONMEM_DIR", "/opt/NONMEM"), + .nonmem_version = Sys.getenv("BBR_TESTS_NONMEM_VERSION", "nm74gf"), + .bbi_args = list(mpi_exec_path = get_mpiexec_path()) + ) + + nmtran_exe <- locate_nmtran(mod1) + nmtran_results <- execute_nmtran( + nmtran_exe, mod_path = basename(get_model_path(mod1)), dir = nmtran_dir + ) + + # Check attributes + expect_equal(nmtran_dir, nmtran_results$run_dir) + expect_equal(nmtran_results$status_val, 0) + expect_equal(nmtran_results$status, "NMTRAN successful") + + # Test failure + data_path <- "test/this/path/data.csv" + modify_data_path_ctl(mod1, data_path) + + nmtran_results <- execute_nmtran( + nmtran_exe, mod_path = basename(get_model_path(mod1)), dir = nmtran_dir + ) + + # Check attributes + expect_equal(nmtran_results$status_val, 4) + expect_equal(nmtran_results$status, "NMTRAN failed. See errors.") + }) + + it("run_nmtran", { + # create model + mod1 <- read_model(file.path(MODEL_DIR_BBI, "1")) + + nmtran_results <- run_nmtran(mod1, delete_on_exit = FALSE) + on.exit(fs::dir_delete(nmtran_results$run_dir)) + + # Check attributes + expect_equal(get_model_path(mod1), nmtran_results$absolute_model_path) + expect_equal( + file.path(nmtran_results$run_dir, basename(get_model_path(mod1))), + nmtran_results$nmtran_model + ) + expect_equal(nmtran_results$nonmem_version, "nm74gf") + expect_equal(nmtran_results$status_val, 0) + expect_equal(nmtran_results$status, "NMTRAN successful") + }) + }) + }) # closing withr::with_options