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
7 changes: 7 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export(ELFsNotFitted)
export(ELFsSkipped)
export(ELFsizePlan)
export(ELFtemplateRaster)
export(activationDudYearShare)
export(activationInterceptTxt)
export(adWeightAuto)
export(addBufferEdge)
export(addFitClimateVariables)
Expand Down Expand Up @@ -227,10 +229,13 @@ export(scorePopulationTerms)
export(setXgbThreads)
export(simulateFireSizes)
export(sizeRuleLandCover)
export(spreadActivationInterceptBounds)
export(spreadAddInteractions)
export(spreadAsymptoteLinks)
export(spreadClimateCols)
export(spreadClimateOptions)
export(spreadClimateRef)
export(spreadClimateTxt)
export(spreadCovCentre)
export(spreadCovCols)
export(spreadDesignCols)
Expand All @@ -239,6 +244,7 @@ export(spreadFitAdditionalColNamesTxt)
export(spreadFitClimateRefTxt)
export(spreadFitClimateVariables)
export(spreadFitCovCentreTxt)
export(spreadFitDudYearShareTxt)
export(spreadFitFileTag)
export(spreadFitFilenameFor)
export(spreadFitFuelScaleTxt)
Expand All @@ -247,6 +253,7 @@ export(spreadFitLedgerFolder)
export(spreadFitLedgerInputLists)
export(spreadFitLedgerInputs)
export(spreadFitLowerAsymptoteTxt)
export(spreadFitPcAtP50Txt)
export(spreadFitPowerLawGammaTxt)
export(spreadFitPowerLawPc50Txt)
export(spreadFitPowerLawPcMarginTxt)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# fireSenseUtils (development version)

