Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# fireSense_dataPrepFit 1.3.0.9011 (development version)

* The SpreadFit ledger is read with `fireSenseUtils::readFitLedger()`: with `doFit` FALSE and a `spreadFitFilename` that is missing or has no rows, the newest production fits (`"latest"`) are read, so a run that only reads a fit needs no ledger of its own. `doFit` is resolved with `fireSenseUtils::moduleDoFit()` (the deprecated aliases count) where the module reads it. Needs fireSenseUtils >= 0.2.3.9202.
* `ageDisturbanceSources = "all"` changes with LandR's rule (Pre-CanLaD or NFDB before 1972; Pre-CanLaD 1972-1984; CanLaD or NTEMS, or NFDB and NBAC agreeing, from 1985), for the per-year stand ages and `disturbancePixelsByYear`, through `LandR::prepInputsDisturbanceYear()`. The parameter also takes record names (any of `"NFDB"`, `"NBAC"`, `"CanLaD"`, `"NTEMS"`, `"PreCanLaD"`). The default `"NFDB"` is unchanged. Needs LandR >= 1.2.0.9069.
* Biomass_borealDataPrep now gets the stand age map and land cover of its own `dataYear` from `dataPrepBuild` (`vegStartYearName()`), not those of the last of `dataYears`. A run whose vegetation starts before the last data year (the attribution hindcasts, `dataYear` 1985) got the 2020 stand ages on 1985 biomass and species: stands burned in 1980-1985 started at about 38 years, and stands that burn in 1986-2020 at about 11. Fits, hindcast validation and NRV runs are unchanged: they read the per-year lists, or start from the last data year.
* The per-year stand age maps (`standAgeMaps`, which the fits' vegetation and the hindcasts use) reset age at the last stand-replacing disturbance from `LandR::prepInputsDisturbanceYear()` (NFDB, NBAC and CanLaD fire and harvest) with the new parameter `ageDisturbanceSources = "all"` (opt in; same parameter as Biomass_borealDataPrep's, checked to match, so set it in `.globals`), so they match the simulation's own starting ages; `youngAge` then follows the same fires and cuts (new `disturbancePixelsByYear`). It is this module's own parameter because a hindcast runs without Biomass_borealDataPrep. With `"NFDB"` (the default) nothing changes. Opting in changes the vegetation the fits are made with.
Expand Down
26 changes: 14 additions & 12 deletions fireSense_dataPrepFit.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.9196)", # 9196 (fireSenseUtils#265): climateCurvePars(), addClimateCurve(), softplus(), climateTermCols(), isClimateTermCol(), ledgerClimateCurvePars(); 9190: modelledFires() (fireSenseUtils#261; set to the real version after merge); 9186: splitSpreadClimate(), climateSplitCols(), climateSplitRefPixels(), isClimateSplitCol() (fireSenseUtils#244); 9184: addCoreFraction(), coreFractionByYear() (moved from this module); 9179: readSpreadFitLedger(escapeDefinition); 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
"PredictiveEcology/fireSenseUtils@development (>= 0.2.3.9202)", # 9202: readFitLedger(), moduleDoFit(); readSpreadFitLedger() is gone (fireSenseUtils#273); 9196 (fireSenseUtils#265): climateCurvePars(), addClimateCurve(), softplus(), climateTermCols(), isClimateTermCol(), ledgerClimateCurvePars(); 9190: modelledFires() (fireSenseUtils#261; set to the real version after merge); 9186: splitSpreadClimate(), climateSplitCols(), climateSplitRefPixels(), isClimateSplitCol() (fireSenseUtils#244); 9184: addCoreFraction(), coreFractionByYear() (moved from this module); 9179: readSpreadFitLedger(escapeDefinition), since replaced by readFitLedger(); 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.9069)", # prepInputsDisturbanceYear(sources)
Expand Down Expand Up @@ -150,7 +150,8 @@ defineModule(sim, list(
"`Init()` stops if `fireSense_spreadFit` has a different value. Any other value is an error.")),
defineParameter("doFit", "logical", default = FALSE,
desc = paste("The same parameter as in `fireSense_spreadFit`: FALSE (default) never fits, and `Init()`",
"stops, before any data preparation, when the SpreadFit ledger (`spreadFitFilename`) has no",
"stops, before any data preparation, when the SpreadFit ledger (`spreadFitFilename`, or the newest production fits when",
"that file is missing or empty) has no",
"fit for this ELF; NA fits only an ELF without a ledger fit; TRUE always refits. When",
"this ELF's ledger fit is reused (FALSE, or NA with a ledger row), the spread formula",
"takes that fit's climate variable(s); when it is fitted, `climateVariablesForFire$spread`",
Expand Down Expand Up @@ -195,7 +196,8 @@ defineModule(sim, list(
NA, NA, paste("Name of the ledger file in `spreadFitGoogleDriveFolder`: study area polygons with",
"their fitted SpreadFit parameters. `\"latest\"` (the default) takes each polygon's newest",
"fit this run can use, from any ledger file (`fireSenseUtils::latestSpreadFits()`); a name (e.g. an",
"experiment's) reads that file only.")),
"experiment's) reads that file only. With `doFit` FALSE, a file that is missing or has no rows reads \"latest\"",
"from the production folder, so a run that only reads a fit needs no ledger of its own.")),
defineParameter("spreadCovariates", "character", "all", NA, NA,
paste("`\"all\"` (the default): the spread formula has the covariates (climate, `youngAge`, fuel,",
"non-forest). `\"none\"`: the null model, the formula `~ 1` (an intercept and no covariate), the",
Expand Down Expand Up @@ -645,10 +647,11 @@ Init <- function(sim) {
if (!isTRUE(is.na(heldOutFold)) && !(length(heldOutFold) == 1L && heldOutFold %in% 1:2))
stop("fireSense_dataPrepFit: parameter 'heldOutFold' must be NA, 1L or 2L; got: ",
paste(format(heldOutFold), collapse = ", "))
doFit <- fireSenseUtils::moduleDoFit(Par, SpaDES.core::currentModule(sim))
sim$spreadFitPreRun <- if (isTRUE(is.na(heldOutFold))) {
readSpreadFitLedger(Par$spreadFitFilename, Par$spreadFitGoogleDriveFolder,
domain = sa, destinationPath = inputPath(sim),
escapeDefinition = fireSenseUtils::escapeDefinitionOf(sim))
readFitLedger("spread", Par$spreadFitFilename, Par$spreadFitGoogleDriveFolder,
domain = sa, destinationPath = inputPath(sim), doFit = doFit,
escapeDefinition = fireSenseUtils::escapeDefinitionOf(sim))
}
mod$haveSpreadFit <- is(sim$spreadFitPreRun, "sf") || is(sim$spreadFitPreRun, "data.frame")
## the year Biomass_borealDataPrep builds the simulation's starting vegetation for; set here, not in the
Expand All @@ -659,7 +662,7 @@ Init <- function(sim) {
## Over a studyArea_fireSense, `.ELFind` names several ELFs (fireSense_ELFs), and each needs its fit.
if (isTRUE(is.na(heldOutFold))) {
ids <- as.character(sim$.ELFind)
fireSenseUtils::mustFit(Par$doFit,
fireSenseUtils::mustFit(doFit,
mod$haveSpreadFit && length(ids) > 0L && !anyNA(ids) &&
all(ids %in% as.character(sim$spreadFitPreRun[[fireSenseUtils::polygonIDTxt]])),
paste(ids, collapse = ", "))
Expand Down Expand Up @@ -728,7 +731,7 @@ Init <- function(sim) {
## This ELF's spread formula takes only its own variable(s): those of its ledger fit when that fit is
## reused, otherwise the requested ones ("auto" chooses in dataPrepBuild). Not the union: a neighbour's
## CMDsm beside this ELF's CMD gave 6.2.2 two near-collinear terms (2026-10-21).
reuseOwnFit <- !is.na(ownRow) && !isTRUE(Par$doFit)
reuseOwnFit <- !is.na(ownRow) && !isTRUE(doFit)
mod$spreadFormulaClimate <- if (reuseOwnFit) outs2[[ownRow]][["theseClimVarsNoUnderscore"]] else
sim$climateVariablesForFire[["spread"]]
## a reused fit keeps its split (or not): the formula follows the ledger row's coefficient names, not `spreadClimateSplit`
Expand Down Expand Up @@ -1731,7 +1734,7 @@ prepEventsToSchedule <- function(which, fitPrep = TRUE, ignitionPrep = TRUE) {
#' 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.
#' (`fireSenseUtils::moduleDoFit()`; 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.
#'
Expand All @@ -1744,10 +1747,9 @@ fitPrepNeeded <- function(sim, module = "fireSense_spreadFit") {
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)))
return(fireSenseUtils::fitDataNeeded(if (is.null(ign)) NA else fireSenseUtils::moduleDoFit(ign, module)))
}
fireSenseUtils::fitDataNeeded(P(sim)$doFit, SpaDES.core::paramCheckOtherMods(sim, "heldOutFold"),
fireSenseUtils::fitDataNeeded(fireSenseUtils::moduleDoFit(P(sim), SpaDES.core::currentModule(sim)), SpaDES.core::paramCheckOtherMods(sim, "heldOutFold"),
params(sim)[[module]][["mode"]])
}

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/helper-ledgerInit.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ ledgerInitEnv <- function(ledger) {
spreadCovariates = "all", spreadIntercept = TRUE)
e$nonEscapedFireSizes <- function(...) NULL
e$inputPath <- function(sim) tempdir()
e$readSpreadFitLedger <- function(...) ledger
e$readFitLedger <- function(...) ledger
e$addFitClimateVariables <- function(climateVariables, ...) climateVariables
e
}
Expand Down
11 changes: 10 additions & 1 deletion tests/testthat/test-heldOutFold.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ initEnv <- function(ownFold, otherFold = NULL, ledgerCalls = NULL) {
e$nonEscapedFireSizes <- function(...) NULL
e$inputPath <- function(sim) tempdir()
e$ledgerCalls <- 0L
e$readSpreadFitLedger <- function(...) {
e$readFitLedger <- function(...) {
e$ledgerArgs <- list(...)
e$ledgerCalls <- e$ledgerCalls + 1L
NULL
}
Expand Down Expand Up @@ -54,6 +55,14 @@ test_that("with heldOutFold NA the SpreadFit ledger is read as before", {
expect_identical(e$ledgerCalls, 1L)
})

test_that("the SpreadFit ledger is read with readFitLedger('spread') and the run's doFit", {
e <- initEnv(NA, otherFold = NA)
e$Par$doFit <- FALSE
suppressMessages(runInitStatements(e))
expect_identical(e$ledgerArgs[[1]], "spread")
expect_false(e$ledgerArgs$doFit)
})

test_that("a heldOutFold that is not NA, 1 or 2 is an error", {
e <- initEnv(3L, otherFold = 3L)
expect_error(suppressMessages(runInitStatements(e)), "heldOutFold")
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-ledgerNotInInputObjectsKey.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ test_that("a ledger differing only in another ELF's rows gives the same Init res
value <- 1
testthat::local_mocked_bindings(latestSpreadFits = function(...) mk(value), .package = "fireSenseUtils")
domain <- sf::st_as_sf(sq(4e4)) # 4.3
first <- suppressMessages(readSpreadFitLedger("latest", "folder", domain, d))
first <- suppressMessages(readFitLedger("spread", "latest", "folder", domain, d))
value <- 99 # ELF 4.1 refitted
second <- suppressMessages(readSpreadFitLedger("latest", "folder", domain, d))
second <- suppressMessages(readFitLedger("spread", "latest", "folder", domain, d))
expect_identical(digest::digest(first), digest::digest(second))
})