diff --git a/NEWS.md b/NEWS.md index 904e145..b1d3a3a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # fireSense_dataPrepFit 1.3.0.9011 (development version) +* With `skipUnusedFitPrep = TRUE`, `prepIgnitionFitData` and `prepEscapeFitData` (the lightning download and ignition covariates, ~14 min in one run) are now left out when `fireSense_ignitionFit`'s own `doFit` is FALSE, as the spread covariates already are; `prepSpreadFitFireData` goes with them. Run when its `doFit` is TRUE or NA. `ignitionFirePoints` and `nonEscapedFireSizesHa` are still made at init. `skipUnusedFitPrep` is now TRUE by default: a run that does not fit prepares no fit data. Set it FALSE (for both this module and `fireSense_spreadFit`, e.g. `.globals = list(skipUnusedFitPrep = FALSE)`) to prepare everything, as before. + * The "Historical Fire Maps" figure is now listed in `outputs(sim)` on every run. Its outer `Cache()` skipped `Plots()` on a hit, so the figure was missing from the run's saved files; `Plots(useCache = TRUE, .cacheExtra = )` (SpaDES.core, PredictiveEcology/SpaDES.core#535) now skips building the maps on a hit and still records the figure. * New parameter `spreadInteractions` (default `character(0)`: nothing changes, the formula string is byte-identical). `"climate:fuel"` adds to the spread formula one interaction term for each spread climate variable and each fuel covariate (`fireSenseUtils::spreadFuelCols()`: not `youngAge`, `nfLCC_*` or `treedWetland`), e.g. `CMD:dom_agb_Pice_mar`, after the main effects, so `fireSense_spreadFit` fits fuel x climate interactions with the other terms (`spreadRHS()`, `checkSpreadInteractions()`). Each term is the product of its two covariates after each is rescaled and centred, 0 where `youngAge` cleared the fuel, in the fit and in `fireSense_spreadPredict`; each main effect is then the slope at the mean of the other covariate, so the drought >= 0 and fuel >= 0 bounds apply at typical conditions (`fireSenseUtils::spreadProbFromIntegerCovs()`). It needs `spreadCovariates = "all"`. Needs fireSenseUtils >= 0.2.3.9177 (#242), and `fireSense_spreadFit` and `fireSense_spreadPredict` with their interaction PRs. Adding `spreadFuelCols` to `prepSpreadFitData`'s `.cacheExtra` re-runs that event once. diff --git a/fireSense_dataPrepFit.R b/fireSense_dataPrepFit.R index 802dddb..d9f66d1 100644 --- a/fireSense_dataPrepFit.R +++ b/fireSense_dataPrepFit.R @@ -146,14 +146,18 @@ 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", + defineParameter("skipUnusedFitPrep", "logical", default = TRUE, + desc = paste("TRUE (default): 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\") 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.", + "without \"validate\" or \"profile\") leaves out the spread covariates, the slowest step and the one", + "only a fit reads, and makes only the spread fire data (`prepSpreadFitFireData`: the fire polygons,", + "`escapeFireSet`) that the ignition and escape data need. The ignition and escape prep", + "(`prepIgnitionFitData`, `prepEscapeFitData`) follows `fireSense_ignitionFit`'s own `doFit`: left out", + "when it is FALSE (the ledger's fits are used), and then so is `prepSpreadFitFireData`; run when it is", + "TRUE or NA. `ignitionFirePoints` and `nonEscapedFireSizesHa` are still made, from the input points.", + "FALSE: prepare everything, as before this parameter existed.", "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`.", + "`.globals = list(skipUnusedFitPrep = FALSE)`; init stops if they differ on it or on `doFit`.", "Not part of the cache keys."), cacheKey = FALSE), defineParameter("fitFileSuffix", "character", "", NA, NA, @@ -513,7 +517,7 @@ doEvent.fireSense_dataPrepFit = function(sim, eventTime, eventType) { eventType, init = { ## schedule future event(s) - for (ev in prepEventsToSchedule(P(sim)$whichModulesToPrepare, fitPrepNeeded(sim))) + for (ev in prepEventsToSchedule(P(sim)$whichModulesToPrepare, fitPrepNeeded(sim), fitPrepNeeded(sim, "fireSense_ignitionFit"))) sim <- scheduleEvent(sim, start(sim), "fireSense_dataPrepFit", ev, eventPriority = 1) sim <- Init(sim) @@ -1560,13 +1564,15 @@ fitYearNames <- function(Par) { #' #' Preparing `fireSense_ignitionFit` schedules `prepIgnitionFitData` and `prepEscapeFitData`: that module #' fits ignition and escape, and the escape data need the ignition prep. `fireSense_EscapeFit` no longer -#' exists, so a project still naming it stops. +#' exists, so a project still naming it stops. With `ignitionPrep = FALSE` (`fitPrepNeeded(sim, "fireSense_ignitionFit")`) neither is +#' scheduled, and nor is `prepSpreadFitFireData`, which only they need when the spread covariates are skipped. #' #' @param which character, `P(sim)$whichModulesToPrepare`. -#' @param fitPrep logical; `FALSE` makes only the spread fire data (`prepSpreadFitFireData`), which the ignition and -#' escape data need, not the spread covariates (`fitPrepNeeded()`). +#' @param fitPrep logical; `FALSE` makes no spread covariates (`fitPrepNeeded()`); with `ignitionPrep` it makes the +#' spread fire data (`prepSpreadFitFireData`), which the ignition and escape data need. +#' @param ignitionPrep logical; `FALSE` leaves out the ignition and escape prep (`fitPrepNeeded(sim, "fireSense_ignitionFit")`). #' @return character, event names in the order to schedule them. -prepEventsToSchedule <- function(which, fitPrep = TRUE) { +prepEventsToSchedule <- function(which, fitPrep = TRUE, ignitionPrep = 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.") @@ -1574,24 +1580,34 @@ prepEventsToSchedule <- function(which, fitPrep = TRUE) { 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 && fitPrep) "prepSpreadFitData", - if ("fireSense_spreadFit" %in% which && !fitPrep && "fireSense_ignitionFit" %in% which) "prepSpreadFitFireData", - if ("fireSense_ignitionFit" %in% which) c("prepIgnitionFitData", "prepEscapeFitData")) + if ("fireSense_spreadFit" %in% which && !fitPrep && "fireSense_ignitionFit" %in% which && + ignitionPrep) "prepSpreadFitFireData", + if ("fireSense_ignitionFit" %in% which && ignitionPrep) c("prepIgnitionFitData", "prepEscapeFitData")) } -#' Will a fit, profile or validation read the spread covariates? +#' Will a fit, profile or validation read this module's prep? #' -#' 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. +#' Always, unless `skipUnusedFitPrep` and `fireSenseUtils::fitDataNeeded()` is `FALSE`. For `fireSense_spreadFit` +#' (the spread covariates) it takes this job's `doFit`, `heldOutFold` and that module's `mode`. For +#' `fireSense_ignitionFit` (the ignition and escape data) it takes that module's own `doFit`, resolved as it does +#' (`fireSenseUtils::resolveDoFit()`; NA if the simList has no `fireSense_ignitionFit` parameters); `heldOutFold` and `mode` are the spread fit's and do not apply. +#' 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`. +#' @param module `"fireSense_spreadFit"` or `"fireSense_ignitionFit"`. #' @return logical, length one. -fitPrepNeeded <- function(sim) { +fitPrepNeeded <- function(sim, module = "fireSense_spreadFit") { if (!isTRUE(P(sim)$skipUnusedFitPrep)) return(TRUE) for (nm in c("skipUnusedFitPrep", "doFit")) SpaDES.core::paramCheckOtherMods(sim, nm, ifSetButDifferent = "error") + if (module == "fireSense_ignitionFit") { + ign <- params(sim)[[module]] + return(fireSenseUtils::fitDataNeeded( + fireSenseUtils::resolveDoFit(ign[["doFit"]] %||% NA, ign[["refitExisting"]] %||% NA, module = module))) + } fireSenseUtils::fitDataNeeded(P(sim)$doFit, SpaDES.core::paramCheckOtherMods(sim, "heldOutFold"), - params(sim)[["fireSense_spreadFit"]][["mode"]]) + params(sim)[[module]][["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 cf34d87..8f43ea7 100644 --- a/tests/testthat/test-skipUnusedFitPrep.R +++ b/tests/testthat/test-skipUnusedFitPrep.R @@ -7,15 +7,16 @@ 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) { +prepSim <- function(doFit = FALSE, heldOutFold = NA, spreadFit = NULL, skip = TRUE, ignitionFit = NULL) { 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 <- c(list(skipUnusedFitPrep = skip, doFit = doFit), spreadFit) + if (!is.null(ignitionFit)) sim@params$fireSense_ignitionFit <- ignitionFit sim@current <- data.table::data.table(moduleName = "fireSense_dataPrepFit") sim } -scheduled <- function(sim) prepEventsToSchedule(bothFits, fitPrepNeeded(sim)) +scheduled <- function(sim) prepEventsToSchedule(bothFits, fitPrepNeeded(sim), fitPrepNeeded(sim, "fireSense_ignitionFit")) test_that("by default every prep event is scheduled, as before", { expect_identical(scheduled(prepSim(FALSE, skip = FALSE)), all4) @@ -34,6 +35,27 @@ test_that("skipUnusedFitPrep: a job that may fit, validate, profile or hold out expect_identical(scheduled(prepSim(FALSE, spreadFit = list(mode = "profile"))), all4) }) +test_that("skipUnusedFitPrep: the ignition and escape prep follows fireSense_ignitionFit's own doFit", { + ## ignitionFit reuses its ledger fit: no ignition or escape data, hence no fire data either + expect_length(scheduled(prepSim(FALSE, ignitionFit = list(doFit = FALSE))), 0L) + expect_identical(scheduled(prepSim(TRUE, ignitionFit = list(doFit = TRUE))), all4) + expect_identical(scheduled(prepSim(NA, ignitionFit = list(doFit = NA))), all4) + expect_identical(scheduled(prepSim(FALSE)), fireOnly) # ignitionFit's doFit unset: its default is NA + expect_identical(scheduled(prepSim(FALSE, ignitionFit = list(refitExisting = TRUE))), fireOnly) + ## a held-out fold and the spread mode are the spread fit's: they do not make the ignition fit read its data + expect_identical(scheduled(prepSim(FALSE, heldOutFold = 1L, ignitionFit = list(doFit = FALSE))), + "prepSpreadFitData") + expect_identical(scheduled(prepSim(FALSE, spreadFit = list(mode = "validate"), ignitionFit = list(doFit = FALSE))), + "prepSpreadFitData") + ## not skipping: unchanged + expect_identical(scheduled(prepSim(FALSE, skip = FALSE, ignitionFit = list(doFit = FALSE))), all4) +}) + +test_that("a project that prepares only the ignition fit and skips its prep schedules nothing", { + expect_length(prepEventsToSchedule("fireSense_ignitionFit", FALSE, FALSE), 0L) + expect_length(prepEventsToSchedule(bothFits, TRUE, FALSE), 1L) +}) + test_that("a project that prepares only the spread fit schedules nothing when it only predicts", { expect_length(prepEventsToSchedule("fireSense_spreadFit", FALSE), 0L) })