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
7 changes: 6 additions & 1 deletion packages/nimble/R/BUGS_modelDef.R
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,9 @@ modelDefClass$methods(assignDimensions = function(dimensions, initsList, dataLis
if(!(length(initDim) == 1 && initDim == 1)) { # i.e., non-scalar inits; 1-length vectors treated as scalars and not passed along as dimension info to avoid conflicts between scalars and one-length vectors/matrices/arrays in various places
if(initName %in% names(dL)) {
if(!identical(as.numeric(dL[[initName]]), as.numeric(initDim))) {
messageIfVerbose(' [Warning] Inconsistent dimensions between inits and dimensions arguments: ', initName, '; ignoring dimensions in inits.')
messageIfVerbose(" [Warning] Inconsistent dimensions between `inits` and `dimensions`\n",
" Ignoring dimensions in `dimensions`.")
dL[[initName]] <- initDim ## Changed 2025-07-09 to avoid inconsistent compiled/uncompiled behavior.
}
} else {
dL[[initName]] <- initDim
Expand Down Expand Up @@ -2732,6 +2734,9 @@ modelDefClass$methods(genVarInfo3 = function() {
if(!(dimVarName %in% names(varInfo))) next
if(length(dimensionsList[[dimVarName]]) != varInfo[[dimVarName]]$nDim) stop('inconsistent dimensions for variable ', dimVarName)
if(any(dimensionsList[[dimVarName]] < varInfo[[dimVarName]]$maxs)) stop(paste0('dimensions specified are smaller than model specification for variable \'', dimVarName, '\''))
if(any(dimensionsList[[dimVarName]] > varInfo[[dimVarName]]$maxs))
messageIfVerbose(" [Warning] dimensions specified are larger than model specification\n",
" for variable `", dimVarName, "`.")
varInfo[[dimVarName]]$maxs <<- dimensionsList[[dimVarName]]
}

Expand Down
31 changes: 27 additions & 4 deletions packages/nimble/tests/testthat/test-models.R
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,29 @@ test_that("test of using dimensions of inits when dimension information not avai
expect_error(m <- nimbleModel(code, data = list(y = rep(1, 3))), info = "expected error because dimension of mu is unknown")
m <- nimbleModel(code, data = list(y = rep(1, 3)), inits = list(k = rep(1, 3), mu = 1:5))
expect_equal(m$modelDef$dimensionsList$mu, 5, info = "dimension for mu not equal to that given in inits")
expect_message(m <- nimbleModel(code, data = list(y = rep(1, 3)), inits = list(k = rep(1, 3), mu = 1:8), dimensions = list(mu = 5)), "Inconsistent dimensions between inits and dimensions")
expect_message(m <- nimbleModel(code, data = list(y = rep(1, 3)), inits = list(k = rep(1, 3), mu = 1:8), dimensions = list(mu = 5)), "Inconsistent dimensions between `inits` and `dimensions`")
})

test_that("test of using dimensions of inits different than LHS dimensions:", {
code <- nimbleCode({
for(i in 1:4){
y[i] ~ dnorm(S[site[i]], 1)
S[i] ~ dnorm(0,1)
}
})
expect_error(m <- nimbleModel(code, data = list(y = rnorm(4)), inits=list(site=rep(1,4), S=rnorm(2))),
"dimensions specified are smaller")

code <- nimbleCode({
for(i in 1:4){
y[i] ~ dnorm(S[site[i]], 1)
S[i] ~ dnorm(0,1)
}
})
expect_message(m <- nimbleModel(code, data = list(y = rnorm(4)), inits=list(site=rep(1,4), S=rnorm(10))),
"dimensions specified are larger than model specification")
cm <- compileNimble(m)
expect_identical(m$S, cm$S)
})

test_that("test of using dimensions of data when dimension information not available:", {
Expand Down Expand Up @@ -945,7 +967,8 @@ test_that("bad size or dimension of initial values", {

## For better or worse, length of 5 gets baked in based on inits.
## TODO: do we want to reconsider whether this should error out?
m <- nimbleModel(code, inits = list(z = rnorm(5)))
expect_message(m <- nimbleModel(code, inits = list(z = rnorm(5))),
"dimensions specified are larger than model specification")
cm <- compileNimble(m)
expect_identical(m$z, cm$z)

Expand All @@ -958,8 +981,8 @@ test_that("bad size or dimension of initial values", {
### matrix

## For better or worse, 4x2 gets baked in based on inits.
## TODO: do we want to reconsider whether this should error out?
m <- nimbleModel(code, inits = list(b = matrix(rnorm(8),4,2)))
expect_message(m <- nimbleModel(code, inits = list(b = matrix(rnorm(8),4,2))),
"dimensions specified are larger than model specification")
cm <- compileNimble(m)
expect_identical(m$b, cm$b)

Expand Down
Loading