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
15 changes: 14 additions & 1 deletion R/create_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
#' functionality in pharmr/Pharmpy.
#'
#' @param data filename of dataset or data.frame as input to NONMEM / nlmixr.
#' @param route route of administration, either `oral` or `iv`
#' @param route route of administration. One of `"auto"` (default), `"oral"`,
#' `"sc"`, `"im"`, `"extravascular"`, or `"iv"`. When `"auto"`, the route is
#' inferred from `data` by comparing the `CMT` of dose events (`EVID=1`) to the
#' `CMT` of observation events (`EVID=0`): if any dose compartment is not also
#' an observation compartment (e.g. doses in `CMT=1`, observations in `CMT=2`),
#' the route is set to `"oral"`; otherwise `"iv"`. Falls back to `"iv"` if `data`
#' is `NULL` or the dataset lacks the required `CMT`/`EVID` columns.
#' @param lag_time add a lag time, default is `FALSE`
#' @param n_transit_compartments number of transit-compartments for absorption
#' model. Default is `0`.
Expand Down Expand Up @@ -200,6 +206,7 @@ create_model <- function(
## Pick route
if(route == "auto") {
route <- get_route_from_data(data)
if(verbose) cli::cli_alert_info("Auto-detected route from data: {route}")
}

## Read base model
Expand Down Expand Up @@ -770,6 +777,9 @@ get_route_from_data <- function(data, default = "iv") {
return(default)
}
dataset <- load_data_wrapper(data)
if(!all(c("EVID", "CMT") %in% names(dataset))) {
return(default)
}
dose_cmt <- dataset |>
dplyr::filter(.data$EVID == 1) |>
dplyr::pull("CMT") |>
Expand All @@ -778,6 +788,9 @@ get_route_from_data <- function(data, default = "iv") {
dplyr::filter(.data$EVID == 0) |>
dplyr::pull("CMT") |>
unique()
if(length(dose_cmt) == 0 || length(obs_cmt) == 0) {
return(default)
}
if(length(setdiff(dose_cmt, obs_cmt)) > 0) {
route <- "oral"
} else {
Expand Down
8 changes: 7 additions & 1 deletion man/create_model.Rd

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

8 changes: 7 additions & 1 deletion man/get_template_modelfile.Rd

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

27 changes: 27 additions & 0 deletions tests/testthat/test-create_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,33 @@ test_that("create_model basic functionality works", {
expect_true(!grepl("POP_KA", mod_iv$code))
})

test_that("route = 'auto' infers route from CMT/EVID in data", {
iv_data <- data.frame(
ID = 1,
TIME = c(0, 1, 2),
DV = c(0, 10, 5),
AMT = c(100, 0, 0),
CMT = 1,
EVID = c(1, 0, 0),
MDV = c(1, 0, 0)
)
oral_data <- iv_data
oral_data$CMT <- c(1, 2, 2)

expect_equal(get_route_from_data(iv_data), "iv")
expect_equal(get_route_from_data(oral_data), "oral")
expect_equal(get_route_from_data(NULL), "iv")
expect_equal(get_route_from_data(iv_data[, c("ID", "TIME", "DV")]), "iv")

mod_iv <- create_model(data = iv_data, verbose = FALSE)
expect_s3_class(mod_iv, "pharmpy.model.external.nonmem.model.Model")
expect_true(!grepl("POP_MAT", mod_iv$code))

mod_oral <- create_model(data = oral_data, verbose = FALSE)
Comment on lines +75 to +79
expect_s3_class(mod_oral, "pharmpy.model.external.nonmem.model.Model")
expect_true(grepl("POP_MAT", mod_oral$code))
})

test_that("model features are correctly added", {
test_data <- data.frame(
ID = 1,
Expand Down
Loading