From c9702983bbeed80773db9091559883ed04e48874 Mon Sep 17 00:00:00 2001 From: Eliot McIntire Date: Sun, 15 Nov 2026 00:15:59 -0700 Subject: [PATCH 1/3] skipUnusedFitPrep: predict-only jobs do not schedule prepSpreadFitData and prepEscapeFitData Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_014G18Mik3fH4z5M5CyMSUS6 --- NEWS.md | 2 ++ fireSense_dataPrepFit.R | 34 ++++++++++++++++++--- tests/testthat/test-metadata.R | 2 +- tests/testthat/test-skipUnusedFitPrep.R | 40 +++++++++++++++++++++++++ 4 files changed, 73 insertions(+), 5 deletions(-) create mode 100644 tests/testthat/test-skipUnusedFitPrep.R diff --git a/NEWS.md b/NEWS.md index db00546..44f30f3 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # fireSense_dataPrepFit 1.3.0.9011 (development version) +* New parameter `skipUnusedFitPrep` (default FALSE: nothing changes). TRUE leaves out `prepSpreadFitData` and `prepEscapeFitData`, the slowest steps, in a job that cannot fit, validate, profile or hold out a fold (`doFit` FALSE, no `heldOutFold`, `fireSense_spreadFit`'s `mode` without "validate" or "profile"): about 2.5 minutes of every hindcast year-job. `prepIgnitionFitData` still runs (`lightningMaps`, `ignitionFitRTM`). `sim$ignitionFirePoints` is then the study area's NFDB points, not the core points made with the spread fires (`fireSense_summary`'s hindcast reads it to count the observed ignitions). Needs a `fireSense_ignitionFit` that uses its stored fit without escape covariates (PredictiveEcology/fireSense_ignitionFit). The same parameter of `fireSense_spreadFit` (`.globals = list(skipUnusedFitPrep = TRUE)`). + * `fireSense_ignitionCovariates` has a new column `coreFraction`: the share of each coarse pixel's flammable cells that are in the core of `rasterToMatchLargeELF` (1 in the core, 0 in the buffer, fractional at the edge; computed per data year from `flammableRTMs`, with `fireSenseUtils::inELFCore()` and the aggregation of the coarse covariates). The ignitions are counted in the core only, so fireSense_ignitionFit weights each pixel by it; without it the fit spread the core's ignitions over core and buffer and hindcasts simulated about 0.8 of the observed ignitions. It is an exposure, not a covariate (`fireSenseUtils::coreFractionTxt`, fireSenseUtils >= 0.2.3.9161), and the escape formula leaves it out. The `prepIgnitionFitData` event re-runs once (its code changed). Used with PredictiveEcology/fireSense_ignitionFit (`Depends-on` there). * New parameter `spreadClimateCandidates` (default: the default ignition variables plus `Tmax_sm` and `PPT_sm`): `climateVariablesForFire$spread = "auto"` now chooses among it, not among `climateVariablesForFire$ignition`, which IgnitionFit still uses whole and unchanged. The candidates' layers are prepared with `climateVariables`, but only the chosen variable enters the spread tables. Precipitation is scored as decreasing with dryness. The default `auto` choice can now be `Tmax_sm` or `PPT_sm`, so spread fits and results can change; they need `Tmaxsm` and `PPTsm` in `fireSenseUtils::climateCovRanges` (PredictiveEcology/fireSenseUtils, branch `climate-ranges-summer`). It is a module parameter, so changing it re-runs the cached events. diff --git a/fireSense_dataPrepFit.R b/fireSense_dataPrepFit.R index b00d815..9499ff6 100644 --- a/fireSense_dataPrepFit.R +++ b/fireSense_dataPrepFit.R @@ -138,6 +138,15 @@ defineModule(sim, list( "takes that fit's climate variable(s); when it is fitted, `climateVariablesForFire$spread`", "as given (`\"auto\"`: chosen afresh). Set it for every fireSense module at once with", "`.globals = list(doFit = ...)`.")), + defineParameter("skipUnusedFitPrep", "logical", default = FALSE, + desc = paste("FALSE (default): unchanged. TRUE: a job that cannot fit, validate, profile or hold out a fold", + "(`doFit` FALSE, no `heldOutFold`, `fireSense_spreadFit`'s `mode` without \"validate\" or \"profile\")", + "does not schedule `prepSpreadFitData` and `prepEscapeFitData`, the slowest steps, whose outputs", + "only those fits read; `prepIgnitionFitData` still runs (`lightningMaps`, `ignitionFitRTM`).", + "`sim$ignitionFirePoints` is then the study area's NFDB points, not the core points made with the", + "spread fires. The same parameter of `fireSense_spreadFit`: set it for both with", + "`.globals = list(skipUnusedFitPrep = TRUE)`. Needs `fireSense_ignitionFit` that does not compare", + "a stored escape fit with escape covariates it was not given.")), defineParameter("fitFileSuffix", "character", "", NA, NA, paste("Text put after the model tag in the ledger file names, e.g. `\"_boundfix_rep81\"`, so a test run", "writes and reads (`\"latest\"`) only its own files, never the production ones. `\"\"` (the", @@ -473,7 +482,7 @@ doEvent.fireSense_dataPrepFit = function(sim, eventTime, eventType) { eventType, init = { ## schedule future event(s) - for (ev in prepEventsToSchedule(P(sim)$whichModulesToPrepare)) + for (ev in prepEventsToSchedule(P(sim)$whichModulesToPrepare, fitPrepNeeded(sim))) sim <- scheduleEvent(sim, start(sim), "fireSense_dataPrepFit", ev, eventPriority = 1) sim <- Init(sim) @@ -1506,16 +1515,33 @@ fitYearNames <- function(Par) { #' exists, so a project still naming it stops. #' #' @param which character, `P(sim)$whichModulesToPrepare`. +#' @param fitPrep logical; `FALSE` leaves out the spread and escape fit data (`fitPrepNeeded()`). #' @return character, event names in the order to schedule them. -prepEventsToSchedule <- function(which) { +prepEventsToSchedule <- function(which, fitPrep = TRUE) { if ("fireSense_EscapeFit" %in% which) stop("fireSense_EscapeFit no longer exists as a module; escape data are prepared with ", "fireSense_ignitionFit. Remove it from parameter whichModulesToPrepare.") if (!all(which %in% c("fireSense_spreadFit", "fireSense_ignitionFit"))) stop("unrecognized module to prepare - review parameter whichModulesToPrepare") ## spread first: its fire start points place the ignitions of fire polygons without an ignition point - c(if ("fireSense_spreadFit" %in% which) "prepSpreadFitData", - if ("fireSense_ignitionFit" %in% which) c("prepIgnitionFitData", "prepEscapeFitData")) + c(if (fitPrep && "fireSense_spreadFit" %in% which) "prepSpreadFitData", + if ("fireSense_ignitionFit" %in% which) c("prepIgnitionFitData", if (fitPrep) "prepEscapeFitData")) +} + +#' Will a fit, profile or validation read the spread and escape fit data? +#' +#' Always, unless `skipUnusedFitPrep` and the job cannot fit: `doFit` FALSE (`Init()` has stopped it if the +#' ledger holds no fit for the ELF; `fireSenseUtils::mustFit()`), no `heldOutFold`, and `fireSense_spreadFit`'s +#' `mode` without "validate" or "profile". With `doFit` NA the ledgers decide, and the ignition ledger is +#' `fireSense_ignitionFit`'s to read, so the data are prepared. +#' +#' @param sim a `simList`. +#' @return logical, length one. +fitPrepNeeded <- function(sim) { + if (!isTRUE(P(sim)$skipUnusedFitPrep)) return(TRUE) + mode <- params(sim)[["fireSense_spreadFit"]][["mode"]] + !isFALSE(P(sim)$doFit) || !isTRUE(is.na(SpaDES.core::paramCheckOtherMods(sim, "heldOutFold"))) || + any(c("validate", "profile") %in% mode) } #' Prepare the covariates and formula for the escape part of fireSense_ignitionFit diff --git a/tests/testthat/test-metadata.R b/tests/testthat/test-metadata.R index 608942c..0d669ef 100644 --- a/tests/testthat/test-metadata.R +++ b/tests/testthat/test-metadata.R @@ -102,7 +102,7 @@ test_that("parameters are the expected names", { "forestedLCC", "fuelClassCol", "fuelCovariates", "heldOutFold", "igAggFactor", "minBufferSize", "minBurnableFireSizeHa", "minCovariateProp", "nonflammableLCC", "nonForestCanBeYoungAge", "scanfiVersion", "sppEquivCol", "spreadClimateCandidates", "spreadFitFilename", - "spreadFitGoogleDriveFolder", "fitFileSuffix", "spreadIntercept", "targetFuelClasses", + "spreadFitGoogleDriveFolder", "fitFileSuffix", "skipUnusedFitPrep", "spreadIntercept", "targetFuelClasses", "useRasterizedFireForSpread", "wholeLandscapeSpreadCovariates", "whichModulesToPrepare")) ) }) diff --git a/tests/testthat/test-skipUnusedFitPrep.R b/tests/testthat/test-skipUnusedFitPrep.R new file mode 100644 index 0000000..6494be5 --- /dev/null +++ b/tests/testthat/test-skipUnusedFitPrep.R @@ -0,0 +1,40 @@ +## `skipUnusedFitPrep`: a job that only predicts from the ledger's fits does not schedule the spread and escape +## fit-data events (`prepSpreadFitData`, `prepEscapeFitData`); `prepIgnitionFitData` stays (it makes `lightningMaps` +## and `ignitionFitRTM`, which the prediction modules read). + +bothFits <- c("fireSense_ignitionFit", "fireSense_spreadFit") + +## a simList with this module current, `doFit` as given, and optionally a spreadFit with its own parameters +prepSim <- function(doFit = FALSE, heldOutFold = NA, spreadFit = NULL, skip = TRUE) { + sim <- suppressMessages(SpaDES.core::simInit()) + sim@params <- list(fireSense_dataPrepFit = list(doFit = doFit, heldOutFold = heldOutFold, + skipUnusedFitPrep = skip)) + if (!is.null(spreadFit)) sim@params$fireSense_spreadFit <- spreadFit + sim@current <- data.table::data.table(moduleName = "fireSense_dataPrepFit") + sim +} +scheduled <- function(sim) prepEventsToSchedule(bothFits, fitPrepNeeded(sim)) + +test_that("by default every prep event is scheduled, as before", { + expect_identical(scheduled(prepSim(FALSE, skip = FALSE)), + c("prepSpreadFitData", "prepIgnitionFitData", "prepEscapeFitData")) +}) + +test_that("skipUnusedFitPrep: a predict-only job keeps only prepIgnitionFitData", { + expect_identical(scheduled(prepSim(FALSE)), "prepIgnitionFitData") + ## a spreadFit that only predicts (its default mode is "fit", which does nothing when the ledger has the fit) + expect_identical(scheduled(prepSim(FALSE, spreadFit = list(mode = "fit", heldOutFold = NA))), "prepIgnitionFitData") +}) + +test_that("skipUnusedFitPrep: a job that may fit, validate, profile or hold out a fold prepares everything", { + all3 <- c("prepSpreadFitData", "prepIgnitionFitData", "prepEscapeFitData") + expect_identical(scheduled(prepSim(TRUE)), all3) + expect_identical(scheduled(prepSim(NA)), all3) # the ledgers decide: not known here + expect_identical(scheduled(prepSim(FALSE, heldOutFold = 2L)), all3) + expect_identical(scheduled(prepSim(FALSE, spreadFit = list(mode = c("fit", "validate")))), all3) + expect_identical(scheduled(prepSim(FALSE, spreadFit = list(mode = "profile"))), all3) +}) + +test_that("a project that prepares only the spread fit schedules nothing when it only predicts", { + expect_length(prepEventsToSchedule("fireSense_spreadFit", FALSE), 0L) +}) From 658c24b05c8603d540cdfb1d8f59187d1b0157a9 Mon Sep 17 00:00:00 2001 From: Eliot McIntire Date: Sun, 15 Nov 2026 01:02:35 -0700 Subject: [PATCH 2/3] skipUnusedFitPrep: keep the spread fire data and the escape data; cacheKey = FALSE; rule from fireSenseUtils::fitDataNeeded() Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_014G18Mik3fH4z5M5CyMSUS6 --- NEWS.md | 2 +- fireSense_dataPrepFit.R | 87 ++++++++++++++++--------- tests/testthat/test-skipUnusedFitPrep.R | 73 ++++++++++++++++----- 3 files changed, 113 insertions(+), 49 deletions(-) diff --git a/NEWS.md b/NEWS.md index 44f30f3..ddd2d5d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,6 @@ # fireSense_dataPrepFit 1.3.0.9011 (development version) -* New parameter `skipUnusedFitPrep` (default FALSE: nothing changes). TRUE leaves out `prepSpreadFitData` and `prepEscapeFitData`, the slowest steps, in a job that cannot fit, validate, profile or hold out a fold (`doFit` FALSE, no `heldOutFold`, `fireSense_spreadFit`'s `mode` without "validate" or "profile"): about 2.5 minutes of every hindcast year-job. `prepIgnitionFitData` still runs (`lightningMaps`, `ignitionFitRTM`). `sim$ignitionFirePoints` is then the study area's NFDB points, not the core points made with the spread fires (`fireSense_summary`'s hindcast reads it to count the observed ignitions). Needs a `fireSense_ignitionFit` that uses its stored fit without escape covariates (PredictiveEcology/fireSense_ignitionFit). The same parameter of `fireSense_spreadFit` (`.globals = list(skipUnusedFitPrep = TRUE)`). +* New parameter `skipUnusedFitPrep` (default FALSE: nothing changes). TRUE, in a job that cannot fit, validate, profile or hold out a fold (`fireSenseUtils::fitDataNeeded()`: `doFit` FALSE, no `heldOutFold`, `fireSense_spreadFit`'s `mode` without "validate" or "profile"), leaves out the spread covariates, the slowest step: about 2.5 minutes of every hindcast year-job. The spread fire data (new event `prepSpreadFitFireData`, the fire half of `prepSpreadFitData`) and `prepIgnitionFitData` and `prepEscapeFitData` still run, so `spreadFirePolys`, `escapeFireSet`, `ignitionFirePoints` and the escape covariates are what a full run makes. The same parameter of `fireSense_spreadFit` (`.globals = list(skipUnusedFitPrep = TRUE)`); init stops if they differ on it or on `doFit`. Not in the cache keys. Needs fireSenseUtils >= 0.2.3.9167 (the version assumed for the PR that adds `fitDataNeeded()`). * `fireSense_ignitionCovariates` has a new column `coreFraction`: the share of each coarse pixel's flammable cells that are in the core of `rasterToMatchLargeELF` (1 in the core, 0 in the buffer, fractional at the edge; computed per data year from `flammableRTMs`, with `fireSenseUtils::inELFCore()` and the aggregation of the coarse covariates). The ignitions are counted in the core only, so fireSense_ignitionFit weights each pixel by it; without it the fit spread the core's ignitions over core and buffer and hindcasts simulated about 0.8 of the observed ignitions. It is an exposure, not a covariate (`fireSenseUtils::coreFractionTxt`, fireSenseUtils >= 0.2.3.9161), and the escape formula leaves it out. The `prepIgnitionFitData` event re-runs once (its code changed). Used with PredictiveEcology/fireSense_ignitionFit (`Depends-on` there). * New parameter `spreadClimateCandidates` (default: the default ignition variables plus `Tmax_sm` and `PPT_sm`): `climateVariablesForFire$spread = "auto"` now chooses among it, not among `climateVariablesForFire$ignition`, which IgnitionFit still uses whole and unchanged. The candidates' layers are prepared with `climateVariables`, but only the chosen variable enters the spread tables. Precipitation is scored as decreasing with dryness. The default `auto` choice can now be `Tmax_sm` or `PPT_sm`, so spread fits and results can change; they need `Tmaxsm` and `PPTsm` in `fireSenseUtils::climateCovRanges` (PredictiveEcology/fireSenseUtils, branch `climate-ranges-summer`). It is a module parameter, so changing it re-runs the cached events. diff --git a/fireSense_dataPrepFit.R b/fireSense_dataPrepFit.R index 9499ff6..7e1002b 100644 --- a/fireSense_dataPrepFit.R +++ b/fireSense_dataPrepFit.R @@ -17,7 +17,7 @@ defineModule(sim, list( reqdPkgs = list("data.table", "digest", "fastDummies", "googledrive", "httr2", "qs2", "Require", "PredictiveEcology/reproducible@development (>= 3.2.1.9042)", # CacheGeo re-reads a changed local file "PredictiveEcology/climateData@development (>= 2.2.3.9006)", - "PredictiveEcology/fireSenseUtils@development (>= 0.2.3.9161)", # 9161: coreFractionTxt; 9159: climateCovRanges Tmaxsm, PPTsm (#224); 9141: spreadFitLedgerInputs(), spreadFitLedgerInputLists(); 9135: fireSenseFireCauses; 9131: fireCauseCodes(); 9130: climateRasterToDataTable(Index) reads only those cells; 9128: burnableHaTxt + "PredictiveEcology/fireSenseUtils@development (>= 0.2.3.9167)", # 9167: fitDataNeeded() (assumed post-merge version); 9161: coreFractionTxt; 9159: climateCovRanges Tmaxsm, PPTsm (#224); 9141: spreadFitLedgerInputs(), spreadFitLedgerInputLists(); 9135: fireSenseFireCauses; 9131: fireCauseCodes(); 9130: climateRasterToDataTable(Index) reads only those cells; 9128: burnableHaTxt "FOR-CAST/fireregimetools@main (>= 0.1.0.9008)", "ggplot2", "methods", "parallel", "purrr", "raster", "sf", "sp", "stats", "utils", "PredictiveEcology/LandR@development (>= 1.2.0.9015)", @@ -140,13 +140,14 @@ defineModule(sim, list( "`.globals = list(doFit = ...)`.")), defineParameter("skipUnusedFitPrep", "logical", default = FALSE, desc = paste("FALSE (default): unchanged. TRUE: a job that cannot fit, validate, profile or hold out a fold", - "(`doFit` FALSE, no `heldOutFold`, `fireSense_spreadFit`'s `mode` without \"validate\" or \"profile\")", - "does not schedule `prepSpreadFitData` and `prepEscapeFitData`, the slowest steps, whose outputs", - "only those fits read; `prepIgnitionFitData` still runs (`lightningMaps`, `ignitionFitRTM`).", - "`sim$ignitionFirePoints` is then the study area's NFDB points, not the core points made with the", - "spread fires. The same parameter of `fireSense_spreadFit`: set it for both with", - "`.globals = list(skipUnusedFitPrep = TRUE)`. Needs `fireSense_ignitionFit` that does not compare", - "a stored escape fit with escape covariates it was not given.")), + "(`fireSenseUtils::fitDataNeeded()`: `doFit` FALSE, no `heldOutFold`, `fireSense_spreadFit`'s `mode`", + "without \"validate\" or \"profile\") makes only the spread fire data (`prepSpreadFitFireData`: the", + "fire polygons, `escapeFireSet`, hence the same `ignitionFirePoints` and escape covariates) and", + "leaves out the spread covariates, the slowest step and the one only a fit reads.", + "The same parameter of `fireSense_spreadFit`: set it for both with", + "`.globals = list(skipUnusedFitPrep = TRUE)`; init stops if they differ on it or on `doFit`.", + "Not part of the cache keys."), + cacheKey = FALSE), defineParameter("fitFileSuffix", "character", "", NA, NA, paste("Text put after the model tag in the ledger file names, e.g. `\"_boundfix_rep81\"`, so a test run", "writes and reads (`\"latest\"`) only its own files, never the production ones. `\"\"` (the", @@ -215,6 +216,11 @@ defineModule(sim, list( list(spreadFitPreRun = sim$spreadFitPreRun), fireSenseUtils::escapeFireSetDeps()))), prepEscapeFitData = list(.cacheExtra = quote(c( list(spreadFitPreRun = sim$spreadFitPreRun), fireSenseUtils::escapeFireSetDeps()))), + prepSpreadFitFireData = list(.cacheExtra = quote(c(list( + fireSenseUtils::bufferToArea, fireSenseUtils::escapedFires, fireSenseUtils::escapeDefinitionOf, + fireSenseUtils::fireCauseCodes, fireSenseUtils::rasterFireBufferDT, + fireSenseUtils::rasterFireSpreadPoints, spreadFitPreRun = sim$spreadFitPreRun), + fireSenseUtils::harmonizeFireDataDeps(), fireSenseUtils::escapeFireSetDeps()))), prepSpreadFitData = list(.cacheExtra = quote(c(list( fireSenseUtils::bufferToArea, fireSenseUtils::chooseDomSecFuelClasses, fireSenseUtils::climateRasterToDataTable, fireSenseUtils::dataYearBefore, @@ -356,13 +362,13 @@ defineModule(sim, list( createsOutput("fireBufferedListDT", "list", paste("list of data.tables with fire id, `pixelID`, and buffer status, of the fires the spread", "model is fitted to: those that start in the core with at least `minBurnableFireSizeHa` on burnable pixels."), - events = "prepSpreadFitData"), + events = c("prepSpreadFitFireData", "prepSpreadFitData")), createsOutput("fuelClassTable", "data.table", "Fuel class assigned to each tree species; only created if fuel classes are estimated.", events = "dataPrepBuild"), createsOutput("spreadFirePolys", "list", "List of annual fire polygons of the escaped fires, core and buffer start, matched to their harmonized start points (`escapeFireSet`); `spreadFirePoints` and `fireBufferedListDT` have the core-start fires only.", - events = c("dataPrepBuild", "prepSpreadFitData")), + events = c("dataPrepBuild", "prepSpreadFitFireData", "prepSpreadFitData")), createsOutput("fireSense_annualSpreadFitCovariates", "list", "List of data.tables, one per fire year, of `pixelID` and the spread climate covariates in the fire buffers (every flammable pixel with `wholeLandscapeSpreadCovariates`).", events = "prepSpreadFitData"), @@ -432,7 +438,7 @@ defineModule(sim, list( paste("List of ignition points, one per fire year, for the fires larger than one pixel that start in the core,", "harmonized with `spreadFirePolys`. Column `fireSenseUtils::burnableHaTxt` is each fire's", "burned area (ha) on burnable pixels, the spread fit's target size."), - events = c(".inputObjects", "prepSpreadFitData")), + events = c(".inputObjects", "prepSpreadFitFireData", "prepSpreadFitData")), createsOutput("escapeFireSet", "SpatVector", paste("The start of every escaped fire (`escapeSizeHa`, `fireCauses`), core and buffer, from", @@ -440,7 +446,7 @@ defineModule(sim, list( "`polyHa`, `burnableHa`, `pixelID` and `core`. Only `core` fires are in `spreadFirePoints` and", "`fireBufferedListDT`; `spreadFirePolys` keeps all, so the ignition and escape data can leave out", "the NFDB points of buffer-start fires."), - events = "prepSpreadFitData"), + events = c("prepSpreadFitFireData", "prepSpreadFitData")), createsOutput("fireSense_spreadFitCorePixels", "integer", paste("`pixelID`s (cells of `rasterToMatch`) in the ELF's core, where `rasterToMatchLargeELF` is", "`fireSenseUtils::fireSenseELFCoreValue`; every pixel without `rasterToMatchLargeELF`. The spread", @@ -505,6 +511,9 @@ doEvent.fireSense_dataPrepFit = function(sim, eventTime, eventType) { prepSpreadFitData = { sim <- prepare_SpreadFit(sim) }, + prepSpreadFitFireData = { + sim <- prepare_SpreadFitFires(sim) + }, plotAndMessage = { sim <- plotAndMessage(sim) }, @@ -846,6 +855,26 @@ dataPrepBuild <- function(sim) { } +#' Prepare the fire data for fireSense_spreadFit +#' +#' The fire half of `prepare_SpreadFit()`: the fire polygons and start points, `fireBufferedListDT` and +#' `escapeFireSet`, which the ignition and escape data also need. A job that only predicts +#' (`skipUnusedFitPrep`) runs only this, so those objects are what a full run makes. Sets `mod$allYears`. +#' +#' @param sim a `simList`. +#' @return the `simList`, invisibly, with `fireBufferedListDT`, `spreadFirePoints`, `spreadFirePolys` and +#' `escapeFireSet`. +prepare_SpreadFitFires <- function(sim) { + mod$allYears <- fitYearNames(Par) # it runs before prepare_IgnitionFit, which set them + ## sim$fireBufferedListDT is made in these functions + if (P(sim)$useRasterizedFireForSpread) { + sim <- prepare_SpreadFitFire_Raster(sim) + } else { + sim <- prepare_SpreadFitFire_Vector(sim) + } + invisible(sim) +} + #' Prepare the covariates and formula for fireSense_spreadFit #' #' Buffers each fire, joins the buffers to the fuel covariates of their data year and to annual @@ -857,7 +886,7 @@ dataPrepBuild <- function(sim) { #' `spreadFirePoints` and `spreadFirePolys`. prepare_SpreadFit <- function(sim) { - mod$allYears <- fitYearNames(Par) # it runs before prepare_IgnitionFit, which set them + sim <- prepare_SpreadFitFires(sim) ## prep veg data --------------------------------------------------------------------------------- doAssertion <- getOption("fireSenseUtils.assertions", TRUE) @@ -936,14 +965,6 @@ prepare_SpreadFit <- function(sim) { }) vegData <- rbindlist(vegData) - # prep the fire data #### - # sim$fireBufferedListDT is made in these functions - if (P(sim)$useRasterizedFireForSpread) { - sim <- prepare_SpreadFitFire_Raster(sim) - } else { - sim <- prepare_SpreadFitFire_Vector(sim) - } - ## the pixels of the ELF's core, where the spread fit scores area burned sim$fireSense_spreadFitCorePixels <- which(fireSenseUtils::inELFCore(seq_len(terra::ncell(sim$rasterToMatch)), sim$rasterToMatchLargeELF, grid = sim$rasterToMatch)) @@ -1515,7 +1536,8 @@ fitYearNames <- function(Par) { #' exists, so a project still naming it stops. #' #' @param which character, `P(sim)$whichModulesToPrepare`. -#' @param fitPrep logical; `FALSE` leaves out the spread and escape fit data (`fitPrepNeeded()`). +#' @param fitPrep logical; `FALSE` makes only the spread fire data (`prepSpreadFitFireData`), which the ignition and +#' escape data need, not the spread covariates (`fitPrepNeeded()`). #' @return character, event names in the order to schedule them. prepEventsToSchedule <- function(which, fitPrep = TRUE) { if ("fireSense_EscapeFit" %in% which) @@ -1524,24 +1546,25 @@ prepEventsToSchedule <- function(which, fitPrep = TRUE) { if (!all(which %in% c("fireSense_spreadFit", "fireSense_ignitionFit"))) stop("unrecognized module to prepare - review parameter whichModulesToPrepare") ## spread first: its fire start points place the ignitions of fire polygons without an ignition point - c(if (fitPrep && "fireSense_spreadFit" %in% which) "prepSpreadFitData", - if ("fireSense_ignitionFit" %in% which) c("prepIgnitionFitData", if (fitPrep) "prepEscapeFitData")) + c(if ("fireSense_spreadFit" %in% which && fitPrep) "prepSpreadFitData", + if ("fireSense_spreadFit" %in% which && !fitPrep && "fireSense_ignitionFit" %in% which) "prepSpreadFitFireData", + if ("fireSense_ignitionFit" %in% which) c("prepIgnitionFitData", "prepEscapeFitData")) } -#' Will a fit, profile or validation read the spread and escape fit data? +#' Will a fit, profile or validation read the spread covariates? #' -#' Always, unless `skipUnusedFitPrep` and the job cannot fit: `doFit` FALSE (`Init()` has stopped it if the -#' ledger holds no fit for the ELF; `fireSenseUtils::mustFit()`), no `heldOutFold`, and `fireSense_spreadFit`'s -#' `mode` without "validate" or "profile". With `doFit` NA the ledgers decide, and the ignition ledger is -#' `fireSense_ignitionFit`'s to read, so the data are prepared. +#' Always, unless `skipUnusedFitPrep` and `fireSenseUtils::fitDataNeeded()` is `FALSE` for this job's `doFit`, +#' `heldOutFold` and `fireSense_spreadFit`'s `mode`. The ledger is not known here (`Init()` reads it after the +#' events are scheduled), so `doFit` NA counts as a job that may fit. #' #' @param sim a `simList`. #' @return logical, length one. fitPrepNeeded <- function(sim) { if (!isTRUE(P(sim)$skipUnusedFitPrep)) return(TRUE) - mode <- params(sim)[["fireSense_spreadFit"]][["mode"]] - !isFALSE(P(sim)$doFit) || !isTRUE(is.na(SpaDES.core::paramCheckOtherMods(sim, "heldOutFold"))) || - any(c("validate", "profile") %in% mode) + for (nm in c("skipUnusedFitPrep", "doFit")) + SpaDES.core::paramCheckOtherMods(sim, nm, ifSetButDifferent = "error") + fireSenseUtils::fitDataNeeded(P(sim)$doFit, SpaDES.core::paramCheckOtherMods(sim, "heldOutFold"), + params(sim)[["fireSense_spreadFit"]][["mode"]]) } #' Prepare the covariates and formula for the escape part of fireSense_ignitionFit diff --git a/tests/testthat/test-skipUnusedFitPrep.R b/tests/testthat/test-skipUnusedFitPrep.R index 6494be5..cf34d87 100644 --- a/tests/testthat/test-skipUnusedFitPrep.R +++ b/tests/testthat/test-skipUnusedFitPrep.R @@ -1,40 +1,81 @@ -## `skipUnusedFitPrep`: a job that only predicts from the ledger's fits does not schedule the spread and escape -## fit-data events (`prepSpreadFitData`, `prepEscapeFitData`); `prepIgnitionFitData` stays (it makes `lightningMaps` -## and `ignitionFitRTM`, which the prediction modules read). +## `skipUnusedFitPrep`: a job that only predicts from the ledger's fits makes only the spread FIRE data +## (`prepSpreadFitFireData`), not the spread covariates. The ignition and escape data are made as in a full run, +## from the same fire data, so `ignitionFirePoints` and `fireSense_escapeCovariates` are the same. bothFits <- c("fireSense_ignitionFit", "fireSense_spreadFit") +all4 <- c("prepSpreadFitData", "prepIgnitionFitData", "prepEscapeFitData") +fireOnly <- c("prepSpreadFitFireData", "prepIgnitionFitData", "prepEscapeFitData") ## a simList with this module current, `doFit` as given, and optionally a spreadFit with its own parameters prepSim <- function(doFit = FALSE, heldOutFold = NA, spreadFit = NULL, skip = TRUE) { sim <- suppressMessages(SpaDES.core::simInit()) sim@params <- list(fireSense_dataPrepFit = list(doFit = doFit, heldOutFold = heldOutFold, skipUnusedFitPrep = skip)) - if (!is.null(spreadFit)) sim@params$fireSense_spreadFit <- spreadFit + if (!is.null(spreadFit)) sim@params$fireSense_spreadFit <- c(list(skipUnusedFitPrep = skip, doFit = doFit), spreadFit) sim@current <- data.table::data.table(moduleName = "fireSense_dataPrepFit") sim } scheduled <- function(sim) prepEventsToSchedule(bothFits, fitPrepNeeded(sim)) test_that("by default every prep event is scheduled, as before", { - expect_identical(scheduled(prepSim(FALSE, skip = FALSE)), - c("prepSpreadFitData", "prepIgnitionFitData", "prepEscapeFitData")) + expect_identical(scheduled(prepSim(FALSE, skip = FALSE)), all4) }) -test_that("skipUnusedFitPrep: a predict-only job keeps only prepIgnitionFitData", { - expect_identical(scheduled(prepSim(FALSE)), "prepIgnitionFitData") - ## a spreadFit that only predicts (its default mode is "fit", which does nothing when the ledger has the fit) - expect_identical(scheduled(prepSim(FALSE, spreadFit = list(mode = "fit", heldOutFold = NA))), "prepIgnitionFitData") +test_that("skipUnusedFitPrep: a predict-only job makes the fire data, not the spread covariates", { + expect_identical(scheduled(prepSim(FALSE)), fireOnly) + expect_identical(scheduled(prepSim(FALSE, spreadFit = list(mode = "fit", heldOutFold = NA))), fireOnly) }) test_that("skipUnusedFitPrep: a job that may fit, validate, profile or hold out a fold prepares everything", { - all3 <- c("prepSpreadFitData", "prepIgnitionFitData", "prepEscapeFitData") - expect_identical(scheduled(prepSim(TRUE)), all3) - expect_identical(scheduled(prepSim(NA)), all3) # the ledgers decide: not known here - expect_identical(scheduled(prepSim(FALSE, heldOutFold = 2L)), all3) - expect_identical(scheduled(prepSim(FALSE, spreadFit = list(mode = c("fit", "validate")))), all3) - expect_identical(scheduled(prepSim(FALSE, spreadFit = list(mode = "profile"))), all3) + expect_identical(scheduled(prepSim(TRUE)), all4) + expect_identical(scheduled(prepSim(NA)), all4) # the ledgers decide: not known here + expect_identical(scheduled(prepSim(FALSE, heldOutFold = 2L)), all4) + expect_identical(scheduled(prepSim(FALSE, spreadFit = list(mode = c("fit", "validate")))), all4) + expect_identical(scheduled(prepSim(FALSE, spreadFit = list(mode = "profile"))), all4) }) test_that("a project that prepares only the spread fit schedules nothing when it only predicts", { expect_length(prepEventsToSchedule("fireSense_spreadFit", FALSE), 0L) }) + +test_that("the modules must agree on skipUnusedFitPrep and doFit", { + sim <- prepSim(FALSE, spreadFit = list()) + sim@params$fireSense_spreadFit$skipUnusedFitPrep <- FALSE + expect_error(fitPrepNeeded(sim), "skipUnusedFitPrep") + sim <- prepSim(FALSE, spreadFit = list()) + sim@params$fireSense_spreadFit$doFit <- NA + expect_error(fitPrepNeeded(sim), "doFit") +}) + +## ignitionFirePoints and the escape covariates read the fire data (spreadFirePolys, escapeFireSet) and nothing else +## that only the spread covariates make; the fire half is one function that a full run calls too +test_that("the fire data are made by one function that the full spread prep calls", { + defs <- moduleDefs() + expect_true("prepare_SpreadFitFires" %in% all.names(defs[["prepare_SpreadFit"]])) + expect_false(any(c("prepare_SpreadFitFire_Raster", "prepare_SpreadFitFire_Vector") %in% + all.names(defs[["prepare_SpreadFit"]]))) + md <- SpaDES.core::moduleMetadata(module = moduleName, path = modulePath) + made <- md$outputObjects$objectName[vapply(md$outputObjects$events, function(e) "prepSpreadFitFireData" %in% e, + logical(1))] + expect_true(all(c("spreadFirePolys", "escapeFireSet") %in% made)) + expect_false(any(c("fireSense_annualSpreadFitCovariates", "fireSense_spreadFormula") %in% made)) +}) + +test_that("prepare_SpreadFitFires runs the raster or the vector builder, as the full run did", { + for (raster in c(TRUE, FALSE)) { + sim <- suppressMessages(SpaDES.core::simInit()) + sim@params <- list(fireSense_dataPrepFit = list(useRasterizedFireForSpread = raster)) + sim@current <- data.table::data.table(moduleName = "fireSense_dataPrepFit") + ran <- character() + e <- new.env(parent = environment(prepare_SpreadFitFires)) + e$mod <- new.env() + e$Par <- list(dataYears = 2000, fireYears = 2001:2002) + e$prepare_SpreadFitFire_Raster <- function(sim) { ran <<- c(ran, "raster"); sim } + e$prepare_SpreadFitFire_Vector <- function(sim) { ran <<- c(ran, "vector"); sim } + f <- prepare_SpreadFitFires + environment(f) <- e + f(sim) + expect_identical(ran, if (raster) "raster" else "vector") + expect_false(is.null(e$mod$allYears)) + } +}) From 1ff308981b7a39e07b4e1982cddcc4fcb472d994 Mon Sep 17 00:00:00 2001 From: Eliot McIntire Date: Sun, 15 Nov 2026 11:45:35 -0700 Subject: [PATCH 3/3] fireSenseUtils floor 0.2.3.9169: fitDataNeeded() (PredictiveEcology/fireSenseUtils#234) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_014G18Mik3fH4z5M5CyMSUS6 --- fireSense_dataPrepFit.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fireSense_dataPrepFit.R b/fireSense_dataPrepFit.R index 7e1002b..1428030 100644 --- a/fireSense_dataPrepFit.R +++ b/fireSense_dataPrepFit.R @@ -17,7 +17,7 @@ defineModule(sim, list( reqdPkgs = list("data.table", "digest", "fastDummies", "googledrive", "httr2", "qs2", "Require", "PredictiveEcology/reproducible@development (>= 3.2.1.9042)", # CacheGeo re-reads a changed local file "PredictiveEcology/climateData@development (>= 2.2.3.9006)", - "PredictiveEcology/fireSenseUtils@development (>= 0.2.3.9167)", # 9167: fitDataNeeded() (assumed post-merge version); 9161: coreFractionTxt; 9159: climateCovRanges Tmaxsm, PPTsm (#224); 9141: spreadFitLedgerInputs(), spreadFitLedgerInputLists(); 9135: fireSenseFireCauses; 9131: fireCauseCodes(); 9130: climateRasterToDataTable(Index) reads only those cells; 9128: burnableHaTxt + "PredictiveEcology/fireSenseUtils@development (>= 0.2.3.9169)", # 9169: fitDataNeeded() (#234); 9161: coreFractionTxt; 9159: climateCovRanges Tmaxsm, PPTsm (#224); 9141: spreadFitLedgerInputs(), spreadFitLedgerInputLists(); 9135: fireSenseFireCauses; 9131: fireCauseCodes(); 9130: climateRasterToDataTable(Index) reads only those cells; 9128: burnableHaTxt "FOR-CAST/fireregimetools@main (>= 0.1.0.9008)", "ggplot2", "methods", "parallel", "purrr", "raster", "sf", "sp", "stats", "utils", "PredictiveEcology/LandR@development (>= 1.2.0.9015)",