Skip to content

Commit

Permalink
addSpecies() now correctly preserves all species_params of the ex…
Browse files Browse the repository at this point in the history
…isting model.
  • Loading branch information
gustavdelius committed Oct 2, 2024
1 parent ea3ae44 commit 836b776
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Authors@R: c(person("Gustav", "Delius", email="gustav.delius@york.ac.uk",
comment = c(ORCID = "0000-0002-8478-3430")),
person("Richard", "Southwell", email="richard.southwell@york.ac.uk",
role=c("ctb", "cph")))
Version: 2.5.1.9003
Version: 2.5.1.9004
License: GPL-3
Imports:
assertthat,
Expand Down
6 changes: 5 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# mizer 2.5.1.9003
# mizer 2.5.1.9004

- `addSpecies()` now correctly preserves all `species_params` of the existing
model.
- New species parameter `w_repro_max` giving the size at which a species
invests 100% of its energy into reproduction. Set to `w_max` by default.
- `getDiet()` now also includes the contribution of the external encounter rate
Expand All @@ -17,6 +19,8 @@
to observed biomass as default (`ratio = T`), as this is more useful visually
to see how far off modelled biomass is from observed biomass.
- Deprecated `matchYields()` and `calibrateYield()`.
- Improved some unit tests.
- Some improvements to documentation.

# mizer 2.5.1

Expand Down
16 changes: 12 additions & 4 deletions R/manipulate_species.R
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ addSpecies <- function(params, species_params,
"has its catchability array protected by a comment.")
}

# set interaction
# set interaction ----
no_old_sp <- nrow(params@species_params)
old_sp <- 1:no_old_sp
no_new_sp <- nrow(species_params)
Expand All @@ -112,9 +112,9 @@ addSpecies <- function(params, species_params,
# combine species params ----

# Move linecolour and linetype into species_params
params@given_species_params$linetype <-
params@species_params$linetype <-
params@linetype[as.character(params@species_params$species)]
params@given_species_params$linecolour <-
params@species_params$linecolour <-
params@linecolour[as.character(params@species_params$species)]

# Make sure that all columns exist in both data frames
Expand All @@ -123,9 +123,16 @@ addSpecies <- function(params, species_params,
missing <- setdiff(names(given_species_params), names(params@given_species_params))
params@given_species_params[missing] <- NA

missing <- setdiff(names(params@species_params), names(species_params))
species_params[missing] <- NA
missing <- setdiff(names(species_params), names(params@species_params))
params@species_params[missing] <- NA

# add the new species (with parameters described by species_params),
# to make a larger species_params dataframe.
combi_species_params <- rbind(params@given_species_params, given_species_params,
combi_species_params <- rbind(params@species_params, species_params,
stringsAsFactors = FALSE)
combi_given_species_params <- rbind(params@given_species_params, given_species_params,
stringsAsFactors = FALSE)

# combine gear params ----
Expand Down Expand Up @@ -194,6 +201,7 @@ addSpecies <- function(params, species_params,
lambda = params@resource_params$lambda,
w_pp_cutoff = params@resource_params$w_pp_cutoff
)
p@given_species_params <- combi_given_species_params

# Set effort ----
new_gear <- setdiff(unique(gear_params$gear),
Expand Down
22 changes: 19 additions & 3 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
expect_message(pa <- addSpecies(p, species_params))
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 @@ -160,9 +160,25 @@ test_that("Added species stay at low abundance", {
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)
expect_lt(finalN(sim)[no_sp, 1] / initialN(sim)[no_sp, 1], 1.05)
})

test_that("addSpecies preserves both given and other species params", {

params <- newTraitParams()
params@given_species_params$b <- 3
params@species_params$w_mat25 <- params@species_params$w_mat25 * 1.01
sp <- data.frame(species = "new",
w_max = 10, test = "test")
expect_message(p <- addSpecies(params, sp))
no_sp <- nrow(params@species_params)
expect_identical(p@species_params$w_mat25[1:no_sp],
params@species_params$w_mat25[1:no_sp])
expect_identical(p@given_species_params$b[1:no_sp],
params@given_species_params$b[1:no_sp])
expect_in("test", names(p@species_params))
expect_in("test", names(p@given_species_params))
})

# removeSpecies ----
test_that("removeSpecies works", {
Expand Down Expand Up @@ -209,7 +225,7 @@ test_that("adding and then removing species leaves params unaltered", {
# two arbitrary species
sp <- data.frame(species = c("new1", "new2"),
w_max = c(10, 100),
k_vb = c(4, 1),
k_vb = c(4, 2),
stringsAsFactors = FALSE)
# add comments to test that they will be preserved as well
comment(params) <- "test"
Expand Down

0 comments on commit 836b776

Please sign in to comment.