Skip to content

Commit

Permalink
Catch all messages
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavdelius committed Apr 4, 2024
1 parent dff641c commit dd4120e
Show file tree
Hide file tree
Showing 15 changed files with 79 additions and 54 deletions.
2 changes: 1 addition & 1 deletion tests/testthat/test-MizerSim-class.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
params <- newMultispeciesParams(NS_species_params_gears, inter)
params <- newMultispeciesParams(NS_species_params_gears, inter, info_level = 0)

# basic constructor sets dimensions properly ----
test_that("basic constructor sets dimensions properly", {
Expand Down
4 changes: 3 additions & 1 deletion tests/testthat/test-analytic_results.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Initialise power law ----
no_w <- 100
no_sp <- 2
p <- newTraitParams(no_sp = no_sp, perfect_scaling = TRUE, no_w = no_w)
expect_message(p <- newTraitParams(no_sp = no_sp, perfect_scaling = TRUE,
no_w = no_w),
"Note: Negative resource abundances")
p@species_params$pred_kernel_type <- "truncated_lognormal"
n0 <- p@initial_n
n0[] <- 0
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-backwards_compatibility.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ data(inter)
test_that("MizerParams() works as in version 2.5.1", {
# expect_warning(params <- MizerParams(NS_species_params_gears, inter), "deprecated")
# warning no longer thrown - NS_species_params_gears is now 2.5.1 (see `params@mizer_version`)
params <- newMultispeciesParams(NS_species_params_gears, inter)
params <- newMultispeciesParams(NS_species_params_gears, inter, info_level = 0)
# expect_known_value(params@search_vol, "values/set_multispecies_model_search_vol")
# expect_known_value(params@intake_max, "values/set_multispecies_model_intake_max")
# expect_known_value(params@psi, "values/set_multispecies_model_psi")
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-get_initial_n.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# get_initial_n ----
test_that("get_initial_n is working properly", {
params <- newMultispeciesParams(NS_species_params_gears, inter)
params <- newMultispeciesParams(NS_species_params_gears, inter, info_level = 0)
n <- get_initial_n(params)
no_sp <- nrow(params@species_params)
for (i in 1:no_sp) {
Expand Down
59 changes: 37 additions & 22 deletions tests/testthat/test-manipulate_species.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ test_that("addSpecies works when adding a second identical species", {
species_params <- p@species_params[5, ]
species_params$species <- "new"
# Adding species 5 again should lead two copies of the species
pa <- addSpecies(p, species_params)
expect_message(pa <- addSpecies(p, species_params))
expect_identical(pa@metab[5, ], pa@metab[no_sp + 1, ])
expect_identical(pa@psi[5, ], pa@psi[no_sp + 1, ])
expect_identical(pa@ft_pred_kernel_e[5, ], pa@ft_pred_kernel_e[no_sp + 1, ])
Expand Down Expand Up @@ -36,23 +36,29 @@ test_that("addSpecies handles gear params correctly", {
# If no initial_effort for new gear is provided, it is 0
# Wrapping in `expect_warning()` to ignore warnings about unrealistic
# reproductive efficiency
expect_warning(pa <- addSpecies(p, sp, gp))
(pa <- addSpecies(p, sp, gp)) |>
expect_message() |>
expect_warning()
expect_identical(pa@initial_effort,
c(knife_edge_gear = 0, gear1 = 0, gear2 = 0))
expect_identical(nrow(pa@gear_params), 5L)

# effort for existing gear is not changed
extra_effort <- c(gear1 = 2, gear2 = 3)
expect_warning(pa <- addSpecies(p, sp, gp, initial_effort = extra_effort))
(pa <- addSpecies(p, sp, gp, initial_effort = extra_effort)) |>
expect_message() |>
expect_warning()
expect_identical(pa@initial_effort, c(knife_edge_gear = 0, extra_effort))

effort <- 2
expect_error(addSpecies(p, sp, gp, initial_effort = effort),
"The `initial_effort` must be a named list or vector")
addSpecies(p, sp, gp, initial_effort = effort) |>
expect_message() |>
expect_error("The `initial_effort` must be a named list or vector")

effort <- c(knife_edge_gear = 1)
expect_error(addSpecies(p, sp, gp, initial_effort = effort),
"The names of the `initial_effort` do not match the names of the new gears.")
addSpecies(p, sp, gp, initial_effort = effort) |>
expect_message() |>
expect_error("The names of the `initial_effort` do not match the names of the new gears.")
})

test_that("addSpecies handles interaction matrix correctly", {
Expand All @@ -66,19 +72,23 @@ test_that("addSpecies handles interaction matrix correctly", {

interaction <- matrix(1:4/4, ncol = 2)
ones <- matrix(rep(1, 4), ncol = 2)
expect_warning(pa <- addSpecies(p, sp, interaction = interaction))
(pa <- addSpecies(p, sp, interaction = interaction)) |>
expect_message() |>
expect_warning()
expect_equal(pa@interaction[3:4, 3:4], interaction, ignore_attr = TRUE)
expect_equal(pa@interaction[1:2, 3:4], ones, ignore_attr = TRUE)
expect_equal(pa@interaction[3:4, 1:2], ones, ignore_attr = TRUE)
expect_equal(pa@interaction[1:2, 1:2], p@interaction, ignore_attr = TRUE)

interaction <- matrix(1:16/16, ncol = 4)
expect_warning(pa <- addSpecies(p, sp, interaction = interaction))
(pa <- addSpecies(p, sp, interaction = interaction)) |>
expect_message() |>
expect_warning("The following species require an unrealistic reproductive efficiency greater than 1: new2")
expect_equal(pa@interaction, interaction, ignore_attr = TRUE)

expect_error(addSpecies(p, sp,
interaction = matrix(1:9, ncol = 3)),
"Interaction matrix has invalid dimensions.")
addSpecies(p, sp, interaction = matrix(1:9, ncol = 3)) |>
expect_warning() |>
expect_error("Interaction matrix has invalid dimensions.")
})
test_that("addSpecies works when adding a species with a larger w_max", {
sp <- data.frame(species = "Blue whale", w_max = 5e4,
Expand All @@ -88,7 +98,8 @@ test_that("addSpecies works when adding a species with a larger w_max", {
# change a slot to test that such changes will be preserved
params <- setMaxIntakeRate(params, 2 * getMaxIntakeRate(params))

p <- addSpecies(params, sp)
(p <- addSpecies(params, sp)) |>
expect_message()
expect_identical(p@w[1:100], params@w)
expect_identical(p@w_full[seq_along(params@w_full)], params@w_full)
expect_lte(5e4, max(p@w))
Expand All @@ -104,7 +115,8 @@ test_that("addSpecies works when adding a species with a smaller w_min", {
# change a slot to test that such changes will be preserved
params <- setMaxIntakeRate(params, 2 * getMaxIntakeRate(params))

p <- addSpecies(params, sp)
(p <- addSpecies(params, sp)) |>
expect_message()
expect_equal(p@w[28:127], params@w)
expect_equal(p@w_full[seq_along(params@w_full)], params@w_full)
expect_gte(1e-5, min(p@w))
Expand All @@ -119,7 +131,8 @@ test_that("addSpecies has other documented properties", {
k_vb = c(4, 1),
n = 2 / 3,
p = 2 / 3)
p <- addSpecies(NS_params, sp)
(p <- addSpecies(NS_params, sp)) |>
expect_message()

# New species have 0 reproduction level
expect_equal(getReproductionLevel(p)[13:14],
Expand All @@ -144,7 +157,7 @@ test_that("Added species stay at low abundance", {
a = 0.0085,
b = 3.11
)
params <- addSpecies(params, species_params)
expect_message(params <- addSpecies(params, species_params))
no_sp <- nrow(params@species_params)
sim <- project(params, t_max = 1, progress_bar = FALSE)
expect_lt(finalN(sim)[no_sp, 1] / initialN(sim)[no_sp, 1], 1.04)
Expand All @@ -156,11 +169,12 @@ test_that("removeSpecies works", {
remove <- NS_species_params$species[2:11]
reduced <- NS_species_params[!(NS_species_params$species %in% remove), ]
params <- newMultispeciesParams(NS_species_params, no_w = 20,
max_w = 39900, min_w_pp = 9e-14)
max_w = 39900, min_w_pp = 9e-14,
info_level = 0)
p1 <- removeSpecies(params, species = remove)
expect_equal(nrow(p1@species_params), nrow(params@species_params) - 10)
p2 <- newMultispeciesParams(reduced, no_w = 20,
max_w = 39900, min_w_pp = 9e-14)
max_w = 39900, min_w_pp = 9e-14, info_level = 0)
p2@linecolour[2] = "#a08dfb" # update line colour
expect_equal(p1, p2, ignore_attr = TRUE)
sim1 <- project(p1, t_max = 0.4, t_save = 0.4)
Expand Down Expand Up @@ -206,8 +220,9 @@ test_that("adding and then removing species leaves params unaltered", {
comment(params@pred_kernel) <- NULL
comment(params@catchability) <- NULL
comment(params@selectivity) <- NULL
params2 <- addSpecies(params, sp) %>%
removeSpecies(c("new1", "new2"))
(params2 <- addSpecies(params, sp) |>
removeSpecies(c("new1", "new2"))) |>
expect_message()

# For now the linecolour and linetype are not preserved
# TODO: fix this in the next overhaul of linecolour and linetype code
Expand All @@ -225,11 +240,11 @@ test_that("adding and then removing species leaves params unaltered", {
# renameSpecies ----
test_that("renameSpecies works", {
sp <- NS_species_params
p <- newMultispeciesParams(sp)
p <- newMultispeciesParams(sp, info_level = 0)
sp$species <- tolower(sp$species)
replace <- NS_species_params$species
names(replace) <- sp$species
p2 <- newMultispeciesParams(sp)
p2 <- newMultispeciesParams(sp, info_level = 0)
p2 <- renameSpecies(p2, replace)
p2@time_modified <- p@time_modified
p2@time_created <- p@time_created
Expand Down
18 changes: 10 additions & 8 deletions tests/testthat/test-newMultispeciesParams.R
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
# newMultispeciesParams ----
# * Dimensions are correct ----
test_that("constructor with species_params and interaction signature gives the right dimensions", {
params <- newMultispeciesParams(NS_species_params, inter)
params <- newMultispeciesParams(NS_species_params, inter, info_level = 0)
# expect_that(params, is_a("MizerParams")) # deprecated, trying to find alternative
expect_equal(class(params)[1], "MizerParams") # alternative?
expect_equal(dim(params@psi)[1], nrow(NS_species_params))
expect_equal(dimnames(params@psi)$sp, as.character(NS_species_params$species))
params_gears <- newMultispeciesParams(NS_species_params_gears, inter)
params_gears <- newMultispeciesParams(NS_species_params_gears, inter, info_level = 0)
expect_equal(unique(dimnames(params_gears@selectivity)$gear),
as.character(unique(params_gears@species_params$gear)))
# pass in other arguments
params_gears <- newMultispeciesParams(NS_species_params_gears, inter, no_w = 50)
params_gears <- newMultispeciesParams(NS_species_params_gears,
inter, no_w = 50, info_level = 0)
expect_length(params_gears@w, 50)
expect_equal(dimnames(params_gears@selectivity)$gear,
unique(NS_species_params_gears$gear))
})

test_that("constructor with only species_params signature gives the right dimensions", {
params <- newMultispeciesParams(NS_species_params)
params <- newMultispeciesParams(NS_species_params, info_level = 0)
expect_true(all(params@interaction == 1))
expect_equal(dim(params@interaction), c(dim(params@psi)[1],
dim(params@psi)[1]))
Expand All @@ -26,23 +27,24 @@ test_that("constructor with only species_params signature gives the right dimens
# * w_min_idx is correct ----
test_that("w_min_idx is being set correctly", {
# default - no w_min in params data so set to first size
params <- newMultispeciesParams(NS_species_params_gears, inter)
params <- newMultispeciesParams(NS_species_params_gears, inter, info_level = 0)
expect_true(all(params@species_params$w_min == params@w[1]))
expect_true(all(params@w_min_idx == 1))
# Set w_min to be the min by hand
NS_species_params_gears$w_min <- 0.001
params <- newMultispeciesParams(NS_species_params_gears, inter)
params <- newMultispeciesParams(NS_species_params_gears, inter, info_level = 0)
expect_true(all(params@w_min_idx == 1))
# Change w_min of one of the species
NS_species_params_gears$w_min <- 0.001
NS_species_params_gears$w_min[7] <- 10
params <- newMultispeciesParams(NS_species_params_gears, inter)
params <- newMultispeciesParams(NS_species_params_gears, inter, info_level = 0)
expect_true(all(params@w_min_idx[c(1:6, 8:12)] == 1))
expect_equal(as.integer(params@w_min_idx[7]), max(which(params@w <= 10)))
})

test_that("Errors are reported", {
expect_error(newMultispeciesParams(NS_species_params, min_w_pp = 1),
expect_error(newMultispeciesParams(NS_species_params, min_w_pp = 1,
info_level = 0),
"min_w_pp must be larger than min_w")
})

Expand Down
8 changes: 5 additions & 3 deletions tests/testthat/test-plots.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ species_params <- NS_species_params_gears
# Make species names numeric because that created problems in the past
species_params$species <- seq_len(nrow(species_params))
species_params$pred_kernel_type <- "truncated_lognormal"
params <- newMultispeciesParams(species_params, inter, no_w = 30,
n = 2 / 3, p = 0.7, lambda = 2.8 - 2 / 3)
(params <- newMultispeciesParams(species_params, inter, no_w = 30,
n = 2 / 3, p = 0.7, lambda = 2.8 - 2 / 3,
info_level = 0)) |>
expect_message("Note: Dimnames of interaction matrix do not match")
sim <- project(params, effort = 1, t_max = 3, dt = 1, t_save = 1)
sim0 <- project(params, effort = 0, t_max = 3, dt = 1, t_save = 1)
species <- c(11, 10)
Expand All @@ -13,7 +15,7 @@ params_bkgrd <- params
params_bkgrd@A[1:3] <- NA
# params object with single species
sp_single <- data.frame(species = 1, w_max = 1000, h = 30)
params_single <- newMultispeciesParams(sp_single, no_w = 30)
params_single <- newMultispeciesParams(sp_single, no_w = 30, info_level = 0)

# Need to use vdiffr conditionally
expect_doppelganger <- function(title, fig, ...) {
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test-project.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
params <- newMultispeciesParams(NS_species_params_gears, inter)
params <- newMultispeciesParams(NS_species_params_gears, inter, info_level = 0)

# time dimension ----
test_that("time dimension is dealt with properly", {
Expand Down Expand Up @@ -112,7 +112,7 @@ test_that("Can pass in initial species", {
test_that("w_min array reference is working OK", {
NS_species_params_gears$w_min <- 0.001
NS_species_params_gears$w_min[1] <- 1
params2 <- newMultispeciesParams(NS_species_params_gears, inter)
params2 <- newMultispeciesParams(NS_species_params_gears, inter, info_level = 0)
sim <- project(params2, effort = 1, t_max = 5)
expect_equal(sim@n[6, 1, 1:(sim@params@w_min_idx[1] - 1)],
rep(0, sim@params@w_min_idx[1] - 1), ignore_attr = TRUE)
Expand Down Expand Up @@ -219,7 +219,7 @@ test_that("Gear checking and sorting is OK", {
# same numerical results as previously ----
test_that("Simulation gives same numerical results as previously",{
params <- newMultispeciesParams(NS_species_params_gears, inter,
n = 2/3, p = 0.7, lambda = 2.8 - 2/3)
n = 2/3, p = 0.7, lambda = 2.8 - 2/3, info_level = 0)
sim <- project(params, t_max = 1)
# expect_known_value(sim@n[2, 3, ], "values/projectn")
# expect_known_value(sim@n_pp[2, ], "values/projectp")
Expand Down
9 changes: 6 additions & 3 deletions tests/testthat/test-project_methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

# North sea
params <- newMultispeciesParams(NS_species_params_gears, inter,
n = 2/3, p = 0.7, lambda = 2.8 - 2/3)
n = 2/3, p = 0.7, lambda = 2.8 - 2/3,
info_level = 0)
no_gear <- dim(params@catchability)[1]
no_sp <- dim(params@catchability)[2]
no_w <- length(params@w)
Expand Down Expand Up @@ -215,7 +216,7 @@ test_that("getPredMort passes correct time", {

test_that("interaction is right way round in getPredMort function", {
inter[, "Dab"] <- 0 # Dab not eaten by anything
params <- newMultispeciesParams(NS_species_params_gears, inter)
params <- newMultispeciesParams(NS_species_params_gears, inter, info_level = 0)
m2 <- getPredMort(params, get_initial_n(params), params@cc_pp)
expect_true(all(m2["Dab", ] == 0))
})
Expand Down Expand Up @@ -535,7 +536,9 @@ test_that("Test that fft based integrator gives similar result as old code", {
species_params$beta[5] <- species_params$beta[5] / 1000
# and use different egg sizes
species_params$w_min <- seq(0.001, 1, length.out = no_sp)
params <- newMultispeciesParams(species_params, inter, no_w = 30, min_w_pp = 1e-12)
params <- newMultispeciesParams(species_params, inter,
no_w = 30, min_w_pp = 1e-12,
info_level = 0)
# create a second params object that does not use fft
params2 <- setPredKernel(params, pred_kernel = getPredKernel(params))
# Test encounter rate integral
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-selectivity_funcs.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ test_that("knife-edge selectivity function is working properly", {
NS_species_params_gears$knife_edge_size[NS_species_params_gears$gear == "Industrial"] <- 500
# Chop off l25, l50, a and b columns - the trawl selectivity
NS_species_params_gears <- NS_species_params_gears[,!(colnames(NS_species_params_gears) %in% c("l25","l50","a","b"))]
params <- newMultispeciesParams(NS_species_params_gears, inter)
params <- newMultispeciesParams(NS_species_params_gears, inter, info_level = 0)
industrial_species <- as.character(NS_species_params_gears$species[NS_species_params_gears$gear == "Industrial"])
pelagic_species <- as.character(NS_species_params_gears$species[NS_species_params_gears$gear == "Pelagic"])
beam_trawl_species <- as.character(NS_species_params_gears$species[NS_species_params_gears$gear == "Beam"])
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-setFishing.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ test_that("validEffort works", {
})
test_that("validEffortParams works when no gears are set up", {
params <- newMultispeciesParams(NS_species_params,
gear_params = data.frame())
gear_params = data.frame(), info_level = 0)
expect_length(validEffortVector(1, params), 0)
expect_length(validEffortVector(NULL, params), 0)
})
Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/test-setInitialValues.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ test_that("We can set and get initial values from sim object", {
})

test_that("setInitialValues gives correct errors", {
params1 <- newMultispeciesParams(NS_species_params, no_w = 20)
params1 <- newMultispeciesParams(NS_species_params, no_w = 20, info_level = 0)
sim <- project(params1, t_max = 2, dt = 1)
params2 <- newMultispeciesParams(NS_species_params, no_w = 30)
params2 <- newMultispeciesParams(NS_species_params, no_w = 30, info_level = 0)
expect_error(setInitialValues(params2, sim),
"The consumer size spectrum of the simulation in `sim` has a different size from that in `params`")

params3 <- newMultispeciesParams(NS_species_params, no_w = 20,
min_w_pp = 1e-4)
min_w_pp = 1e-4, info_level = 0)
expect_error(setInitialValues(params3, sim),
"The resource size spectrum of the simulation in `sim` has a different size from that in `params`.")
params4 <- setComponent(params1, "test",
Expand All @@ -49,7 +49,7 @@ test_that("setInitialValues gives correct errors", {

test_that("Can set initial values in a model with a single species", {
species_params <- NS_species_params[1, ]
params <- newMultispeciesParams(species_params)
params <- newMultispeciesParams(species_params, info_level = 0)
sim <- project(params, t_max = 0.1, t_save = 0.1)
p <- setInitialValues(params, sim)
expect_identical(finalN(sim), initialN(p))
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-single_species.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# We choose the largest species for our single-species
params <- newMultispeciesParams(NS_species_params_gears[12, ])
params <- newMultispeciesParams(NS_species_params_gears[12, ], info_level = 0)
n <- params@initial_n
npp <- params@initial_n_pp
effort <- array(abs(rnorm(10)), dim = c(10, 1))
Expand Down Expand Up @@ -39,6 +39,6 @@ test_that("Can set up model with minimal information", {
stringsAsFactors = FALSE)
sp$w_max <- 1000
sp$k_vb <- 10
params <- newMultispeciesParams(sp)
params <- newMultispeciesParams(sp, info_level = 0)
expect_error(project(params, t_max = 1), NA)
})
Loading

0 comments on commit dd4120e

Please sign in to comment.