Skip to content

Commit

Permalink
version 0.0.7
Browse files Browse the repository at this point in the history
  • Loading branch information
philipdelff authored and cran-robot committed Jan 9, 2024
1 parent a42f9d5 commit 0a207cb
Show file tree
Hide file tree
Showing 244 changed files with 36,724 additions and 760 deletions.
12 changes: 6 additions & 6 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
Package: NMsim
Type: Package
Title: Seamless 'Nonmem' Simulation Platform
Version: 0.0.6
Version: 0.0.7
Authors@R: c(person("Philip", "Delff", email = "philip@delff.dk",role = c("aut","cre")),
person("Matthew","Fidler", role = c("ctb"), email = "matt.fidler@novartis.com", comment="Co-author on NMreadCov"))
Maintainer: Philip Delff <philip@delff.dk>
Description: A complete and seamless 'Nonmem' simulation interface from within R. Turns 'Nonmem' control streams into simulation control streams, executes them with specified simulation input data and returns the results. The simulation is performed by 'Nonmem', eliminating time spent and risks of re-implementation of models in other tools.
License: MIT + file LICENSE
RoxygenNote: 7.2.3
Depends: R (>= 3.5.0)
Imports: data.table, NMdata (>= 0.1.1), R.utils, MASS
Suggests: testthat, knitr, rmarkdown, fst, dplyr, ggplot2, patchwork,
tibble, tracee, tidyr
Imports: data.table, NMdata (>= 0.1.3), R.utils, MASS, fst
Suggests: testthat, knitr, rmarkdown, dplyr, ggplot2, patchwork,
tibble, tracee, tidyr, tidyvpc
Encoding: UTF-8
BugReports: https://github.com/philipdelff/NMsim/issues
Language: en-US
URL: https://philipdelff.github.io/NMsim/
NeedsCompilation: no
Packaged: 2023-11-28 17:08:37 UTC; philipde
Packaged: 2024-01-08 17:18:29 UTC; philipde
Author: Philip Delff [aut, cre],
Matthew Fidler [ctb] (Co-author on NMreadCov)
Repository: CRAN
Date/Publication: 2023-11-28 17:40:13 UTC
Date/Publication: 2024-01-09 00:30:02 UTC
298 changes: 239 additions & 59 deletions MD5

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
# Generated by roxygen2: do not edit by hand

S3method(cbind,NMsimTab)
S3method(dimnames,NMsimTab)
S3method(merge,NMsimTab)
S3method(rbind,NMsimTab)
S3method(t,NMsimTab)
export(NMcreateDoses)
export(NMexec)
export(NMreadSim)
export(NMsim)
export(NMsim_VarCov)
export(NMsim_asis)
Expand All @@ -12,9 +18,13 @@ export(addEVID2)
export(addResVar)
export(genPhiFile)
export(inputArchiveDefault)
export(is.NMsimTab)
export(unNMsimTab)
import(MASS)
import(NMdata)
import(data.table)
import(fst)
importFrom(MASS,mvrnorm)
importFrom(R.utils,getAbsolutePath)
importFrom(stats,runif)
importFrom(utils,packageVersion)
22 changes: 19 additions & 3 deletions NEWS → NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
# 0.0.6
# NMsim 0.0.7
## New features

* Function `simPopEtas()` to generate a population from a model. The
population can be saved as a `phi` file to be reused in subsequent
simulations.

* Function NMreadSim provides a very simple interface to reading
simulation results. Especailly in cases where the simulation is
being parallelized or otherwise spawns multiple Nonmem jobs, this is
a useful feature.

* A list of simulation data sets will now be simulated with separate
Nonmem runs. This is an efficient way to parellelize large
simulation runs.

# NMsim 0.0.6
## New features

* Support for parallelization of simulations added when using PSN. It
Expand All @@ -11,7 +27,7 @@ separate threads. Now single simulation runs can be parallelized on
* A simple mistake would create problems in `genPhiFile()` when having
more than 10 ETAs in a model. Now fixed.

# 0.0.5
# NMsim 0.0.5
## New features

* Full support for models estimated with SAEM. Especially, simulation
Expand All @@ -37,7 +53,7 @@ separate threads. Now single simulation runs can be parallelized on
`phi`, `shk` - in addition to output table files and the archived
input data of course.

# 0.0.2
# NMsim 0.0.2
## New features

