You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
min_w is an argument of newMultispeciesParams()/emptyParams() and it sets the w_min species parameter. But w_min never reaches given_species_params, so any operation that rebuilds the species parameters from the given ones discards the user's value and silently substitutes the default 0.001.
The principle this breaks: a species parameter that is an argument to a function belongs in given_species_params, even when the user does not override the argument's default. n and p are handled that way; w_min is not.
Reproduction
sp<-data.frame(species= c("a", "b"), w_inf= c(100, 200))
params<- newMultispeciesParams(sp, min_w=1e-4)
species_params(params)$w_min[1] # 1e-04"w_min"%in% names(given_species_params(params)) # FALSE <- the bug# any round-trip through given_species_params<- rebuilds the table
given_species_params(params)$alpha<-0.7
species_params(params)$w_min[1] # 0.001 <- silently changed
The egg size changes by a factor of ten with no warning, and w_min_idx moves from 1 to 16.
Why
emptyParams() snapshots the given species parameters before it applies the min_w argument:
validParams() has a guard that raises w_min when it drops below the weight grid. When min_w is larger than 0.001 that guard fires and restores the value — at the cost of a spurious warning on a round-trip that changed nothing about sizes:
params<- newMultispeciesParams(sp, min_w=0.01)
given_species_params(params)$alpha<-0.7#> Warning: The minimum weight of some species was smaller than the minimum weight#> of the weight grid in the model: a, b. I have increased it to 0.01. ...
When min_w is smaller than 0.001, the reset value sits above the grid minimum, the guard never fires, and the change is silent. That is the case in the reproduction above.
So there are two symptoms: silent loss of a small min_w, and a spurious warning for a large one.
Suggested fix
Apply the min_w argument before taking the snapshot, i.e. move line 555 above line 551 in emptyParams(), so that w_min lands in given_species_params the way n and p do.
Worth checking while doing so: validGivenSpeciesParams() contains a correction that lowers w_min when it is not smaller than w_mat (w_min[wrong] <- pmin(0.001, w_mat[wrong] / 10)). Once w_min is present at snapshot time that correction will start applying to it, which is probably desirable but is a behaviour change and should get a test.
A regression test should assert that w_min survives a given_species_params<- round-trip for min_w both above and below 0.001.
Related
Give each species parameter default a single home #458 — gives each species parameter default a single home. This is the same family of problem (w_min has a default in emptyParams() via min_w and another in species_params.data.frame()), but the PR does not touch it: it deliberately leaves the emptyParams()w_min default alone, since that one has to run before validSpeciesParams() or the min_w argument would be ignored entirely.
Summary
min_wis an argument ofnewMultispeciesParams()/emptyParams()and it sets thew_minspecies parameter. Butw_minnever reachesgiven_species_params, so any operation that rebuilds the species parameters from the given ones discards the user's value and silently substitutes the default0.001.The principle this breaks: a species parameter that is an argument to a function belongs in
given_species_params, even when the user does not override the argument's default.nandpare handled that way;w_minis not.Reproduction
The egg size changes by a factor of ten with no warning, and
w_min_idxmoves from 1 to 16.Why
emptyParams()snapshots the given species parameters before it applies themin_wargument:https://github.com/sizespectrum/mizer/blob/master/R/MizerParams-class.R#L551-L556
So
w_minis added tospecies_paramsbut never togiven_species_params.given_species_params<-then rebuilds withand
validSpeciesParams()applies its own hardcodedw_min = 0.001.Contrast
newMultispeciesParams(), which gets this right for itsnandparguments by injecting them into the data frame beforeemptyParams()is called:https://github.com/sizespectrum/mizer/blob/master/R/newMultispeciesParams.R#L179-L180
Why it is not always visible
validParams()has a guard that raisesw_minwhen it drops below the weight grid. Whenmin_wis larger than0.001that guard fires and restores the value — at the cost of a spurious warning on a round-trip that changed nothing about sizes:When
min_wis smaller than0.001, the reset value sits above the grid minimum, the guard never fires, and the change is silent. That is the case in the reproduction above.So there are two symptoms: silent loss of a small
min_w, and a spurious warning for a large one.Suggested fix
Apply the
min_wargument before taking the snapshot, i.e. move line 555 above line 551 inemptyParams(), so thatw_minlands ingiven_species_paramsthe waynandpdo.Worth checking while doing so:
validGivenSpeciesParams()contains a correction that lowersw_minwhen it is not smaller thanw_mat(w_min[wrong] <- pmin(0.001, w_mat[wrong] / 10)). Oncew_minis present at snapshot time that correction will start applying to it, which is probably desirable but is a behaviour change and should get a test.A regression test should assert that
w_minsurvives agiven_species_params<-round-trip formin_wboth above and below0.001.Related
w_minhas a default inemptyParams()viamin_wand another inspecies_params.data.frame()), but the PR does not touch it: it deliberately leaves theemptyParams()w_mindefault alone, since that one has to run beforevalidSpeciesParams()or themin_wargument would be ignored entirely.🤖 Generated with Claude Code
https://claude.ai/code/session_01QZop1EdWQNm9SXGCqwWg8k