From 36e29c5c04c08132a83ec19bb42f2a5273d70de7 Mon Sep 17 00:00:00 2001 From: Eliot McIntire Date: Mon, 16 Nov 2026 17:24:56 -0700 Subject: [PATCH 1/2] Predict a split climate fit (_yr, _dev terms) from the year's climate over the ELF's own burnable pixels Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_014G18Mik3fH4z5M5CyMSUS6 --- NEWS.md | 2 + fireSense_spreadPredict.R | 28 +++++++++++-- tests/testthat/test-climateSplit.R | 64 ++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 4 deletions(-) create mode 100644 tests/testthat/test-climateSplit.R diff --git a/NEWS.md b/NEWS.md index 6b1de7c..8b42d0e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # fireSense_spreadPredict (development version) +* A ledger row fitted with the split climate covariate (`fireSense_dataPrepFit`'s `spreadClimateSplit`: parameters `_yr` and `_dev`) is predicted with the same two terms: `spreadProbOneELF()` makes them from the year's climate with `fireSenseUtils::splitSpreadClimate()`, the median being over the ELF's own burnable pixels (`elfPixels`: every row with one fitted ELF, the ELF's own pixels of `rasterToMatchLargeELF` with several, not the blend zone). A row without split terms predicts as before. Needs fireSenseUtils with `splitSpreadClimate()` (Depends-on PredictiveEcology/fireSenseUtils); the `reqdPkgs` floor is set after that merges. + - 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. - New parameters `paramSets` (path to an `.rds` or `.csv` of spread parameter vectors for the simulated ELF; default `NA`, behaviour unchanged) and `repsPerSet`. The sets replace the simulated ELF's ledger rows (neighbour ELFs keep theirs); replicate r uses set `ceiling(r / repsPerSet)`, calls `set.seed(k)` with k its number within the set (common random numbers across sets), and the surface is recomputed only when the set changes. The link, lower asymptote, `gamma` and covariate centres come from the ledger row; a set lacking a coefficient of the row stops. The set index is output in `fireSense_SpreadParamSet`. New input `.ELFind`, as in `fireSense_dataPrepPredict`. diff --git a/fireSense_spreadPredict.R b/fireSense_spreadPredict.R index 49a6ff8..0870c69 100644 --- a/fireSense_spreadPredict.R +++ b/fireSense_spreadPredict.R @@ -281,6 +281,8 @@ spreadPredictRun <- function(sim) { p <- do.call(spreadProbOneELF, c(list(covs[these], params = parsI, covMinMax = sa$covMinMax_spread[[i]], yr = time(sim), maxFireSpread = P(sim)$maxFireSpread, lowerSpreadProb = P(sim)$lowerSpreadProb, + elfPixels = if (any(fireSenseUtils::isClimateSplitCol(names(parsI)))) + which(ELFcellLabels(sim$rasterToMatchLargeELF[[1]]) %in% ids[i]), covCentre = ledgerCovCentre(sa, i), climateRef = ledgerClimateRef(sa, i)), lks[[i]])) rows <- these[match(p$pixelID, covs$pixelID[these])] acc[rows] <- acc[rows] + w[rows, i] * p$spreadProb @@ -421,6 +423,16 @@ rasterVersion <- function(name, r) { cur$version } +#' The ELF of each cell +#' +#' @param r one layer of `sim$rasterToMatchLargeELF`: categorical (the labels are the second column of its levels) or the id. +#' @return character, the ELF label of each cell, in cell order. +ELFcellLabels <- function(r) { + v <- terra::values(r, mat = FALSE) + lv <- terra::levels(r)[[1]] + if (is.data.frame(lv) && NCOL(lv) >= 2) as.character(lv[[2]][match(v, lv[[1]])]) else as.character(v) +} + #' Each ELF's weight at each pixel #' #' An ELF's raw weight is 1 on its own pixels and falls linearly to 0 at `width` metres from them (distance @@ -443,9 +455,7 @@ ELFblendWeights <- function(elfRas, template, pixelID, ids, width) { stop("fireSense_spreadPredict: sim$rasterToMatchLargeELF is not on the grid of sim$flammableRTM", call. = FALSE) r <- elfRas[[1]] - v <- terra::values(r, mat = FALSE) - lv <- terra::levels(r)[[1]] - lab <- if (is.data.frame(lv) && NCOL(lv) >= 2) as.character(lv[[2]][match(v, lv[[1]])]) else as.character(v) + lab <- ELFcellLabels(r) w <- vapply(ids, function(id) { core <- r; terra::values(core) <- ifelse(lab %in% id, 1, NA) if (all(is.na(terra::values(core, mat = FALSE)))) return(numeric(length(pixelID))) @@ -467,6 +477,9 @@ ELFblendWeights <- function(elfRas, template, pixelID, ids, width) { #' @param params one-row `data.frame`: the ELF's parameter set for this replicate (`paramSetForRep()`). #' @param covMinMax the ELF's `covMinMax_spread` (`ledgerCovMinMax()`): the ranges the fit rescaled its #' covariates with. Every fitted covariate must be a column of it, or it is left unscaled. +#' @param elfPixels integer; the `pixelID`s of the ELF's own burnable pixels, the pixels the year's median of a split +#' climate covariate (`_yr`, `_dev`; `fireSenseUtils::splitSpreadClimate()`) is taken over, as in the fit. +#' `NULL` (one fitted ELF) is every row of `covs`. Not used by a fit without split terms. #' @param yr,maxFireSpread,lowerSpreadProb as for `fireSenseUtils::spreadProbFromIntegerCovs()` and #' `fireSenseUtils::logisticAll()`. #' @param covCentre the ELF's `covCentre_spread` (`ledgerCovCentre()`): the covariate means the fit subtracted @@ -490,7 +503,7 @@ ELFblendWeights <- function(elfRas, template, pixelID, ids, width) { #' `ledgerRichardsNu()`): with `"powerLawSmooth"` and `"richards"`, the lower asymptote, and `nu` of `"richards"`. #' @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. -spreadProbOneELF <- function(covs, params, covMinMax, yr, maxFireSpread, lowerSpreadProb, +spreadProbOneELF <- function(covs, params, covMinMax, yr, maxFireSpread, lowerSpreadProb, elfPixels = NULL, covCentre = NULL, climateRef = NULL, powerLawGamma = NULL, powerLawShape = fireSenseUtils::spreadPowerLawShape, link = if (!is.null(powerLawGamma)) "powerLaw", @@ -522,6 +535,13 @@ spreadProbOneELF <- function(covs, params, covMinMax, yr, maxFireSpread, lowerSp ## 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)) + ## a fit made with fireSense_dataPrepFit's spreadClimateSplit has the climate as `_yr` and `_dev`: the + ## year's median of the variable over this ELF's burnable pixels, and the departure from it, made as the fit made them + splitVars <- unique(fireSenseUtils::climateSplitBase(fitted[fireSenseUtils::isClimateSplitCol(fitted)])) + if (length(splitVars) && all(splitVars %in% names(covs))) { + ref <- covs[if (is.null(elfPixels)) TRUE else covs$pixelID %in% elfPixels, splitVars, with = FALSE] + fireSenseUtils::splitSpreadClimate(covs, splitVars, ref) + } noCov <- setdiff(fitted, names(covs)) if (length(noCov)) { stop( diff --git a/tests/testthat/test-climateSplit.R b/tests/testthat/test-climateSplit.R new file mode 100644 index 0000000..37e47a7 --- /dev/null +++ b/tests/testthat/test-climateSplit.R @@ -0,0 +1,64 @@ +## A fit made with fireSense_dataPrepFit's spreadClimateSplit has the climate as two terms, `MDC_yr` (the year's +## median over the ELF's burnable pixels) and `MDC_dev` (MDC minus it). Prediction builds them from the year's +## climate with fireSenseUtils::splitSpreadClimate(), as the fit did, over the ELF's own pixels. +## +## Rescaling: MDC_yr with c(0, 200), MDC_dev with c(-100, 100) (fireSenseUtils::climateSplitRanges()). + +splitParams <- function(yr = 1.5, dev = 0.8, fuel = 1, name = "fuelA") { + d <- data.frame(maxAsymptote = 0.25, hillSlope1 = 2, inflectionPoint1 = 1, MDC_yr = yr, MDC_dev = dev, x = fuel) + names(d)[6] <- name + d +} +cmmSplit <- function(fuel = "fuelA") { + d <- data.table::data.table(MDC_yr = c(0, 200), MDC_dev = c(-100, 100), f = c(0, 8)) + data.table::setnames(d, "f", fuel) + d +} + +test_that("a split fit predicts from the year's median over the burnable pixels and each pixel's departure from it", { + covs <- toyCovariates() + ins <- toyInputs(covs = covs, params = splitParams(), formula = "~ MDC_yr + MDC_dev + fuelA - 1") + ins$covMinMax_spread <- cmmSplit() + v <- predVals(toyRun(ins)) + o <- order(covs$pixelID) + mdc <- covs$MDC[o]; fuel <- covs$fuelA[o] + med <- stats::median(covs$MDC) # 100: over the 7 burnable pixels of the covariate table + x <- 1.5 * (med / 200) + 0.8 * ((mdc - med + 100) / 200) + 1 * (fuel / 8) + expect_equal(v[cells <- c(1, 2, 3, 4, 6, 7, 8)], handLogistic3(x), tolerance = 1e-7) +}) + +test_that("the between term is the same for every pixel: only the within term moves with the pixel", { + covs <- toyCovariates() + both <- function(yr, dev) { + ins <- toyInputs(covs = data.table::copy(covs), params = splitParams(yr, dev, fuel = 0), formula = "~ MDC_yr + MDC_dev + fuelA - 1") + ins$covMinMax_spread <- cmmSplit() + predVals(toyRun(ins)) + } + yrOnly <- both(1.5, 0) + expect_equal(yrOnly[c(1, 2, 3, 4, 6, 7, 8)], rep(handLogistic3(1.5 * 0.5), 7), tolerance = 1e-7) # median 100 / 200 everywhere + devOnly <- both(0, 0.8) + expect_gt(length(unique(round(devOnly[c(1, 2, 3, 4, 6, 7, 8)], 8))), 1L) +}) + +test_that("with several ELFs each takes the median over its own pixels", { + ins <- multiInputs() + covs <- data.table::data.table(pixelID = 1:10, MDC = c(10, 20, 30, 40, 50, 100, 110, 120, 130, 140), fuelA = 2, fuelB = 3) + ins$fireSense_SpreadCovariates <- covs + d <- function(fuel) { x <- data.frame(maxAsymptote = 0.25, hillSlope1 = 2, inflectionPoint1 = 1, MDC_yr = 1.5, MDC_dev = 0.8, f = 1) + names(x)[6] <- fuel; x } + ins$studyAreaWithSpreadParams$params <- list(d("fuelA"), d("fuelB")) + ins$studyAreaWithSpreadParams$covMinMax_spread <- list(cmmSplit("fuelA"), cmmSplit("fuelB")) + ins$fireSense_spreadFormula <- "~ MDC_yr + MDC_dev + fuelA - 1" + v <- predVals(toyRun(ins, params = list(ELFblendWidth = 20000))) + ## pixel 1 is only A's (25 km from B): A's median of pixels 1:5 is 30 + xA <- 1.5 * (30 / 200) + 0.8 * ((10 - 30 + 100) / 200) + 1 * (2 / 8) + expect_equal(v[1], handLogistic3(xA), tolerance = 1e-7) + ## pixel 10 is only B's: B's median of pixels 6:10 is 120 + xB <- 1.5 * (120 / 200) + 0.8 * ((140 - 120 + 100) / 200) + 1 * (3 / 8) + expect_equal(v[10], handLogistic3(xB, upper = 0.25), tolerance = 1e-7) +}) + +test_that("a fit without split terms predicts as before: no median is taken", { + expect_identical(predVals(toyRun()), predVals(toyRun(toyInputs()))) + expect_false(any(fireSenseUtils::isClimateSplitCol(names(toyParams())))) +}) From 87761f874b30cd39694f44c4d1c16ab089780f88 Mon Sep 17 00:00:00 2001 From: Eliot McIntire Date: Mon, 16 Nov 2026 17:36:12 -0700 Subject: [PATCH 2/2] Reused ledger fits keep their split; the median's reference pixels come from fireSenseUtils::climateSplitRefPixels(); cell labels from elfCellLabels() Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_014G18Mik3fH4z5M5CyMSUS6 --- NEWS.md | 2 +- fireSense_spreadPredict.R | 25 ++++++++----------------- tests/testthat/test-climateSplit.R | 14 ++++++++++++++ 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/NEWS.md b/NEWS.md index 8b42d0e..d4e87bc 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,6 @@ # fireSense_spreadPredict (development version) -* A ledger row fitted with the split climate covariate (`fireSense_dataPrepFit`'s `spreadClimateSplit`: parameters `_yr` and `_dev`) is predicted with the same two terms: `spreadProbOneELF()` makes them from the year's climate with `fireSenseUtils::splitSpreadClimate()`, the median being over the ELF's own burnable pixels (`elfPixels`: every row with one fitted ELF, the ELF's own pixels of `rasterToMatchLargeELF` with several, not the blend zone). A row without split terms predicts as before. Needs fireSenseUtils with `splitSpreadClimate()` (Depends-on PredictiveEcology/fireSenseUtils); the `reqdPkgs` floor is set after that merges. +* A ledger row fitted with the split climate covariate (`fireSense_dataPrepFit`'s `spreadClimateSplit`: parameters `_yr` and `_dev`) is predicted with the same two terms: `spreadProbOneELF()` makes them from the year's climate with `fireSenseUtils::splitSpreadClimate()`, the median being over the ELF's own burnable pixels (`elfPixels`: every row with one fitted ELF, the ELF's own pixels of `rasterToMatchLargeELF` with several, not the blend zone), and only its flammable pixels (`fireSenseUtils::climateSplitRefPixels()`, the choice the fit makes). `ELFblendWeights()` reads the cells' ELF labels with `fireSenseUtils::elfCellLabels()`. A row without split terms predicts as before. Needs fireSenseUtils with `splitSpreadClimate()` (Depends-on PredictiveEcology/fireSenseUtils); the `reqdPkgs` floor is set after that merges. - 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 0870c69..69aca3f 100644 --- a/fireSense_spreadPredict.R +++ b/fireSense_spreadPredict.R @@ -244,7 +244,7 @@ spreadPredictRun <- function(sim) { if (NROW(sa) == 1L) { params1 <- paramSetForRep(sa$params[[1]], rep) lk1 <- ledgerLinkArgs(sa, 1L) - pred <- do.call(spreadProbOneELF, c(list(covs, params = params1, covMinMax = ledgerCovMinMax(sa, 1L, sim$covMinMax_spread), + pred <- do.call(spreadProbOneELF, c(list(covs, params = params1, flammableRTM = sim$flammableRTM, covMinMax = ledgerCovMinMax(sa, 1L, sim$covMinMax_spread), yr = time(sim), maxFireSpread = P(sim)$maxFireSpread, lowerSpreadProb = P(sim)$lowerSpreadProb, covCentre = ledgerCovCentre(sa, 1L), climateRef = ledgerClimateRef(sa, 1L)), lk1)) @@ -279,10 +279,10 @@ spreadPredictRun <- function(sim) { if (!length(these)) next parsI <- paramSetForRep(sa$params[[i]], rep) p <- do.call(spreadProbOneELF, c(list(covs[these], params = parsI, covMinMax = sa$covMinMax_spread[[i]], - yr = time(sim), maxFireSpread = P(sim)$maxFireSpread, + yr = time(sim), maxFireSpread = P(sim)$maxFireSpread, flammableRTM = sim$flammableRTM, lowerSpreadProb = P(sim)$lowerSpreadProb, elfPixels = if (any(fireSenseUtils::isClimateSplitCol(names(parsI)))) - which(ELFcellLabels(sim$rasterToMatchLargeELF[[1]]) %in% ids[i]), + which(fireSenseUtils::elfCellLabels(sim$rasterToMatchLargeELF[[1]]) %in% ids[i]), covCentre = ledgerCovCentre(sa, i), climateRef = ledgerClimateRef(sa, i)), lks[[i]])) rows <- these[match(p$pixelID, covs$pixelID[these])] acc[rows] <- acc[rows] + w[rows, i] * p$spreadProb @@ -423,16 +423,6 @@ rasterVersion <- function(name, r) { cur$version } -#' The ELF of each cell -#' -#' @param r one layer of `sim$rasterToMatchLargeELF`: categorical (the labels are the second column of its levels) or the id. -#' @return character, the ELF label of each cell, in cell order. -ELFcellLabels <- function(r) { - v <- terra::values(r, mat = FALSE) - lv <- terra::levels(r)[[1]] - if (is.data.frame(lv) && NCOL(lv) >= 2) as.character(lv[[2]][match(v, lv[[1]])]) else as.character(v) -} - #' Each ELF's weight at each pixel #' #' An ELF's raw weight is 1 on its own pixels and falls linearly to 0 at `width` metres from them (distance @@ -455,7 +445,7 @@ ELFblendWeights <- function(elfRas, template, pixelID, ids, width) { stop("fireSense_spreadPredict: sim$rasterToMatchLargeELF is not on the grid of sim$flammableRTM", call. = FALSE) r <- elfRas[[1]] - lab <- ELFcellLabels(r) + lab <- fireSenseUtils::elfCellLabels(r) w <- vapply(ids, function(id) { core <- r; terra::values(core) <- ifelse(lab %in% id, 1, NA) if (all(is.na(terra::values(core, mat = FALSE)))) return(numeric(length(pixelID))) @@ -479,7 +469,8 @@ ELFblendWeights <- function(elfRas, template, pixelID, ids, width) { #' covariates with. Every fitted covariate must be a column of it, or it is left unscaled. #' @param elfPixels integer; the `pixelID`s of the ELF's own burnable pixels, the pixels the year's median of a split #' climate covariate (`_yr`, `_dev`; `fireSenseUtils::splitSpreadClimate()`) is taken over, as in the fit. -#' `NULL` (one fitted ELF) is every row of `covs`. Not used by a fit without split terms. +#' `NULL` (one fitted ELF) is every row of `covs` that is flammable. Not used by a fit without split terms. +#' @param flammableRTM `sim$flammableRTM`; with `elfPixels` it limits the median to the ELF's flammable pixels (`fireSenseUtils::climateSplitRefPixels()`, the fit's choice). #' @param yr,maxFireSpread,lowerSpreadProb as for `fireSenseUtils::spreadProbFromIntegerCovs()` and #' `fireSenseUtils::logisticAll()`. #' @param covCentre the ELF's `covCentre_spread` (`ledgerCovCentre()`): the covariate means the fit subtracted @@ -503,7 +494,7 @@ ELFblendWeights <- function(elfRas, template, pixelID, ids, width) { #' `ledgerRichardsNu()`): with `"powerLawSmooth"` and `"richards"`, the lower asymptote, and `nu` of `"richards"`. #' @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. -spreadProbOneELF <- function(covs, params, covMinMax, yr, maxFireSpread, lowerSpreadProb, elfPixels = NULL, +spreadProbOneELF <- function(covs, params, covMinMax, yr, maxFireSpread, lowerSpreadProb, elfPixels = NULL, flammableRTM = NULL, covCentre = NULL, climateRef = NULL, powerLawGamma = NULL, powerLawShape = fireSenseUtils::spreadPowerLawShape, link = if (!is.null(powerLawGamma)) "powerLaw", @@ -539,7 +530,7 @@ spreadProbOneELF <- function(covs, params, covMinMax, yr, maxFireSpread, lowerSp ## year's median of the variable over this ELF's burnable pixels, and the departure from it, made as the fit made them splitVars <- unique(fireSenseUtils::climateSplitBase(fitted[fireSenseUtils::isClimateSplitCol(fitted)])) if (length(splitVars) && all(splitVars %in% names(covs))) { - ref <- covs[if (is.null(elfPixels)) TRUE else covs$pixelID %in% elfPixels, splitVars, with = FALSE] + ref <- covs[covs$pixelID %in% fireSenseUtils::climateSplitRefPixels(covs$pixelID, flammableRTM, elfPixels), splitVars, with = FALSE] fireSenseUtils::splitSpreadClimate(covs, splitVars, ref) } noCov <- setdiff(fitted, names(covs)) diff --git a/tests/testthat/test-climateSplit.R b/tests/testthat/test-climateSplit.R index 37e47a7..53f449f 100644 --- a/tests/testthat/test-climateSplit.R +++ b/tests/testthat/test-climateSplit.R @@ -27,6 +27,20 @@ test_that("a split fit predicts from the year's median over the burnable pixels expect_equal(v[cells <- c(1, 2, 3, 4, 6, 7, 8)], handLogistic3(x), tolerance = 1e-7) }) +test_that("a non-flammable row in the covariate table is left out of the median, as the fit's burnable pixels are", { + covs <- toyCovariates() + extra <- data.table::data.table(pixelID = 5L, MDC = 5000, youngAge = 0, fuelA = 0) # cell 5 of the toy flammableRTM is 0 + withExtra <- data.table::rbindlist(list(covs, extra)) + run <- function(cv) { + ins <- toyInputs(covs = cv, params = splitParams(), formula = "~ MDC_yr + MDC_dev + fuelA - 1") + ins$covMinMax_spread <- cmmSplit() + predVals(toyRun(ins)) + } + a <- run(covs); b <- run(withExtra) + cellsB <- c(1, 2, 3, 4, 6, 7, 8) + expect_equal(b[cellsB], a[cellsB], tolerance = 1e-12) # median 100 with and without the extra row +}) + test_that("the between term is the same for every pixel: only the within term moves with the pixel", { covs <- toyCovariates() both <- function(yr, dev) {