Skip to content

Commit

Permalink
Test plotting functions with all saved models
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Claessens authored and Scott Claessens committed Jul 24, 2024
1 parent 5c6c429 commit a29f39f
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 60 deletions.
38 changes: 29 additions & 9 deletions tests/testthat/test-coev_plot_delta_theta.R
Original file line number Diff line number Diff line change
@@ -1,31 +1,51 @@
test_that("coev_plot_delta_theta() produces expected errors and output", {
# load model
m <- readRDS(test_path("fixtures", "coevfit_example1.rds"))
m <- reload_fit(m, filename = "coevfit_example1-1.csv")
# load models
m1 <- readRDS(test_path("fixtures", "coevfit_example1.rds"))
m2 <- readRDS(test_path("fixtures", "coevfit_example2.rds"))
m3 <- readRDS(test_path("fixtures", "coevfit_example3.rds"))
m4 <- readRDS(test_path("fixtures", "coevfit_example4.rds"))
m5 <- readRDS(test_path("fixtures", "coevfit_example5.rds"))
m6 <- readRDS(test_path("fixtures", "coevfit_example6.rds"))
m1 <- reload_fit(m1, filename = "coevfit_example1-1.csv")
m2 <- reload_fit(m2, filename = "coevfit_example2-1.csv")
m3 <- reload_fit(m3, filename = "coevfit_example3-1.csv")
m4 <- reload_fit(m4, filename = "coevfit_example4-1.csv")
m5 <- reload_fit(m5, filename = "coevfit_example5-1.csv")
m6 <- reload_fit(m6, filename = "coevfit_example6-1.csv")
# expect the following errors
expect_error(
coev_plot_delta_theta(object = "fail"),
"Argument 'object' must be a fitted coevolutionary model of class coevfit."
)
expect_error(
coev_plot_delta_theta(object = m, variables = NA),
coev_plot_delta_theta(object = m1, variables = NA),
"Argument 'variables' must be a character vector."
)
expect_error(
coev_plot_delta_theta(object = m, variables = "fail"),
coev_plot_delta_theta(object = m1, variables = "fail"),
"Argument 'variables' must be of length > 1."
)
expect_error(
coev_plot_delta_theta(object = m, variables = c("x", "y", "fail")),
coev_plot_delta_theta(object = m1, variables = c("x", "y", "fail")),
"Some variables in 'variables' are not included in the fitted model."
)
expect_error(
coev_plot_delta_theta(object = m, variables = c("x", "y", "y")),
coev_plot_delta_theta(object = m1, variables = c("x", "y", "y")),
"Argument 'variables' contains duplicates."
)
# suppress warnings
SW <- suppressWarnings
# should run without error and produce ggplot object
expect_no_error(SW(coev_plot_delta_theta(m)))
expect_true(methods::is(SW(coev_plot_delta_theta(m)), "ggplot"))
expect_no_error(SW(coev_plot_delta_theta(m1)))
expect_no_error(SW(coev_plot_delta_theta(m2)))
expect_no_error(SW(coev_plot_delta_theta(m3)))
expect_no_error(SW(coev_plot_delta_theta(m4)))
expect_no_error(SW(coev_plot_delta_theta(m5)))
expect_no_error(SW(coev_plot_delta_theta(m6)))
expect_true(methods::is(SW(coev_plot_delta_theta(m1)), "ggplot"))
expect_true(methods::is(SW(coev_plot_delta_theta(m2)), "ggplot"))
expect_true(methods::is(SW(coev_plot_delta_theta(m3)), "ggplot"))
expect_true(methods::is(SW(coev_plot_delta_theta(m4)), "ggplot"))
expect_true(methods::is(SW(coev_plot_delta_theta(m5)), "ggplot"))
expect_true(methods::is(SW(coev_plot_delta_theta(m6)), "ggplot"))
})
51 changes: 34 additions & 17 deletions tests/testthat/test-coev_plot_flowfield.R
Original file line number Diff line number Diff line change
@@ -1,45 +1,62 @@
test_that("coev_plot_flowfield() produces expected errors and output", {
# load model
m <- readRDS(test_path("fixtures", "coevfit_example1.rds"))
m <- reload_fit(m, filename = "coevfit_example1-1.csv")
# load models
m1 <- readRDS(test_path("fixtures", "coevfit_example1.rds"))
m2 <- readRDS(test_path("fixtures", "coevfit_example2.rds"))
m3 <- readRDS(test_path("fixtures", "coevfit_example3.rds"))
m4 <- readRDS(test_path("fixtures", "coevfit_example4.rds"))
m5 <- readRDS(test_path("fixtures", "coevfit_example5.rds"))
m6 <- readRDS(test_path("fixtures", "coevfit_example6.rds"))
m1 <- reload_fit(m1, filename = "coevfit_example1-1.csv")
m2 <- reload_fit(m2, filename = "coevfit_example2-1.csv")
m3 <- reload_fit(m3, filename = "coevfit_example3-1.csv")
m4 <- reload_fit(m4, filename = "coevfit_example4-1.csv")
m5 <- reload_fit(m5, filename = "coevfit_example5-1.csv")
m6 <- reload_fit(m6, filename = "coevfit_example6-1.csv")
# expect the following errors
expect_error(
coev_plot_flowfield(object = "fail", var1 = "x", var2 = "y"),
"Argument 'object' must be a fitted coevolutionary model of class coevfit."
)
expect_error(
coev_plot_flowfield(object = m, var1 = 0:1, var2 = "y"),
coev_plot_flowfield(object = m1, var1 = 0:1, var2 = "y"),
"Argument 'var1' must be a character string of length one."
)
expect_error(
coev_plot_flowfield(object = m, var1 = "z", var2 = "y"),
coev_plot_flowfield(object = m1, var1 = "z", var2 = "y"),
"Argument 'var1' must be a variable included in the fitted model."
)
expect_error(
coev_plot_flowfield(object = m, var1 = "x", var2 = 0:1),
coev_plot_flowfield(object = m1, var1 = "x", var2 = 0:1),
"Argument 'var2' must be a character string of length one."
)
expect_error(
coev_plot_flowfield(object = m, var1 = "x", var2 = "z"),
coev_plot_flowfield(object = m1, var1 = "x", var2 = "z"),
"Argument 'var2' must be a variable included in the fitted model."
)
expect_error(
coev_plot_flowfield(object = m, var1 = "x", var2 = "x"),
coev_plot_flowfield(object = m1, var1 = "x", var2 = "x"),
"Argument 'var1' and 'var2' must refer to different variables."
)
expect_error(
coev_plot_flowfield(
object = m, var1 = "x", var2 = "y", nullclines = "hello"
object = m1, var1 = "x", var2 = "y", nullclines = "hello"
),
"Argument 'nullclines' must be logical."
)
# suppress warnings
SW <- suppressWarnings
# should run without error
expect_no_error(
SW(coev_plot_flowfield(m, var1 = "x", var2 = "y"))
)
expect_no_error(
SW(coev_plot_flowfield(m, var1 = "x", var2 = "y", nullclines = TRUE))
)
fun <- function(model, var1, var2, nullclines = FALSE) {
suppressWarnings(coev_plot_flowfield(model, var1, var2, nullclines))
}
expect_no_error(fun(m1, "x", "y"))
expect_no_error(fun(m2, "w", "x"))
expect_no_error(fun(m3, "w", "x"))
expect_no_error(fun(m4, "y", "z"))
expect_no_error(fun(m5, "w", "x"))
expect_no_error(fun(m6, "w", "x"))
expect_no_error(fun(m1, "x", "y", nullclines = TRUE))
expect_no_error(fun(m2, "w", "x", nullclines = TRUE))
expect_no_error(fun(m3, "w", "x", nullclines = TRUE))
expect_no_error(fun(m4, "y", "z", nullclines = TRUE))
expect_no_error(fun(m5, "w", "x", nullclines = TRUE))
expect_no_error(fun(m6, "w", "x", nullclines = TRUE))
})
47 changes: 36 additions & 11 deletions tests/testthat/test-coev_plot_predictive_check.R
Original file line number Diff line number Diff line change
@@ -1,44 +1,69 @@
test_that("coev_plot_predictive_check() produces expected errors and output", {
# load model
m <- readRDS(test_path("fixtures", "coevfit_example1.rds"))
m <- reload_fit(m, filename = "coevfit_example1-1.csv")
# load models
m1 <- readRDS(test_path("fixtures", "coevfit_example1.rds"))
m2 <- readRDS(test_path("fixtures", "coevfit_example2.rds"))
m3 <- readRDS(test_path("fixtures", "coevfit_example3.rds"))
m4 <- readRDS(test_path("fixtures", "coevfit_example4.rds"))
m5 <- readRDS(test_path("fixtures", "coevfit_example5.rds"))
m6 <- readRDS(test_path("fixtures", "coevfit_example6.rds"))
m1 <- reload_fit(m1, filename = "coevfit_example1-1.csv")
m2 <- reload_fit(m2, filename = "coevfit_example2-1.csv")
m3 <- reload_fit(m3, filename = "coevfit_example3-1.csv")
m4 <- reload_fit(m4, filename = "coevfit_example4-1.csv")
m5 <- reload_fit(m5, filename = "coevfit_example5-1.csv")
m6 <- reload_fit(m6, filename = "coevfit_example6-1.csv")
# expect the following errors
expect_error(
coev_plot_predictive_check(object = "fail"),
"Argument 'object' must be a fitted coevolutionary model of class coevfit."
)
expect_error(
coev_plot_predictive_check(object = m, variables = 0),
coev_plot_predictive_check(object = m1, variables = 0),
paste0(
"Argument 'variables' must be a character string or a ",
"vector of character strings."
)
)
expect_error(
coev_plot_predictive_check(object = m, variables = "fail"),
coev_plot_predictive_check(object = m1, variables = "fail"),
paste0(
"Argument 'variables' contains variable names that are not ",
"included in the fitted model."
)
)
expect_error(
coev_plot_predictive_check(object = m, ndraws = "fail"),
coev_plot_predictive_check(object = m1, ndraws = "fail"),
"Argument 'ndraws' must be a single integer."
)
expect_error(
coev_plot_predictive_check(object = m, ndraws = 0L),
coev_plot_predictive_check(object = m1, ndraws = 0L),
"Argument 'ndraws' must be between 1 and the total number of draws."
)
expect_error(
coev_plot_predictive_check(
object = m, ndraws = as.integer(nrow(m$fit$draws()) + 1)
object = m1, ndraws = as.integer(nrow(m1$fit$draws()) + 1)
),
"Argument 'ndraws' must be between 1 and the total number of draws."
)
# suppress warnings
SW <- suppressWarnings
# should run without error and produce list of ggplot objects
expect_no_error(SW(coev_plot_predictive_check(m)))
expect_no_error(SW(coev_plot_predictive_check(m, variables = c("u","v"))))
expect_no_error(SW(coev_plot_predictive_check(m, ndraws = 1L)))
expect_no_error(SW(coev_plot_predictive_check(m1)))
expect_no_error(SW(coev_plot_predictive_check(m2)))
expect_no_error(SW(coev_plot_predictive_check(m3)))
expect_no_error(SW(coev_plot_predictive_check(m4)))
expect_no_error(SW(coev_plot_predictive_check(m5)))
expect_no_error(SW(coev_plot_predictive_check(m6)))
expect_no_error(SW(coev_plot_predictive_check(m1, variables = "x")))
expect_no_error(SW(coev_plot_predictive_check(m2, variables = "w")))
expect_no_error(SW(coev_plot_predictive_check(m3, variables = "w")))
expect_no_error(SW(coev_plot_predictive_check(m4, variables = "y")))
expect_no_error(SW(coev_plot_predictive_check(m5, variables = "w")))
expect_no_error(SW(coev_plot_predictive_check(m6, variables = "w")))
expect_no_error(SW(coev_plot_predictive_check(m1, ndraws = 1L)))
expect_no_error(SW(coev_plot_predictive_check(m2, ndraws = 1L)))
expect_no_error(SW(coev_plot_predictive_check(m3, ndraws = 1L)))
expect_no_error(SW(coev_plot_predictive_check(m4, ndraws = 1L)))
expect_no_error(SW(coev_plot_predictive_check(m5, ndraws = 1L)))
expect_no_error(SW(coev_plot_predictive_check(m6, ndraws = 1L)))
})
63 changes: 40 additions & 23 deletions tests/testthat/test-coev_plot_selection_gradient.R
Original file line number Diff line number Diff line change
@@ -1,51 +1,68 @@
test_that("coev_plot_selection_gradient() produces expected errors and output", {
# load model
m <- readRDS(test_path("fixtures", "coevfit_example1.rds"))
m <- reload_fit(m, filename = "coevfit_example1-1.csv")
# load models
m1 <- readRDS(test_path("fixtures", "coevfit_example1.rds"))
m2 <- readRDS(test_path("fixtures", "coevfit_example2.rds"))
m3 <- readRDS(test_path("fixtures", "coevfit_example3.rds"))
m4 <- readRDS(test_path("fixtures", "coevfit_example4.rds"))
m5 <- readRDS(test_path("fixtures", "coevfit_example5.rds"))
m6 <- readRDS(test_path("fixtures", "coevfit_example6.rds"))
m1 <- reload_fit(m1, filename = "coevfit_example1-1.csv")
m2 <- reload_fit(m2, filename = "coevfit_example2-1.csv")
m3 <- reload_fit(m3, filename = "coevfit_example3-1.csv")
m4 <- reload_fit(m4, filename = "coevfit_example4-1.csv")
m5 <- reload_fit(m5, filename = "coevfit_example5-1.csv")
m6 <- reload_fit(m6, filename = "coevfit_example6-1.csv")
# expect the following errors
expect_error(
coev_plot_selection_gradient(object = "fail", var1 = "x", var2 = "y"),
"Argument 'object' must be a fitted coevolutionary model of class coevfit."
)
expect_error(
coev_plot_selection_gradient(object = m, var1 = 0:1, var2 = "y"),
coev_plot_selection_gradient(object = m1, var1 = 0:1, var2 = "y"),
"Argument 'var1' must be a character string of length one."
)
expect_error(
coev_plot_selection_gradient(object = m, var1 = "z", var2 = "y"),
coev_plot_selection_gradient(object = m1, var1 = "z", var2 = "y"),
"Argument 'var1' must be a variable included in the fitted model."
)
expect_error(
coev_plot_selection_gradient(object = m, var1 = "x", var2 = 0:1),
coev_plot_selection_gradient(object = m1, var1 = "x", var2 = 0:1),
"Argument 'var2' must be a character string of length one."
)
expect_error(
coev_plot_selection_gradient(object = m, var1 = "x", var2 = "z"),
coev_plot_selection_gradient(object = m1, var1 = "x", var2 = "z"),
"Argument 'var2' must be a variable included in the fitted model."
)
expect_error(
coev_plot_selection_gradient(object = m, var1 = "x", var2 = "x"),
coev_plot_selection_gradient(object = m1, var1 = "x", var2 = "x"),
"Argument 'var1' and 'var2' must refer to different variables."
)
expect_error(
coev_plot_selection_gradient(
object = m, var1 = "x", var2 = "y", contour = "hello"
object = m1, var1 = "x", var2 = "y", contour = "hello"
),
"Argument 'contour' must be logical."
)
# suppress warnings
SW <- suppressWarnings
# should run without error and produce ggplot object
expect_no_error(
SW(coev_plot_selection_gradient(m, var1 = "x", var2 = "y"))
)
expect_no_error(
SW(coev_plot_selection_gradient(m, var1 = "x", var2 = "y", contour = TRUE))
)
expect_true(
methods::is(
SW(coev_plot_selection_gradient(m, var1 = "x", var2 = "y")),
"ggplot"
)
)
fun <- function(model, var1, var2, contour = FALSE) {
suppressWarnings(coev_plot_selection_gradient(model, var1, var2, contour))
}
expect_no_error(fun(m1, "x", "y"))
expect_no_error(fun(m2, "w", "x"))
expect_no_error(fun(m3, "w", "x"))
expect_no_error(fun(m4, "y", "z"))
expect_no_error(fun(m5, "w", "x"))
expect_no_error(fun(m6, "w", "x"))
expect_no_error(fun(m1, "x", "y", contour = TRUE))
expect_no_error(fun(m2, "w", "x", contour = TRUE))
expect_no_error(fun(m3, "w", "x", contour = TRUE))
expect_no_error(fun(m4, "y", "z", contour = TRUE))
expect_no_error(fun(m5, "w", "x", contour = TRUE))
expect_no_error(fun(m6, "w", "x", contour = TRUE))
expect_true(methods::is(fun(m1, "x", "y"), "ggplot"))
expect_true(methods::is(fun(m2, "w", "x"), "ggplot"))
expect_true(methods::is(fun(m3, "w", "x"), "ggplot"))
expect_true(methods::is(fun(m4, "y", "z"), "ggplot"))
expect_true(methods::is(fun(m5, "w", "x"), "ggplot"))
expect_true(methods::is(fun(m6, "w", "x"), "ggplot"))
})

0 comments on commit a29f39f

Please sign in to comment.