Skip to content

Commit

Permalink
better warning messages
Browse files Browse the repository at this point in the history
  • Loading branch information
mnwright committed Jun 13, 2024
1 parent 9dc2fc5 commit 6b28747
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
21 changes: 18 additions & 3 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,12 @@ cforde <- function(params,
stop("For all entered evidence rows, no matching leaves could be found. This is probably because evidence lies outside of the distribution calculated by FORDE. For continuous data, consider setting epsilon>0 or finite_bounds='no' in forde(). For categorical data, consider setting alpha>0 in forde().")
} else {
if (grepl("warning$", nomatch)) {
warning("For some entered evidence rows, no matching leaves could be found. This is probably because evidence lies outside of the distribution calculated by FORDE. For continuous data, consider setting epsilon>0 or finite_bounds='no' in forde(). For categorical data, consider setting alpha>0 in forde().")
wrn <- "For some entered evidence rows, no matching leaves could be found. This is probably because evidence lies outside of the distribution calculated by FORDE. For continuous data, consider setting epsilon>0 or finite_bounds='no' in forde(). For categorical data, consider setting alpha>0 in forde()."
if (grepl("^force", nomatch)) {
warning(paste(wrn, "Sampling from all leaves with equal probability (can be changed with 'nomatch' argument)."))
} else {
warning(paste(wrn, "Returning NA for those rows (can be changed with 'nomatch' argument)."))
}
}
conds_impossible <- conds_conditioned[!(conds_conditioned %in% relevant_leaves[,unique(c_idx)])]
relevant_leaves <- setorder(rbind(relevant_leaves, data.table(c_idx = conds_impossible, f_idx = NA_integer_, f_idx_uncond = NA_integer_)))
Expand Down Expand Up @@ -459,7 +464,12 @@ cforde <- function(params,
cvg_new[, cvg := NA]
}
if (grepl("warning$", nomatch)) {
warning("All leaves have zero likelihood. This is probably because evidence contains an (almost) impossible combination. Sampling from all leaves with equal probability.")
wrn <- "All leaves have zero likelihood. This is probably because evidence contains an (almost) impossible combination."
if (grepl("^force", nomatch)) {
warning(paste(wrn, "Sampling from all possible leaves with equal probability."))
} else {
warning(paste(wrn, "Returning NA."))
}
}
} else {
cvg_new[, cvg := exp(cvg - max(cvg))]
Expand All @@ -474,7 +484,12 @@ cforde <- function(params,
cvg_new <- cvg_new[leaf_zero_lik == FALSE, ]
}
if (grepl("warning$", nomatch)) {
warning("All leaves have zero likelihood for some entered evidence rows. This is probably because evidence contains an (almost) impossible combination. Sampling from all leaves with equal probability.")
wrn <- "All leaves have zero likelihood for some entered evidence rows. This is probably because evidence contains an (almost) impossible combination."
if (grepl("^force", nomatch)) {
warning(paste(wrn, "Sampling from all possible leaves with equal probability (can be changed with 'nomatch' argument)."))
} else {
warning(paste(wrn, "Returning NA for those rows (can be changed with 'nomatch' argument)."))
}
}
}
if (any(cvg_new[, !leaf_zero_lik])) {
Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/test-conditions.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ test_that("if nomatch='force_warning', run through with a warning", {
psi_no <- forde(arf, iris, finite_bounds = "no", parallel = FALSE)
expect_warning(x_synth <- forge(psi_no, evidence = data.frame(Sepal.Length = 100),
nomatch = "force_warning", n_synth = 10, parallel = FALSE),
"All leaves have zero likelihood for some entered evidence rows\\. This is probably because evidence contains an \\(almost\\) impossible combination\\. Sampling from all leaves with equal probability\\.")
"All leaves have zero likelihood for some entered evidence rows\\. This is probably because evidence contains an \\(almost\\) impossible combination\\. Sampling from all possible leaves with equal probability \\(can be changed with 'nomatch' argument\\)\\.")
expect_true(all(!is.na(x_synth)))

# No matching leaf case (finite bounds)
psi_global <- forde(arf, iris, finite_bounds = "global", parallel = FALSE)
expect_warning(x_synth <- forge(psi_global, evidence = data.frame(Sepal.Length = 100),
nomatch = "force_warning", n_synth = 10, parallel = FALSE),
"For some entered evidence rows, no matching leaves could be found\\. This is probably because evidence lies outside of the distribution calculated by FORDE\\. For continuous data, consider setting epsilon>0 or finite_bounds='no' in forde\\(\\)\\. For categorical data, consider setting alpha>0 in forde\\(\\)\\.")
"For some entered evidence rows, no matching leaves could be found\\. This is probably because evidence lies outside of the distribution calculated by FORDE\\. For continuous data, consider setting epsilon>0 or finite_bounds='no' in forde\\(\\)\\. For categorical data, consider setting alpha>0 in forde\\(\\)\\. Sampling from all leaves with equal probability \\(can be changed with 'nomatch' argument\\)\\.")
expect_true(all(!is.na(x_synth)))
})

Expand All @@ -60,14 +60,14 @@ test_that("if nomatch='na_warning', run through with a warning and return NA", {
psi_no <- forde(arf, iris, finite_bounds = "no", parallel = FALSE)
expect_warning(x_synth <- forge(psi_no, evidence = data.frame(Sepal.Length = 100),
nomatch = "na_warning", n_synth = 10, parallel = FALSE),
"All leaves have zero likelihood for some entered evidence rows\\. This is probably because evidence contains an \\(almost\\) impossible combination\\. Sampling from all leaves with equal probability\\.")
"All leaves have zero likelihood for some entered evidence rows\\. This is probably because evidence contains an \\(almost\\) impossible combination\\. Returning NA for those rows \\(can be changed with 'nomatch' argument\\)\\.")
expect_true(all(is.na(x_synth[, -1])))

# No matching leaf case (finite bounds)
psi_global <- forde(arf, iris, finite_bounds = "global", parallel = FALSE)
expect_warning(x_synth <- forge(psi_global, evidence = data.frame(Sepal.Length = 100),
nomatch = "na_warning", n_synth = 10, parallel = FALSE),
"For some entered evidence rows, no matching leaves could be found\\. This is probably because evidence lies outside of the distribution calculated by FORDE\\. For continuous data, consider setting epsilon>0 or finite_bounds='no' in forde\\(\\)\\. For categorical data, consider setting alpha>0 in forde\\(\\)\\.")
"For some entered evidence rows, no matching leaves could be found\\. This is probably because evidence lies outside of the distribution calculated by FORDE\\. For continuous data, consider setting epsilon>0 or finite_bounds='no' in forde\\(\\)\\. For categorical data, consider setting alpha>0 in forde\\(\\)\\. Returning NA for those rows \\(can be changed with 'nomatch' argument\\)\\.")
expect_true(all(is.na(x_synth[, -1])))
})

Expand Down

0 comments on commit 6b28747

Please sign in to comment.