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
182 changes: 181 additions & 1 deletion tests/testthat/setup.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# =============================================================================
# Shared models compiled once at test session startup
# =============================================================================

# --- Library models (standard PK models) ---
mod_1cmt_iv <- new_ode_model("pk_1cmt_iv")
mod_2cmt_iv <- new_ode_model("pk_2cmt_iv")
mod_1cmt_oral <- new_ode_model("pk_1cmt_oral")

# --- Custom 1cmt oral with lagtime ---
mod_1cmt_oral_lagtime <- new_ode_model(
code = "
dAdt[0] = -KA * A[0]
Expand All @@ -11,7 +18,9 @@ mod_1cmt_oral_lagtime <- new_ode_model(
dose = list(cmt = 1, bioav = 1),
parameters = list(CL = 5, V = 50, KA = 0.5, TLAG = 0.83)
)
oral_1cmt_allometric <- new_ode_model( # also timevarying and dose-dependence factor

# --- 1cmt oral with allometric scaling, time-varying CL, dose-dependent V ---
oral_1cmt_allometric <- new_ode_model(
code = "
if(t<168.0) {
CLi = CL * pow(WT/70, 0.75)
Expand All @@ -32,3 +41,174 @@ oral_1cmt_allometric <- new_ode_model( # also timevarying and dose-dependence fa
declare_variables = c("CLi", "Vi"),
parameters = list(KA = 0.5, CL = 5, V = 50)
)

# --- From test_iov.R: IOV models ---
pars_iov <- list(
"kappa_CL_1" = 0,
"kappa_CL_2" = 0,
"kappa_CL_3" = 0,
"eta_CL" = 0,
"CL" = 5,
"V" = 50,
"KA" = 1
)
pars_iov_no_iov <- list(
"CL" = 5,
"V" = 50,
"KA" = 1
)
pk_iov_none <- new_ode_model(
code = "
dAdt[1] = -KA * A[1]
dAdt[2] = +KA * A[1] -(CL/V) * A[2]
",
obs = list(cmt = 2, scale = "V"),
dose = list(cmt = 1, bioav = 1),
parameters = names(pars_iov_no_iov),
cpp_show_code = FALSE
)
pk_iov <- new_ode_model(
code = "
CL_iov = CL * exp(kappa_CL + eta_CL);
dAdt[1] = -KA * A[1]
dAdt[2] = +KA * A[1] -(CL_iov/V) * A[2]
",
iov = list(
cv = list(CL = 0.2),
n_bins = 3
),
obs = list(cmt = 2, scale = "V"),
dose = list(cmt = 1, bioav = 1),
declare_variables = c("kappa_CL", "CL_iov"),
parameters = names(pars_iov),
cpp_show_code = FALSE
)

# --- From test_compare_results.R ---
dose_in_cmt_2 <- new_ode_model(
code = "
dAdt[1] = -KA * A[1];
dAdt[2] = KA*A[1] -(CL/V) * A[2]
dAdt[3] = S2*(A[2]-A[3])
",
obs = list(cmt = 2, scale = "V"),
dose = list(cmt = 2),
cpp_show_code = FALSE
)

# --- From test_cmt_mapping.R: 1cmt oral with cmt_mapping ---
pk1cmt_oral_cmt_mapping <- new_ode_model(
code = "dAdt[1] = -KA*A[1]; dAdt[2] = KA*A[1] - (CL/V)*A[2];",
obs = list(cmt = 2, scale = "V"),
cmt_mapping = list(oral = 1, infusion = 2, bolus = 2)
)

# --- From test_multi_obs.R: Multi-observation model ---
vars_multi_obs <- c("CONC", "METAB", "METAB2", "ACT")
pk_multi_obs <- new_ode_model(
code = "dAdt[1] = -(CL/V)*A[1]; CONC = 1000*A[1]/V; METAB = CONC/2; METAB2 = CONC * t; ACT = 15",
obs = list(variable = vars_multi_obs),
declare_variables = vars_multi_obs,
cpp_show_code = FALSE
)

# --- From test_mixture_model.R ---
covs_mixture <- list(WT = new_covariate(70))
mod_mixture <- new_ode_model(
code = "
dAdt[0] = -(CL*(WT/70.0)/V)*A[0];
",
pk_code = " ",
obs = list(cmt = 1, scale = "V"),
mixture = list(CL = list(values = c(5, 15), probability = 0.3)),
covariates = covs_mixture
)

# --- Conditional models (skip on CRAN due to compilation time) ---
if (identical(Sys.getenv("NOT_CRAN"), "true")) {

# Library models
pk_3cmt_iv <- new_ode_model("pk_3cmt_iv")
pk_2cmt_oral <- new_ode_model("pk_2cmt_oral")
pk_3cmt_oral <- new_ode_model("pk_3cmt_oral")
mod_1cmt_iv_mm <- new_ode_model("pk_1cmt_iv_mm")

# --- From test_advan_with_auc.R: Models with AUC compartments ---
parameters_advan_auc <- list(
CL = 10, V = 50, KA = 0.5, Q = 5, V2 = 100, Q2 = 3, V3 = 150, F1 = 1
)
mod_1cmt_auc <- new_ode_model(
code = "dAdt[1] = -(CL/V)*A[1]; dAdt[2] = A[1]/V;",
parameters = parameters_advan_auc
)
mod_2cmt_auc <- new_ode_model(
code = "
dAdt[1] = -(CL/V)*A[1] - (Q/V)*A[1] + (Q/V2)*A[2];
dAdt[2] = +(Q/V)*A[1] - (Q/V2)*A[2];
dAdt[3] = A[1]/V;
",
parameters = parameters_advan_auc
)
mod_3cmt_auc <- new_ode_model(
code = "
dAdt[1] = -(CL/V)*A[1] - (Q/V)*A[1] + (Q/V2)*A[2] - (Q2/V)*A[1] + (Q2/V3)*A[3];
dAdt[2] = (Q/V)*A[1] -(Q/V2)*A[2] ;
dAdt[3] = (Q2/V)*A[1] - (Q2/V3)*A[3];
dAdt[4] = A[1]/V;
",
parameters = parameters_advan_auc
)

# --- From test_timevar_cov.R: 2cmt with time-varying covariates ---
mod_2cmt_timevar <- new_ode_model(
code = "
dAdt[1] = -(Q/V)*A[1] + (Q/V2)*A[2] -(CLi/V)*A[1];
dAdt[2] = -(Q/V2)*A[2] + (Q/V)*A[1];
",
pk_code = "CLi = CL + CRCL",
obs = list(cmt = 2, scale = "V"),
covariates = list(CRCL = new_covariate(5)),
declare_variables = "CLi",
cpp = FALSE
)

# --- From test_t_init.R: Model with state initialization ---
mod_t_init <- new_ode_model(
code = "CLi = CL; Vi = V; dAdt[1] = -(CLi/Vi)*A[1]; CONC = A[1]/Vi",
state_init = "A[1] = TDM_INIT * Vi",
parameters = list(CL = 7.67, V = 97.7, TDM_INIT = 500),
obs = list(cmt = 1, scale = "Vi"),
declare_variables = c("CONC", "CLi", "Vi"),
cpp_show_code = FALSE
)

# --- From test_reparametrization.R: Carreno 2cmt model ---
covs_carreno <- list(
CRCL = new_covariate(5),
CL_HEMO = new_covariate(0)
)
model_carreno <- new_ode_model(
code = "
CLi = SCLInter + SCLSlope * (CRCL*16.667) + CL_HEMO \
Vi = V \
Qi = K12 * Vi \
V2i = Qi / K21 \
dAdt[0] = -(CLi/V)*A[0] - K12*A[0] + K21*A[1] \
dAdt[1] = K12*A[0] - K21*A[1] \
dAdt[2] = A[0]/V
",
parameters = list(
V = 25.76, SCLSlope = 0.036, K12 = 2.29, K21 = 1.44,
SCLInter = 0.18, TDM_INIT = 0
),
declare_variables = c("CLi", "Qi", "Vi", "V2i"),
covariates = covs_carreno,
obs = list(cmt = 1, scale = "V"),
reparametrization = list(
"CL" = "SCLInter + SCLSlope * (CRCL*16.667) + CL_HEMO",
"V" = "V",
"Q" = "K12 * V",
"V2" = "(K12 * V) / K21"
)
)
}
52 changes: 11 additions & 41 deletions tests/testthat/test_advan_with_auc.R
Original file line number Diff line number Diff line change
@@ -1,43 +1,14 @@
# Uses models and parameters defined in setup.R (conditional, NOT_CRAN only):
# - mod_1cmt_auc, mod_2cmt_auc, mod_3cmt_auc
# - parameters_advan_auc

if (identical(Sys.getenv("NOT_CRAN"), "true")) {
dose <- 100
interval <- 12
n_days <- 5
t_inf <- 1.5
parameters <- list(
CL = 10,
V = 50,
KA = 0.5,
Q = 5,
V2 = 100,
Q2 = 3,
V3 = 150,
F1 = 1
)
t_obs <- c(3, 6, 8, 23, 47)

## ODE models for testing
mod_1cmt <- new_ode_model(
code="dAdt[1] = -(CL/V)*A[1]; dAdt[2] = A[1]/V;",
parameters = parameters
)
mod_2cmt <- new_ode_model(
code="
dAdt[1] = -(CL/V)*A[1] - (Q/V)*A[1] + (Q/V2)*A[2];
dAdt[2] = +(Q/V)*A[1] - (Q/V2)*A[2];
dAdt[3] = A[1]/V;
",
parameters = parameters
)
mod_3cmt <- new_ode_model(
code="
dAdt[1] = -(CL/V)*A[1] - (Q/V)*A[1] + (Q/V2)*A[2] - (Q2/V)*A[1] + (Q2/V3)*A[3];
dAdt[2] = (Q/V)*A[1] -(Q/V2)*A[2] ;
dAdt[3] = (Q2/V)*A[1] - (Q2/V3)*A[3];
dAdt[4] = A[1]/V;
",
parameters = parameters
)

## bolus dataset
reg_bolus <- new_regimen(
amt = dose,
Expand All @@ -47,7 +18,7 @@ if (identical(Sys.getenv("NOT_CRAN"), "true")) {
)
data_bolus <- advan_create_data(
reg_bolus,
parameters = parameters,
parameters = parameters_advan_auc,
cmts = 5,
t_obs = t_obs
)
Expand All @@ -61,7 +32,7 @@ if (identical(Sys.getenv("NOT_CRAN"), "true")) {
)
data_infusion <- advan_create_data(
reg_infusion,
parameters = parameters,
parameters = parameters_advan_auc,
cmts = 6,
t_obs = t_obs
)
Expand All @@ -71,7 +42,7 @@ test_that("One compartment bolus ADVAN runs", {
skip_on_cran()
res1_iv_r <- advan("1cmt_iv_bolus", cpp=FALSE)(data_bolus)
res1_iv_c <- advan("1cmt_iv_bolus", cpp=TRUE)(data_bolus)
res1_iv_ode <- sim(ode = mod_1cmt, regimen = reg_bolus, parameters = parameters, t_obs = t_obs)
res1_iv_ode <- sim(ode = mod_1cmt_auc, regimen = reg_bolus, parameters = parameters_advan_auc, t_obs = t_obs)

# AUC R
expect_equal(round(res1_iv_r[res1_iv_r$TIME %in% t_obs,]$AUC, 5), round(res1_iv_ode[res1_iv_ode$comp == 2,]$y, 5))
Expand All @@ -84,7 +55,7 @@ test_that("Two compartment bolus ADVAN runs", {
skip_on_cran()
res2_iv_r <- advan("2cmt_iv_bolus", cpp=FALSE)(data_bolus)
res2_iv_c <- advan("2cmt_iv_bolus", cpp=TRUE)(data_bolus)
res2_iv_ode <- sim(ode = mod_2cmt, regimen = reg_bolus, parameters = parameters, t_obs = t_obs)
res2_iv_ode <- sim(ode = mod_2cmt_auc, regimen = reg_bolus, parameters = parameters_advan_auc, t_obs = t_obs)
expect_equal(
round(res2_iv_r[res2_iv_r$TIME %in% t_obs,]$AUC, 5),
round(res2_iv_c[res2_iv_c$TIME %in% t_obs,]$AUC, 5)
Expand All @@ -106,7 +77,7 @@ test_that("Two compartment infusion ADVAN runs", {
skip_on_cran()
res2_inf_r <- advan("2cmt_iv_infusion", cpp=FALSE)(data_infusion)
res2_inf_c <- advan("2cmt_iv_infusion", cpp=TRUE)(data_infusion)
res2_inf_ode <- sim(ode = mod_2cmt, regimen = reg_infusion, parameters = parameters, t_obs = t_obs)
res2_inf_ode <- sim(ode = mod_2cmt_auc, regimen = reg_infusion, parameters = parameters_advan_auc, t_obs = t_obs)

expect_equal(
round(res2_inf_r[res2_inf_r$TIME %in% t_obs,]$AUC, 5),
Expand All @@ -131,7 +102,7 @@ test_that("Three compartment bolus ADVAN runs", {
skip_on_cran()
res3_iv_r <- advan("3cmt_iv_bolus", cpp=FALSE)(data_bolus)
res3_iv_c <- advan("3cmt_iv_bolus", cpp=TRUE)(data_bolus)
res3_iv_ode <- sim(ode = mod_3cmt, regimen = reg_bolus, parameters = parameters, t_obs = t_obs)
res3_iv_ode <- sim(ode = mod_3cmt_auc, regimen = reg_bolus, parameters = parameters_advan_auc, t_obs = t_obs)
expect_equal(
round(res3_iv_r[res3_iv_r$TIME %in% t_obs,]$AUC, 5),
round(res3_iv_c[res3_iv_c$TIME %in% t_obs,]$AUC, 5)
Expand All @@ -153,7 +124,7 @@ test_that("Three compartment iv ADVAN runs", {
skip_on_cran()
res3_iv_r <- advan("3cmt_iv_infusion", cpp=FALSE)(data_infusion)
res3_iv_c <- advan("3cmt_iv_infusion", cpp=TRUE)(data_infusion)
res3_iv_ode <- sim(ode = mod_3cmt, regimen = reg_infusion, parameters = parameters, t_obs = t_obs)
res3_iv_ode <- sim(ode = mod_3cmt_auc, regimen = reg_infusion, parameters = parameters_advan_auc, t_obs = t_obs)
expect_equal(
round(res3_iv_r[res3_iv_r$TIME %in% t_obs,]$AUC, 5),
round(res3_iv_c[res3_iv_c$TIME %in% t_obs,]$AUC, 5)
Expand All @@ -170,4 +141,3 @@ test_that("Three compartment iv ADVAN runs", {
round(res3_iv_ode[res3_iv_ode$comp == 4,]$y, 5)
)
})

13 changes: 4 additions & 9 deletions tests/testthat/test_calc_ss_analytic.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Uses models defined in setup.R:
# - mod_1cmt_iv, mod_2cmt_iv, mod_1cmt_oral
# - pk_3cmt_iv, pk_2cmt_oral, pk_3cmt_oral (conditional, NOT_CRAN only)

# shared parameters
dose <- 100
interval <- 12
Expand All @@ -6,13 +10,6 @@ reg_oral <- new_regimen(amt = dose, interval = interval, n = n_ss, type = "oral"
reg_bolus <- new_regimen(amt = dose, interval = interval, n = n_ss, type = "bolus")
reg_inf <- new_regimen(amt = dose, interval = interval, n = n_ss, type = "infusion")
t_obs <- max(reg_oral$dose_times) + interval
# Uses models defined in setup.R

if (identical(Sys.getenv("NOT_CRAN"), "true")) {
pk_3cmt_iv <- new_ode_model("pk_3cmt_iv")
}

#delta <- function(x, ref) { abs(x-ref)/ref }

test_that("1 cmt oral", {
par <- list(CL = 5, V = 100, KA = 1)
Expand Down Expand Up @@ -72,7 +69,6 @@ test_that("1-cmt iv infusion", {

test_that("2-cmt oral", {
skip_on_cran()
pk_2cmt_oral <- new_ode_model("pk_2cmt_oral")
par <- list(CL = 5, V = 100, Q = 3, V2 = 150, KA = 1)
res_ana <- calc_ss_analytic(f = "2cmt_oral", dose = dose, interval = interval, parameters = par)
res_ode <- sim(pk_2cmt_oral, parameters = par, regimen = reg_oral, t_obs = t_obs, only_obs = F)$y
Expand All @@ -98,7 +94,6 @@ test_that("2-cmt infusion", {

test_that("3-cmt oral", {
skip_on_cran()
pk_3cmt_oral <- new_ode_model("pk_3cmt_oral")
par <- list(CL = 5, V = 100, Q = 3, V2 = 150, Q2 = 6, V3 = 250, KA = 1)
res_ana <- calc_ss_analytic(f = "3cmt_oral", dose = dose, interval = interval, parameters = par)
res_ode <- sim(pk_3cmt_oral, parameters = par, regimen = reg_oral, t_obs = t_obs, only_obs = F)$y
Expand Down
13 changes: 5 additions & 8 deletions tests/testthat/test_cmt_mapping.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
pk1cmt_oral_code <- new_ode_model(
code = "dAdt[1] = -KA*A[1]; dAdt[2] = KA*A[1] - (CL/V)*A[2];",
obs = list(cmt = 2, scale="V"),
cmt_mapping = list(oral = 1, infusion = 2, bolus = 2)
)
# Uses model defined in setup.R:
# - pk1cmt_oral_cmt_mapping

test_that("Compartment mapping is added to attributes", {
expect_equal(attr(pk1cmt_oral_code, "cmt_mapping")[["oral"]], 1)
expect_equal(attr(pk1cmt_oral_code, "cmt_mapping")[["infusion"]], 2)
expect_equal(attr(pk1cmt_oral_cmt_mapping, "cmt_mapping")[["oral"]], 1)
expect_equal(attr(pk1cmt_oral_cmt_mapping, "cmt_mapping")[["infusion"]], 2)
})

test_that("Admin route is interpreted and simulated correctly", {
Expand All @@ -18,7 +15,7 @@ test_that("Admin route is interpreted and simulated correctly", {
)
p <- list(KA = 1, CL = 5, V = 50)
res <- sim_ode(
ode = pk1cmt_oral_code,
ode = pk1cmt_oral_cmt_mapping,
parameters = p,
regimen = regimen,
only_obs = FALSE
Expand Down
Loading
Loading