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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# fireSense_dataPrepFit 1.3.0.9011 (development version)

* New parameter `spreadClimateCurve` (`FALSE` by default: nothing changes). `TRUE` adds a covariate `<C>_curve = softplus(C - k, s)` (smooth, no corner) for each spread climate variable `C`, with its own coefficient, so the climate response can steepen in dry years; `k` and `s` are the ELF's median and sd of `C` over the burnable pixels of all the fit years. They are in `sim$climateCurvePars`, which `fireSense_spreadFit` writes to the ledger row, and `fireSense_spreadPredict` builds the term from them. A negative coefficient flattens the dry side. A reused ledger fit keeps its own curve terms and their `k` and `s`. The code is `fireSenseUtils::climateCurvePars()`, `addClimateCurve()`, `softplus()` and `climateTermCols()`, in the `prepSpreadFitData` cache key. Needs `fireSense_spreadFit`, `fireSense_spreadPredict` and fireSenseUtils with the curve (fireSenseUtils >= 0.2.3.9196).

* No prescribed burn is fitted: the spread fit's fire polygons and the ignition fit's NFDB points are selected by `fireSenseUtils::modelledFires()` (the causes of `fireCauses`, and no NBAC `PRESCRIBED`, NFDB `PRESCRIBED`, `FIRE_TYPE` or `H-PB` fire). It replaces the module's `polysOfCauses()`. Needs fireSenseUtils with `modelledFires()`; spread and ignition fits made before this included the prescribed fires of the wanted causes.

