Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Env is compressed #245

Merged
merged 6 commits into from
Sep 30, 2022
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
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,14 @@ S3method(nmObjGet,dataSav)
S3method(nmObjGet,default)
S3method(nmObjGet,env)
S3method(nmObjGet,estimationModel)
S3method(nmObjGet,finalUi)
S3method(nmObjGet,foceiControl)
S3method(nmObjGet,foceiThetaEtaParameters)
S3method(nmObjGet,hini)
S3method(nmObjGet,hmax)
S3method(nmObjGet,hmin)
S3method(nmObjGet,idLvl)
S3method(nmObjGet,iniUi)
S3method(nmObjGet,innerModel)
S3method(nmObjGet,innerModelForce)
S3method(nmObjGet,ipredModel)
Expand Down Expand Up @@ -109,6 +111,7 @@ S3method(nmObjGet,saemTransformedData)
S3method(nmObjGet,seed)
S3method(nmObjGet,sigma)
S3method(nmObjGet,simInfo)
S3method(nmObjGet,ui)
S3method(nmObjGet,warnings)
S3method(nmObjGetControl,default)
S3method(nmObjGetControl,fo)
Expand Down
1 change: 1 addition & 0 deletions R/focei.R
Original file line number Diff line number Diff line change
Expand Up @@ -1662,6 +1662,7 @@ attr(rxUiGet.foceiOptEnv, "desc") <- "Get focei optimization environment"
rm(list=.item, envir=.env)
}
}
assign("ui", rxode2::rxUiCompress(.env$ui), envir=.env)
})
if (any(names(.ret) == "CWRES") && regexpr("^fo", est) == -1) {
# focei is available; add objective function
Expand Down
9 changes: 7 additions & 2 deletions R/nlmixr2.R
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ nlmixr2 <- function(object, data, est = NULL, control = list(),
envir = parent.frame()) {
rxode2::rxUnloadAll()
assignInMyNamespace(".nlmixr2Time", proc.time())
assignInMyNamespace(".finalUiCompressed", FALSE)
.objectName <- try(as.character(substitute(object)), silent=TRUE)
if (inherits(.objectName, "try-error")) .objectName <- "object"
if (!identical(.objectName, "object")) {
Expand Down Expand Up @@ -156,6 +157,7 @@ nlmixr <- nlmixr2
assignInMyNamespace(".nlmixr2pipeControl", NULL)
assignInMyNamespace(".nlmixr2pipeTable", NULL)
assignInMyNamespace(".nlmixr2pipeEst", NULL)
assignInMyNamespace(".finalUiCompressed", TRUE)
rxode2::rxSetCovariateNamesForPiping(NULL)
}

Expand All @@ -165,7 +167,8 @@ nlmixr2.function <- function(object, data=NULL, est = NULL, control = NULL, tabl
save = NULL, envir = parent.frame()) {
on.exit(.nlmixr2clearPipe())
.args <- as.list(match.call(expand.dots = TRUE))[-1]
.uif <- rxode2::rxode(object)
.uif <- rxode2::rxode2(object)
.uif <- rxode2::rxUiDecompress(.uif)
if (!is.null(.nlmixr2objectName)) {
if (!identical(.nlmixr2objectName, "object")) {
assign("modelName", .nlmixr2objectName, envir=.uif)
Expand Down Expand Up @@ -220,6 +223,7 @@ nlmixr2.rxUi <- function(object, data=NULL, est = NULL, control = NULL, table =
.modelName <- try(as.character(substitute(object)), silent=TRUE)
if (inherits(.modelName, "try-error")) .modelName <- NULL
.uif <- object
.uif <- rxode2::rxUiDecompress(.uif)
if (is.null(.uif$modelName)) assign("modelName", .modelName, envir=.uif)
if (is.null(data) && missing(est)) {
return(.uif)
Expand Down Expand Up @@ -306,7 +310,8 @@ nlmixr2.nlmixr2FitCore <- function(object, data=NULL, est = NULL, control = NULL
table <- getValidNlmixrControl(table, "tableControl")
}
.env <- new.env(parent=emptyenv())
.env$ui <- .nlmixrPreprocessUi(object$ui)
.ui <- rxode2::rxUiDecompress(object$ui)
.env$ui <- .nlmixrPreprocessUi(.ui)
.env$data <- data
.env$control <- control
.env$table <- table
Expand Down
6 changes: 5 additions & 1 deletion R/nlmixr2Est.R
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ nlmixr2Est.default <- function(env, ...) {
#' @noRd
nlmixr2Est0 <- function(env, ...) {
rxode2::rxUnloadAll()
.ui <- rxode2::rxUiDecompress(env$ui)
assign("ui", .ui, envir=env)
if (!exists("missingTable", envir=env)) {
assign("missingTable", FALSE, envir=env)
}
Expand All @@ -127,7 +129,9 @@ nlmixr2Est0 <- function(env, ...) {
}
if (inherits(env$ui, "rxUi")) {
.modelName <- env$ui$modelName
assign("ui", env$ui$fun(), envir=env) # re-evaluate so it doesn't overwrite inital ui
assign("ui",
rxode2::rxUiDecompress(env$ui$fun()),
envir=env) # re-evaluate so it doesn't overwrite inital ui
assign("modelName", .modelName, envir=env$ui)
}
.doIt <- TRUE
Expand Down
4 changes: 2 additions & 2 deletions R/nlmixr2output.R
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ VarCorr.nlmixr2FitCoreSilent <- VarCorr.nlmixr2FitCore
.sigma <- function(x) {
.ret <- x$nlme
if (is.null(.ret)) {
if (exists("uif", envir = x$env)) {
if (exists("ui", envir = x$env)) {
.df <- as.data.frame(x$uif$ini)
.errs <- paste(.df[which(!is.na(.df$err)), "name"])
return(fixef(x)[.errs])
Expand Down Expand Up @@ -521,7 +521,7 @@ vcov.nlmixr2FitCoreSilent <- vcov.nlmixr2FitCore
# Update initial estimates to match current initial estimates
.ui <- x$ui
.iniDf <- .ui$iniDf

assign("iniDf0", .iniDf, envir=x)
if (exists("fullTheta", x)) {
.thetas <- x$fullTheta
} else if (exists("fixef", x)) {
Expand Down
31 changes: 31 additions & 0 deletions R/nmObjGet.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,33 @@ nmObjGet <- function(x, ...) {
UseMethod("nmObjGet")
}

#' @export
nmObjGet.iniUi <- function(x, ...) {
.env <- x[[1]]
.ui <- .cloneEnv(rxode2::rxUiDecompress(get("ui", .env)))
.iniDf <- get("iniDf0", envir=.env)
if (is.null(.iniDf)) return(NULL)
assign("iniDf", .iniDf, envir=.ui)
rxode2::rxUiCompress(.ui)
}
attr(nmObjGet.iniUi, "desc") <- "The initial ui used to run the model"

.finalUiCompressed <- TRUE
#' @export
nmObjGet.finalUi <- function(x, ...) {
.env <- x[[1]]
.ui <- .cloneEnv(rxode2::rxUiDecompress(get("ui", .env)))
if (.finalUiCompressed) {
rxode2::rxUiCompress(.ui)
} else {
rxode2::rxUiDecompress(.ui)
}
}
attr(nmObjGet.finalUi, "desc") <- "The final ui used to run the model"

#' @export
nmObjGet.ui <- nmObjGet.finalUi

#' Get an item from a nlmixr2FitData object
#'
#' @param x A specialized list with:
Expand Down Expand Up @@ -104,6 +131,10 @@ nmObjGet.default <- function(x, ...) {
}
# Now get the ui, install the control object temporarily and use `rxUiGet`
.ui <- get("ui", envir=.env)
.ui <- rxode2::rxUiDecompress(.ui)
on.exit({
assign("ui", rxode2::rxUiCompress(.ui), envir=.env)
})
.ctl <- nmObjGetControl(.createEstObject(x[[1]]), ...)
if (!is.null(.ctl)) {
assign("control", .ctl, envir=.ui)
Expand Down
2 changes: 1 addition & 1 deletion R/preProcessModel.R
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
.ret <- ui
.zeroEtas <- .getZeroEtasFromModel(.ret)
if (length(.zeroEtas) > 0) {
assignInMyNamespace(".nlmixrPureInputUi", .ret)
assignInMyNamespace(".nlmixrPureInputUi", rxode2::rxUiDecompress(.ret))
.minfo(paste0("the following etas are removed from the model since their initial estimates are zero: ",
paste(.zeroEtas, collapse=", ")))
.ret <- .downgradeEtas(ui, zeroEtas=.zeroEtas)
Expand Down