From 8bb1eb696a006fe87ce350a7bf9247d0f2ef5d82 Mon Sep 17 00:00:00 2001 From: Eliot McIntire Date: Mon, 16 Nov 2026 22:13:57 -0700 Subject: [PATCH 1/2] A species or fuel class absent from the vegetation is 0 for a stored fit Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_014G18Mik3fH4z5M5CyMSUS6 --- NEWS.md | 1 + fireSense_spreadPredict.R | 14 +++++++++----- tests/testthat/test-spreadInteractions.R | 11 +++++++++-- tests/testthat/test-youngAgeExclusivity.R | 19 +++++++++++++++++++ 4 files changed, 38 insertions(+), 7 deletions(-) diff --git a/NEWS.md b/NEWS.md index cab60f1..b3d7550 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,6 @@ # fireSense_spreadPredict (development version) +- A fitted fuel covariate that this run's covariates lack (the species or fuel class is absent from the vegetation) is added as no biomass (`logMinB(0)`, as `fireSenseUtils::fuelCovsToCoarse()` fills it) before the covariates are rescaled, instead of stopping with "have no covariate". Climate, non-forest and other missing covariates still stop. Uses the new `fireSenseUtils::addAbsentSpeciesCols()` (`Depends-on: PredictiveEcology/fireSenseUtils`; set the `reqdPkgs` floor after it merges). - A ledger row with an interaction coefficient (`a:b`, e.g. `CMD:dom_agb_Pice_mar`, from a `fireSense_spreadFit` fitted with `fireSense_dataPrepFit`'s `spreadInteractions = "climate:fuel"`) predicts with it: `spreadProbOneELF()` needs the covariates `a` and `b` (and their ranges in `covMinMax_spread`), and `fireSenseUtils::spreadProbFromIntegerCovs()` forms their product after each is rescaled and centred with the row's `covCentre_spread` entry for it (0 where `youngAge` cleared the fuel), and multiplies it by the coefficient, as the fit does. A row without interactions predicts exactly as before. Needs fireSenseUtils >= 0.2.3.9177 (#242). - With `paramSets`: the cache of a parameter set's surface no longer reads all the values of `flammableRTM` and `rasterToMatchLargeELF` every replicate (a new raster object or a change at 1000 sampled cells is a change) and returns the stored surface without copying it (a changed stored surface is recomputed). On a 20-million-cell raster a replicate that reuses a surface takes 0.35 s instead of 0.9 s, most of it SpaDES. `paramSets` stops if the ledger rows have the simulated ELF twice, or if a file of ledger rows has none for it. diff --git a/fireSense_spreadPredict.R b/fireSense_spreadPredict.R index 3cb885f..cc72c14 100644 --- a/fireSense_spreadPredict.R +++ b/fireSense_spreadPredict.R @@ -507,6 +507,15 @@ spreadProbOneELF <- function(covs, params, covMinMax, yr, maxFireSpread, lowerSp params[randomEffects] <- NULL } + ## the intercept, if the fit has one, is a coefficient with no covariate: it multiplies a column of 1s + hasIntercept <- fireSenseUtils::spreadInterceptTxt %in% names(params) + fitted <- setdiff(names(params), c(unlist(fireSenseUtils::logisticParamNames), fireSenseUtils::spreadInterceptTxt)) + ## an interaction (`CMD:fuel`) is made from its two covariates by spreadProbFromIntegerCovs(), as in the fit + needed <- fireSenseUtils::spreadSourceCols(fitted) + ## a fuel class the vegetation lacks has no biomass: logMinB(0), as fireSenseUtils::fuelCovsToCoarse() fills + ## it. Set here, with the other fuel columns, so a linear-fuel fit's log is undone for it too. + covs <- fireSenseUtils::addAbsentSpeciesCols(covs, needed, value = fireSenseUtils::logMinB(0)) + ## Fuel biomass arrives logged (fireSenseUtils::logMinB()). A fit made on LINEAR fuel biomass has ## fireSenseUtils::fuelLinearRange, c(0, 1e4), as that covariate's covMinMax_spread, and its ## coefficients only mean anything for biomass / 1e4: undo the log with the function the fit used. @@ -521,11 +530,6 @@ spreadProbOneELF <- function(covs, params, covMinMax, yr, maxFireSpread, lowerSp ## every fitted coefficient needs a covariate: without one the logistic gets no covariates and ## fails later in rowMeans(), with no hint of the cause - ## the intercept, if the fit has one, is a coefficient with no covariate: it multiplies a column of 1s - hasIntercept <- fireSenseUtils::spreadInterceptTxt %in% names(params) - fitted <- setdiff(names(params), c(unlist(fireSenseUtils::logisticParamNames), fireSenseUtils::spreadInterceptTxt)) - ## an interaction (`CMD:fuel`) is made from its two covariates by spreadProbFromIntegerCovs(), as in the fit - needed <- fireSenseUtils::spreadSourceCols(fitted) noCov <- setdiff(needed, names(covs)) if (length(noCov)) { stop( diff --git a/tests/testthat/test-spreadInteractions.R b/tests/testthat/test-spreadInteractions.R index 0f6ef4d..b3099b3 100644 --- a/tests/testthat/test-spreadInteractions.R +++ b/tests/testthat/test-spreadInteractions.R @@ -35,12 +35,19 @@ test_that("with an intercept and centres, the interaction is the product of the expect_equal(predVals(toyRun(ins))[c(1, 2, 3, 4, 6, 7, 8)], handLogistic3(x), tolerance = 1e-7) }) -test_that("a fitted interaction whose covariate is missing stops, naming it", { +test_that("a fitted interaction whose fuel covariate is absent is formed from no biomass; another missing covariate stops, naming it", { covs <- toyCovariates() + withFuel <- data.table::copy(covs) + data.table::set(withFuel, j = "fuelA", value = fireSenseUtils::logMinB(rep(0, NROW(covs)))) data.table::set(covs, j = "fuelA", value = NULL) ins <- insX() ins$fireSense_SpreadCovariates <- covs - expect_error(toyRun(ins), "fuelA") + ref <- insX() + ref$fireSense_SpreadCovariates <- withFuel + expect_equal(suppressMessages(predVals(toyRun(ins))), predVals(toyRun(ref))) + data.table::set(covs, j = "youngAge", value = NULL) + ins$fireSense_SpreadCovariates <- covs + expect_error(toyRun(ins), "youngAge") }) test_that("the prediction is the fit's spreadProb for the same covariates, with fuel cleared where youngAge is 1", { diff --git a/tests/testthat/test-youngAgeExclusivity.R b/tests/testthat/test-youngAgeExclusivity.R index d68b371..f12f763 100644 --- a/tests/testthat/test-youngAgeExclusivity.R +++ b/tests/testthat/test-youngAgeExclusivity.R @@ -32,3 +32,22 @@ test_that("a young pixel's covariates entering the link are youngAge = 1 and all ## x = 0.75 + 0 + 0.5 + 0 = 1.25 expect_equal(v[3], handLogistic3(1.25), tolerance = 1e-7) }) + +test_that("a fuel class the vegetation lacks predicts as a fuel class with no biomass, not a 'no covariate' stop", { + withFuel <- toyCovariates() + withFuel$fuelA <- fireSenseUtils::logMinB(rep(0, NROW(withFuel))) # no biomass, as the covariates would have it + covs <- withFuel + covs$fuelA <- NULL # the species is not in the vegetation + formula <- "~ MDC + youngAge + fuelA - 1" + for (range in list(c(0, 1e4), c(0, 10))) { # linear-fuel fit, and a log-scale fit + run <- function(cv) { + ins <- toyInputs(covs = cv, params = toyParams(), formula = formula) + ins$covMinMax_spread$fuelA <- range + predVals(toyRun(ins)) + } + expect_equal(suppressMessages(run(covs)), run(withFuel)) + } + ## a missing climate covariate is still an error + ins <- toyInputs(covs = covs, params = toyParams(CMDsp = 1.5), formula = "~ MDC + CMDsp - 1") + expect_error(toyRun(ins)) +}) From 99eee3e35b19380cfad1a503d14e78454181d481 Mon Sep 17 00:00:00 2001 From: Eliot McIntire Date: Mon, 16 Nov 2026 22:28:12 -0700 Subject: [PATCH 2/2] Zero-fill only when the covariates have no species column the fit lacks; exclude drought indices from species columns Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_014G18Mik3fH4z5M5CyMSUS6 --- tests/testthat/test-youngAgeExclusivity.R | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/testthat/test-youngAgeExclusivity.R b/tests/testthat/test-youngAgeExclusivity.R index f12f763..a271008 100644 --- a/tests/testthat/test-youngAgeExclusivity.R +++ b/tests/testthat/test-youngAgeExclusivity.R @@ -47,6 +47,11 @@ test_that("a fuel class the vegetation lacks predicts as a fuel class with no bi } expect_equal(suppressMessages(run(covs)), run(withFuel)) } + ## a fuel class the fit never saw is a renamed class, not an absent species: the stop stays + other <- covs + other$fuelB <- fireSenseUtils::logMinB(rep(0, NROW(other))) + ins <- toyInputs(covs = other, params = toyParams(), formula = formula) + expect_error(toyRun(ins), "fuelA") ## a missing climate covariate is still an error ins <- toyInputs(covs = covs, params = toyParams(CMDsp = 1.5), formula = "~ MDC + CMDsp - 1") expect_error(toyRun(ins))