* A third place for climate in the spread link, `spreadClimate = "activation"` in `fireSense_spreadFit`: fuel sets each pixel's own ceiling and climate decides how much of it is reached. `spreadLinkInputs()` (and `spreadProbFromCovs()`) take `activationIntercept`: then `pc_i = f + (p50 - f) * plogis(eta_i)`, with `eta_i` the linear predictor of the non-climate columns, and `x_i = activationIntercept + sum(beta_clim * (climate - climateRef))`, so `p <= p50` everywhere and the ratio of `p - f` between two pixels does not change with climate. It needs `climateRef`, `powerLawPc50` and `"powerLawSmooth"` with `lowerAsymptote = lowerSpreadProb` (below the floor the other forms fall faster for a higher `pc`, which would rank rich fuel below poor in a wet year). In the fit's parameters `activationIntercept` (`activationInterceptTxt`, default bounds `spreadActivationInterceptBounds`, `c(-10, 10)`) comes right before the random-effect sds and `maxAsymptote` is not fitted: the objective puts `powerLawPc50` in its place, so the gates and the saturation summary use p50 as the ceiling, and they look at `p`, not at `p` at the reference climate. `spreadRandomEffectArgs()` sets the objective's new `fitActivationIntercept` from the bounds, as for the random effects, so `runDEoptim()`, its re-score and an emulator pass it; `spreadProbGates()` and `spreadFitValidationData()` find it by name. New ledger constants `spreadClimateTxt` (`"spreadClimate"`, the mode of a fit) and `spreadClimateOptions`. The same mechanism fixes p_c at p50 in the "predictor" link: with the new `pcAtP50 = TRUE` (`.objfunSpreadFit()`, `runDEoptim()`, `spreadProbGates()`, `spreadFitValidationData()`; `fireSense_spreadFit`'s `powerLawPcAtP50`) `maxAsymptote` is left out of the fitted parameters and the objective puts `powerLawPc50` in its place, so the bounds never have a zero width (an emulator start divides by it). New `activationDudYearShare()`: the share of years whose year-effect draw puts every pixel at the floor at the reference climate, `pnorm(-activationIntercept / yearSpreadSD)`, which `fireSense_spreadFit` stores in an activation row (`spreadFitDudYearShareTxt`); `spreadFitPcAtP50Txt` marks a row whose p_c was fixed. With `powerLawPc50` given, the objective now stops on an unnamed `par` whose length does not match the formula and the link (a missing `pcAtP50` would otherwise read the first coefficient as p_c), and `runDEoptim()`'s start-up message lists `activationIntercept` apart from the logistic parameters. Fits without `activationIntercept` or `pcAtP50` give what they gave, and their calls and cache keys are unchanged.
* New `yearClimateColumn()`, `yearClimateMeans()` and `yearClimateMultiplier()`: the one place the year-level climate term of the expected ignitions lives. `fireSense_ignitionFit` fits `exp(beta * z)` (`beta >= 0`, `z` the standardised yearly mean of one climate covariate) and stores `list(variable, center, scale, beta)`; `fireSense_ignitionPredict` applies it with `yearClimateMultiplier()`.
* The spread and ignition ledgers now have separate Google Drive folders, in a new `FireSenseData` folder: `spreadFitLedgerFolder()` is `FireSenseData/SpreadFitParams` (the old `FireSense_SCANFI` folder, renamed; same id) and `ignitionFitLedgerFolder()` is `FireSenseData/IgnitionFitParams`, each with its own `"testing"` subfolder (`testing = TRUE`) and an `"archived"` subfolder for superseded ledgers. `options(fireSenseUtils.ignitionFitLedgerFolder = )` no longer follows `fireSenseUtils.spreadFitLedgerFolder`: set each option on its own. New `elfCacheFolder()` (option `fireSenseUtils.elfCacheFolder`), `FireSenseData/ELFs`, is where fireSense_ELFs shares the national ELF map, which was cached in the spread ledger folder.
* Climate can move the spread ceiling toward the landscape's runaway threshold. With a power-law link (`"powerLaw"`, `"powerLawSmooth"`), a `climateRef` and the landscape's `powerLawPc50` ([calibratePowerLaw()]`$p50`), each pixel's `pc` is `max(p50 - (p50 - pc0) * exp(-shift), lowerSpreadProb)`, `shift = sum(beta_clim * (climate - climateRef))`, and the linear predictor holds only the non-climate columns. `p` never passes `p50`, `pc = pc0` at the reference climate, and `log(p50 - pc)` is linear in climate, so log mean fire size is too. The one place the split and `pc` are computed is the new exported `spreadLinkInputs()` (linear predictor and link parameters, a matrix with one row per pixel in this mode); `spreadProbFromCovs()` and the objective call it, and the objective gives `spreadCpp()` a per-cell `linkPars` matrix on the whole landscape (cells, and rows with no climate, that cannot burn get `unburnableLinkPars()`). `logisticAll()`, `spreadProbFromCovs()` and the objective compute every linear link through `spreadLinkInputs()`; with `climateRef` it stops when `pc0` is above `powerLawPc50`, and `plotSpreadFitResponse()` draws `p50` as the ceiling. `spreadProbFromCovs()`, `.objfunSpreadFit()`, `runDEoptim()`, `spreadFitValidationData()`, `spreadProbGates()` and `plotSpreadFitResponse()` take `powerLawPc50`; `powerLawPcBounds(climateCeiling = TRUE)` bounds `pc0` above by `p50`. Before, `climateRef` with a power-law link stopped, and without `climateRef` nothing changes.
Expand Down
21 changes: 17 additions & 4 deletions R/DEoptim_fns.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ deoptimPopulationSize <- function(nParams) {
#' If named and including `yearSpreadSD` (which must be last), the per-year random effect (a seasonal departure) of
#' [.objfunSpreadFit()] (`fitYearSpreadSD`) is fitted, in the fit and in the re-score. Likewise `fireSpreadSD`, the
#' per-fire random effect (`fitFireSpreadSD`; needs `wholeLandscape = TRUE`), which comes just before
#' `yearSpreadSD`, or last without it. A fit without `fireSpreadSD` keeps its cache keys.
#' `yearSpreadSD`, or last without it. A fit without `fireSpreadSD` keeps its cache keys. With `activationIntercept`
#' ([activationInterceptTxt]), right before them and without `maxAsymptote`, the fit is `spreadClimate = "activation"`
#' (`fitActivationIntercept` of [.objfunSpreadFit()]); it needs `climateRef` and `powerLawPc50`.
#'
#' @param upper Numeric vector. Upper bounds for the parameters being optimized.
#' Passed to [DEoptim::DEoptim].
Expand All @@ -117,6 +119,8 @@ deoptimPopulationSize <- function(nParams) {
#' @param powerLawPc50 Passed to [.objfunSpreadFit()] with `climateRef`: `NULL` (default), or the landscape's runaway
#' threshold ([calibratePowerLaw()]), which with a power-law `link` puts climate in the ceiling ([spreadLinkInputs()]).
#' `NULL` leaves the call and its cache key as they were.
#' @param pcAtP50 Passed to [.objfunSpreadFit()] like `powerLawPc50`: `TRUE` when `lower` has no `maxAsymptote` because
#' p_c is fixed at `powerLawPc50`. `NULL` (default) leaves the call and its cache key as they were.
#' @param powerLawGamma The `"powerLaw"` and `"powerLawSmooth"` links' `gamma` ([powerLawLink()]), passed to [.objfunSpreadFit()] in the
#' fit and the re-score. Used only with those links; with any other link it is left out of the cache
#' keys ([powerLawOmitArgs()]) and the re-score's arguments, so they are what they were.
Expand Down Expand Up @@ -277,6 +281,7 @@ runDEoptim <- function(landscape,
convergence = NULL,
climateRef = NULL,
powerLawPc50 = NULL,
pcAtP50 = NULL,
powerLawGamma = spreadPowerLawGamma,
powerLawShape = spreadPowerLawShape,
lowerAsymptote = spreadProbFloor,
Expand Down Expand Up @@ -346,16 +351,19 @@ runDEoptim <- function(landscape,
## name the parameters from `lower`. termsInDEoptim() called every non-formula parameter a "logit"
## term, so yearSpreadSD was reported as a third logistic parameter.
covTerms <- spreadDesignCols(formulaToFit) # includes the intercept, if the formula has one
logisticTerms <- setdiff(names(lower), c(covTerms, spreadRandomEffectTxt))
message("Fitting ", length(lower), " parameters: logistic: ", paste(logisticTerms, collapse = ", "),
logisticTerms <- setdiff(names(lower), c(covTerms, spreadRandomEffectTxt, activationInterceptTxt))
message("Fitting ", length(lower), " parameters: logistic: ",
if (length(logisticTerms)) paste(logisticTerms, collapse = ", ") else "none (maxAsymptote fixed at powerLawPc50)",
"; covariates: ", paste(covTerms, collapse = ", "),
if (activationInterceptTxt %in% names(lower)) paste0("; activation: ", activationInterceptTxt),
if (fireSpreadSDTxt %in% names(lower)) paste0("; fire effect: ", fireSpreadSDTxt),
if (yearSpreadSDTxt %in% names(lower)) paste0("; year effect: ", yearSpreadSDTxt))
## the random effects are fitted when the bounds include them; DEoptim passes `par` unnamed, so the objective is
## told explicitly
reArgs <- spreadRandomEffectArgs(lower, wholeLandscape)
fitYearSpreadSD <- reArgs$fitYearSpreadSD
fitFireSpreadSDArg <- reArgs$fitFireSpreadSD
fitActivationInterceptArg <- reArgs$fitActivationIntercept # NULL without it: the call and its key are what they were

## clusterSetup() digested the objects it shipped once; the fit, its re-score, profile and simulated members
## all run the objective on them
Expand All @@ -378,6 +386,7 @@ runDEoptim <- function(landscape,
covCentre = covCentre,
climateRef = climateRef,
powerLawPc50 = powerLawPc50,
pcAtP50 = pcAtP50,
powerLawGamma = powerLawGamma,
powerLawShape = powerLawShape,
lowerAsymptote = lowerAsymptote,
Expand All @@ -402,6 +411,7 @@ runDEoptim <- function(landscape,
link = link,
fitYearSpreadSD = fitYearSpreadSD,
fitFireSpreadSD = fitFireSpreadSDArg,
fitActivationIntercept = fitActivationInterceptArg,
escapeSizeHa = escapeSizeHa,
jumpTries = jumpTries,
jumpMeanDist = jumpMeanDist,
Expand All @@ -424,7 +434,8 @@ runDEoptim <- function(landscape,
if (identical(gateRefusal, "minLik")) "gateRefusal",
omitNullArgs(crpsWeights = crpsWeights, covCentre = covCentre, runawayBufferMultiple = runawayBufferMultiple,
convergence = convergence, climateRef = climateRef, powerLawPc50 = powerLawPc50, wholeLandscape = wholeLandscapeArg,
fitFireSpreadSD = fitFireSpreadSDArg)),
fitFireSpreadSD = fitFireSpreadSDArg, fitActivationIntercept = fitActivationInterceptArg,
pcAtP50 = pcAtP50)),
## The data the objective runs on reaches the workers through clusterSetup(objsNeeded), not as an
## argument above, so it is not in this key by itself: two held-out folds of one ELF shared their
## whole fit (2026-09-29). clusterSetup() digested those objects once; use that digest.
Expand Down Expand Up @@ -455,13 +466,15 @@ runDEoptim <- function(landscape,
rescoreArgs$covCentre <- covCentre
rescoreArgs$climateRef <- climateRef
rescoreArgs$powerLawPc50 <- powerLawPc50
rescoreArgs$pcAtP50 <- pcAtP50
omitted <- powerLawOmitArgs(link, powerLawShape)
for (nm in setdiff(c("powerLawGamma", "powerLawShape", "lowerAsymptote", "richardsNu"), omitted))
rescoreArgs[[nm]] <- get(nm)
rescoreArgs$runawayBufferMultiple <- runawayBufferMultiple
rescoreArgs$crpsWeights <- crpsWeights
rescoreArgs$wholeLandscape <- wholeLandscapeArg
rescoreArgs$fitFireSpreadSD <- fitFireSpreadSDArg
rescoreArgs$fitActivationIntercept <- fitActivationInterceptArg
if (!identical(gateRefusal, "minLik")) rescoreArgs$gateRefusal <- gateRefusal
if (isTRUE(rescoreReps > 0) && !is.null(finalPop)) {
colnames(finalPop) <- names(lower)
Expand Down
36 changes: 35 additions & 1 deletion R/constants.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,29 @@
#' [scoreFireSizesByYear()]; areas are in pixels. A fit without the diagnostics (a cross-validation fold, `simulateMembers = 0`)
#' writes no such column, and a row written before it has none.
#' - `spreadPowerLawPGrid`: `numeric`. The spread probabilities [calibratePowerLaw()] simulates by default.
#' - `spreadClimateTxt`: `character(1)`. Name of the ledger column that holds where a fit put climate
#' (`"spreadClimate"`): one of `spreadClimateOptions`. A row without it (written before it existed) is
#' `"ceiling"` when it has `"climateRef_spread"` and a power-law `"spreadLink"`, else `"predictor"`.
#' - `spreadClimateOptions`: `character`. The values of `fireSense_spreadFit`'s `spreadClimate`: `"predictor"`
#' (climate in the linear predictor with the fuels), `"ceiling"` (climate moves the power-law ceiling,
#' [spreadLinkInputs()]) and `"activation"` (fuel sets each pixel's ceiling, climate is the linear predictor).
#' - `activationInterceptTxt`: `character(1)`. Name of the parameter of `spreadClimate = "activation"` that is
#' the linear predictor at the reference climate (`"activationIntercept"`); in `lower`/`upper` and in a ledger
#' row's `params` it comes right after the covariate coefficients, before the random-effect sds.
#' - `spreadActivationInterceptBounds`: `numeric(2)`. Its default bounds, `c(-10, 10)`.
#' - `spreadFitDudYearShareTxt`: `character(1)`. Name of the ledger column of an `"activation"` fit with the share of
#' years whose draw of the year effect puts every pixel at the floor at the reference climate, one per parameter set
#' ([activationDudYearShare()]; `"dudYearShare_spread"`).
#' - `spreadFitPcAtP50Txt`: `character(1)`. Name of the ledger column that marks a fit whose `maxAsymptote` was fixed at
#' p50, not fitted (`fireSense_spreadFit`'s `powerLawPcAtP50`; `"pcAtP50_spread"`), so its value is not read as a
#' fitted one at its bound.
#'
#' - `treedWetlandAgbTxt`: `character(1)`. Name of the pooled treed-wetland biomass column
#' built by `fireSenseCovariatesCreate(fuelCovariates = "domSecWetland")` (currently
#' `"treedWetland_agb"`).
#'
#' @name fireSenseUtils-constants
#' @aliases polygonIDTxt nonNFColNamesTxt yearTxt polyCauseColTxt polySizeColTxt youngAgeTxt burnableHaTxt treedWetlandTxt treedWetlandAgbTxt ignitionsTxt escapesTxt coreFractionTxt ignitionObjectiveTxt spreadFitAdditionalColNamesTxt ignitionFitAdditionalColNamesTxt spreadInterceptTxt spreadFitCovCentreTxt spreadLinkTxt spreadFitClimateRefTxt spreadFitPowerLawGammaTxt spreadFitPowerLawShapeTxt spreadLinkOptions spreadFitLowerAsymptoteTxt spreadFitRichardsNuTxt spreadRichardsNu spreadRichardsB spreadLowerAsymptoteMin spreadLinearLinks spreadPowerLawLinks spreadAsymptoteLinks spreadLinkParNames spreadPowerLawGamma spreadPowerLawShape spreadFitPowerLawPc50Txt spreadFitPowerLawR2Txt spreadFitPowerLawPcMarginTxt spreadFitInSampleSizesTxt spreadPowerLawPGrid
#' @aliases polygonIDTxt nonNFColNamesTxt yearTxt polyCauseColTxt polySizeColTxt youngAgeTxt burnableHaTxt treedWetlandTxt treedWetlandAgbTxt ignitionsTxt escapesTxt coreFractionTxt ignitionObjectiveTxt spreadFitAdditionalColNamesTxt ignitionFitAdditionalColNamesTxt spreadInterceptTxt spreadFitCovCentreTxt spreadLinkTxt spreadFitClimateRefTxt spreadFitPowerLawGammaTxt spreadFitPowerLawShapeTxt spreadLinkOptions spreadFitLowerAsymptoteTxt spreadFitRichardsNuTxt spreadRichardsNu spreadRichardsB spreadLowerAsymptoteMin spreadLinearLinks spreadPowerLawLinks spreadAsymptoteLinks spreadLinkParNames spreadPowerLawGamma spreadPowerLawShape spreadFitPowerLawPc50Txt spreadFitPowerLawR2Txt spreadFitPowerLawPcMarginTxt spreadFitInSampleSizesTxt spreadPowerLawPGrid spreadClimateTxt spreadClimateOptions activationInterceptTxt spreadActivationInterceptBounds spreadFitDudYearShareTxt spreadFitPcAtP50Txt
NULL

#' @export
Expand Down Expand Up @@ -170,6 +186,24 @@ spreadFitPowerLawShapeTxt <- "powerLawShape_spread"
#' @export
spreadLinkOptions <- c("ceiling", "climateOutside", "powerLaw", "powerLawSmooth", "richards")

#' @export
spreadClimateTxt <- "spreadClimate"

#' @export
spreadClimateOptions <- c("predictor", "ceiling", "activation")

#' @export
activationInterceptTxt <- "activationIntercept"

#' @export
spreadActivationInterceptBounds <- c(-10, 10)

#' @export
spreadFitDudYearShareTxt <- "dudYearShare_spread"

#' @export
spreadFitPcAtP50Txt <- "pcAtP50_spread"

#' @export
spreadFitLowerAsymptoteTxt <- "lowerAsymptote_spread"

Expand Down
12 changes: 11 additions & 1 deletion R/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,22 @@ richardsLink <- function(x, pc, lower = spreadProbFloor, nu = spreadRichardsNu,
#' `fireSense_spreadPredict` for a ledger row. `lower` must be one number from [spreadLowerAsymptoteMin] to [spreadProbFloor] with
#' `"powerLawSmooth"` and `"richards"`; `nu` one number, 0 or more, with `"richards"`. Other links have nothing to check.
#'
#' With `activation` (`spreadClimate = "activation"`, [spreadLinkInputs()]) the link must be `"powerLawSmooth"` with
#' `lower` equal to `floor`: below the floor the other forms fall faster for a higher `pc`, and with the activation `pc`
#' is set by fuel, so a wet year would rank rich fuel below poor.
#'
#' @param link The spread link.
#' @param lower,nu The `lowerAsymptote` and `richardsNu` to check; `NULL` is not a number.
#' @param activation Logical; `TRUE` for a fit or a ledger row with `spreadClimate = "activation"`.
#' @param floor The `lowerSpreadProb` the activation's `lower` must equal; default [spreadProbFloor].
#' @return `TRUE`, invisibly; an error otherwise.
#' @export
checkLinkParams <- function(link, lower = NULL, nu = NULL) {
checkLinkParams <- function(link, lower = NULL, nu = NULL, activation = FALSE, floor = spreadProbFloor) {
one <- function(v) is.numeric(v) && length(v) == 1L && is.finite(v)
if (isTRUE(activation) && !(identical(link, "powerLawSmooth") && one(lower) && isTRUE(all.equal(lower, floor))))
stop("spreadClimate = 'activation' needs the link 'powerLawSmooth' with lowerAsymptote = lowerSpreadProb (",
format(floor), "); got ", dQuote(format(link), FALSE), " and ", format(lower), ". Below the floor the other ",
"forms fall faster for a higher pc, so a wet year would rank rich fuel below poor.", call. = FALSE)
if (any(link %in% spreadAsymptoteLinks) &&
!(one(lower) && lower >= spreadLowerAsymptoteMin && lower <= spreadProbFloor))
stop("lowerAsymptote must be one number from ", spreadLowerAsymptoteMin, " to ", spreadProbFloor, "; got ", format(lower),
Expand Down
Loading
Loading