From 765d6d70f2574ad1cd1e4761bb5960d5371c2f0f Mon Sep 17 00:00:00 2001 From: Gustav Delius Date: Mon, 14 Oct 2024 16:13:19 +0100 Subject: [PATCH] A patch update so that users who had changed `w_max` manually in their model will not get unhelpful error messages when trying to use this model in the new version. --- DESCRIPTION | 4 ++-- NAMESPACE | 1 + NEWS.md | 6 ++++++ R/MizerSim-class.R | 22 ++++++++++++++++++++++ R/compareParams.R | 2 -- R/extension.R | 3 +-- R/newMultispeciesParams.R | 1 - R/upgrade.R | 14 +++++++++++++- R/validSpeciesParams.R | 2 +- man/validSim.Rd | 22 ++++++++++++++++++++++ 10 files changed, 68 insertions(+), 9 deletions(-) create mode 100644 man/validSim.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 450ab43c..2ca05e9e 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: mizer Title: Dynamic Multi-Species Size Spectrum Modelling -Date: 2024-11-13 +Date: 2024-11-14 Type: Package Description: A set of classes and methods to set up and run multi-species, trait based and community size spectrum ecological models, focused on the marine @@ -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.2 +Version: 2.5.2.1 License: GPL-3 Imports: assertthat, diff --git a/NAMESPACE b/NAMESPACE index 5bdd9a2e..c102b565 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -246,6 +246,7 @@ export(validEffortVector) export(validGearParams) export(validGivenSpeciesParams) export(validParams) +export(validSim) export(validSpeciesParams) export(valid_gears_arg) export(valid_species_arg) diff --git a/NEWS.md b/NEWS.md index ac6321a3..8741874f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,9 @@ +# mizer 2.5.2.1 + +A patch update so that users who had changed `w_max` manually in their model +will not get unhelpful error messages when trying to use this model in the +new version. + # mizer 2.5.2 - Fixed bug that had led `newCommunityParams()` to set up resource parameters diff --git a/R/MizerSim-class.R b/R/MizerSim-class.R index 2a5180a4..07044188 100644 --- a/R/MizerSim-class.R +++ b/R/MizerSim-class.R @@ -241,6 +241,28 @@ MizerSim <- function(params, t_dimnames = NA, t_max = 100, t_save = 1) { return(sim) } +#' Validate MizerParams object and upgrade if necessary +#' +#' Checks that the given MizerParams object is valid and upgrades it if +#' necessary by calling [upgradeParams()]. +#' +#' Besides upgrading if necessary, the only change that may be made to the +#' given MizerParams object is that the `w_min_idx` slot is recalculated. +#' +#' @param params The MizerParams object to validate +#' @return A valid MizerParams object +#' @export +validSim <- function(sim) { + assert_that(is(sim, "MizerSim")) + if (needs_upgrading(sim)) { + params <- suppressWarnings(upgradeSim(sim)) + warning("Your MizerSim object was created with an earlier version of mizer. You can upgrade it with `sim <- upgradeSim(sim)` where you should replace `sim` by the name of the variable that holds your MizerSim object.") + } + sim@params <- validParams(sim@params) + validObject(sim) + sim +} + #' Time series of size spectra #' #' Fetch the simulation results for the size spectra over time. diff --git a/R/compareParams.R b/R/compareParams.R index e9c5862f..47668161 100644 --- a/R/compareParams.R +++ b/R/compareParams.R @@ -12,8 +12,6 @@ #' species_params(params2)$w_mat[1] <- 10 #' compareParams(params1, params2) compareParams <- function(params1, params2) { - validObject(params1) - validObject(params2) assert_that(is(params1, "MizerParams")) assert_that(is(params2, "MizerParams")) diff --git a/R/extension.R b/R/extension.R index 0c1f0371..2da7efc2 100644 --- a/R/extension.R +++ b/R/extension.R @@ -83,8 +83,7 @@ setRateFunction <- function(params, rate, fun) { #' `rate` argument. #' @export getRateFunction <- function(params, rate) { - assert_that(is(params, "MizerParams")) - validObject(params) + params <- validParams(params) if (missing(rate)) { return(params@rates_funcs) } diff --git a/R/newMultispeciesParams.R b/R/newMultispeciesParams.R index 855a9f46..5b1a7712 100644 --- a/R/newMultispeciesParams.R +++ b/R/newMultispeciesParams.R @@ -340,7 +340,6 @@ setParams <- function(params, interaction = NULL, ...) { params@ft_mask <- t(sapply(params@species_params$w_max, function(x) params@w_full < x)) - params <- suppressWarnings(validParams(params)) params <- setInteraction(params, interaction) params <- setPredKernel(params, ...) params <- setMaxIntakeRate(params, ...) diff --git a/R/upgrade.R b/R/upgrade.R index bf861a79..25f29aeb 100644 --- a/R/upgrade.R +++ b/R/upgrade.R @@ -15,7 +15,7 @@ needs_upgrading <- function(object) { stop("The object you supplied is neither a MizerParams nor a MizerSim object.") } !.hasSlot(params, "mizer_version") || - params@mizer_version < "2.4.1.9002" + params@mizer_version < "2.5.2" } #' Upgrade MizerParams object from earlier mizer versions @@ -360,6 +360,18 @@ upgradeParams <- function(params) { params@species_params$w_mat / (3 ^ (1 / 10))) } + # Before 2.5.2 ---- + if (version < "2.5.2") { + # setParams() will update the ft_mask if necessary + # It will also warn if `w_max` is not valid + ft_mask <- params@ft_mask + params <- setParams(params) + if (!identical(as.vector(params@ft_mask), + as.vector(ft_mask))) { + warning("The ft_mask slot has been updated.") + } + } + params@mizer_version <- packageVersion("mizer") params@time_modified <- lubridate::now() validObject(params) diff --git a/R/validSpeciesParams.R b/R/validSpeciesParams.R index 75f5cf59..290f7727 100644 --- a/R/validSpeciesParams.R +++ b/R/validSpeciesParams.R @@ -146,7 +146,7 @@ validGivenSpeciesParams <- function(species_params) { if (any(wrong)) { warning("For the species ", paste(sp$species[wrong], collapse = ", "), - " the value for `w_max` is smaller than that of `w_repro_max`.", + " the value for `w_max` is larger than that of `w_repro_max`.", " I have corrected that by setting `w_max` equal to `w_repro_max`.") sp$w_max[wrong] <- sp$w_repro_max[wrong] } diff --git a/man/validSim.Rd b/man/validSim.Rd new file mode 100644 index 00000000..69387a2a --- /dev/null +++ b/man/validSim.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/MizerSim-class.R +\name{validSim} +\alias{validSim} +\title{Validate MizerParams object and upgrade if necessary} +\usage{ +validSim(sim) +} +\arguments{ +\item{params}{The MizerParams object to validate} +} +\value{ +A valid MizerParams object +} +\description{ +Checks that the given MizerParams object is valid and upgrades it if +necessary by calling \code{\link[=upgradeParams]{upgradeParams()}}. +} +\details{ +Besides upgrading if necessary, the only change that may be made to the +given MizerParams object is that the \code{w_min_idx} slot is recalculated. +}