Skip to content

Commit

Permalink
species_params<-() and given_species_params<-() now check that sp…
Browse files Browse the repository at this point in the history
…ecies names match.
  • Loading branch information
gustavdelius committed Dec 17, 2024
1 parent c2b7c50 commit 322dcc2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
- `validParams()` also calls `validGearParams()`.
- `validParams()` checks that `w_min` is valid for all species and increases it
if necessary.
- `species_params<-()` and `given_species_params<-()` now check that species
names match.

## Bug fixes

Expand Down
6 changes: 6 additions & 0 deletions R/species_params.R
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ species_params <- function(params) {
`species_params<-` <- function(params, value) {
assert_that(is(params, "MizerParams"))
value <- validSpeciesParams(value)
if (!all(value$species == params@species_params$species)) {
stop("The species names in the new species parameter data frame do not match the species names in the model.")
}
params@species_params <- value
suppressMessages(setParams(params))
}
Expand All @@ -157,6 +160,9 @@ given_species_params <- function(params) {
`given_species_params<-` <- function(params, value) {
assert_that(is(params, "MizerParams"))
value <- validGivenSpeciesParams(value)
if (!all(value$species == params@species_params$species)) {
stop("The species names in the new species parameter data frame do not match the species names in the model.")
}
old_value <- params@given_species_params

# Create data frame which contains only the values that have changed
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/test-species_params.R
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,15 @@ test_that("Setting species params works", {
expect_identical(params@given_species_params$beta, beta)
})

test_that("Error if species names don't match", {
sp <- NS_species_params
sp$species[2] <- "not a species"
expect_error(species_params(NS_params) <- sp,
"The species names in the new species parameter data frame do not match")
expect_error(given_species_params(NS_params) <- sp,
"The species names in the new species parameter data frame do not match")
})


test_that("set_species_params_from_length works", {
sp <- data.frame(species = 1:2, a = 0.01, b = 3)
Expand Down

0 comments on commit 322dcc2

Please sign in to comment.