-
Notifications
You must be signed in to change notification settings - Fork 40
Centralise validation of mvepiestim inputs #245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sangeetabhatia03
wants to merge
14
commits into
main
Choose a base branch
from
centralise-mvepiestim-inputs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
67f46a2
WIP
sangeetabhatia03 74e89b6
Merge remote-tracking branch 'origin/main' into centralise-mvepiestim…
sangeetabhatia03 c9a3197
Create input validation helper
sangeetabhatia03 c3ddcb6
Valiation helpers
sangeetabhatia03 20a5da2
All tests pass except for si summing to 1
sangeetabhatia03 966b945
Normalise SI
sangeetabhatia03 e8bb72a
Resolve merge conflict
sangeetabhatia03 b1234af
Always normalise SI
sangeetabhatia03 294f8b9
Normalise properly avoiding vector recycling
sangeetabhatia03 fae4867
Modify test as error demoted to warning
sangeetabhatia03 28422d3
Add functions to index
sangeetabhatia03 3380093
Merge remote-tracking branch 'origin/main' into centralise-mvepiestim…
sangeetabhatia03 181e113
Fix roxygen2 docs and split imports over lines
sangeetabhatia03 4890c79
Add .call=FALSE and mark internal functions
sangeetabhatia03 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| #' Check incidence input for MV-EpiEstim | ||
| #' | ||
| #' Check that incid is a 3-dimensional array with non-negatuve enteries. | ||
| #' | ||
| #' @inheritParams estimate_advantage | ||
| #' @returns Silently returns TRUE if the checks are passed, otherwise throws an | ||
| #' error. | ||
| #' @author Sangeeta Bhatia | ||
| #' @keywords internal | ||
| check_incidence <- function(incid) { | ||
| if (any(incid < 0)) { | ||
| stop("incid must be >=0") | ||
| } | ||
| if (!is.array(incid) || length(dim(incid)) != 3) { | ||
| stop( | ||
| "incid must be a 3-dimensional array with dimensions time, location, variant", | ||
| .call = FALSE | ||
| ) | ||
| } | ||
| invisible(TRUE) | ||
| } | ||
|
|
||
|
|
||
| #' Check priors for MV-EpiEstim | ||
| #' | ||
| #' Check that priors is a list of the correct format and that the mean of the | ||
| #' prior for epsilon is 1 (as currently only priors with mean of epsilon equal | ||
| #' to 1 is supported). | ||
| #' | ||
| #' @inheritParams estimate_advantage | ||
| #' @returns Silently returns TRUE if the checks are passed, otherwise throws an | ||
| #' error. | ||
| #' @author Sangeeta Bhatia | ||
| #' @keywords internal | ||
| check_priors <- function(priors) { | ||
|
|
||
| if (!identical(priors, default_priors())) { | ||
| warning( | ||
| "Priors where the mean of epsilon is different from 1 are not currently supported.", | ||
| .call = FALSE | ||
| ) | ||
| } | ||
| invisible(TRUE) | ||
| } | ||
|
|
||
| #' Check MCMC control parameters for MV-EpiEstim | ||
| #' | ||
| #' Check that (1) mcmc_control is a list of the correct format, (2) n_iter, burnin | ||
| #' and thin are positive integers and (3) n_iter is greater than burnin + thin. | ||
| #' | ||
| #' @inheritParams estimate_advantage | ||
| #' @returns Silently returns TRUE if the checks are passed, otherwise throws an | ||
| #' error. | ||
| #' @author Sangeeta Bhatia | ||
| #' @keywords internal | ||
| check_mcmc_control <- function(mcmc_control) { | ||
|
|
||
| if (mcmc_control$n_iter < 0 || !is.integer(mcmc_control$n_iter)) { | ||
| stop("n_iter in mcmc_control must be a positive integer", .call = FALSE) | ||
| } | ||
| if (mcmc_control$burnin < 0 || !is.integer(mcmc_control$burnin)) { | ||
| stop("burnin in mcmc_control must be a positive integer", .call = FALSE) | ||
| } | ||
| if (mcmc_control$thin < 0 || !is.integer(mcmc_control$thin)) { | ||
| stop("thin in mcmc_control must be a positive integer", .call = FALSE) | ||
| } | ||
| if (mcmc_control$n_iter < mcmc_control$burnin + mcmc_control$thin) { | ||
| stop("In mcmc_control, n_iter must be greater than burnin + thin", .call = FALSE) | ||
| } | ||
| invisible(TRUE) | ||
| } | ||
|
|
||
| #' Validate t_min and t_max inputs for MV-EpiEstim | ||
| #' | ||
| #' Check that (1) t_min and t_max are integers, (2) that they are >= 2, that | ||
| #' they are <= nrow(incid) and (3) t_min is not greater than t_max. | ||
| #' | ||
| #' @inheritParams estimate_advantage | ||
| #' @returns Silently returns TRUE if the checks are passed, otherwise throws an | ||
| #' error. | ||
| #' @author Sangeeta Bhatia | ||
| #' @keywords internal | ||
| check_t_min_t_max <- function(t_min, t_max, incid) { | ||
| if (!is.integer(t_min) || !is.integer(t_max)) { | ||
| stop("t_min and t_max must be integers", .call = FALSE) | ||
| } | ||
| if (t_min < 2 || t_max < 2) { | ||
| stop("t_min and t_max must be >=2", .call = FALSE) | ||
| } | ||
| if (t_min > nrow(incid) || t_max > nrow(incid)) { | ||
| stop("t_min and t_max must be <= nrow(incid)", .call = FALSE) | ||
| } | ||
| if (t_min > t_max) { | ||
| stop( | ||
| "t_min is greater than t_max. You can specify a smaller t_min or increase t_max.", | ||
| .call = FALSE | ||
| ) | ||
| } | ||
| invisible(TRUE) | ||
| } | ||
|
|
||
|
|
||
| #' Validate seed input for MV-EpiEstim | ||
| #' | ||
| #' Validate that the seed is not null and is numeric | ||
| #' | ||
| #' @inheritParams estimate_advantage | ||
| #' @return Silently returns TRUE if the checks are passed, otherwise throws an error. | ||
| #' @author Sangeeta Bhatia | ||
| #' @keywords internal | ||
| check_seed <- function(seed) { | ||
| if (!is.null(seed) && !is.numeric(seed)) { | ||
| stop("supplied seed is not a valid integer", .call = FALSE) | ||
| } | ||
| if (!is.null(seed)) set.seed(seed) | ||
| invisible(TRUE) | ||
| } | ||
|
|
||
|
|
||
| #' Validate inputs to estimate_advantage | ||
| #' | ||
| #' Runs all available input checks for \code{\link{estimate_advantage}} based on | ||
| #' the supplied arguments. Each check is called only if the corresponding | ||
| #' argument is present. | ||
| #' | ||
| #' @param ... Named arguments passed to \code{\link{estimate_advantage}}. | ||
| #' @returns Invisibly returns \code{NULL}; throws an error if any input check | ||
| #' fails. | ||
| #' @author Sangeeta Bhatia | ||
| #' @keywords internal | ||
| check_estimate_advantage_inputs <- function(...) { | ||
| estimate_advantage_args <- list(...) | ||
| arg_names <- names(estimate_advantage_args) | ||
| if ("incid" %in% arg_names) | ||
| check_incidence(estimate_advantage_args$incid) | ||
|
|
||
| if ("si" %in% arg_names) | ||
| apply(estimate_advantage_args$si_distr, 2, check_si_distr) | ||
|
|
||
| if ("priors" %in% arg_names) | ||
| check_priors(estimate_advantage_args$priors) | ||
|
|
||
| if ("mcmc_control" %in% arg_names) | ||
| check_mcmc_control(estimate_advantage_args$mcmc_control) | ||
|
|
||
| if (all(c("t_min", "t_max", "incid") %in% arg_names)) { | ||
| check_t_min_t_max( | ||
| estimate_advantage_args$t_min, estimate_advantage_args$t_max, | ||
| estimate_advantage_args$incid | ||
| ) | ||
| } | ||
| if ("seed" %in% arg_names) | ||
| check_seed(estimate_advantage_args$seed) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.