From 25c7ecfdef8b01c356c9b6badae10cfe25827151 Mon Sep 17 00:00:00 2001 From: Eliot McIntire Date: Thu, 19 Nov 2026 22:58:41 -0700 Subject: [PATCH 1/3] coreFraction: one shared implementation in fireSenseUtils Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01Lx3DXhXsFis2pbqBTh2PPh --- NEWS.md | 1 + fireSense_dataPrepPredict.R | 11 +++++++++-- tests/testthat/test-covariates.R | 20 +++++++++++++++++++- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/NEWS.md b/NEWS.md index 3ee6ed9..21b313c 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,6 @@ # fireSense_dataPrepPredict (development version) +* `fireSense_igAndEscapePred_Covariates` has a `coreFraction` column, the share of each coarse pixel's flammable cells that are core (`fireSenseUtils::addCoreFraction()` on `flammableRTM` and `rasterToMatchLargeELF`, with `igAggFactor`, which is forced to the fit module's value), as `fireSense_dataPrepFit` gives it to the fit. `fireSense_ignitionPredict`'s year climate term averages the core pixels only, as the fit does; without the column it would average the buffer too. A multi-ELF categorical raster or none gives 1. Needs fireSenseUtils (>= 0.2.3.9183). * `fitFileSuffix` is removed: in a run without `fireSense_dataPrepFit`, name the ledger to predict from in full (`spreadFitFilename`); `"latest"` takes each ELF's newest usable fit from any ledger file. The stop for a ledger with no fit for the study area names `spreadFitFilename`. * `lightningMaps` and `ignitionFitRTM` are now built here whenever they are NULL when the ignition covariates are made, not only when no other module declares them. `fireSense_dataPrepFit` declares both but makes neither when it skips its ignition prep (`skipUnusedFitPrep`, a ledger ignition fit reused), which left `fireSense_ignitionPredict` without `ignitionFitRTM`. Runs where `fireSense_dataPrepFit` makes them, or is absent, are unchanged. diff --git a/fireSense_dataPrepPredict.R b/fireSense_dataPrepPredict.R index 0233b7d..49d334c 100644 --- a/fireSense_dataPrepPredict.R +++ b/fireSense_dataPrepPredict.R @@ -21,7 +21,7 @@ defineModule(sim, list( "PredictiveEcology/climateData@development", # latestHistoricalYear() "data.table", "googledrive", - "PredictiveEcology/fireSenseUtils@development (>= 0.2.3.9176)", # 9142: readSpreadFitLedger() and the other predict-only helpers (#206); fuelCovsToCoarse() hands back the static it used (#191) + "PredictiveEcology/fireSenseUtils@development (>= 0.2.3.9183)", # 9183: addCoreFraction(), coreFractionByYear(); 9142: readSpreadFitLedger() and the other predict-only helpers (#206); fuelCovsToCoarse() hands back the static it used (#191) "httr2", "PredictiveEcology/LandR@development", "PredictiveEcology/reproducible@development", @@ -276,7 +276,9 @@ defineModule(sim, list( createsOutput("fireSense_igAndEscapePred_Covariates", "data.table", desc = paste( "Ignition and escape covariates at the aggregated (`igAggFactor`) resolution;", - "`pixelID` is the cell index of the aggregated raster.")), + "`pixelID` is the cell index of the aggregated raster. `coreFraction` is the share of the pixel's flammable", + "cells in the core of `rasterToMatchLargeELF` (`fireSenseUtils::addCoreFraction()`, as `fireSense_dataPrepFit`", + "gives it to the fit); it is an exposure, not a covariate.")), createsOutput("fireSense_SpreadCovariates", "data.table", desc = "Spread covariates; `pixelID` is the cell index of `flammableRTM`."), createsOutput("nonForest_timeSinceDisturbance", "SpatRaster", @@ -702,6 +704,11 @@ prepare_IgnitionAndEscapePredict <- function(sim) { useCache = FALSE) set(sim$fireSense_igAndEscapePred_Covariates, NULL, "ignitions", NULL) + ## the share of each coarse pixel's flammable cells that are core, as fireSense_dataPrepFit gives it to the fit + ## (same function, same `igAggFactor`: forced to the fit module's value above): the year climate term of + ## fireSense_ignitionPredict averages the core pixels only, as the fit does + fireSenseUtils::addCoreFraction(sim$fireSense_igAndEscapePred_Covariates, list(yrLab) |> setNames(time(sim)), + list(sim$flammableRTM), sim$rasterToMatchLargeELF, P(sim)$igAggFactor) return(invisible(sim)) } diff --git a/tests/testthat/test-covariates.R b/tests/testthat/test-covariates.R index 9e9b724..58930da 100644 --- a/tests/testthat/test-covariates.R +++ b/tests/testthat/test-covariates.R @@ -77,7 +77,7 @@ test_that("prepIgAndEscPredictData aggregates to the igAggFactor grid", { ## the year column carries time(sim), not the layer index expect_identical(unique(df$year), 2001) expect_setequal(names(df), c("pixelID", "MDC", "youngAge", "nfLCC_wetland", "nfLCC_grass", - "class1", "class2", "year", "lightningDays")) + "class1", "class2", "year", "lightningDays", "coreFraction")) ## `ignitions` belongs to the Fit table and is dropped here expect_false("ignitions" %in% names(df)) @@ -95,6 +95,24 @@ test_that("prepIgAndEscPredictData aggregates to the igAggFactor grid", { expect_equal(df$nfLCC_wetland, c(0, 0, 0.5, 0)) }) +test_that("coreFraction is the share of each coarse pixel's flammable cells in the core, as the fit gets it", { + ## no rasterToMatchLargeELF: everything is core + expect_equal(covDF(toyPrepRun()$fireSense_igAndEscapePred_Covariates)$coreFraction, rep(1, 4)) + ## core (2) in fine columns 1-2, buffer (1) in 3-4: coarse pixels 1 and 3 are core, 2 and 4 buffer + ## (4 also holds the non-flammable cell 16, which does not count) + o <- toyObjects() + o$rasterToMatchLargeELF <- toyRast(rep(c(2, 2, 1, 1), 4)) + df <- covDF(toyPrepRun(o)$fireSense_igAndEscapePred_Covariates) + expect_equal(df$coreFraction, c(1, 0, 1, 0)) + ## the yearly mean the year climate term uses is over the core pixels (1 and 3: MDC 103.5, 111.5), not all four, + ## and is what the fit's own table gives: the same table with coreFraction added as fireSense_dataPrepFit does + fitStyle <- data.table::as.data.table(df[, setdiff(names(df), "coreFraction")]) + fitStyle <- fireSenseUtils::addCoreFraction(fitStyle, list(`2001` = "year2001"), list(o$flammableRTM), + o$rasterToMatchLargeELF, fact = 2) + expect_equal(fireSenseUtils::yearClimateMeans(df, "MDC")$mean, 107.5) + expect_equal(fireSenseUtils::yearClimateMeans(fitStyle, "MDC"), fireSenseUtils::yearClimateMeans(df, "MDC")) +}) + test_that("igAggFactor 1 leaves the ignition covariates at flammableRTM resolution", { sim <- toyPrepRun(params = list(igAggFactor = 1)) df <- covDF(sim$fireSense_igAndEscapePred_Covariates) From 998afe5b1e84ead1666d0a7b0af1571e3a348911 Mon Sep 17 00:00:00 2001 From: Eliot McIntire Date: Thu, 19 Nov 2026 23:26:26 -0700 Subject: [PATCH 2/3] Review fixes: raw-table year mean in predict, onMissing and cached coreFraction, level helper, warnings Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01Lx3DXhXsFis2pbqBTh2PPh --- NEWS.md | 2 +- fireSense_dataPrepPredict.R | 14 ++++++-- tests/testthat/test-covariatesOncePerYear.R | 39 +++++++++++++++++++++ 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/NEWS.md b/NEWS.md index 21b313c..0c2bf90 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,6 @@ # fireSense_dataPrepPredict (development version) -* `fireSense_igAndEscapePred_Covariates` has a `coreFraction` column, the share of each coarse pixel's flammable cells that are core (`fireSenseUtils::addCoreFraction()` on `flammableRTM` and `rasterToMatchLargeELF`, with `igAggFactor`, which is forced to the fit module's value), as `fireSense_dataPrepFit` gives it to the fit. `fireSense_ignitionPredict`'s year climate term averages the core pixels only, as the fit does; without the column it would average the buffer too. A multi-ELF categorical raster or none gives 1. Needs fireSenseUtils (>= 0.2.3.9183). +* `fireSense_igAndEscapePred_Covariates` has a `coreFraction` column, the share of each coarse pixel's flammable cells that are core (`fireSenseUtils::addCoreFraction()` on `flammableRTM` and `rasterToMatchLargeELF`, with `igAggFactor`, which is forced to the fit module's value), as `fireSense_dataPrepFit` gives it to the fit. `fireSense_ignitionPredict`'s year climate term averages the core pixels only, as the fit does; without the column it would average the buffer too. It does not depend on the year, so it is kept in the module and remade only when `flammableRTM`, `rasterToMatchLargeELF` or `igAggFactor` change. A pixel with no flammable cell gets 0 with one warning. A multi-ELF categorical raster or none gives 1. Needs fireSenseUtils (>= 0.2.3.9183). * `fitFileSuffix` is removed: in a run without `fireSense_dataPrepFit`, name the ledger to predict from in full (`spreadFitFilename`); `"latest"` takes each ELF's newest usable fit from any ledger file. The stop for a ledger with no fit for the study area names `spreadFitFilename`. * `lightningMaps` and `ignitionFitRTM` are now built here whenever they are NULL when the ignition covariates are made, not only when no other module declares them. `fireSense_dataPrepFit` declares both but makes neither when it skips its ignition prep (`skipUnusedFitPrep`, a ledger ignition fit reused), which left `fireSense_ignitionPredict` without `ignitionFitRTM`. Runs where `fireSense_dataPrepFit` makes them, or is absent, are unchanged. diff --git a/fireSense_dataPrepPredict.R b/fireSense_dataPrepPredict.R index 49d334c..9560d27 100644 --- a/fireSense_dataPrepPredict.R +++ b/fireSense_dataPrepPredict.R @@ -706,9 +706,17 @@ prepare_IgnitionAndEscapePredict <- function(sim) { set(sim$fireSense_igAndEscapePred_Covariates, NULL, "ignitions", NULL) ## the share of each coarse pixel's flammable cells that are core, as fireSense_dataPrepFit gives it to the fit ## (same function, same `igAggFactor`: forced to the fit module's value above): the year climate term of - ## fireSense_ignitionPredict averages the core pixels only, as the fit does - fireSenseUtils::addCoreFraction(sim$fireSense_igAndEscapePred_Covariates, list(yrLab) |> setNames(time(sim)), - list(sim$flammableRTM), sim$rasterToMatchLargeELF, P(sim)$igAggFactor) + ## fireSense_ignitionPredict averages the core pixels only, as the fit does. It depends on the flammable cells, the + ## core raster and `igAggFactor`, not on the year, so it is kept in `mod` and remade when any of them changes (as + ## `mod$fuelCovsStatic`); the year label is set each year. A pixel with no flammable cell gets 0, with a warning. + coreKey <- fireSenseUtils::coreFractionKey(list(sim$flammableRTM), sim$rasterToMatchLargeELF, P(sim)$igAggFactor) + coreYears <- list(yrLab) |> setNames(time(sim)) + if (!identical(mod$coreFraction$key, coreKey)) + mod$coreFraction <- list(key = coreKey, fractions = fireSenseUtils::coreFractionByYear( + coreYears, list(sim$flammableRTM), sim$rasterToMatchLargeELF, P(sim)$igAggFactor)) + coreFractions <- copy(mod$coreFraction$fractions) + set(coreFractions, NULL, fireSenseUtils::yearTxt, as.numeric(gsub(fireSenseUtils::yearTxt, "", yrLab))) + fireSenseUtils::addCoreFraction(sim$fireSense_igAndEscapePred_Covariates, onMissing = "zero", fractions = coreFractions) return(invisible(sim)) } diff --git a/tests/testthat/test-covariatesOncePerYear.R b/tests/testthat/test-covariatesOncePerYear.R index 476b4b6..9abc30d 100644 --- a/tests/testthat/test-covariatesOncePerYear.R +++ b/tests/testthat/test-covariatesOncePerYear.R @@ -101,3 +101,42 @@ test_that("a change of the simulated ELF rebuilds", { r <- stepsAfterChange(toyObjects(), function(sim) { sim$.ELFind <- "7"; sim }, "fireSense_spreadPredict") expect_identical(r$n, 1L) }) + +## coreFraction does not depend on the year: made once, remade when the flammable cells or the core raster change +countCoreFractionCalls <- function(code) { + n <- 0L + orig <- fireSenseUtils::coreFractionByYear + testthat::local_mocked_bindings( + coreFractionByYear = function(...) { n <<- n + 1L; orig(...) }, + .package = "fireSenseUtils") + force(code) + n +} + +test_that("coreFraction is made once over several years, and has each year's label", { + o <- toyObjects(); o$rasterToMatchLargeELF <- toyRast(rep(c(2, 2, 1, 1), 4)) + n <- countCoreFractionCalls(sim <- toyPrepRun(o, times = list(start = 2001, end = 2003), + params = list(whichModulesToPrepare = "fireSense_ignitionPredict"))) + expect_identical(n, 1L) + df <- covDF(sim$fireSense_igAndEscapePred_Covariates) + expect_identical(unique(df$year), 2003) + expect_equal(df$coreFraction, c(1, 0, 1, 0)) +}) + +test_that("a change of flammableRTM or of rasterToMatchLargeELF remakes coreFraction", { + o <- toyObjects(); o$rasterToMatchLargeELF <- toyRast(rep(c(2, 2, 1, 1), 4)) + run <- function(change) { + sim <- SpaDES.core::spades(toyPrepSim(o, times = list(start = 2001, end = 2001), + params = list(whichModulesToPrepare = "fireSense_ignitionPredict")), debug = FALSE) + sim <- change(sim) + SpaDES.core::end(sim) <- 2002 + n <- countCoreFractionCalls(sim <- SpaDES.core::spades(sim, debug = FALSE)) + list(n = n, core = covDF(sim$fireSense_igAndEscapePred_Covariates)$coreFraction) + } + expect_identical(run(identity)$n, 0L) + r <- run(function(sim) { sim$rasterToMatchLargeELF <- toyRast(rep(c(2, 2, 2, 1), 4)); sim }) + expect_identical(r$n, 1L) + expect_equal(r$core, c(1, 0.5, 1, 2 / 3)) # pixel 4: cells 11, 12, 15 flammable, 11 and 15 core + r <- run(function(sim) { sim$flammableRTM <- toyRast(c(0, rep(1, 14), 0)); sim }) # cell 1 no longer flammable + expect_identical(r$n, 1L) +}) From 11ab5ecbe2415c4e2d8c5c1cee86972bf0539d08 Mon Sep 17 00:00:00 2001 From: Eliot McIntire Date: Fri, 20 Nov 2026 06:29:28 -0700 Subject: [PATCH 3/3] fireSenseUtils floor 0.2.3.9184 (#254 merged as 9184, after #255) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Lx3DXhXsFis2pbqBTh2PPh --- fireSense_dataPrepPredict.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fireSense_dataPrepPredict.R b/fireSense_dataPrepPredict.R index 9560d27..c11511b 100644 --- a/fireSense_dataPrepPredict.R +++ b/fireSense_dataPrepPredict.R @@ -21,7 +21,7 @@ defineModule(sim, list( "PredictiveEcology/climateData@development", # latestHistoricalYear() "data.table", "googledrive", - "PredictiveEcology/fireSenseUtils@development (>= 0.2.3.9183)", # 9183: addCoreFraction(), coreFractionByYear(); 9142: readSpreadFitLedger() and the other predict-only helpers (#206); fuelCovsToCoarse() hands back the static it used (#191) + "PredictiveEcology/fireSenseUtils@development (>= 0.2.3.9184)", # 9184: addCoreFraction(), coreFractionByYear(); 9142: readSpreadFitLedger() and the other predict-only helpers (#206); fuelCovsToCoarse() hands back the static it used (#191) "httr2", "PredictiveEcology/LandR@development", "PredictiveEcology/reproducible@development",