Skip to content

Commit

Permalink
debug(diseasy-model-ode): Hashing issue 4
Browse files Browse the repository at this point in the history
  • Loading branch information
RasmusSkytte committed Feb 3, 2025
1 parent 4e0ae6b commit c92fd77
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 36 deletions.
73 changes: 37 additions & 36 deletions R/DiseasyBaseModule.R
Original file line number Diff line number Diff line change
Expand Up @@ -147,43 +147,44 @@ DiseasyBaseModule <- R6::R6Class(
purrr::keep(~ inherits(., "DiseasyBaseModule")) |>
purrr::discard(~ isTRUE(attr(., "clone"))) |>
purrr::walk(~ .$finalize())
}
),
},

active = list(

#' @field hash (`character`)\cr
#' Computes a hash value for the module. Useful for logging and caching. Read only.
hash = purrr::partial(
.f = active_binding,
name = "hash",
expr = {
# Capture module environment (parent of this environment)
public_names <- ls(self) # public (fields and functions)
public_names <- public_names[public_names != "hash"] # avoid recursion
public_env <- public_names |>
purrr::map(~ {
# Some of the active bindings return informative errors when a module is
# not fully configured. This should not stop the hashing process, so we capture the
# errors and hash these instead
tryCatch(purrr::pluck(self, .), error = function(e) e)
})
names(public_env) <- public_names

# Iteratively map the public environment to hashes
hash_list <- hash_environment(public_env)

# Add the class name to "salt" the hashes
hash_list <- c(hash_list, class = class(self)[1])

# Reduce to single hash and return
hash <- withr::with_locale(
new = c("LC_COLLATE" = "C"), # Ensure consistent hashing
rlang::hash(hash_list[order(names(hash_list))])
)
return(hash)


hash = function(debug = FALSE) {
# Capture module environment (parent of this environment)
public_names <- ls(self) # public (fields and functions)
public_names <- public_names[public_names != "hash"] # avoid recursion
public_env <- public_names |>
purrr::map(~ {
# Some of the active bindings return informative errors when a module is
# not fully configured. This should not stop the hashing process, so we capture the
# errors and hash these instead
tryCatch(purrr::pluck(self, .), error = function(e) e)
})
names(public_env) <- public_names

# Iteratively map the public environment to hashes
hash_list <- hash_environment(public_env)

# Add the class name to "salt" the hashes
hash_list <- c(hash_list, class = class(self)[1])

if (debug) {
hash_list |>
purrr::map_chr(~ substr(., 1, 5)) |>
toString() |>
stop()
}
)

# Reduce to single hash and return
hash <- withr::with_locale(
new = c("LC_COLLATE" = "C"), # Ensure consistent hashing
rlang::hash(hash_list[order(names(hash_list))])
)
return(hash)
}

),

private = list(
Expand Down Expand Up @@ -307,7 +308,7 @@ DiseasyBaseModule <- R6::R6Class(

# Find all relevant hashes
hash_list <- c(
module_hash = self$hash, # Hash of the module (state of public fields)
module_hash = self$hash(debug), # Hash of the module (state of public fields)
hash_environment(function_environment), # hash everything in the function environment
class = class(self)[1] # And add the module name to the hash
)
Expand Down
6 changes: 6 additions & 0 deletions vignettes/diseasy-model-ode.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,13 @@ model$get_results(
prediction_length = 5
)
# Iteration 1 - debug(diseasy-model-ode): Hashing issue 3
# d4730, 9c588, 62ed9, 8cf2e, ae743, ae743, Disea
# 52c58, 9c588, 62ed9, 8cf2e, ae743, ae743, Disea
# Iteration 2 -
# ceb7b, 1f9e1, 1107a, 2d296, 049ce, 466eb, 2abe8, ec360, 9f3c0, 1c191, 576a0, 29f32, 64a24, ceb7b, a442e, 0a066, 62765, ceb7b, d791e, fd243, e4114, 65e03, e4114, ceb7b, Disea
```

... and we can plot the predictions along with observations for the data
Expand Down
Binary file not shown.

0 comments on commit c92fd77

Please sign in to comment.