diff --git a/NEWS.md b/NEWS.md index 3de2aeb..d8a34f5 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # fireSense_dataPrepPredict (development version) +* `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. + * A hindcast or forecast with a `~ 1` spread fit (no spread climate variable, so `climateVariablesForFire$spread` is empty) stopped when the covariates were built (`terra` cannot select no layer). The spread covariate table now has `pixelID` and `youngAge` and the fuels, with no climate column; the ignition covariates are unchanged. Together with `fireSenseUtils::climateRasterToDataTable()` returning a table for no raster (PredictiveEcology/fireSenseUtils, null-arm hindcasts of `fireSense_dataPrepFit`). * A hindcast of year Y built `youngAge` differently from the fit, for the spread and the ignition covariates: it used the cohort ages and the data year's time since disturbance as they were, and did not see a fire between the data year and Y (youngAge was 2.8% of burned pixels in ELF 6.6.2 in 2010, 1.5% in the fit). It now resolves it as `fireSense_dataPrepFit` does: the data year's time since disturbance aged to Y and every fire after it, from `fireSenseUtils::youngAgeAtYear()` (spread) and `fireSenseUtils::prepare_FuelCovsCoarseByYear()` (ignition), with the fires from `historicalFireRaster`, else `firePolysForAge` (`fireSenseUtils::firePixelsByYear()`, as the fit chooses). Fuel, non-forest land cover and treed wetland are cleared where `youngAge` is 1 in the covariate table itself (`fireSenseUtils::clearYoungAge()`, which the ignition fit uses), so a fit on log-scale fuel, for which `fireSense_spreadPredict` clears only the indicators, is right too. A hindcast with `nonForestCanBeYoungAge = FALSE` stops, as the fit does (`fireSenseUtils::stopIfNonForestCannotBeYoung()`). New inputs `firePolysForAge` and `historicalFireRaster` (from `fireSense_dataPrepFit`); a hindcast with neither stops. Hindcast covariates, and so their cache keys, change. Forecasts (a module simulates `cohortData`) are unchanged. Needs the fireSenseUtils version after PredictiveEcology/fireSenseUtils#239 merges; the `reqdPkgs` floor is set after that merge's version bump. diff --git a/fireSense_dataPrepPredict.R b/fireSense_dataPrepPredict.R index 9646d65..bf2f2f2 100644 --- a/fireSense_dataPrepPredict.R +++ b/fireSense_dataPrepPredict.R @@ -178,8 +178,8 @@ defineModule(sim, list( expectsInput("lightningMaps", "SpatRaster", sourceURL = NA, desc = paste( "Lightning layers: lightningDays, lightningDensity, positiveCG, positiveCGdensity.", - "Only `lightningDays` is used. Unsupplied: `fireSenseUtils::prepare_LightningData()`, as", - "`fireSense_dataPrepFit` makes it.")), + "Only `lightningDays` is used. Unsupplied, or declared by `fireSense_dataPrepFit` but not made (it", + "skips its ignition prep): `fireSenseUtils::prepare_LightningData()`, as `fireSense_dataPrepFit` makes it.")), expectsInput("pixelGroupMap", "SpatRaster", sourceURL = NA, desc = "Map of the `pixelGroup`s in `cohortData`."), expectsInput("projectedClimateRasters", "list", sourceURL = NA, @@ -290,10 +290,12 @@ defineModule(sim, list( createsOutput("climateVariables", "list", desc = "Set by `.inputObjects` when unsupplied (see `expectsInput`), for canClimateData."), createsOutput("ignitionFitRTM", "SpatRaster", - desc = paste("Unless `fireSense_dataPrepFit` (or a user) supplies it: the coarse grid", + desc = paste("Unless `fireSense_dataPrepFit` (which skips its ignition prep when it reuses a ledger fit) or a user", + "has made it by then: the coarse grid", "(`igAggFactor`) of `fireSense_igAndEscapePred_Covariates`, whose `pixelID`s are its", "cells; `fireSense_ignitionPredict` predicts on it. Set with those covariates.")), - createsOutput("lightningMaps", "SpatRaster", desc = "Set by `.inputObjects` when unsupplied (see `expectsInput`)."), + createsOutput("lightningMaps", "SpatRaster", + desc = "Set by `.inputObjects`, or at `prepIgAndEscPredictData` if still NULL (see `expectsInput`)."), createsOutput("sppEquivs", "list", desc = "Set by `.inputObjects` from the SpreadFit ledger in a run without `fireSense_dataPrepFit`."), createsOutput("nonForestedLCCGroupsList", "list", @@ -355,6 +357,9 @@ doEvent.fireSense_dataPrepPredict <- function(sim, eventTime, eventType) { ) }, prepIgAndEscPredictData = { + ## `fireSense_dataPrepFit` makes `lightningMaps` at start(sim), priority 1, before this (priority 5); when it + ## declares them but skips its ignition prep (ledger fit reused) they are still NULL here + if (is.null(sim$lightningMaps)) sim$lightningMaps <- lightningMapsFor(sim) if (!covariatesBuilt(sim, "ignition", list(sim$lightningMaps[["lightningDays"]]))) sim <- prepare_IgnitionAndEscapePredict(sim) sim <- scheduleEvent(sim, time(sim) + P(sim)$fireTimeStep, @@ -684,8 +689,9 @@ prepare_IgnitionAndEscapePredict <- function(sim) { useCache = FALSE) ## the coarse grid of these covariates (their `pixelID`s), which fireSense_ignitionPredict predicts on; - ## fireSense_dataPrepFit makes it for the fit, from the same template, when it is in the run - if (!suppliedElsewhere("ignitionFitRTM", sim, where = c("user", "otherModules"))) + ## fireSense_dataPrepFit makes it for the fit, from the same template, when it is in the run and prepares + ## ignition (it declares it either way, so test for the object, not for who supplies it) + if (is.null(sim$ignitionFitRTM)) sim$ignitionFitRTM <- terra::setValues(terra::rast(fuelCovsCoarse, nlyrs = 1), 1) yrLab <- paste0("year", time(sim)) @@ -1260,6 +1266,9 @@ predictInputsFromLedger <- function(sim) { #' @param sim A `simList`. #' #' @return The `simList`, invisibly. +lightningMapsFor <- function(sim) + fireSenseUtils::prepare_LightningData(sim$rasterToMatch, P(sim)$igAggFactor, dPath = asPath(inputPath(sim), 1)) + .inputObjects <- function(sim) { if (isReplay(sim)) { # a replay job: the saved inputs, nothing built message(currentModule(sim), ": replaying the inputs saved in '", P(sim)$replayFrom, "'.") @@ -1280,7 +1289,7 @@ predictInputsFromLedger <- function(sim) { ## only the ignition covariates use lightning if ("fireSense_ignitionPredict" %in% P(sim)$whichModulesToPrepare && !suppliedElsewhere("lightningMaps", sim)) - sim$lightningMaps <- fireSenseUtils::prepare_LightningData(sim$rasterToMatch, P(sim)$igAggFactor, dPath = dPath) + sim$lightningMaps <- lightningMapsFor(sim) if (!suppliedElsewhere("climateVariablesForFire", sim)) { sim$climateVariablesForFire <- list( diff --git a/tests/testthat/test-ignitionInputsOtherModule.R b/tests/testthat/test-ignitionInputsOtherModule.R new file mode 100644 index 0000000..ec4c39d --- /dev/null +++ b/tests/testthat/test-ignitionInputsOtherModule.R @@ -0,0 +1,54 @@ +## fireSense_dataPrepFit declares `lightningMaps` and `ignitionFitRTM` as outputs, so they count as supplied by +## another module; but when it skips its ignition prep (fireSense_ignitionFit only reuses a ledger fit) it +## never makes them. This module then builds them itself, in prepIgAndEscPredictData (priority 5), after +## dataPrepFit's prepIgnitionFitData (start(sim), priority 1) would have run. + +stubFitModule <- function() { + mp <- withr::local_tempdir(.local_envir = parent.frame()) + dir.create(file.path(mp, "fitStub")) + writeLines(' +defineModule(sim, list(name = "fitStub", description = "", keywords = "", + authors = person("A", "B", email = "a@b.com", role = c("aut", "cre")), + childModules = character(0), version = list(fitStub = "0.0.1"), + timeframe = as.POSIXlt(c(NA, NA)), timeunit = "year", citation = list(), documentation = list(), + reqdPkgs = list(), parameters = rbind(), + inputObjects = bindrows(), + outputObjects = bindrows(createsOutput("lightningMaps", "SpatRaster", desc = ""), + createsOutput("ignitionFitRTM", "SpatRaster", desc = "")))) +doEvent.fitStub <- function(sim, eventTime, eventType, debug = FALSE) invisible(sim) +', file.path(mp, "fitStub", "fitStub.R")) + mp +} + +declaredOnlySim <- function(objects) { + mp <- stubFitModule() + SpaDES.core::simInit(times = list(start = 2001, end = 2001, timeunit = "year"), + modules = c(moduleName, "fitStub"), + params = stats::setNames(list(list(igAggFactor = 2)), moduleName), + objects = objects, + paths = utils::modifyList(testPaths, list(modulePath = c(testPaths$modulePath, mp)))) +} + +test_that("lightningMaps and ignitionFitRTM another module declares but does not make are built here", { + objs <- toyObjects() + lm <- objs$lightningMaps + objs$lightningMaps <- NULL + n <- 0L + testthat::local_mocked_bindings( + prepare_LightningData = function(...) { n <<- n + 1L; lm }, .package = "fireSenseUtils") + sim <- suppressMessages(SpaDES.core::spades(declaredOnlySim(objs), debug = FALSE)) + expect_identical(n, 1L) + expect_s4_class(sim$lightningMaps, "SpatRaster") + expect_s4_class(sim$ignitionFitRTM, "SpatRaster") + expect_identical(sort(sim$fireSense_igAndEscapePred_Covariates$pixelID), which(!is.na(terra::values(sim$ignitionFitRTM))) |> sort()) +}) + +test_that("lightningMaps and ignitionFitRTM another module makes are used, not rebuilt", { + objs <- toyObjects() + objs$ignitionFitRTM <- terra::setValues(terra::rast(toyRast(1), nlyrs = 1), 1) + names(objs$ignitionFitRTM) <- "mine" + testthat::local_mocked_bindings( + prepare_LightningData = function(...) stop("lightning rebuilt"), .package = "fireSenseUtils") + sim <- suppressMessages(SpaDES.core::spades(declaredOnlySim(objs), debug = FALSE)) + expect_identical(names(sim$ignitionFitRTM), "mine") +})