Skip to content

Commit

Permalink
validate presence of required cols
Browse files Browse the repository at this point in the history
  • Loading branch information
hillalex committed Oct 2, 2024
1 parent ec279a1 commit cf94cd1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 12 deletions.
15 changes: 4 additions & 11 deletions R/biokinetics.R
Original file line number Diff line number Diff line change
Expand Up @@ -237,17 +237,13 @@ biokinetics <- R6::R6Class(
covariate_formula = ~0,
preds_sd = 0.25,
time_type = "relative") {
if (!inherits(priors, "biokinetics_priors")) {
stop("'priors' must be of type 'biokinetics_priors'")
}
validate_priors(priors)
private$priors <- priors
validate_numeric(preds_sd)
private$preds_sd <- preds_sd
validate_time_type(time_type)
private$time_type <- time_type
if (!(class(covariate_formula) == "formula")) {
stop("'covariate_formula' must be a formula")
}
validate_formula(covariate_formula)
private$covariate_formula <- covariate_formula
private$all_formula_vars <- all.vars(covariate_formula)
if (is.null(data) && is.null(file_path)) {
Expand All @@ -264,11 +260,8 @@ biokinetics <- R6::R6Class(
}
private$data <- data
}
unknown_vars <- private$all_formula_vars[which(!(private$all_formula_vars %in% names(private$data)))]
if (length(unknown_vars) > 0) {
stop(paste("All variables in 'covariate_formula' must correspond to data columns. Found unknown variables:",
paste(unknown_vars, collapse = ", ")))
}
validate_required_cols(private$data)
validate_formula_vars(private$all_formula_vars, private$data)
logger::log_info("Preparing data for stan")
private$data <- convert_log_scale(private$data, "value")
private$data[, `:=`(titre_type_num = as.numeric(as.factor(titre_type)),
Expand Down
29 changes: 29 additions & 0 deletions R/validation.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,32 @@ validate_time_type <- function(time_type) {
stop("'time_type' must be one of 'relative' or 'absolute'")
}
}

validate_formula <- function(covariate_formula) {
if (!(class(covariate_formula) == "formula")) {
stop("'covariate_formula' must be a formula")
}
}

validate_formula_vars <- function(formula_vars, data) {
unknown_vars <- formula_vars[which(!(formula_vars %in% names(data)))]
if (length(unknown_vars) > 0) {
stop(paste("All variables in 'covariate_formula' must correspond to data columns. Found unknown variables:",
paste(unknown_vars, collapse = ", ")))
}
}

validate_required_cols <- function(dat) {
required_cols <- c("pid", "date", "last_exp_date", "titre_type", "value", "censored")
missing_cols <- required_cols[!(required_cols %in% colnames(dat))]
if (length(missing_cols) > 0) {
stop(paste("Missing required columns:",
paste(missing_cols, collapse = ", ")))
}
}

validate_priors <- function(priors) {
if (!inherits(priors, "biokinetics_priors")) {
stop("'priors' must be of type 'biokinetics_priors'")
}
}
7 changes: 6 additions & 1 deletion tests/testthat/test-input-validation.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,14 @@ test_that("Covariate formula must be a formula", {
})

test_that("Covariates must be present in data", {
expect_error(biokinetics$new(file_path = system.file("delta_full.rds", package = "epikinetics"), covariate_formula = 0~ bad),
"All variables in 'covariate_formula' must correspond to data columns. Found unknown variables: bad")
})

test_that("Required columns must be present in data", {
dat <- data.table(test = 1)
expect_error(biokinetics$new(data = dat, covariate_formula = 0~ bad),
"All variables in 'covariate_formula' must correspond to data columns. Found unknown variables: bad")
"Missing required columns: pid, date, last_exp_date, titre_type, value, censored")
})

test_that("Data must be a data.table", {
Expand Down

0 comments on commit cf94cd1

Please sign in to comment.