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_dataPrepPredict (development version)

* `fireSense_igAndEscapePred_Covariates` has a `coreFraction` column, the share of each coarse pixel's flammable cells that are core (`fireSenseUtils::addCoreFraction()` on `flammableRTM` and `rasterToMatchLargeELF`, with `igAggFactor`, which is forced to the fit module's value), as `fireSense_dataPrepFit` gives it to the fit. `fireSense_ignitionPredict`'s year climate term averages the core pixels only, as the fit does; without the column it would average the buffer too. It does not depend on the year, so it is kept in the module and remade only when `flammableRTM`, `rasterToMatchLargeELF` or `igAggFactor` change. A pixel with no flammable cell gets 0 with one warning. A multi-ELF categorical raster or none gives 1. Needs fireSenseUtils (>= 0.2.3.9183).
* `fitFileSuffix` is removed: in a run without `fireSense_dataPrepFit`, name the ledger to predict from in full (`spreadFitFilename`); `"latest"` takes each ELF's newest usable fit from any ledger file. The stop for a ledger with no fit for the study area names `spreadFitFilename`.
* `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.

Expand Down
19 changes: 17 additions & 2 deletions fireSense_dataPrepPredict.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ defineModule(sim, list(
"PredictiveEcology/climateData@development", # latestHistoricalYear()
"data.table",
"googledrive",
"PredictiveEcology/fireSenseUtils@development (>= 0.2.3.9176)", # 9142: readSpreadFitLedger() and the other predict-only helpers (#206); fuelCovsToCoarse() hands back the static it used (#191)
"PredictiveEcology/fireSenseUtils@development (>= 0.2.3.9184)", # 9184: addCoreFraction(), coreFractionByYear(); 9142: readSpreadFitLedger() and the other predict-only helpers (#206); fuelCovsToCoarse() hands back the static it used (#191)
"httr2",
"PredictiveEcology/LandR@development",
"PredictiveEcology/reproducible@development",
Expand Down Expand Up @@ -276,7 +276,9 @@ defineModule(sim, list(
createsOutput("fireSense_igAndEscapePred_Covariates", "data.table",
desc = paste(
"Ignition and escape covariates at the aggregated (`igAggFactor`) resolution;",
"`pixelID` is the cell index of the aggregated raster.")),
"`pixelID` is the cell index of the aggregated raster. `coreFraction` is the share of the pixel's flammable",
"cells in the core of `rasterToMatchLargeELF` (`fireSenseUtils::addCoreFraction()`, as `fireSense_dataPrepFit`",
"gives it to the fit); it is an exposure, not a covariate.")),
createsOutput("fireSense_SpreadCovariates", "data.table",
desc = "Spread covariates; `pixelID` is the cell index of `flammableRTM`."),
createsOutput("nonForest_timeSinceDisturbance", "SpatRaster",
Expand Down Expand Up @@ -702,6 +704,19 @@ prepare_IgnitionAndEscapePredict <- function(sim) {
useCache = FALSE)

set(sim$fireSense_igAndEscapePred_Covariates, NULL, "ignitions", NULL)
## the share of each coarse pixel's flammable cells that are core, as fireSense_dataPrepFit gives it to the fit
## (same function, same `igAggFactor`: forced to the fit module's value above): the year climate term of
## fireSense_ignitionPredict averages the core pixels only, as the fit does. It depends on the flammable cells, the
## core raster and `igAggFactor`, not on the year, so it is kept in `mod` and remade when any of them changes (as
## `mod$fuelCovsStatic`); the year label is set each year. A pixel with no flammable cell gets 0, with a warning.
coreKey <- fireSenseUtils::coreFractionKey(list(sim$flammableRTM), sim$rasterToMatchLargeELF, P(sim)$igAggFactor)
coreYears <- list(yrLab) |> setNames(time(sim))
if (!identical(mod$coreFraction$key, coreKey))
mod$coreFraction <- list(key = coreKey, fractions = fireSenseUtils::coreFractionByYear(
coreYears, list(sim$flammableRTM), sim$rasterToMatchLargeELF, P(sim)$igAggFactor))
coreFractions <- copy(mod$coreFraction$fractions)
set(coreFractions, NULL, fireSenseUtils::yearTxt, as.numeric(gsub(fireSenseUtils::yearTxt, "", yrLab)))
fireSenseUtils::addCoreFraction(sim$fireSense_igAndEscapePred_Covariates, onMissing = "zero", fractions = coreFractions)
return(invisible(sim))
}

Expand Down
20 changes: 19 additions & 1 deletion tests/testthat/test-covariates.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ test_that("prepIgAndEscPredictData aggregates to the igAggFactor grid", {
## the year column carries time(sim), not the layer index
expect_identical(unique(df$year), 2001)
expect_setequal(names(df), c("pixelID", "MDC", "youngAge", "nfLCC_wetland", "nfLCC_grass",
"class1", "class2", "year", "lightningDays"))
"class1", "class2", "year", "lightningDays", "coreFraction"))
## `ignitions` belongs to the Fit table and is dropped here
expect_false("ignitions" %in% names(df))

Expand All @@ -95,6 +95,24 @@ test_that("prepIgAndEscPredictData aggregates to the igAggFactor grid", {
expect_equal(df$nfLCC_wetland, c(0, 0, 0.5, 0))
})

test_that("coreFraction is the share of each coarse pixel's flammable cells in the core, as the fit gets it", {
## no rasterToMatchLargeELF: everything is core
expect_equal(covDF(toyPrepRun()$fireSense_igAndEscapePred_Covariates)$coreFraction, rep(1, 4))
## core (2) in fine columns 1-2, buffer (1) in 3-4: coarse pixels 1 and 3 are core, 2 and 4 buffer
## (4 also holds the non-flammable cell 16, which does not count)
o <- toyObjects()
o$rasterToMatchLargeELF <- toyRast(rep(c(2, 2, 1, 1), 4))
df <- covDF(toyPrepRun(o)$fireSense_igAndEscapePred_Covariates)
expect_equal(df$coreFraction, c(1, 0, 1, 0))
## the yearly mean the year climate term uses is over the core pixels (1 and 3: MDC 103.5, 111.5), not all four,
## and is what the fit's own table gives: the same table with coreFraction added as fireSense_dataPrepFit does
fitStyle <- data.table::as.data.table(df[, setdiff(names(df), "coreFraction")])
fitStyle <- fireSenseUtils::addCoreFraction(fitStyle, list(`2001` = "year2001"), list(o$flammableRTM),
o$rasterToMatchLargeELF, fact = 2)
expect_equal(fireSenseUtils::yearClimateMeans(df, "MDC")$mean, 107.5)
expect_equal(fireSenseUtils::yearClimateMeans(fitStyle, "MDC"), fireSenseUtils::yearClimateMeans(df, "MDC"))
})

test_that("igAggFactor 1 leaves the ignition covariates at flammableRTM resolution", {
sim <- toyPrepRun(params = list(igAggFactor = 1))
df <- covDF(sim$fireSense_igAndEscapePred_Covariates)
Expand Down
39 changes: 39 additions & 0 deletions tests/testthat/test-covariatesOncePerYear.R
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,42 @@ test_that("a change of the simulated ELF rebuilds", {
r <- stepsAfterChange(toyObjects(), function(sim) { sim$.ELFind <- "7"; sim }, "fireSense_spreadPredict")
expect_identical(r$n, 1L)
})

## coreFraction does not depend on the year: made once, remade when the flammable cells or the core raster change
countCoreFractionCalls <- function(code) {
n <- 0L
orig <- fireSenseUtils::coreFractionByYear
testthat::local_mocked_bindings(
coreFractionByYear = function(...) { n <<- n + 1L; orig(...) },
.package = "fireSenseUtils")
force(code)
n
}

test_that("coreFraction is made once over several years, and has each year's label", {
o <- toyObjects(); o$rasterToMatchLargeELF <- toyRast(rep(c(2, 2, 1, 1), 4))
n <- countCoreFractionCalls(sim <- toyPrepRun(o, times = list(start = 2001, end = 2003),
params = list(whichModulesToPrepare = "fireSense_ignitionPredict")))
expect_identical(n, 1L)
df <- covDF(sim$fireSense_igAndEscapePred_Covariates)
expect_identical(unique(df$year), 2003)
expect_equal(df$coreFraction, c(1, 0, 1, 0))
})

test_that("a change of flammableRTM or of rasterToMatchLargeELF remakes coreFraction", {
o <- toyObjects(); o$rasterToMatchLargeELF <- toyRast(rep(c(2, 2, 1, 1), 4))
run <- function(change) {
sim <- SpaDES.core::spades(toyPrepSim(o, times = list(start = 2001, end = 2001),
params = list(whichModulesToPrepare = "fireSense_ignitionPredict")), debug = FALSE)
sim <- change(sim)
SpaDES.core::end(sim) <- 2002
n <- countCoreFractionCalls(sim <- SpaDES.core::spades(sim, debug = FALSE))
list(n = n, core = covDF(sim$fireSense_igAndEscapePred_Covariates)$coreFraction)
}
expect_identical(run(identity)$n, 0L)
r <- run(function(sim) { sim$rasterToMatchLargeELF <- toyRast(rep(c(2, 2, 2, 1), 4)); sim })
expect_identical(r$n, 1L)
expect_equal(r$core, c(1, 0.5, 1, 2 / 3)) # pixel 4: cells 11, 12, 15 flammable, 11 and 15 core
r <- run(function(sim) { sim$flammableRTM <- toyRast(c(0, rep(1, 14), 0)); sim }) # cell 1 no longer flammable
expect_identical(r$n, 1L)
})