Skip to content
Open
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ vignettes/*.pdf
**/.DS_Store

# Symlink to inst/include for devtools::load_all()
nimbleModel/include
nimbleModel/include
20 changes: 13 additions & 7 deletions nimbleModel/R/all_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,12 @@ messageIfVerbose <- function(...) {


# Temporarily here as need for initial testing of processing.
# Mis-named now as it also allows for nFunctions.
is.rcf <- function(x, inputIsName = FALSE, where = -1) {
if (inputIsName) {
x <- get(x, pos = where)
}
if (inherits(x, "nfMethodRC")) {
if (inherits(x, "nfMethodRC") || inherits(x, "nFunction")) {
return(TRUE)
}
if (is.function(x)) {
Expand All @@ -184,12 +185,6 @@ is.rcf <- function(x, inputIsName = FALSE, where = -1) {
nimbleUniqueID <- labelFunctionCreator("UID")
nimbleModelID <- labelFunctionCreator("MID")


# temporary, as we have some tests of use of `c()` in indexing
nimC <- function(...) {
c(...)
}

evalNumeric <- function(expr) {
if (length(expr) == 1) {
return(expr)
Expand All @@ -202,3 +197,14 @@ evalNumeric <- function(expr) {
}
return(expr)
}

# Placeholders while we sort out packaging.

nC <- function(...) {
c(...)
}

nMul <- function(x, y) {
x %*% y
}

8 changes: 4 additions & 4 deletions nimbleModel/R/context.R
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ expandContextAndReplacements <- function(allReplacements, allReplacementNameExpr
innerLoopCode <- as.call(c(list(quote(`{`)), replacementRecordingCode, indexRecordingCode, quote(iAns <- iAns + 1)))

innerLoopCode <- context$embedCodeInForLoop(innerLoopCode, useContext)
# This is a hacky way to deal with `nimC`, which is not in `constantsCopy`.
if (length(innerLoopCode[[3]]) > 1 && innerLoopCode[[3]][[1]] == "nimC") {
# This is a hacky way to deal with `nC`, which is not in `constantsCopy`.
if (length(innerLoopCode[[3]]) > 1 && innerLoopCode[[3]][[1]] == "nC") {
innerLoopCode[[3]][[1]] <- quote(c)
}

Expand Down Expand Up @@ -265,8 +265,8 @@ determineContextSize <- function(context, useContext = rep(TRUE, length(context$
innerLoopCode <- quote(iAns <- iAns + 1)
innerLoopCode <- context$embedCodeInForLoop(innerLoopCode, useContext)

# This is a hacky way to deal with `nimC`, which is not in `evalEnv`.
if (length(innerLoopCode[[3]]) > 1 && innerLoopCode[[3]][[1]] == "nimC") {
# This is a hacky way to deal with `nC`, which is not in `evalEnv`.
if (length(innerLoopCode[[3]]) > 1 && innerLoopCode[[3]][[1]] == "nC") {
innerLoopCode[[3]][[1]] <- quote(c)
}

Expand Down
4 changes: 2 additions & 2 deletions nimbleModel/R/getSymbolicParentNodes.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ nimblePreevaluationFunctionNames <- c(
# distribution functions
paste0(c("d", "q", "p"), "t"),
paste0(c("d", "q", "p"), "exp"),
"nimC", "nimRep", "nimSeq", "diag",
"nC", "nimRep", "nimSeq", "diag", "nMul",
"nimNumeric", "nimMatrix", "nimArray",
"length"
)
Expand Down Expand Up @@ -143,7 +143,7 @@ nimbleOrRfunctionNames <- c(
# distribution functions
paste0(c("d", "r", "q", "p"), "t"),
paste0(c("d", "r", "q", "p"), "exp"),
"nimC", "nimRep", "nimSeq",
"nimC", "nimRep", "nimSeq", "nC", "nMul",
"diag",
"length"
)
Expand Down
2 changes: 1 addition & 1 deletion nimbleModel/R/indexConstraint.R
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ newIndexConstraint_fromSimple <- function(expr, slot, constants) {
slot
))
} else { # various other cases such as `x[2]`, `x[k+1]`, `x[c(2,3,5)]`, `x[c(2,3,k)]`.
if (expr[[1]] == "nimC") {
if (expr[[1]] == "nC") {
expr[[1]] <- quote(c)
}
result <- eval(expr, envir = constants)
Expand Down
4 changes: 2 additions & 2 deletions nimbleModel/R/indexRuleConstant.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ indexRuleConstant_setup <- function(toIndexExprList,
eval(toIndexExpr[[2]], envir = constants),
eval(toIndexExpr[[3]], envir = constants)
)
} else if (length(toIndexExpr) > 1 && toIndexExpr[[1]] == "nimC") {
toIndexExpr[[1]] <- quote(c) # `nimC` not readily available in series of environments.
} else if (length(toIndexExpr) > 1 && toIndexExpr[[1]] == "nC") { # TODO: revisit handling of c().
toIndexExpr[[1]] <- quote(c) # `nC` not readily available in series of environments.
toConstant <- indexRangeMatrixClass$new(as.matrix(eval(toIndexExpr, envir = constants)))
} else {
stop("input error in `", safeDeparse(toIndexExprList), "`.")
Expand Down
11 changes: 6 additions & 5 deletions nimbleModel/R/miscFunctions.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This needs to be sourced after `nimbleFunction` is defined, so can't be done in `distributions_inputList.R`.
calc_dmnormAltParams <- nimble::nimbleFunction(
calc_dmnormAltParams <- nCompiler::nFunction(
name = "calc_dmnormAltParams",
run = function(cholesky = double(2), prec_param = double(), return_prec = double()) {
fun = function(cholesky = double(2), prec_param = double(), return_prec = double()) {
if (prec_param == return_prec) {
ans <- t(cholesky) %*% cholesky
} else {
Expand All @@ -16,9 +16,9 @@ calc_dmnormAltParams <- nimble::nimbleFunction(
}
)

calc_dwishAltParams <- nimble::nimbleFunction(
calc_dwishAltParams <- nCompiler::nFunction(
name = "calc_dwishAltParams",
run = function(cholesky = double(2), scale_param = double(), return_scale = double()) {
fun = function(cholesky = double(2), scale_param = double(), return_scale = double()) {
if (scale_param == return_scale) {
ans <- t(cholesky) %*% cholesky
} else {
Expand All @@ -31,4 +31,5 @@ calc_dwishAltParams <- nimble::nimbleFunction(
returnType(double(2))
return(ans)
}
)
)

2 changes: 1 addition & 1 deletion nimbleModel/R/modelBaseClass.R
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ modelBase_nClass <- nClass(
getDependencies = function(nodes, omit = NULL, self = TRUE,
determOnly = FALSE, stochOnly = FALSE,
includeData = TRUE, dataOnly = FALSE,
includePredictive = nimble::getNimbleOption('getDependenciesIncludesPredictiveNodes'),
includePredictive = getNimbleModelOption('getDependenciesIncludesPredictiveNodes'),
predictiveOnly = FALSE, includeRHSonly = FALSE,
downstream = FALSE, immediateOnly = FALSE,
nodesAsChars = getNimbleModelOption("nodesAsChars"),
Expand Down
4 changes: 2 additions & 2 deletions nimbleModel/R/modelDecl.R
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ modelDeclClass <- R6Class(
if (!stoch) {
return(NULL)
}
types <- nimble:::distributions[[distributionName]]$types
types <- nimbleModel:::distributions[[distributionName]]$types
if (is.null(types)) {
return(NULL)
}
Expand Down Expand Up @@ -352,7 +352,7 @@ modelDeclClass <- R6Class(
}
}
if (is.c(origIndex)) { # Handles new cases like `y[c(2,3,5)] ~ dmnorm(...)`.
if (origIndex[[1]] == "nimC") {
if (origIndex[[1]] == "nC") {
logProbExpr[[i]] <- origIndex[[2]]
} else {
stop("unexpected input in `", safeDeparse(origIndex), "`.")
Expand Down
8 changes: 6 additions & 2 deletions nimbleModel/R/modelDef.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ modelDefClass <- R6Class(
checkAndAssignConstants(constants, code)

modelCode <<- codeProcessIfThenElse(code, constants, userEnv)
modelCode <<- nimble:::nf_changeNimKeywords(modelCode) # Formerly in `assignBUGScode`.

# Switch keyword name transformations as a test while we determine what happens here vs in nimble2.
# We need to not use nimble anymore.
#
modelCode <<- nCompiler:::nf_changeKeywords(modelCode) # Formerly in `assignBUGScode`.

# TODO: add this later.
# setModelValuesClassName()
Expand Down Expand Up @@ -575,7 +579,7 @@ modelDefClass <- R6Class(
if (decl$stoch) {
params <- as.list(valueExpr[-1]) # Extract the original distribution parameters.
paramNames <- names(valueExpr)[-1]
types <- nimble:::distributions[[decl$distributionName]]$types
types <- nimbleModel:::distributions[[decl$distributionName]]$types
# `types` may be NULL if all are scalar.

for (iParam in seq_along(params)) {
Expand Down
6 changes: 3 additions & 3 deletions nimbleModel/R/modelFunctions.R
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ taggedClass <- R6Class(
getDependencies <- function(model, nodes, omit = NULL,
self = TRUE, determOnly = FALSE, stochOnly = FALSE,
includeData = TRUE, dataOnly = FALSE,
includePredictive = nimble::getNimbleOption('getDependenciesIncludesPredictiveNodes'),
includePredictive = getNimbleModelOption('getDependenciesIncludesPredictiveNodes'),
predictiveOnly = FALSE, includeRHSonly = FALSE,
downstream = FALSE, immediateOnly = FALSE,
nodesAsChars = getNimbleModelOption("nodesAsChars"),
Expand Down Expand Up @@ -722,7 +722,7 @@ setupMargNodes <- function(model, paramNodes, randomEffectsNodes, calcNodes,
if (length(reCheck)) {
errorNodes <- head(reCheck, n = 4)
if (length(reCheck) > 4) moreText <- ", .." else moreText <- ""
extraMsg <- if (isTRUE(nimble::getNimbleOption("includeUnneededLatents"))) "" else " They will be omitted, but one can force inclusion with\n `nimbleOptions(includeUnneededLatents=TRUE)`.\n"
extraMsg <- if (isTRUE(getNimbleModelOption("includeUnneededLatents"))) "" else " They will be omitted, but one can force inclusion with\n `nimbleOptions(includeUnneededLatents=TRUE)`.\n"
messageIfVerbose(
" [Warning] There are some `randomEffectsNodes` provided that look like\n",
" they are not needed for the provided (or default) `paramNodes`:\n",
Expand All @@ -731,7 +731,7 @@ setupMargNodes <- function(model, paramNodes, randomEffectsNodes, calcNodes,
" (potentially in the control list) for the algorithm or as\n",
" an argument to `setupMargNodes`."
)
if (!isTRUE(nimble::getNimbleOption("includeUnneededLatents"))) {
if (!isTRUE(getNimbleModelOption("includeUnneededLatents"))) {
randomEffectsNodes <- setdiff_nodes(randomEffectsNodes, reCheck)
}
}
Expand Down
4 changes: 2 additions & 2 deletions nimbleModel/R/model_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ is.vectorized <- function(node) {

is.c <- function(node) {
if (is.character(node)) {
return(grepl("^nimC", node))
return(grepl("^nC", node))
} else {
return("nimC" %in% all.names(node))
return("nC" %in% all.names(node))
}
}

Expand Down
4 changes: 3 additions & 1 deletion nimbleModel/R/options.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
disallowMultivariateArgumentExpressions = TRUE,
legacy_nimble = FALSE, # TRUE means stricter use of original nimble behavior in some cases.
nodesAsChars = FALSE,
verbose = TRUE
verbose = TRUE,
getDependenciesIncludesPredictiveNodes = TRUE, ## may be toggled off during MCMC sampler building
includeUnneededLatents = FALSE
)
)

Expand Down
2 changes: 1 addition & 1 deletion nimbleModel/tests/testthat/test-graphRules.R
Original file line number Diff line number Diff line change
Expand Up @@ -2058,7 +2058,7 @@ test_that("non-sequential indexing in for loop or indexRule", {
NULL
)

rule <- graphRuleClass$new(toExpr = quote(y[nimC(2,3,5)]),
rule <- graphRuleClass$new(toExpr = quote(y[nC(2,3,5)]),
fromExpr = quote(x[1:3]),
context = context_0)

Expand Down
5 changes: 2 additions & 3 deletions nimbleModel/tests/testthat/test-setupMargNodes.R
Original file line number Diff line number Diff line change
Expand Up @@ -496,13 +496,12 @@ test_that("`setupMargNodes` handling of missing/extra latents", {
expect_identical(result$randomEffectsNodes[[1]]$toNodeChars(), c("b[1]", "b[2]"))
expect_identical(result$paramNodes[[1]]$toNodeChars(), c("mu"))

# TODO: presumably rework how this option is handled in nimble 2.
nimble::nimbleOptions(includeUnneededLatents = TRUE)
setNimbleModelOption('includeUnneededLatents', TRUE)
expect_message(result <- setupMargNodes(m, randomEffectsNodes = 'b'), "they are not needed for the provided")
expect_identical(result$randomEffectsNodes[[1]]$toNodeChars(), c("b[1]", "b[2]", "b[3]"))
expect_identical(result$paramNodes[[1]]$toNodeChars(), c("mu"))
## Note `b[3]` is not in `calcNodes` because `predictiveNodes` are excluded.
nimble::nimbleOptions(includeUnneededLatents = FALSE)
setNimbleModelOption('includeUnneededLatents', FALSE)
})

test_that("getConditionallyIndependentSets works with grouped data", {
Expand Down
Loading