Skip to content

Commit

Permalink
Merge pull request #114 from nlmixr2/113-na-babelmixr2
Browse files Browse the repository at this point in the history
Fix the `NA` runCommand that doesn't work as advertised
  • Loading branch information
mattfidler authored Aug 11, 2024
2 parents ee03d62 + 216d1f3 commit bd453c2
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 13 deletions.
12 changes: 11 additions & 1 deletion R/monolixNlmixr2est.R
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@
cmd <- rxode2::rxGetControl(ui, "runCommand", "")
if (is.character(cmd)) {
cmd <- .monolixRunCommand
} else if (is.na(cmd)) {
.minfo("run Monolix manually and rerun nlmixr()")
return(NULL)
} else if (!is.function(cmd)) {
stop("invalid value for monolixControl(runCommand=)",
call.=FALSE)
Expand Down Expand Up @@ -231,6 +234,7 @@
.mlxtran <- .ui$monolixMlxtranFile
.runLock <- .ui$monolixRunLock

.cmd <- rxode2::rxGetControl(.ui, "runCommand", "")
if (file.exists(.qs)) {
.minfo("load saved nlmixr2 object")
.ret <- qs::qread(.qs)
Expand Down Expand Up @@ -258,9 +262,11 @@
return(invisible())
}
.runLS <- FALSE
.cmd <- rxode2::rxGetControl(.ui, "runCommand", "")
if (!identical(.cmd, "")) {
.monolixRunner(ui=.ui)
if (is.na(.cmd)) {
return(invisible())
}
} else {
if (.hasLixoftConnectors()) {
.x <- try(lixoftConnectors::loadProject(.mlxtran), silent=TRUE)
Expand All @@ -282,6 +288,10 @@
}
}
} else {
if (is.na(.cmd)) {
.minfo(paste0("leaving alone monolix files because '", .model, "' is present"))
return(invisible())
}
.minfo(paste0("assuming monolix is running because '", .model, "' is present"))
}
if (!dir.exists(.exportPath)) {
Expand Down
10 changes: 4 additions & 6 deletions R/nonmemNlmixr2est.R
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,11 @@
quote=FALSE)
.minfo("done")
}
if (!rxode2::rxGetControl(.ui, "run", TRUE)) {
.cmd <- rxode2::rxGetControl(.ui, "runCommand", "")
if (!rxode2::rxGetControl(.ui, "run", TRUE) ||
is.na(.cmd)) {
.minfo("only exported NONMEM control stream/data")
return(invisible())
}
if (is.na(rxode2::rxGetControl(.ui, "runCommand", ""))) {
.minfo("not running NONMEM")
return(.ui)
return(invisible(.ui))
}
if (!file.exists(file.path(.exportPath, .ui$nonmemXml))) {
print(file.path(.exportPath, .ui$nonmemXml))
Expand Down
46 changes: 45 additions & 1 deletion tests/testthat/test-monolix.R
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ if (FALSE) {
low=NA_real_, hi=NA_real_)),
NULL)
})

}


Expand Down Expand Up @@ -267,3 +267,47 @@ test_that("monolix dsl", {
.ee(.rxToM("add.sd"), "add__sd")

})

test_that("monolix model creation without running", {
withr::with_tempdir({

one.cmt <- function() {
ini({
tka <- 0.45 ; label("Ka")
tcl <- log(c(0, 2.7, 100)) ; label("Log Cl")
tv <- 3.45; label("log V")
cl.wt <- 0
v.wt <- 0
eta.ka ~ 0.6
eta.cl ~ 0.3
eta.v ~ 0.1
add.sd <- 0.7
})
model({
ka <- exp(tka + eta.ka)
cl <- exp(tcl + eta.cl + WT * cl.wt)
v <- exp(tv + eta.v)+ WT ^ 2 * v.wt
d/dt(depot) <- -depot*ka
d/dt(central) <- depot*ka - cl*central/v
cp <-central/v
cp ~ add(add.sd)
})
}

files <- c("monolixTest-monolix.csv", "monolixTest-monolix.mlxtran",
"monolixTest-monolix.md5", "monolixTest-monolix.txt")

nlmixr2(one.cmt, nlmixr2data::theo_sd, "monolix",
monolixControl(runCommand=NA, modelName="monolixTest"))

lapply(files, function(f) { expect_true(file.exists(f)) })

nlmixr2(one.cmt, nlmixr2data::theo_sd, "monolix",
monolixControl(runCommand=NA, modelName="monolixTest"))
lapply(files, function(f) {
expect_true(file.exists(f))
unlink(f)
})

})
})
60 changes: 55 additions & 5 deletions tests/testthat/test-nonmem.R
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ withr::with_tempdir({
})

test_that("NONMEM dsl, full model", {

one.cmt <- function() {
ini({
tka <- 0.45 ; label("Ka")
Expand All @@ -145,16 +146,18 @@ withr::with_tempdir({
cp ~ add(add.sd)
})
}

expect_message(
ui <-
nlmixr(
one.cmt, data=nlmixr2data::Oral_1CPT, est="nonmem",
control=nonmemControl(runCommand=NA)
),
regexp="not running NONMEM"
regexp="only exported NONMEM"
)

expect_s3_class(ui, "rxUi")
expect_type(ui$nonmemModel, "character")

expect_equal(
ui$nonmemModel,
paste(
Expand Down Expand Up @@ -223,6 +226,7 @@ withr::with_tempdir({
collapse="\n"
)
)

})

# pk.turnover.emax3 <- function() {
Expand Down Expand Up @@ -600,13 +604,13 @@ withr::with_tempdir({
d/dt(A_tr2) = KTR * A_tr1 - KTR * A_tr2;
d/dt(A_tr3) = KTR * A_tr2 - KTR * A_tr3;
d/dt(A_circ) = KTR * A_tr3 - KTR * A_circ;

CIRC ~ prop(prop.err)
})
}

w <- wbc()

expect_equal(
w$nonmemErrF,
paste(c(
Expand Down Expand Up @@ -761,3 +765,49 @@ withr::with_tempdir({
})
})
})


test_that("nonmem model creation without running", {
withr::with_tempdir({

one.cmt <- function() {
ini({
tka <- 0.45 ; label("Ka")
tcl <- log(c(0, 2.7, 100)) ; label("Log Cl")
tv <- 3.45; label("log V")
cl.wt <- 0
v.wt <- 0
eta.ka ~ 0.6
eta.cl ~ 0.3
eta.v ~ 0.1
add.sd <- 0.7
})
model({
ka <- exp(tka + eta.ka)
cl <- exp(tcl + eta.cl + WT * cl.wt)
v <- exp(tv + eta.v)+ WT ^ 2 * v.wt
d/dt(depot) <- -depot*ka
d/dt(central) <- depot*ka - cl*central/v
cp <-central/v
cp ~ add(add.sd)
})
}

files <- c("nonmemTest.csv", "nonmemTest.md5", "nonmemTest.nmctl")

nlmixr2(one.cmt, nlmixr2data::theo_sd, "nonmem",
nonmemControl(runCommand=NA, modelName="nonmemTest"))

lapply(files, function(f) { expect_true(file.exists(file.path("nonmemTest-nonmem", f))) })

nlmixr2(one.cmt, nlmixr2data::theo_sd, "nonmem",
nonmemControl(runCommand=NA, modelName="nonmemTest"))

lapply(files, function(f) {
expect_true(file.exists(file.path("nonmemTest-nonmem", f)))
unlink(file.path("nonmemTest-nonmem", f))
})
unlink("nonmemTest-nonmem", recursive = TRUE)

})
})

0 comments on commit bd453c2

Please sign in to comment.