From be6a76a9deec9600af02a1b5fb170e6a3e40a877 Mon Sep 17 00:00:00 2001 From: Eliot McIntire Date: Thu, 19 Nov 2026 22:28:58 -0700 Subject: [PATCH 1/2] Predict a spreadClimate = "activation" ledger row as the activation ledgerSpreadClimate() reads the row's mode (fireSenseUtils::spreadClimateTxt); a row without it is "ceiling" with climateRef_spread and a power-law link, else "predictor", as before. ledgerLinkArgs() passes spreadClimate = "activation" for such a row only; spreadProbOneELF() then takes activationIntercept out of the covariate coefficients and gives it to fireSenseUtils::spreadLinkInputs(), and stops when the set and the row disagree. ledgerLinkNumber() checks the activation's link with checkLinkParams(activation =). The per-pixel parameters go out through linkParsRas() as with the ceiling, so fireSense_burn is unchanged. Floor fireSenseUtils >= 0.2.3.9183 (PredictiveEcology/fireSenseUtils#255). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Lx3DXhXsFis2pbqBTh2PPh --- NEWS.md | 2 + fireSense_spreadPredict.R | 60 +++++++++++++--- tests/testthat/test-climateActivation.R | 93 +++++++++++++++++++++++++ 3 files changed, 145 insertions(+), 10 deletions(-) create mode 100644 tests/testthat/test-climateActivation.R diff --git a/NEWS.md b/NEWS.md index 32df969..f33ef36 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # fireSense_spreadPredict (development version) +- A ledger row of `fireSense_spreadFit`'s `spreadClimate = "activation"` predicts as that fit: fuel sets each pixel's ceiling `pc` below p50 and climate is the linear predictor (`fireSenseUtils::spreadLinkInputs(activationIntercept =)`). The row's mode is read by the new `ledgerSpreadClimate()` (column `fireSenseUtils::spreadClimateTxt`); a row without it is "ceiling" when it has `climateRef_spread` and a power-law link, else "predictor", as before. The parameter set's `activationIntercept` is not a covariate coefficient: it goes to the link, and a set and its row that disagree stop. `fireSense_SpreadPowerLaw` is then a per-pixel raster, and `fireSense_SpreadLinear` is the climate term, so `fireSense_burn` adds the year effect to it unchanged. Other rows predict as before. Needs fireSenseUtils >= 0.2.3.9183 (PredictiveEcology/fireSenseUtils#255). + - The yearly spread probability map (`.plots = "png"`) is bigger. Its png is now 10 x 8 inches at 144 dpi (1440 x 1152 pixels), as fireSense_burn's; it was R's `png()` default of 480 x 480, which looked small in `SpaDES.shiny::shine()`. - A ledger row from a `fireSense_spreadFit` with `spreadClimate = "ceiling"` (a power-law `spreadLink` with `climateRef_spread` and `powerLawPc50_spread`) is predicted with climate moving the ceiling `pc` toward p50 (`fireSenseUtils::spreadLinkInputs()`): new `ledgerPowerLawPc50()`; `spreadProbOneELF()` takes `powerLawPc50`, returns the fuels' linear predictor as `linearPredictor` and the link's parameters as the attribute `linkPars` (one row per pixel here). `fireSense_SpreadPowerLaw` is then a raster with a `pc` per pixel even for one ELF, and several ELFs blend the per-pixel parameters (the vector `powerLawParsOf()` is gone: `spreadLinkInputs()` makes the parameters). Rows without `climateRef_spread` predict exactly as before. `fireSense_burn` already reads a per-cell raster of the link's parameters. Needs fireSenseUtils >= 0.2.3.9181 (PredictiveEcology/fireSenseUtils#250). diff --git a/fireSense_spreadPredict.R b/fireSense_spreadPredict.R index c90c809..2e099ef 100644 --- a/fireSense_spreadPredict.R +++ b/fireSense_spreadPredict.R @@ -17,7 +17,7 @@ defineModule(sim, list( documentation = list("README.txt", "fireSense_spreadPredict.Rmd"), reqdPkgs = list("data.table", "magrittr", "Matrix", "methods", "terra", "SpaDES.core (>=3.0.4)", "stats", "ggplot2", "viridis", - "PredictiveEcology/fireSenseUtils@development (>= 0.2.3.9181)"), # 9181: spreadLinkInputs(), powerLawPc50 (fireSenseUtils#250); 9165: lowerAsymptote minimum (#230); 9162: smooth links, checkLinkParams() (#229); 9138: powerLawShape + "PredictiveEcology/fireSenseUtils@development (>= 0.2.3.9183)"), # 9183: spreadLinkInputs(activationIntercept =), spreadClimateTxt (fireSenseUtils#255); 9181: spreadLinkInputs(), powerLawPc50 (fireSenseUtils#250); 9165: lowerAsymptote minimum (#230); 9162: smooth links, checkLinkParams() (#229); 9138: powerLawShape parameters = bindrows( defineParameter(name = "lowerSpreadProb", class = "numeric", default = fireSenseUtils::spreadProbFloor, desc = paste("Lower asymptote of the 2- and 3-parameter logistic. Default", @@ -131,7 +131,9 @@ defineModule(sim, list( "A numeric vector with one fitted ELF; with several, a raster with one layer per name,", "blended like `fireSense_SpreadSD`. With a fit made with climate in the ceiling", "(`fireSense_spreadFit`'s `spreadClimate = \"ceiling\"`: a ledger row with `climateRef_spread` and a", - "power-law link), `pc` differs by pixel and year, so it is a raster even with one ELF.")), + "power-law link), `pc` differs by pixel and year, so it is a raster even with one ELF.", + "So with `spreadClimate = \"activation\"` (the row's `fireSenseUtils::spreadClimateTxt`), where fuel sets", + "each pixel's `pc`.")), createsOutput(objectName = "fireSense_SpreadLinear", objectClass = "SpatRaster", desc = paste("Only for a fit made with a link of `fireSenseUtils::spreadLinearLinks` (else NULL): the linear predictor x of", "each flammable pixel (covariates %*% coefficients), the step before", @@ -508,6 +510,10 @@ ELFblendWeights <- function(elfRas, template, pixelID, ids, width) { #' @param powerLawPc50 the ELF's `powerLawPc50_spread` (`ledgerPowerLawPc50()`): with `climateRef` and a power-law `link` #' (a fit with `fireSense_spreadFit`'s `spreadClimate = "ceiling"`), the runaway threshold the ceiling moves toward; #' `fireSenseUtils::spreadLinkInputs()` ignores it without `climateRef`. +#' @param spreadClimate `NULL`, or `"activation"` for a row whose `spreadClimate` (`ledgerSpreadClimate()`) is that +#' (`ledgerLinkArgs()`); anything else predicts as before. With `"activation"` the parameter set's +#' `activationIntercept` (`fireSenseUtils::activationInterceptTxt`) is not a covariate coefficient: it goes to +#' `fireSenseUtils::spreadLinkInputs()`, where fuel sets each pixel's `pc` below `powerLawPc50` and climate is `x`. #' @return `data.table` with `pixelID` and `spreadProb`, from the one parameter set in `params`; with a `link` #' also `linearPredictor`, the `x` that `spreadProb` is the link of (the fuels' only with climate in the ceiling), and #' the attribute `linkPars`, the link's parameters (`fireSenseUtils::linearLinkPars()`): a vector, or with climate in @@ -517,11 +523,20 @@ spreadProbOneELF <- function(covs, params, covMinMax, yr, maxFireSpread, lowerSp powerLawShape = fireSenseUtils::spreadPowerLawShape, link = if (!is.null(powerLawGamma)) "powerLaw", lowerAsymptote = fireSenseUtils::spreadProbFloor, - richardsNu = fireSenseUtils::spreadRichardsNu) { + richardsNu = fireSenseUtils::spreadRichardsNu, spreadClimate = NULL) { moduleName <- "fireSense_spreadPredict" covs <- copy(covs) - ## the random effects are not covariate coefficients: fireSense_burn applies them (randomEffectOutputs) - randomEffects <- intersect(names(params), fireSenseUtils::spreadRandomEffectTxt) + actTxt <- fireSenseUtils::activationInterceptTxt + activation <- identical(spreadClimate, "activation") + if (activation && !actTxt %in% names(params)) + stop(moduleName, "> a parameter set of spreadClimate 'activation' has no '", actTxt, "'", call. = FALSE) + if (!activation && actTxt %in% names(params)) + stop(moduleName, "> a parameter set has '", actTxt, "', but its ledger row's spreadClimate is not 'activation'", + call. = FALSE) + activationIntercept <- if (activation) params[[actTxt]][[1L]] + ## the random effects are not covariate coefficients: fireSense_burn applies them (randomEffectOutputs); nor is the + ## activation's intercept, which goes to the link (spreadLinkInputs()) + randomEffects <- intersect(names(params), c(fireSenseUtils::spreadRandomEffectTxt, actTxt)) if (length(randomEffects)) { params <- as.data.frame(params) params[randomEffects] <- NULL @@ -618,7 +633,8 @@ spreadProbOneELF <- function(covs, params, covMinMax, yr, maxFireSpread, lowerSp if (is.null(powerLawGamma)) powerLawGamma <- fireSenseUtils::spreadPowerLawGamma inputs <- spreadLinkInputs(mat, logisticPars, covPars, lowerSpreadProb, link, climateRef, powerLawPc50, powerLawGamma = powerLawGamma, - powerLawShape = powerLawShape, lowerAsymptote = lowerAsymptote, richardsNu = richardsNu) + powerLawShape = powerLawShape, lowerAsymptote = lowerAsymptote, richardsNu = richardsNu, + activationIntercept = activationIntercept) ## the link's input: the power law clips it at 0, so it cannot be recovered from spreadProb out <- data.table(pixelID = shortAnnDT$pixelID, spreadProb = linearLinkProb(inputs$x, link, inputs$pars), linearPredictor = inputs$x) @@ -767,7 +783,8 @@ ledgerLinkNumber <- function(sa, i, col, links) { if (!any(link %in% links)) return(NULL) num <- function(cn) if (cn %in% names(sa)) sa[[cn]][[i]] tryCatch(fireSenseUtils::checkLinkParams(link, num(fireSenseUtils::spreadFitLowerAsymptoteTxt), - num(fireSenseUtils::spreadFitRichardsNuTxt)), + num(fireSenseUtils::spreadFitRichardsNuTxt), + activation = identical(ledgerSpreadClimate(sa, i), "activation")), error = function(e) stop("fireSense_spreadPredict: ledger row ", i, " has spreadLink '", link, "': ", conditionMessage(e), " (columns '", fireSenseUtils::spreadFitLowerAsymptoteTxt, "', '", fireSenseUtils::spreadFitRichardsNuTxt, "')", call. = FALSE)) @@ -797,14 +814,37 @@ ledgerRichardsNu <- function(sa, i) #' #' @inheritParams ledgerLinkNumber #' @return A list of `link` (only for a link of `fireSenseUtils::spreadLinearLinks`), `powerLawGamma` (not `"richards"`), -#' `powerLawShape`, `lowerAsymptote` and `richardsNu` (the links that have them); the others are left out, so they -#' take `spreadProbOneELF()`'s defaults. A row of any other link, or without one, has `powerLawShape` only. +#' `powerLawShape`, `lowerAsymptote` and `richardsNu` (the links that have them), and `spreadClimate` for an +#' `"activation"` row; the others are left out, so they take `spreadProbOneELF()`'s defaults. A row of any other link, +#' or without one, has `powerLawShape` only. ledgerLinkArgs <- function(sa, i) { link <- ledgerSpreadLink(sa, i) if (!any(link %in% fireSenseUtils::spreadLinearLinks)) link <- NULL + mode <- ledgerSpreadClimate(sa, i) Filter(Negate(is.null), list(link = link, powerLawGamma = ledgerPowerLawGamma(sa, i), powerLawShape = ledgerPowerLawShape(sa, i), - lowerAsymptote = ledgerLowerAsymptote(sa, i), richardsNu = ledgerRichardsNu(sa, i))) + lowerAsymptote = ledgerLowerAsymptote(sa, i), richardsNu = ledgerRichardsNu(sa, i), + spreadClimate = if (identical(mode, "activation")) mode)) +} + +#' Where the fit of one ledger row put climate +#' +#' `fireSense_spreadFit` records its `spreadClimate` in the row (`fireSenseUtils::spreadClimateTxt`). A row without it +#' (written before it existed) is read as the fit made it: `"ceiling"` when it has `climateRef_spread` +#' (`ledgerClimateRef()`) and a power-law link, else `"predictor"`. +#' +#' @inheritParams ledgerLinkNumber +#' @return One of `fireSenseUtils::spreadClimateOptions`; an unknown value stops, naming the row. +ledgerSpreadClimate <- function(sa, i) { + col <- fireSenseUtils::spreadClimateTxt + mode <- if (col %in% names(sa)) as.character(sa[[col]][[i]]) + if (!length(mode) || is.na(mode)) + return(if (!is.null(ledgerClimateRef(sa, i)) && any(ledgerSpreadLink(sa, i) %in% fireSenseUtils::spreadPowerLawLinks)) + "ceiling" else "predictor") + if (!mode %in% fireSenseUtils::spreadClimateOptions) + stop("fireSense_spreadPredict: ledger row ", i, " has an unknown '", col, "' (", mode, "); one of ", + paste(fireSenseUtils::spreadClimateOptions, collapse = ", "), " expected", call. = FALSE) + mode } #' The power-law curvature exponent of one ledger row diff --git a/tests/testthat/test-climateActivation.R b/tests/testthat/test-climateActivation.R new file mode 100644 index 0000000..7c9afd9 --- /dev/null +++ b/tests/testthat/test-climateActivation.R @@ -0,0 +1,93 @@ +## A ledger row from a fit made with fireSense_spreadFit's spreadClimate = "activation" records the mode +## (fireSenseUtils::spreadClimateTxt), has a "powerLawSmooth" link at the floor, the climate reference, p50, p50 as +## maxAsymptote and an activationIntercept. Fuel then sets each pixel's ceiling and climate is x +## (fireSenseUtils::spreadLinkInputs()): +## pc = floor + (p50 - floor) * plogis(eta), eta = the non-climate terms; x = a + sum(beta_clim * (climate - climateRef)). +## Toy covariates and coefficients: setup-toySpread.R (MDC is the climate, rescaled MDC / 200). + +modeColCA <- fireSenseUtils::spreadClimateTxt +actTxtCA <- fireSenseUtils::activationInterceptTxt + +inputsCA <- function(mode = "activation", a = 0.8, ref = list(MDC = 0.4), p50 = 0.3, shape = 0.5) { + ins <- inputsPL(gamma = 1.9, link = "powerLawSmooth") + sa <- ins$studyAreaWithSpreadParams + sa[[fireSenseUtils::spreadFitLowerAsymptoteTxt]] <- 0.13 + sa[[shapeColPL]] <- shape + sa[[fireSenseUtils::spreadFitClimateRefTxt]] <- list(ref) + sa[[fireSenseUtils::spreadFitPowerLawPc50Txt]] <- p50 + if (!is.null(mode)) sa[[modeColCA]] <- mode + pars <- sa$params[[1]] + pars$maxAsymptote <- p50 + if (!is.null(a)) pars[[actTxtCA]] <- a + sa$params <- list(pars) + ins$studyAreaWithSpreadParams <- sa + ins +} + +## by hand, in cell order +handCA <- function(a = 0.8, p50 = 0.3, floor = 0.13, ref = 0.4, gamma = 1.9, shape = 0.5) { + cv <- toyCovariates(); o <- order(cv$pixelID) + eta <- -0.5 * cv$youngAge[o] + 1 * cv$fuelA[o] / 8 + pc <- floor + (p50 - floor) * plogis(eta) + x <- a + 1.5 * (cv$MDC[o] / 200 - ref) + list(p = ifelse(x >= 0, pc - (pc - floor) * exp(-(pmax(x, 0) / gamma)^shape), floor), pc = pc, x = x) +} + +test_that("an activation row predicts with fuel setting each pixel's ceiling and climate in x", { + sim <- toyRun(inputsCA()) + h <- handCA() + expect_equal(predVals(sim)[cellsPL], h$p, tolerance = 1e-9) + expect_true(all(predVals(sim)[cellsPL] <= 0.3)) + expect_equal(terra::values(sim$fireSense_SpreadLinear, mat = FALSE)[cellsPL], h$x, tolerance = 1e-9) + pl <- sim$fireSense_SpreadPowerLaw + expect_s4_class(pl, "SpatRaster") + expect_identical(names(pl), fireSenseUtils::spreadLinkParNames[["powerLawSmooth"]]) + expect_equal(terra::values(pl[["pc"]], mat = FALSE)[cellsPL], h$pc, tolerance = 1e-9) + ## the intercept is not a covariate: the random effects come through as they did, and the row predicts as activation, + ## not as climate in the ceiling (the same row without the mode) + expect_false(isTRUE(all.equal(predVals(sim), predVals(toyRun(inputsCA(mode = NULL, a = NULL)))))) +}) + +test_that("fireSense_burn's reading of the outputs gives the predicted p, and the fit's own link gives it too", { + sim <- toyRun(inputsCA()) + pl <- sim$fireSense_SpreadPowerLaw + ## as fireSense_burn's powerLawPars(): one column per name, one row per cell + pars <- vapply(names(pl), function(nm) terra::values(pl[[nm]], mat = FALSE), numeric(terra::ncell(pl))) + x <- terra::values(sim$fireSense_SpreadLinear, mat = FALSE) + ok <- !is.na(x) + expect_false(anyNA(pars[ok, ])) + expect_equal(fireSenseUtils::linearLinkProb(x[ok], "powerLawSmooth", pars[ok, , drop = FALSE]), predVals(sim)[ok], + tolerance = 1e-9) + ## the fit's objective calls spreadLinkInputs() on its design matrix (rescaled covariates, in the formula's order) + cv <- toyCovariates(); o <- order(cv$pixelID) + mat <- cbind(MDC = cv$MDC[o] / 200, youngAge = cv$youngAge[o], fuelA = cv$fuelA[o] / 8) + fit <- fireSenseUtils::spreadLinkInputs(mat, c(0.3, 2, 1), c(MDC = 1.5, youngAge = -0.5, fuelA = 1), 0.13, + "powerLawSmooth", list(MDC = 0.4), 0.3, powerLawGamma = 1.9, powerLawShape = 0.5, + lowerAsymptote = 0.13, activationIntercept = 0.8) + expect_equal(predVals(sim)[cellsPL], fireSenseUtils::linearLinkProb(fit$x, "powerLawSmooth", fit$pars), tolerance = 1e-12) +}) + +test_that("a row without the mode column predicts as it did: ceiling with climateRef and a power-law link", { + ceil <- inputsCA(mode = NULL, a = NULL) + sa <- ceil$studyAreaWithSpreadParams + expect_identical(ledgerSpreadClimate(sa, 1L), "ceiling") + sa[[modeColCA]] <- "ceiling" + ceil2 <- ceil; ceil2$studyAreaWithSpreadParams <- sa + expect_identical(predVals(toyRun(ceil)), predVals(toyRun(ceil2))) + ## and a row with neither is "predictor" + sa0 <- inputsPL()$studyAreaWithSpreadParams + expect_identical(ledgerSpreadClimate(sa0, 1L), "predictor") + expect_false("spreadClimate" %in% names(ledgerLinkArgs(sa0, 1L))) + expect_identical(ledgerLinkArgs(inputsCA()$studyAreaWithSpreadParams, 1L)$spreadClimate, "activation") +}) + +test_that("an activation row and its parameters must agree, and the link must be powerLawSmooth at the floor", { + expect_error(toyRun(inputsCA(a = NULL)), "has no 'activationIntercept'") + expect_error(toyRun(inputsCA(mode = "ceiling")), "is not 'activation'") + bad <- inputsCA() + bad$studyAreaWithSpreadParams[[fireSenseUtils::spreadFitLowerAsymptoteTxt]] <- 0.05 + expect_error(toyRun(bad), "lowerAsymptote = lowerSpreadProb") + sa <- inputsCA()$studyAreaWithSpreadParams + sa[[modeColCA]] <- "wet" + expect_error(ledgerSpreadClimate(sa, 1L), "unknown") +}) From f3fe3ac35679616dee9c9433d23ee81207f6e070 Mon Sep 17 00:00:00 2001 From: Eliot McIntire Date: Thu, 19 Nov 2026 23:31:37 -0700 Subject: [PATCH 2/2] Pass a recorded spreadClimate through; refuse blending an activation ELF ledgerLinkArgs() passes the mode whenever the row records it (Fable review L4): 'predictor' then keeps climate in x even with a climateRef, and 'ceiling' or 'activation' without one stops. An activation ELF blended with an ELF of another mode stops beside the link check (M1); ceiling with predictor blends as before. The activation's lower-asymptote check uses the run's lowerSpreadProb (L3). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Lx3DXhXsFis2pbqBTh2PPh --- NEWS.md | 2 +- fireSense_spreadPredict.R | 46 ++++++++++++++++--------- tests/testthat/test-climateActivation.R | 34 ++++++++++++++++++ 3 files changed, 65 insertions(+), 17 deletions(-) diff --git a/NEWS.md b/NEWS.md index f33ef36..f62d92b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,6 @@ # fireSense_spreadPredict (development version) -- A ledger row of `fireSense_spreadFit`'s `spreadClimate = "activation"` predicts as that fit: fuel sets each pixel's ceiling `pc` below p50 and climate is the linear predictor (`fireSenseUtils::spreadLinkInputs(activationIntercept =)`). The row's mode is read by the new `ledgerSpreadClimate()` (column `fireSenseUtils::spreadClimateTxt`); a row without it is "ceiling" when it has `climateRef_spread` and a power-law link, else "predictor", as before. The parameter set's `activationIntercept` is not a covariate coefficient: it goes to the link, and a set and its row that disagree stop. `fireSense_SpreadPowerLaw` is then a per-pixel raster, and `fireSense_SpreadLinear` is the climate term, so `fireSense_burn` adds the year effect to it unchanged. Other rows predict as before. Needs fireSenseUtils >= 0.2.3.9183 (PredictiveEcology/fireSenseUtils#255). +- A ledger row of `fireSense_spreadFit`'s `spreadClimate = "activation"` predicts as that fit: fuel sets each pixel's ceiling `pc` below p50 and climate is the linear predictor (`fireSenseUtils::spreadLinkInputs(activationIntercept =)`). The row's mode is read by the new `ledgerSpreadClimate()` (column `fireSenseUtils::spreadClimateTxt`); a row without it is "ceiling" when it has `climateRef_spread` and a power-law link, else "predictor", as before. The parameter set's `activationIntercept` is not a covariate coefficient: it goes to the link, and a set and its row that disagree stop. `fireSense_SpreadPowerLaw` is then a per-pixel raster, and `fireSense_SpreadLinear` is the climate term, so `fireSense_burn` adds the year effect to it unchanged. A row that records its mode is predicted by it: "predictor" keeps climate in x even with a `climateRef_spread`, and "ceiling" or "activation" without one stops. An activation ELF is not blended with an ELF of another mode (an error, beside the one for different links; ceiling with predictor still blends), and the activation's lower-asymptote check uses the run's `lowerSpreadProb`. Other rows predict as before. Needs fireSenseUtils >= 0.2.3.9183 (PredictiveEcology/fireSenseUtils#255). - The yearly spread probability map (`.plots = "png"`) is bigger. Its png is now 10 x 8 inches at 144 dpi (1440 x 1152 pixels), as fireSense_burn's; it was R's `png()` default of 480 x 480, which looked small in `SpaDES.shiny::shine()`. diff --git a/fireSense_spreadPredict.R b/fireSense_spreadPredict.R index 2e099ef..9a4e7fd 100644 --- a/fireSense_spreadPredict.R +++ b/fireSense_spreadPredict.R @@ -255,7 +255,7 @@ spreadPredictRun <- function(sim) { if (NROW(sa) == 1L) { params1 <- paramSetForRep(sa$params[[1]], rep) - lk1 <- ledgerLinkArgs(sa, 1L) + lk1 <- ledgerLinkArgs(sa, 1L, floor = P(sim)$lowerSpreadProb) pred <- do.call(spreadProbOneELF, c(list(covs, params = params1, covMinMax = ledgerCovMinMax(sa, 1L, sim$covMinMax_spread), yr = time(sim), maxFireSpread = P(sim)$maxFireSpread, lowerSpreadProb = P(sim)$lowerSpreadProb, @@ -279,11 +279,17 @@ spreadPredictRun <- function(sim) { warning("fireSense_spreadPredict: ", sum(none), " flammable pixels are more than ", P(sim)$ELFblendWidth, " m from every fitted ELF; they get no spread probability", call. = FALSE) acc <- numeric(NROW(covs)); wsum <- numeric(NROW(covs)); accSD <- matrix(0, NROW(covs), length(randomEffectOutputs), dimnames = list(NULL, names(randomEffectOutputs))) - lks <- lapply(seq_along(ids), function(i) ledgerLinkArgs(sa, i)) + lks <- lapply(seq_along(ids), function(i) ledgerLinkArgs(sa, i, floor = P(sim)$lowerSpreadProb)) links <- unique(unlist(lapply(lks, `[[`, "link"))) if (length(links) > 1L) stop("fireSense_spreadPredict: the fitted ELFs have different spread links (", paste(links, collapse = ", "), "), whose parameters cannot be blended", call. = FALSE) + ## with the activation x is climate and pc a fuel potential; in the other modes x holds the fuels: a blend of an + ## activation ELF with another is neither model. (A ceiling ELF blended with a predictor ELF is allowed, as before.) + modes <- unique(vapply(seq_along(ids), function(i) ledgerSpreadClimate(sa, i), character(1))) + if (length(modes) > 1L && "activation" %in% modes) + stop("fireSense_spreadPredict: the fitted ELFs were fitted with different spreadClimate (", paste(modes, collapse = ", "), + "), whose linear predictors and parameters cannot be blended", call. = FALSE) sim$fireSense_SpreadLink <- links linkParNames <- if (length(links)) fireSenseUtils::spreadLinkParNames[[links]] else character() accPL <- matrix(0, NROW(covs), length(linkParNames), dimnames = list(NULL, linkParNames)) @@ -510,8 +516,9 @@ ELFblendWeights <- function(elfRas, template, pixelID, ids, width) { #' @param powerLawPc50 the ELF's `powerLawPc50_spread` (`ledgerPowerLawPc50()`): with `climateRef` and a power-law `link` #' (a fit with `fireSense_spreadFit`'s `spreadClimate = "ceiling"`), the runaway threshold the ceiling moves toward; #' `fireSenseUtils::spreadLinkInputs()` ignores it without `climateRef`. -#' @param spreadClimate `NULL`, or `"activation"` for a row whose `spreadClimate` (`ledgerSpreadClimate()`) is that -#' (`ledgerLinkArgs()`); anything else predicts as before. With `"activation"` the parameter set's +#' @param spreadClimate `NULL` (a row that does not record it: predicted as it always was), or the row's recorded +#' `spreadClimate` (`ledgerLinkArgs()`). `"predictor"` predicts with climate in x even when the row has a `climateRef`; +#' `"ceiling"` and `"activation"` need one. With `"activation"` the parameter set's #' `activationIntercept` (`fireSenseUtils::activationInterceptTxt`) is not a covariate coefficient: it goes to #' `fireSenseUtils::spreadLinkInputs()`, where fuel sets each pixel's `pc` below `powerLawPc50` and climate is `x`. #' @return `data.table` with `pixelID` and `spreadProb`, from the one parameter set in `params`; with a `link` @@ -534,6 +541,11 @@ spreadProbOneELF <- function(covs, params, covMinMax, yr, maxFireSpread, lowerSp stop(moduleName, "> a parameter set has '", actTxt, "', but its ledger row's spreadClimate is not 'activation'", call. = FALSE) activationIntercept <- if (activation) params[[actTxt]][[1L]] + ## a recorded mode is the fit's: "predictor" keeps climate in x, even with a climateRef; the other two need one + if (!is.null(link) && identical(spreadClimate, "predictor")) climateRef <- NULL + if (any(spreadClimate %in% c("ceiling", "activation")) && is.null(climateRef)) + stop(moduleName, "> a ledger row of spreadClimate '", spreadClimate, "' has no '", fireSenseUtils::spreadFitClimateRefTxt, + "'", call. = FALSE) ## the random effects are not covariate coefficients: fireSense_burn applies them (randomEffectOutputs); nor is the ## activation's intercept, which goes to the link (spreadLinkInputs()) randomEffects <- intersect(names(params), c(fireSenseUtils::spreadRandomEffectTxt, actTxt)) @@ -777,14 +789,15 @@ ledgerSpreadLink <- function(sa, i) { #' @param i integer; the row. #' @param col the ledger column. #' @param links the links that write `col`; any other row gives `NULL`. +#' @param floor the run's `lowerSpreadProb`, which an activation row's lower asymptote must equal. #' @return The value, or `NULL` for a row of another link. -ledgerLinkNumber <- function(sa, i, col, links) { +ledgerLinkNumber <- function(sa, i, col, links, floor = fireSenseUtils::spreadProbFloor) { link <- ledgerSpreadLink(sa, i) if (!any(link %in% links)) return(NULL) num <- function(cn) if (cn %in% names(sa)) sa[[cn]][[i]] tryCatch(fireSenseUtils::checkLinkParams(link, num(fireSenseUtils::spreadFitLowerAsymptoteTxt), num(fireSenseUtils::spreadFitRichardsNuTxt), - activation = identical(ledgerSpreadClimate(sa, i), "activation")), + activation = identical(ledgerSpreadClimate(sa, i), "activation"), floor = floor), error = function(e) stop("fireSense_spreadPredict: ledger row ", i, " has spreadLink '", link, "': ", conditionMessage(e), " (columns '", fireSenseUtils::spreadFitLowerAsymptoteTxt, "', '", fireSenseUtils::spreadFitRichardsNuTxt, "')", call. = FALSE)) @@ -798,8 +811,8 @@ ledgerLinkNumber <- function(sa, i, col, links) { #' #' @inheritParams ledgerLinkNumber #' @return A number from 0 to `fireSenseUtils::spreadProbFloor`; `NULL` for a row of another link. -ledgerLowerAsymptote <- function(sa, i) - ledgerLinkNumber(sa, i, fireSenseUtils::spreadFitLowerAsymptoteTxt, fireSenseUtils::spreadAsymptoteLinks) +ledgerLowerAsymptote <- function(sa, i, floor = fireSenseUtils::spreadProbFloor) + ledgerLinkNumber(sa, i, fireSenseUtils::spreadFitLowerAsymptoteTxt, fireSenseUtils::spreadAsymptoteLinks, floor) #' The `nu` of one ledger row #' @@ -807,24 +820,25 @@ ledgerLowerAsymptote <- function(sa, i) #' #' @inheritParams ledgerLinkNumber #' @return A number, 0 or more; `NULL` for a row of another link. -ledgerRichardsNu <- function(sa, i) - ledgerLinkNumber(sa, i, fireSenseUtils::spreadFitRichardsNuTxt, "richards") +ledgerRichardsNu <- function(sa, i, floor = fireSenseUtils::spreadProbFloor) + ledgerLinkNumber(sa, i, fireSenseUtils::spreadFitRichardsNuTxt, "richards", floor) #' The link of one ledger row and its parameters, as the arguments of `spreadProbOneELF()` #' #' @inheritParams ledgerLinkNumber #' @return A list of `link` (only for a link of `fireSenseUtils::spreadLinearLinks`), `powerLawGamma` (not `"richards"`), -#' `powerLawShape`, `lowerAsymptote` and `richardsNu` (the links that have them), and `spreadClimate` for an -#' `"activation"` row; the others are left out, so they take `spreadProbOneELF()`'s defaults. A row of any other link, +#' `powerLawShape`, `lowerAsymptote` and `richardsNu` (the links that have them), and `spreadClimate` when the row +#' records it; the others are left out, so they take `spreadProbOneELF()`'s defaults. A row of any other link, #' or without one, has `powerLawShape` only. -ledgerLinkArgs <- function(sa, i) { +ledgerLinkArgs <- function(sa, i, floor = fireSenseUtils::spreadProbFloor) { link <- ledgerSpreadLink(sa, i) if (!any(link %in% fireSenseUtils::spreadLinearLinks)) link <- NULL - mode <- ledgerSpreadClimate(sa, i) + col <- fireSenseUtils::spreadClimateTxt + recorded <- col %in% names(sa) && !is.na(sa[[col]][[i]]) Filter(Negate(is.null), list(link = link, powerLawGamma = ledgerPowerLawGamma(sa, i), powerLawShape = ledgerPowerLawShape(sa, i), - lowerAsymptote = ledgerLowerAsymptote(sa, i), richardsNu = ledgerRichardsNu(sa, i), - spreadClimate = if (identical(mode, "activation")) mode)) + lowerAsymptote = ledgerLowerAsymptote(sa, i, floor), richardsNu = ledgerRichardsNu(sa, i, floor), + spreadClimate = if (recorded) ledgerSpreadClimate(sa, i))) } #' Where the fit of one ledger row put climate diff --git a/tests/testthat/test-climateActivation.R b/tests/testthat/test-climateActivation.R index 7c9afd9..a4730ac 100644 --- a/tests/testthat/test-climateActivation.R +++ b/tests/testthat/test-climateActivation.R @@ -91,3 +91,37 @@ test_that("an activation row and its parameters must agree, and the link must be sa[[modeColCA]] <- "wet" expect_error(ledgerSpreadClimate(sa, 1L), "unknown") }) + +test_that("a recorded mode is the fit's: an explicit 'predictor' row ignores a climateRef; 'ceiling' needs one", { + ## a power-law row with climateRef and p50 is predicted as ceiling when it records nothing, as predictor when it says so + pred <- inputsCA(mode = "predictor", a = NULL) + noRef <- inputsCA(mode = NULL, a = NULL) + noRef$studyAreaWithSpreadParams[[fireSenseUtils::spreadFitClimateRefTxt]] <- NULL + expect_identical(predVals(toyRun(pred)), predVals(toyRun(noRef))) + expect_false(isTRUE(all.equal(predVals(toyRun(pred)), predVals(toyRun(inputsCA(mode = NULL, a = NULL)))))) + bad <- inputsCA(mode = "ceiling", a = NULL) + bad$studyAreaWithSpreadParams[[fireSenseUtils::spreadFitClimateRefTxt]] <- NULL + expect_error(toyRun(bad), "spreadClimate 'ceiling' has no") +}) + +test_that("the activation's floor check uses the run's lowerSpreadProb", { + sa <- inputsCA()$studyAreaWithSpreadParams + sa[[fireSenseUtils::spreadFitLowerAsymptoteTxt]] <- 0.1 + expect_identical(ledgerLowerAsymptote(sa, 1L, floor = 0.1), 0.1) + expect_error(ledgerLowerAsymptote(sa, 1L), "lowerAsymptote = lowerSpreadProb") + expect_identical(ledgerLinkArgs(sa, 1L, floor = 0.1)$lowerAsymptote, 0.1) +}) + +test_that("ELFs fitted in different spreadClimate modes are not blended", { + ins <- multiInputs() + sa <- ins$studyAreaWithSpreadParams + sa[[linkColPL]] <- c("powerLawSmooth", "powerLawSmooth") + sa[[gammaColPL]] <- c(2, 2) + sa[[fireSenseUtils::spreadFitLowerAsymptoteTxt]] <- c(0.13, 0.13) + sa[[fireSenseUtils::spreadFitPowerLawPc50Txt]] <- c(0.3, 0.3) + sa[[fireSenseUtils::spreadFitClimateRefTxt]] <- list(list(MDC = 0.4), list(MDC = 0.4)) + sa[[modeColCA]] <- c("activation", "ceiling") + sa$params[[1]][[actTxtCA]] <- 0.8 + ins$studyAreaWithSpreadParams <- sa + expect_error(toyRun(ins, params = list(ELFblendWidth = 20000)), "different spreadClimate") +})