* NMsim supports `type.sim="typical"` which means all OMEGAS will be
Expand Down
36 changes: 25 additions & 11 deletions R/NMexec.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@
##' @param system.type A charachter string, either \"windows\" or
##' \"linux\" - case insensitive. Windows is only experimentally
##' supported. Default is to use \code{Sys.info()[["sysname"]]}.
##' @param quiet Suppress messages on what NMexec is doing? Default is
##' FALSE.
##' @details Use this to read the archived input data when retrieving
##' the nonmem results:
##' \code{NMdataConf(file.data=inputArchiveDefault)}
Expand Down Expand Up @@ -118,7 +120,7 @@ NMexec <- function(files,file.pattern,dir,sge=TRUE,input.archive,
nc=64,dir.data=NULL,wait=FALSE, args.psn.execute,
update.only=FALSE,nmquiet=FALSE,
method.execute="psn",dir.psn,path.nonmem,system.type,
files.needed){
files.needed,quiet=FALSE){


#### Section start: Dummy variables, only not to get NOTE's in pacakge checks ####
Expand Down Expand Up @@ -205,11 +207,10 @@ NMexec <- function(files,file.pattern,dir,sge=TRUE,input.archive,
files.exec <- findUpdated(files.all)
}

## message(paste(files.exec,collapse=", "))

for(file.mod in files.exec){
file.mod <- NMdata:::filePathSimple(file.mod)
message(paste0("Executing ",file.mod))
if(!quiet) message(paste0("Executing ",file.mod))
if(!file.exists(file.mod)){
stop(paste("Could not find file:",file.mod))
}
Expand All @@ -231,6 +232,13 @@ NMexec <- function(files,file.pattern,dir,sge=TRUE,input.archive,
}


if((sge && nc > 1)||(sge && method.execute=="psn")){
if(nc>1){
file.pnm <- file.path(rundir,"NMexec.pnm")
pnm <- NMgenPNM(nc=nc,file=file.pnm)
}
}

if(method.execute=="psn"){
##if(system.tpe=="linux"){

Expand All @@ -242,8 +250,6 @@ NMexec <- function(files,file.pattern,dir,sge=TRUE,input.archive,
if(sge){
string.cmd <- paste0(string.cmd," -run_on_sge")
if(nc>1){
file.pnm <- file.path(rundir,"NMexec.pnm")
pnm <- NMgenPNM(nc=nc,file=file.pnm)
string.cmd <- paste0(string.cmd," -sge_prepend_flags=\"-pe orte ",nc," -V\" -parafile=",basename(pnm)," -nodes=",nc)
}
}
Expand All @@ -261,12 +267,19 @@ NMexec <- function(files,file.pattern,dir,sge=TRUE,input.archive,
}
string.cmd <- NMexecDirectory(file.mod,path.nonmem,files.needed=files.needed)
if(sge) {

## string.cmd <- sprintf("cd %s; qsub -terse -wd \'%s\' %s",getwd(),dirname(string.cmd),string.cmd)
## I am not sure if absolute path is needed here.
string.cmd <- sprintf('cd "%s"; qsub -terse -wd \'%s\' %s',
getwd(),getAbsolutePath(dirname(string.cmd)),string.cmd)
## string.cmd <- paste0("CURWD=",getwd()," ",string.cmd)

if(nc==1){
## string.cmd <- sprintf("cd %s; qsub -terse -wd \'%s\' %s",getwd(),dirname(string.cmd),string.cmd)
## I am not sure if absolute path is needed here.
string.cmd <- sprintf('cd "%s"; qsub -terse -wd \'%s\' %s',
getwd(),getAbsolutePath(dirname(string.cmd)),string.cmd)
## string.cmd <- paste0("CURWD=",getwd()," ",string.cmd)

##### for nc>1 this can be used <nc> is nc evaluated
## qsub -pe orte <nc> -V -N <name for qstat> -j y -cwd -b y /opt/NONMEM/nm75/run/nmfe75 psn.mod psn.lst -background -parafile=/path/to/pnm [nodes]=<nc>
} else {
string.cmd <- sprintf('cd %s; qsub -pe orte %s -V -N NMsim -j y -cwd -b y %s %s %s -background -parafile=%s [nodes]=%s' ,getwd(),nc,path.nonmem,file.mod,fnExtension(file.mod,"lst"),pnm,nc)
}
wait <- TRUE
} else {
string.cmd <- sprintf("cd %s; ./%s",dirname(string.cmd),basename(string.cmd))
Expand All @@ -285,6 +298,7 @@ NMexec <- function(files,file.pattern,dir,sge=TRUE,input.archive,
shell(shQuote(path.script,type="cmd") )
}
if(system.type=="linux"){

if(nmquiet) string.cmd <- paste(string.cmd, ">/dev/null 2>&1")
if(!wait) string.cmd <- paste(string.cmd,"&")

Expand Down
4 changes: 1 addition & 3 deletions R/NMexecDirectory.R
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,7 @@ NMexecDirectory <- function(file.mod,path.nonmem,files.needed,dir.data=".."){
)

path.script <- file.path(dir.tmp,"run_nonmem.sh")
## con.newfile <- file(path.script,"wb")
## writeLines(lines.bash,con=con.newfile)
## close(con.newfile)

writeTextFile(lines.bash,path.script)
Sys.chmod(path.script,mode="0577")

Expand Down
7 changes: 4 additions & 3 deletions R/NMgenPNM.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ $IDRANGES ; USED IF PARSE_TYPE=3
1:1,50
2:51,100",as.character(nc),as.character(nc-1))

con.newfile <- file(file, "wb")
writeLines(text, con = con.newfile)
close(con.newfile)
## con.newfile <- file(file, "wb")
## writeLines(text, con = con.newfile)
## close(con.newfile)
writeTextFile(lines=text,file=file)

return(file)

Expand Down
2 changes: 2 additions & 0 deletions R/NMreadExt.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

NMreadExt <- function(path.ext){

.Deprecated("NMdata::NMreadExt")

#### Section start: Dummy variables, only not to get NOTE's in pacakge checks ####

ITERATION <- NULL
Expand Down
2 changes: 2 additions & 0 deletions R/NMreadPhi.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

NMreadPhi <- function(file){

.Deprecated("NMdata::NMreadPhi")

#### Section start: Dummy variables, only not to get NOTE's in pacakge checks ####

par.type <- NULL
Expand Down
93 changes: 93 additions & 0 deletions R/NMreadSim.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
##' Read simulation results based on NMsim's track of model runs
##' @param x Path to the simulation-specific rds file generated by
##' NMsim, typically called `NMsim_paths.rds`. Or a table of
##' simulation runs as returned by NMsim when `wait=FALSE`.
##' @param as.fun The default is to return data as a data.frame. Pass
##' a function (say tibble::as_tibble) in as.fun to convert to
##' something else. If data.tables are wanted, use
##' as.fun="data.table". The default can be configured using
##' NMdataConf.
##' @return A data set of class defined by as.fun
##' @import NMdata
##' @import fst
##' @export

NMreadSim <- function(x,as.fun){

. <- NULL
ROWMODEL2 <- NULL
args.NMscanData <- NULL
path.sim.lst <- NULL

file.res.data <- NULL

if(missing(as.fun)) as.fun <- NULL
as.fun <- NMdata:::NMdataDecideOption("as.fun",as.fun)

## when to look for combined and saved results?



## if path is a dir, search for rds

## if an rds, just read it
if(!is.list(x) && is.character(x)) {
tab.paths <- readRDS(x)

if(!inherits(tab.paths,"NMsimTab")) {
stop("The provided rds file does not contain a NMsimTab object")
}
file.res.data <- fnAppend(fnExtension(x,"fst"),"res")
} else if(is.NMsimTab(x)){
tab.paths <- x
}

## if an lst, read it
if(!is.null(file.res.data) &&
file.exists(file.res.data) &&
file.mtime(file.res.data)>file.mtime(x)){
res <- read_fst(file.res.data,as.data.table=TRUE)
} else {

### read all sim results
if(F){
## this simple approach may fail if the models return incompatible columns
res <- tab.paths[,{
cat(ROWMODEL2," ")
## the rds table must keep NMscanData arguments
args.NM <- args.NMscanData[[1]]
if(! "quiet" %in% names(args.NM)){
args.NM$quiet <- TRUE
}

do.call(NMscanData,c(list(file=path.sim.lst),args.NM))
},keyby=.(ROWMODEL2)]
}

#### must read each model into list elements. Then rbind(fill=T)
### this is to make sure results from different models with
### incompatible columns can be combined.

res.list <- lapply(split(tab.paths,by="ROWMODEL2"),function(dat){
dat[,{
## cat(ROWMODEL2," ")
## the rds table must keep NMscanData arguments
args.NM <- args.NMscanData[[1]]
if(! "quiet" %in% names(args.NM)){
args.NM$quiet <- TRUE
}

do.call(NMscanData,c(list(file=path.sim.lst),args.NM))
},by=.(ROWMODEL2)]
})
res <- rbindlist(res.list,fill=TRUE)
res[,ROWMODEL2:=NULL]

if(!is.null(file.res.data)){
NMwriteData(res,file=file.res.data,formats.write="fst",genText=F)
}
}

setattr(res,"NMsim-models",tab.paths)
return(as.fun(res))
}

0 comments on commit 0a207cb

Please sign in to comment.