* `spreadCovariates` takes two more values, `"climate"` (the intercept and the spread climate variable(s) only) and `"fuel"` (the intercept, `youngAge`, fuel and non-forest classes), beside `"all"` (default) and `"none"`, for an ANOVA of what drives spread-fit quality. The covariate tables are still made. `spreadInteractions = "climate:fuel"` stops with `"fuel"` and `"climate"` (it needs both). The default is unchanged.
Expand Down
6 changes: 5 additions & 1 deletion R/spreadFormula.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,15 @@ spreadInteractionClimateFuel <- "climate:fuel"
#' @param spreadInteractions the module's `spreadInteractions`; `NULL` or `character(0)` for none.
#' @param climateSplit the module's `spreadClimateSplit`: `TRUE` replaces each climate variable `C` by `C_yr` and
#' `C_dev` (`fireSenseUtils::climateSplitCols()`).
#' @param climateCurve `TRUE` adds each variable's curve term (`C_curve`, `fireSenseUtils::climateTermCols()`) after its
#' other climate terms; it does not interact.
#' @return a character string, or `character()` for the null model.
spreadRHS <- function(spreadCovariates, spreadVars, vegColsForRHS, spreadInteractions = character(), climateSplit = FALSE) {
spreadRHS <- function(spreadCovariates, spreadVars, vegColsForRHS, spreadInteractions = character(), climateSplit = FALSE,
climateCurve = FALSE) {
if (identical(spreadCovariates, "none")) return(character())
fuel <- if (spreadInteractionClimateFuel %in% spreadInteractions) fireSenseUtils::spreadFuelCols(vegColsForRHS)
climCols <- if (isTRUE(climateSplit)) fireSenseUtils::climateSplitCols(spreadVars) else spreadVars
if (isTRUE(climateCurve)) climCols <- c(climCols, fireSenseUtils::climateTermCols(spreadVars, fireSenseUtils::climateCurveSuffix))
## a split climate variable interacts through its between-year term only, never `_dev`
interClim <- if (isTRUE(climateSplit)) paste0(spreadVars, fireSenseUtils::climateSplitYrSuffix) else spreadVars
climateTerms <- if (!identical(spreadCovariates, "fuel")) paste0(climCols, collapse = " + ")
Expand Down
67 changes: 57 additions & 10 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.9190)", # 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.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
"FOR-CAST/fireregimetools@main (>= 0.1.0.9008)",
"ggplot2", "methods", "parallel", "purrr", "raster", "sf", "sp", "stats", "utils",
"PredictiveEcology/LandR@development (>= 1.2.0.9015)",
Expand Down Expand Up @@ -217,6 +217,17 @@ defineModule(sim, list(
"`fireSense_spreadPredict` computes the same value from a year's climate layer",
"(`fireSenseUtils::splitSpreadClimate()`). `CMD*` and `MDC*` terms are bounded below by 0, as before; others",
"are not. Ignored if `sim$fireSense_spreadFormula` is supplied. A prediction needs no setting: `fireSense_spreadPredict` reads the terms from the ledger row. A ledger fit that is reused (`doFit` FALSE) keeps its own split, whatever this says.")),
defineParameter("spreadClimateCurve", "logical", FALSE, NA, NA,
paste("`FALSE` (the default): the spread formula is unchanged. `TRUE`: it has an extra term for each spread",
"climate variable `C`, `<C>_curve = softplus(C - k, s)` (`fireSenseUtils::softplus()`: smooth, no corner), with its own coefficient, so the climate response",
"can steepen above `k` when the coefficient is positive; a negative one flattens the dry side (the sign is free). `k` and `s` are the median and sd of `C`",
"can steepen (or flatten) above `k` (dry years); `C` keeps its own term. `k` is the ELF's median of `C`",
"over the burnable pixels in all the fit's years, one number for the ELF. It is stored in the ledger row",
"(`fireSenseUtils::spreadFitClimateCurveParsTxt`), and `fireSense_spreadPredict` builds the term from it",
"(`fireSenseUtils::addClimateCurve()`). With `spreadClimateSplit` the curve is of `C`, the pixel's value,",
"beside the `_yr` and `_dev` terms. Ignored if `sim$fireSense_spreadFormula` is supplied. A prediction needs no",
"setting: it reads `k` and `s` from the ledger row. A ledger fit that is reused (`doFit` FALSE) keeps its own",
"curve terms and their `k` and `s`, whatever this says. Needs a `fireSense_spreadFit` that writes them.")),
defineParameter("targetFuelClasses", "numeric", 5, 1, 7,
"the target number of unique fuel classes when using semi-automated approach"),
defineParameter("useRasterizedFireForSpread", "logical", FALSE, NA, NA,
Expand Down Expand Up @@ -264,7 +275,8 @@ defineModule(sim, list(
fireSenseUtils::rasterFireSpreadPoints, spreadFitPreRun = sim$spreadFitPreRun),
fireSenseUtils::harmonizeFireDataDeps(), fireSenseUtils::escapeFireSetDeps()))),
prepSpreadFitData = list(.cacheExtra = quote(c(list(
fireSenseUtils::bufferToArea, fireSenseUtils::chooseDomSecFuelClasses,
fireSenseUtils::addClimateCurve, fireSenseUtils::bufferToArea, fireSenseUtils::chooseDomSecFuelClasses,
fireSenseUtils::climateCurvePars, fireSenseUtils::climateTermCols, fireSenseUtils::softplus,
fireSenseUtils::climateRasterToDataTable, fireSenseUtils::climateSplitCols, fireSenseUtils::climateSplitRefPixels,
fireSenseUtils::dataYearBefore,
fireSenseUtils::dataYearElement,
Expand Down Expand Up @@ -416,6 +428,10 @@ defineModule(sim, list(
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"),
createsOutput("climateCurvePars", "list",
paste("With `spreadClimateCurve`, `k` and `s` of each spread climate variable's curve term (`fireSenseUtils::climateCurvePars()`),",
"which `fireSense_spreadFit` stores in the ledger row; `NULL` without curve terms."),
events = "prepSpreadFitData"),
createsOutput("fireSense_escapeCovariates", "data.table",
"ignition covariates with added column of escapes",
events = "prepEscapeFitData"),
Expand Down Expand Up @@ -682,6 +698,11 @@ Init <- function(sim) {
## a reused fit keeps its split (or not): the formula follows the ledger row's coefficient names, not `spreadClimateSplit`
mod$spreadClimateSplit <- if (reuseOwnFit)
any(fireSenseUtils::isClimateSplitCol(colnames(sim$spreadFitPreRun$params[[ownRow]])))
## a reused fit keeps its curve terms, and the `k` and `s` it was fitted with
mod$spreadClimateCurve <- if (reuseOwnFit)
any(fireSenseUtils::isClimateTermCol(colnames(sim$spreadFitPreRun$params[[ownRow]]), fireSenseUtils::climateCurveSuffix))
mod$climateCurvePars <- if (isTRUE(mod$spreadClimateCurve))
fireSenseUtils::ledgerClimateCurvePars(sim$spreadFitPreRun, ownRow)
## spread: the union, for prediction; ignition (xgboost) can use every one of them
sim$climateVariablesForFire <- addFitClimateVariablesForFire(sim$climateVariablesForFire, fitClimVars)

Expand Down Expand Up @@ -1090,7 +1111,8 @@ prepare_SpreadFit <- function(sim) {
vegColsForRHS <- setdiff(vegCols, c(youngAgeTxt, spreadVars))
## a reused fit's split (mod$spreadClimateSplit) can differ from the parameter
checkSpreadInteractions(Par$spreadInteractions, Par$spreadCovariates)
RHS <- spreadRHS(Par$spreadCovariates, spreadVars, vegColsForRHS, Par$spreadInteractions, useClimateSplit(mod, Par))
RHS <- spreadRHS(Par$spreadCovariates, spreadVars, vegColsForRHS, Par$spreadInteractions, useClimateSplit(mod, Par),
climateCurve = useClimateCurve(mod, Par))

## this is a funny way to get years but avoids years with 0 fires
allYears <- unname(unlist(mod$allYears))
Expand Down Expand Up @@ -1119,7 +1141,9 @@ prepare_SpreadFit <- function(sim) {
annualCovariates <- annualSpreadCovariates(
pixelsByYear, spreadClimate, mod$allYears, burnablePixels,
tsds = sim$nonForest_timeSinceDisturbances, firePixelsByYear = firePixels,
cutoffForYoungAge = P(sim)$cutoffForYoungAge, climateSplit = useClimateSplit(mod, Par))
cutoffForYoungAge = P(sim)$cutoffForYoungAge, climateSplit = useClimateSplit(mod, Par),
climateCurve = if (useClimateCurve(mod, Par)) mod$climateCurvePars %||% TRUE else FALSE)
sim$climateCurvePars <- attr(annualCovariates, fireSenseUtils::spreadFitClimateCurveParsTxt)

# Confirm that YoungAge is the right amount. If there are "normal" amount of fires,
# then it should be < 0.2
Expand Down Expand Up @@ -1166,6 +1190,15 @@ prepare_SpreadFit <- function(sim) {
#' @return logical(1).
useClimateSplit <- function(mod, Par) isTRUE(mod$spreadClimateSplit %||% Par$spreadClimateSplit)

#' Does the spread climate have a curve term?
#'
#' A fit reused from the ledger (`mod$spreadClimateCurve`, set by `Init()` from the row's coefficient names) keeps
#' its own curve terms; otherwise the `spreadClimateCurve` parameter decides.
#'
#' @param mod,Par the module's `mod` and `Par`.
#' @return logical(1).
useClimateCurve <- function(mod, Par) isTRUE(mod$spreadClimateCurve %||% Par$spreadClimateCurve)

#' The annual spread covariates of the given pixels
#'
#' Climate of each fire year's pixels, then `youngAge`, grouped by data year. `prepare_SpreadFit()` calls it
Expand All @@ -1179,10 +1212,14 @@ useClimateSplit <- function(mod, Par) isTRUE(mod$spreadClimateSplit %||% Par$spr
#' @param tsds,firePixelsByYear,cutoffForYoungAge see [addAnnualYoungAge()].
#' @param climateSplit logical; `TRUE` replaces each climate variable `C` by `C_yr`, the year's median of `C` over
#' the burnable pixels, and `C_dev`, `C` minus it ([fireSenseUtils::splitSpreadClimate()]).
#' @param climateCurve `FALSE` (none); `TRUE`, which adds `C_curve = softplus(C - k, s)` for each climate variable `C`, with `k` and `s`
#' the median and sd of `C` over the burnable pixels of all the years ([fireSenseUtils::climateCurvePars()]); or the
#' `c(k = , s = )` of each variable (a named list) of a reused fit, used as they are ([fireSenseUtils::addClimateCurve()]). `C` stays.
#' @return list named by data year of lists named `year<year>` (every year of `allYears`, empty if no pixels)
#' of data.tables of `pixelID`, the climate variables (or their two terms) and `youngAge`.
#' of data.tables of `pixelID`, the climate variables (or their terms) and `youngAge`. With `climateCurve` its attribute
#' `fireSenseUtils::spreadFitClimateCurveParsTxt` is `k` and `s` of each variable.
annualSpreadCovariates <- function(pixelsByYear, spreadClimate, allYears, burnablePixels, tsds,
firePixelsByYear, cutoffForYoungAge, climateSplit = FALSE) {
firePixelsByYear, cutoffForYoungAge, climateSplit = FALSE, climateCurve = FALSE) {
## only these pixels of each year are read from the rasters; the join below keeps no others
climateAt <- function(Index)
climateRasterToDataTable(historicalClimateRasters = spreadClimate, Index = Index) |>
Expand All @@ -1193,11 +1230,19 @@ annualSpreadCovariates <- function(pixelsByYear, spreadClimate, allYears, burnab
rmCols <- setdiff(colnames(fbl), c("pixelID", fireSenseUtils::yearTxt))
set(fbl, NULL, rmCols, NULL)
fbl <- climateDT[fbl, on = c(fireSenseUtils::yearTxt, "pixelID"), nomatch = NULL]
if (isTRUE(climateSplit)) {
useCurve <- !isFALSE(climateCurve)
if (isTRUE(climateSplit) || isTRUE(climateCurve)) {
## the median is over each year's burnable pixels of the ELF (what a prediction has), not over the fitted pixels
burnableIndex <- lapply(burnablePixels[names(pixelsByYear)], function(p) data.table(pixelID = fireSenseUtils::climateSplitRefPixels(p)))
fireSenseUtils::splitSpreadClimate(fbl, names(spreadClimate), climateAt(burnableIndex), by = fireSenseUtils::yearTxt)
burnableClimate <- climateAt(burnableIndex)
}
## the curve is of the pixel's own value, so it is made before the split removes the variable
pars <- if (useCurve) {
if (isTRUE(climateCurve)) fireSenseUtils::climateCurvePars(burnableClimate, names(spreadClimate)) else climateCurve
}
if (useCurve) fireSenseUtils::addClimateCurve(fbl, pars)
if (isTRUE(climateSplit))
fireSenseUtils::splitSpreadClimate(fbl, names(spreadClimate), burnableClimate, by = fireSenseUtils::yearTxt)

annuals <- split(fbl, by = fireSenseUtils::yearTxt, keep.by = FALSE)

Expand All @@ -1217,8 +1262,10 @@ annualSpreadCovariates <- function(pixelsByYear, spreadClimate, allYears, burnab
## youngAge of each fire year's pixels: the data year's time since disturbance aged to that year,
## reset by every fire since (all fires, not only those fitted). spreadFit clears fuel, non-forest
## land cover and treed wetland where it is 1, after joining these annual tables to the fuels.
addAnnualYoungAge(annualCovariates, tsds = tsds, firePixelsByYear = firePixelsByYear,
cutoffForYoungAge = cutoffForYoungAge)
annualCovariates <- addAnnualYoungAge(annualCovariates, tsds = tsds, firePixelsByYear = firePixelsByYear,
cutoffForYoungAge = cutoffForYoungAge)
if (useCurve) attr(annualCovariates, fireSenseUtils::spreadFitClimateCurveParsTxt) <- pars
annualCovariates
}

#' The non-annual (fuel) spread covariates of one data year
Expand Down
32 changes: 32 additions & 0 deletions tests/testthat/helper-spreadFixtures.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
## Fixtures shared by several test files: test files do not see each other's functions, helpers are sourced into all.

## the whole-landscape fixture (test-wholeLandscapeSpreadCovariates.R, test-spreadClimateCurve.R)
wholeFixture <- function() {
r <- terra::rast(nrows = 4, ncols = 5)
years <- 2001:2004
clim <- list(CMD = terra::rast(lapply(years, function(y) { x <- r; terra::values(x) <- seq_len(20) + y; x })))
names(clim$CMD) <- paste0("year", years)
tsd <- function(v) { x <- r; terra::values(x) <- v; x }
tsds <- list(year2000 = tsd(c(rep(100, 19), 3)), year2002 = tsd(rep(100, 20)))
allYears <- list(`2000` = c("year2001", "year2002"), `2002` = c("year2003", "year2004"))
burnable <- seq_len(16) # pixels 17:20 cannot burn
burnablePixels <- setNames(rep(list(burnable), 4), unlist(allYears))
buffers <- list(year2001 = data.table::data.table(pixelID = c(2L, 3L, 5L)),
year2002 = data.table::data.table(pixelID = integer(0)), # a year without fire
year2003 = data.table::data.table(pixelID = c(3L, 16L, 17L)),
year2004 = data.table::data.table(pixelID = c(7L, 8L)))
list(clim = clim, tsds = tsds, allYears = allYears, burnablePixels = burnablePixels, buffers = buffers,
fires = list(`2003` = 9L))
}

## a ledger with this ELF (4.2.2, CMD) and a neighbour (4.2.1, CMDsm) (test-spreadClimateOwnELF.R, test-spreadClimateCurve.R)
ownAndNeighbourLedger <- function() {
own <- oneELFLedger() # 4.2.2, fitted with CMD
nb <- oneELFLedger()
nb[[fireSenseUtils::polygonIDTxt]] <- "4.2.1"
nbPars <- own$params[[1]]
names(nbPars)[names(nbPars) == "CMD"] <- "CMDsm"
ledger <- rbind(nb, own)
ledger$params <- list(nbPars, own$params[[1]])
ledger
}
12 changes: 12 additions & 0 deletions tests/testthat/test-annualYoungAge.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ test_that("addAnnualYoungAge puts youngAge in each fire year's table, from the d
expect_true(all(c("pixelID", "CMD") %in% names(out$year2004)))
})

test_that("addAnnualYoungAge returns the list it was given, its tables changed in place (annualSpreadCovariates() relies on it)", {
tsds <- list(year2000 = tsdRaster(c(100, 3, 100, 100)))
tab <- data.table::data.table(pixelID = 1:3, CMD = 1)
ac <- list(`2000` = list(year2001 = tab))
out <- addAnnualYoungAge(ac, tsds, list(), 15)
expect_identical(names(out), names(ac))
expect_identical(names(out[["2000"]]), names(ac[["2000"]]))
expect_true("youngAge" %in% names(tab)) # the given table was changed by reference
expect_identical(data.table::address(out[["2000"]]$year2001), data.table::address(tab))
expect_identical(out, ac)
})

test_that("addAnnualYoungAge keeps the empty table of a year without fires", {
tsds <- list(year2000 = tsdRaster(c(100, 3, 100, 100)))
ac <- list(`2000` = list(year2001 = data.table::data.table(pixelID = 1:3, CMD = 1),
Expand Down
Loading