Skip to content

Commit f3182ab

Browse files
committed
Update FORLOOP tests to use nimbleModel
1 parent 44e602b commit f3182ab

File tree

1 file changed

+69
-53
lines changed

1 file changed

+69
-53
lines changed

tests/testthat/test_FORLOOP.R

Lines changed: 69 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -210,87 +210,102 @@ test_that("replaceRanges", {
210210
test_that("FORLOOP", {
211211
comments_on <- nimbleOptions()$enableMacroComments
212212
nimbleOptions(enableMacroComments=FALSE)
213+
214+
# Basic for loop
215+
code <- nimbleCode({
216+
beta[1:10] ~ FORLOOP(dnorm(0, sd=10))
217+
})
218+
mod <- nimbleModel(code)
219+
213220
expect_equal(
214-
# macro
215-
nimble:::codeProcessModelMacros(nimbleCode({
216-
beta[1:10] ~ FORLOOP(dnorm(0, sd=10))
217-
}), modelInfo=list(), env=environment())$code,
218-
# reference
219-
nimbleCode({
220-
for (i_1 in 1:10){
221+
mod$getCode(),
222+
quote({
223+
for (i_1 in 1:10) {
221224
beta[i_1] ~ dnorm(0, sd = 10)
222-
}
223-
})
225+
}
226+
})
224227
)
225228

229+
# Nested for loops
230+
code <- nimbleCode({
231+
beta[1:2,1:10,1] ~ FORLOOP(dnorm(0, sd=10))
232+
})
233+
mod <- nimbleModel(code)
234+
226235
expect_equal(
227-
# macro
228-
nimble:::codeProcessModelMacros(nimbleCode({
229-
beta[1:2,1:10,1] ~ FORLOOP(dnorm(0, sd=10))
230-
}), modelInfo=list(), env=environment())$code,
231-
# reference
232-
nimbleCode({
233-
for (i_1 in 1:2) {
236+
mod$getCode(),
237+
quote({
238+
for (i_1 in 1:2) {
234239
for (i_2 in 1:10) {
235240
beta[i_1, i_2, 1] ~ dnorm(0, sd = 10)
236241
}
237-
}
242+
}
238243
})
239244
)
240245

246+
# Ignore macro if no bracket on LHS
247+
code <- nimbleCode({
248+
sigma ~ FORLOOP(dunif(0,10))
249+
})
250+
mod <- nimbleModel(code)
251+
241252
expect_equal(
242-
# macro
243-
nimble:::codeProcessModelMacros(nimbleCode({
244-
sigma ~ FORLOOP(dunif(0,10))
245-
}), modelInfo=list(), env=environment())$code,
246-
# reference
247-
nimbleCode({
248-
sigma ~ dunif(0, 10)
253+
mod$getCode(),
254+
quote({
255+
sigma ~ dunif(0, 10)
249256
})
250257
)
251258

259+
# Ignore macro if no index ranges on LHS
260+
code <- nimbleCode({
261+
beta[1,2] ~ FORLOOP(dnorm(0, sd=10))
262+
})
263+
mod <- nimbleModel(code)
264+
252265
expect_equal(
253-
# macro
254-
nimble:::codeProcessModelMacros(nimbleCode({
255-
beta[1,2] ~ FORLOOP(dnorm(0, sd=10))
256-
}), modelInfo=list(), env=environment())$code,
257-
# reference
258-
nimbleCode({
259-
beta[1,2] ~ dnorm(0, sd=10)
266+
mod$getCode(),
267+
quote({
268+
beta[1, 2] ~ dnorm(0, sd = 10)
260269
})
261270
)
262271

272+
# Three-layer for loop with combo of index ranges and parameters on LHS and RHS
273+
constants <- list(k=3, l=4)
274+
code <- nimbleCode({
275+
beta[1:10,1:k,1:l] ~ FORLOOP(dnorm(alpha[1:k, 1:10], sigma[1:l]))
276+
})
277+
mod <- nimbleModel(code, constants=constants)
278+
263279
expect_equal(
264-
# macro
265-
nimble:::codeProcessModelMacros(nimbleCode({
266-
beta[1:10,1:k,1:l] ~ FORLOOP(dnorm(alpha[1:k, 1:10], sigma[1:l]))
267-
}), modelInfo=list(), env=environment())$code,
268-
# reference
269-
nimbleCode({
270-
for (i_1 in 1:10) {
280+
mod$getCode(),
281+
quote({
282+
for (i_1 in 1:10) {
271283
for (i_2 in 1:k) {
272284
for (i_3 in 1:l) {
273-
beta[i_1, i_2, i_3] ~ dnorm(alpha[i_2, i_1], sigma[i_3])
285+
beta[i_1, i_2, i_3] ~ dnorm(alpha[i_2, i_1],
286+
sigma[i_3])
274287
}
275288
}
276-
}
289+
}
277290
})
278291
)
279292

280293
# Very complex nested brackets
294+
constants <- list(M=3, NS=c(2,3,4), sind=c(3,2,1), IDX=matrix(1:12, 3, 4))
295+
code <- nimbleCode({
296+
beta[sind[1:M],IDX[sind[1:M], 1:NS[1:M]]] ~ FORLOOP(dnorm(alpha[sind[1:M],IDX[sind[1:M], 1:NS[1:M]]], sigma[1:M]))
297+
})
298+
mod <- nimbleModel(code, constants=constants)
299+
281300
expect_equal(
282-
# macro
283-
nimble:::codeProcessModelMacros(nimbleCode({
284-
beta[sind[1:M],IDX[sind[1:M], 1:NS[1:M]]] ~ FORLOOP(dnorm(alpha[sind[1:M],IDX[sind[1:M], 1:NS[1:M]]], sigma[1:M]))
285-
}), modelInfo=list(), env=environment())$code,
286-
# reference
287-
nimbleCode({
288-
for (i_1 in 1:M) {
289-
for (i_2 in 1:NS[i_1]) {
290-
beta[sind[i_1], IDX[sind[i_1], i_2]] ~ dnorm(alpha[sind[i_1],
291-
IDX[sind[i_1], i_2]], sigma[i_1])
292-
}
293-
}
301+
mod$getCode(),
302+
quote({
303+
for (i_1 in 1:M) {
304+
for (i_2 in 1:NS[i_1]) {
305+
beta[sind[i_1], IDX[sind[i_1], i_2]] ~ dnorm(alpha[sind[i_1],
306+
IDX[sind[i_1], i_2]], sigma[i_1])
307+
}
308+
}
294309
})
295310
)
296311

@@ -299,7 +314,8 @@ test_that("FORLOOP", {
299314
expect_error(
300315
# macro
301316
FORLOOP$process(quote(y[1:M, 1:M] ~ FORLOOP(dnorm(mu[1:M, 1:M]))),
302-
modelInfo=modelInfo, .env=environment())
317+
modelInfo=modelInfo, .env=environment()),
318+
"duplicated index"
303319
)
304320

305321
nimbleOptions(enableMacroComments=comments_on)

0 commit comments

Comments
 (0)