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

- The ledger's escape definition is checked with `fireSenseUtils::checkEscapeDefinition()` on the whole one-row-per-ELF table (the error names the ELF, not the row number), this run's definition is `fireSenseUtils::escapeDefinitionOf()`, and the random-effect names are `fireSenseUtils::fireSpreadSDTxt` and `yearSpreadSDTxt` (PredictiveEcology/fireSenseUtils#222), instead of local copies. Needs fireSenseUtils with that PR.

- A ledger row with the new fitted parameter `fireSpreadSD` (the per-fire random-effect sd of `fireSense_spreadFit`) no longer stops with "have no covariate": `spreadProbOneELF()` drops every name in `fireSenseUtils::spreadRandomEffectTxt` from the fitted coefficients, so spread probabilities are unchanged. The value is a new output `fireSense_SpreadFireSD` for `fireSense_burn`, shaped like `fireSense_SpreadSD` (one number with one ELF, a blended raster with several; 0 for a row without it). New output `fireSense_EscapeDefinition` (one row per ELF) holds the escape definition recorded in the ledger (`fireSenseUtils::escapeDefinition()`), and each row is checked against this run's new parameters `escapeSizeHa` and `fireCauses` (shared with the other fireSense modules) with `fireSenseUtils::checkEscapeDefinition()`: a mismatch stops. A row without a recorded definition is not checked. Merge this before any fit writes `fireSpreadSD`. Needs fireSenseUtils#221.

- A ledger row's `powerLawShape_spread` (`fireSenseUtils::spreadFitPowerLawShapeTxt`, written by `fireSense_spreadFit`'s new `powerLawShape`) is the curvature exponent of the `"powerLaw"` link, `p = pc - (pc - floor) * exp(-sign(x) * abs(x / gamma)^shape)`, applied in `spreadProbOneELF()` through `fireSenseUtils::spreadProbFromCovs()` (`ledgerPowerLawShape()`). A row without the column, or with `NA`, has shape 1, the original link, so such rows predict exactly as before. `fireSense_SpreadPowerLaw` gains a fourth element or layer, `shape`, for `fireSense_burn`. Needs fireSenseUtils >= 0.2.3.9137.
Expand Down
25 changes: 11 additions & 14 deletions fireSense_spreadPredict.R
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ doEvent.fireSense_spreadPredict <- function(sim, eventTime, eventType, debug = F
eventType,
init = {

for (pc in c("maxFireSpread", "escapeSizeHa", "fireCauses"))
SpaDES.core::paramCheckOtherMods(sim, paramToCheck = pc, moduleToUse = "all", ifSetButDifferent = "error")
SpaDES.core::paramCheckOtherMods(sim, paramToCheck = "maxFireSpread", moduleToUse = "all", ifSetButDifferent = "error")
fireSenseUtils::escapeDefinitionOf(sim)

sim <- scheduleEvent(sim, eventTime = P(sim)$.runInitialTime, moduleName, "run",
eventPriority = 5.12)
Expand Down Expand Up @@ -188,7 +188,9 @@ spreadPredictRun <- function(sim) {
"). Either fireSense_spreadFit has not run yet -- its `run` event must precede this ",
"module's -- or the shared ledger has no row for this polygon.", call. = FALSE)

sim$fireSense_EscapeDefinition <- checkedEscapeDefinition(sa, fireSenseUtils::escapeDefinition(P(sim)$escapeSizeHa, P(sim)$fireCauses))
sim$fireSense_EscapeDefinition <- ledgerEscapeDefinition(sa)
fireSenseUtils::checkEscapeDefinition(sim$fireSense_EscapeDefinition, fireSenseUtils::escapeDefinitionOf(sim),
"the ledger row")

rep <- if (isTRUE(P(sim)$paramSetByReplicate))
replicateOf(time(sim), P(sim)$.runInitialTime, P(sim)$.runInterval) else P(sim)$.rep
Expand Down Expand Up @@ -445,12 +447,10 @@ linearPredictorRas <- function(template, dt) {
r
}

yearSpreadSDTxt <- "yearSpreadSD"
fireSpreadSDTxt <- "fireSpreadSD"

## the outputs that carry a fitted random-effect sd, named by the parameter each takes it from; together they
## are `fireSenseUtils::spreadRandomEffectTxt`
randomEffectOutputs <- list(fireSense_SpreadFireSD = fireSpreadSDTxt, fireSense_SpreadSD = yearSpreadSDTxt)
randomEffectOutputs <- list(fireSense_SpreadFireSD = fireSenseUtils::fireSpreadSDTxt,
fireSense_SpreadSD = fireSenseUtils::yearSpreadSDTxt)

#' The names of `fireSense_SpreadPowerLaw`
powerLawParNames <- c("pc", "gamma", "floor", "shape")
Expand Down Expand Up @@ -602,26 +602,23 @@ replicateOf <- function(time, start, interval) {
#' probability; `fireSpreadSD` is one per fire. Both are in `fireSenseUtils::spreadRandomEffectTxt`.
#'
#' @param params one-row `data.frame`: the parameter set in use (`paramSetForRep()`).
#' @param txt the parameter name, `yearSpreadSDTxt` or `fireSpreadSDTxt`.
#' @param txt the parameter name, `fireSenseUtils::yearSpreadSDTxt` or `fireSenseUtils::fireSpreadSDTxt`.
#' @return Numeric; 0 when the fit has no such parameter (a ledger row written before it existed).
randomEffectSDOf <- function(params, txt) {
if (!txt %in% names(params)) return(0)
params[[txt]]
}

#' The escape definition of each ledger row, checked against this run's
#' The escape definition each ledger row recorded
#'
#' @param sa `sim$studyAreaWithSpreadParams`, one row per ELF.
#' @param current this run's definition, from `fireSenseUtils::escapeDefinition()`.
#' @return A `data.frame` with the columns of `fireSenseUtils::escapeDefinition()` (`NA` for a row without them) and,
#' if `sa` has it, `polygonID`. Stops if a row's definition differs from `current`.
checkedEscapeDefinition <- function(sa, current) {
#' if `sa` has it, `polygonID`: what `fireSenseUtils::checkEscapeDefinition()` checks against this run's definition.
ledgerEscapeDefinition <- function(sa) {
tmpl <- fireSenseUtils::escapeDefinition()
def <- as.data.frame(lapply(stats::setNames(names(tmpl), names(tmpl)), function(cl)
if (cl %in% names(sa)) rep_len(sa[[cl]], NROW(sa)) else rep(tmpl[[cl]][NA_integer_], NROW(sa))))
idCol <- fireSenseUtils::polygonIDTxt
if (idCol %in% names(sa)) def[[idCol]] <- as.character(sa[[idCol]])
for (i in which(!is.na(def$escapeSizeHa)))
fireSenseUtils::checkEscapeDefinition(def[i, , drop = FALSE], current, what = paste0("ledger row ", i))
def
}
1 change: 1 addition & 0 deletions tests/testthat/test-multiELF.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ test_that("with several ELFs each row's escape definition is checked", {
ins$studyAreaWithSpreadParams$escapeSizeHa <- c(50, 100)
ins$studyAreaWithSpreadParams$fireCauses <- "natural,unknown"
expect_error(toyRun(ins), "escape definition")
expect_error(toyRun(ins), "ELF B") # the row that differs, by its polygonID
ins$studyAreaWithSpreadParams$escapeSizeHa <- 50
sim <- toyRun(ins)
expect_identical(NROW(sim$fireSense_EscapeDefinition), 2L)
Expand Down
Loading