Skip to content

Commit

Permalink
Merge pull request #134 from mrc-ide/mrc-6118
Browse files Browse the repository at this point in the history
Fix error while generating odin model
  • Loading branch information
weshinsley authored Dec 16, 2024
2 parents d98d665 + 40915b7 commit 250dc81
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: odin2
Title: Next generation odin
Version: 0.3.14
Version: 0.3.15
Authors@R: c(person("Rich", "FitzJohn", role = c("aut", "cre"),
email = "[email protected]"),
person("Wes", "Hinsley", role = "aut"),
Expand Down
9 changes: 6 additions & 3 deletions R/dependencies.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ find_dependencies <- function(expr) {
variables <- collector()
descend <- function(e) {
if (is.recursive(e)) {
nm <- deparse(e[[1L]])
functions$add(nm)
if (nm %in% c("length", "dim") && length(e) == 2 && is.symbol(e[[2]])) {
if (rlang::is_call(e) && is.symbol(e[[1]])) {
functions$add(deparse(e[[1L]]))
}
is_dim_read <- rlang::is_call(e, c("length", "dim")) &&
length(e) == 2 && is.symbol(e[[2]])
if (is_dim_read) {
variables$add(odin_dim_name(deparse(e[[2]])))
} else {
for (el in as.list(e[-1])) {
Expand Down
14 changes: 14 additions & 0 deletions tests/testthat/test-dependencies.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,17 @@ test_that("can join dependencies", {
list(functions = c("a", "f", "g", "h", "i"),
variables = c("a", "b", "c", "d", "e")))
})


test_that("cope with poorly deparsing epxressions", {
expr <- quote(
OdinStochasticCall(
sample = "binomial",
mean = S_leave[i, j, k] * (S_rates[i, j, k, 1] / S_leave_rate[i, j, k])
)(S_leave[i, j, k], S_rates[i, j, k, 1]/S_leave_rate[i, j, k]))
res <- find_dependencies(expr)
expect_setequal(res$functions, c("[", "/"))
expect_setequal(
res$variables,
c("S_leave", "i", "j", "k", "S_rates", "S_leave_rate"))
})

0 comments on commit 250dc81

Please sign in to comment.