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 functions that read a ledger row (`ledgerLinkArgs()`, `ledgerCovCentre()`, `ledgerClimateRef()`, `ledgerPowerLawPc50()`, `ledgerSpreadClimate()`, `ledgerCovMinMax()` and their helpers) now live in `fireSenseUtils`, which also uses them (`spreadFuelEffect()`, the fuel effect on the ELF map), so a row is read in one place. They are moved unchanged; the only difference is that their errors say "spread ledger row" instead of "fireSense_spreadPredict: ledger row". Needs fireSenseUtils >= 0.2.3.9191 (PredictiveEcology/fireSenseUtils#260, version to be set after it merges).

- A ledger row of `fireSense_spreadFit`'s `spreadClimate = "activation"` predicts as that fit: fuel sets each pixel's ceiling `pc` below p50 and climate is the linear predictor (`fireSenseUtils::spreadLinkInputs(activationIntercept =)`). The row's mode is read by the new `ledgerSpreadClimate()` (column `fireSenseUtils::spreadClimateTxt`); a row without it is "ceiling" when it has `climateRef_spread` and a power-law link, else "predictor", as before. The parameter set's `activationIntercept` is not a covariate coefficient: it goes to the link, and a set and its row that disagree stop. `fireSense_SpreadPowerLaw` is then a per-pixel raster, and `fireSense_SpreadLinear` is the climate term, so `fireSense_burn` adds the year effect to it unchanged. A row that records its mode is predicted by it: "predictor" keeps climate in x even with a `climateRef_spread`, and "ceiling" or "activation" without one stops. An activation ELF is not blended with an ELF of another mode (an error, beside the one for different links; ceiling with predictor still blends), and the activation's lower-asymptote check uses the run's `lowerSpreadProb`. Other rows predict as before. Needs fireSenseUtils >= 0.2.3.9183 (PredictiveEcology/fireSenseUtils#255).

- The yearly spread probability map (`.plots = "png"`) is bigger. Its png is now 10 x 8 inches at 144 dpi (1440 x 1152 pixels), as fireSense_burn's; it was R's `png()` default of 480 x 480, which looked small in `SpaDES.shiny::shine()`.
Expand Down
243 changes: 20 additions & 223 deletions fireSense_spreadPredict.R

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions tests/testthat/test-climateActivation.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ test_that("fireSense_burn's reading of the outputs gives the predicted p, and th
test_that("a row without the mode column predicts as it did: ceiling with climateRef and a power-law link", {
ceil <- inputsCA(mode = NULL, a = NULL)
sa <- ceil$studyAreaWithSpreadParams
expect_identical(ledgerSpreadClimate(sa, 1L), "ceiling")
expect_identical(fireSenseUtils::ledgerSpreadClimate(sa, 1L), "ceiling")
sa[[modeColCA]] <- "ceiling"
ceil2 <- ceil; ceil2$studyAreaWithSpreadParams <- sa
expect_identical(predVals(toyRun(ceil)), predVals(toyRun(ceil2)))
## and a row with neither is "predictor"
sa0 <- inputsPL()$studyAreaWithSpreadParams
expect_identical(ledgerSpreadClimate(sa0, 1L), "predictor")
expect_false("spreadClimate" %in% names(ledgerLinkArgs(sa0, 1L)))
expect_identical(ledgerLinkArgs(inputsCA()$studyAreaWithSpreadParams, 1L)$spreadClimate, "activation")
expect_identical(fireSenseUtils::ledgerSpreadClimate(sa0, 1L), "predictor")
expect_false("spreadClimate" %in% names(fireSenseUtils::ledgerLinkArgs(sa0, 1L)))
expect_identical(fireSenseUtils::ledgerLinkArgs(inputsCA()$studyAreaWithSpreadParams, 1L)$spreadClimate, "activation")
})

test_that("an activation row and its parameters must agree, and the link must be powerLawSmooth at the floor", {
Expand All @@ -89,7 +89,7 @@ test_that("an activation row and its parameters must agree, and the link must be
expect_error(toyRun(bad), "lowerAsymptote = lowerSpreadProb")
sa <- inputsCA()$studyAreaWithSpreadParams
sa[[modeColCA]] <- "wet"
expect_error(ledgerSpreadClimate(sa, 1L), "unknown")
expect_error(fireSenseUtils::ledgerSpreadClimate(sa, 1L), "unknown")
})

test_that("a recorded mode is the fit's: an explicit 'predictor' row ignores a climateRef; 'ceiling' needs one", {
Expand All @@ -107,9 +107,9 @@ test_that("a recorded mode is the fit's: an explicit 'predictor' row ignores a c
test_that("the activation's floor check uses the run's lowerSpreadProb", {
sa <- inputsCA()$studyAreaWithSpreadParams
sa[[fireSenseUtils::spreadFitLowerAsymptoteTxt]] <- 0.1
expect_identical(ledgerLowerAsymptote(sa, 1L, floor = 0.1), 0.1)
expect_error(ledgerLowerAsymptote(sa, 1L), "lowerAsymptote = lowerSpreadProb")
expect_identical(ledgerLinkArgs(sa, 1L, floor = 0.1)$lowerAsymptote, 0.1)
expect_identical(fireSenseUtils::ledgerLowerAsymptote(sa, 1L, floor = 0.1), 0.1)
expect_error(fireSenseUtils::ledgerLowerAsymptote(sa, 1L), "lowerAsymptote = lowerSpreadProb")
expect_identical(fireSenseUtils::ledgerLinkArgs(sa, 1L, floor = 0.1)$lowerAsymptote, 0.1)
})

test_that("ELFs fitted in different spreadClimate modes are not blended", {
Expand Down
12 changes: 6 additions & 6 deletions tests/testthat/test-climateCeiling.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ handCC <- function(gamma = 1.9, pc0 = 0.25, floor = 0.13, p50 = 0.3, ref = 0.4)
list(p = pmax(pc - (pc - floor) * exp(-sign(x) * abs(x / gamma)), 0), pc = pc, x = x)
}

test_that("ledgerPowerLawPc50() reads the row's p50, NULL when absent or NA", {
test_that("fireSenseUtils::ledgerPowerLawPc50() reads the row's p50, NULL when absent or NA", {
sa <- toyInputs()$studyAreaWithSpreadParams
expect_null(ledgerPowerLawPc50(sa, 1L))
expect_null(fireSenseUtils::ledgerPowerLawPc50(sa, 1L))
sa[[pc50ColCC]] <- 0.3
expect_identical(ledgerPowerLawPc50(sa, 1L), 0.3)
expect_identical(fireSenseUtils::ledgerPowerLawPc50(sa, 1L), 0.3)
sa[[pc50ColCC]] <- NA_real_
expect_null(ledgerPowerLawPc50(sa, 1L)) # no climateRef: not needed
expect_null(fireSenseUtils::ledgerPowerLawPc50(sa, 1L)) # no climateRef: not needed
})

test_that("a row with climateRef and a power-law link predicts with climate moving the ceiling toward p50", {
Expand Down Expand Up @@ -72,10 +72,10 @@ test_that("a climateRef row without a finite p50 stops, naming the row", {
expect_error(toyRun(inputsCC(p50 = NULL)), "ledger row 1 has climate in the ceiling")
expect_error(toyRun(inputsCC(p50 = NA_real_)), "ledger row 1 has climate in the ceiling")
sa <- inputsCC(p50 = NA_real_)$studyAreaWithSpreadParams
expect_error(ledgerPowerLawPc50(sa, 1L), "no finite")
expect_error(fireSenseUtils::ledgerPowerLawPc50(sa, 1L), "no finite")
## a row without climateRef has no use for it: NULL, no error
sa[[refColCC]] <- NULL
expect_null(ledgerPowerLawPc50(sa, 1L))
expect_null(fireSenseUtils::ledgerPowerLawPc50(sa, 1L))
})

test_that("with several ELFs the per-pixel parameters are blended, an ELF without climateRef contributing its vector", {
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test-climateOutside.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ test_that("a row without a reference predicts exactly as before", {
withRef <- predVals(toyRun(inputsCO(ref = list(MDC = 0.5))))
expect_false(isTRUE(all.equal(withRef, base)))
sa <- toyInputs()$studyAreaWithSpreadParams
expect_null(ledgerClimateRef(sa, 1L))
expect_null(fireSenseUtils::ledgerClimateRef(sa, 1L))
sa[[refCol]] <- list(list())
expect_null(ledgerClimateRef(sa, 1L))
expect_null(fireSenseUtils::ledgerClimateRef(sa, 1L))
sa[[refCol]] <- list(list(MDC = 0.4))
expect_identical(ledgerClimateRef(sa, 1L), list(MDC = 0.4))
expect_identical(fireSenseUtils::ledgerClimateRef(sa, 1L), list(MDC = 0.4))
})

test_that("with several ELFs each row applies its own reference, blended like the probabilities", {
Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/test-ledgerCovMinMax.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ test_that("a row without ranges (an older ledger) falls back to sim$covMinMax_sp
sa <- data.frame(polygonID = c("A", "B"))
cmm <- data.table::data.table(MDC = c(0, 200))
sa$covMinMax_spread <- list(cmm, NULL)
expect_identical(ledgerCovMinMax(sa, 1L, "default"), cmm)
expect_identical(ledgerCovMinMax(sa, 2L, "default"), "default")
expect_identical(ledgerCovMinMax(data.frame(polygonID = "A"), 1L, "default"), "default")
expect_null(ledgerCovMinMax(data.frame(polygonID = "A"), 1L))
expect_identical(fireSenseUtils::ledgerCovMinMax(sa, 1L, "default"), cmm)
expect_identical(fireSenseUtils::ledgerCovMinMax(sa, 2L, "default"), "default")
expect_identical(fireSenseUtils::ledgerCovMinMax(data.frame(polygonID = "A"), 1L, "default"), "default")
expect_null(fireSenseUtils::ledgerCovMinMax(data.frame(polygonID = "A"), 1L))
})

test_that("a fitted covariate with no range stops instead of staying unscaled", {
Expand Down
16 changes: 8 additions & 8 deletions tests/testthat/test-powerLaw.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ test_that("a row of any other link predicts exactly as before", {
expect_identical(predVals(toyRun(inputsPL(gamma = NULL, link = NULL))), base)
expect_identical(predVals(toyRun(inputsPL(gamma = 4, link = "ceiling"))), base) # gamma ignored
sa <- toyInputs()$studyAreaWithSpreadParams
expect_null(ledgerPowerLawGamma(sa, 1L))
expect_null(fireSenseUtils::ledgerPowerLawGamma(sa, 1L))
sa[[linkColPL]] <- "climateOutside"
expect_null(ledgerPowerLawGamma(sa, 1L))
expect_null(fireSenseUtils::ledgerPowerLawGamma(sa, 1L))
sa[[linkColPL]] <- "powerLaw"; sa[[gammaColPL]] <- 2.2
expect_identical(ledgerPowerLawGamma(sa, 1L), 2.2)
expect_identical(fireSenseUtils::ledgerPowerLawGamma(sa, 1L), 2.2)
## a powerLaw row without a usable gamma stops, instead of predicting with the wrong link
sa[[gammaColPL]] <- NA_real_
expect_error(ledgerPowerLawGamma(sa, 1L), "no valid")
expect_error(fireSenseUtils::ledgerPowerLawGamma(sa, 1L), "no valid")
sa[[gammaColPL]] <- NULL
expect_error(ledgerPowerLawGamma(sa, 1L), "no valid")
expect_error(fireSenseUtils::ledgerPowerLawGamma(sa, 1L), "no valid")
})

test_that("with several ELFs each row applies its own link, blended like the probabilities", {
Expand Down Expand Up @@ -136,11 +136,11 @@ test_that("a row's powerLawShape_spread bends the link; a row without it, or NA,
ins$studyAreaWithSpreadParams[[shapeColPL]] <- 1
expect_identical(predVals(toyRun(ins))[cellsPL], base)
sa <- toyInputs()$studyAreaWithSpreadParams
expect_identical(ledgerPowerLawShape(sa, 1L), 1)
expect_identical(fireSenseUtils::ledgerPowerLawShape(sa, 1L), 1)
sa[[shapeColPL]] <- 2.5
expect_identical(ledgerPowerLawShape(sa, 1L), 2.5)
expect_identical(fireSenseUtils::ledgerPowerLawShape(sa, 1L), 2.5)
sa[[shapeColPL]] <- -1
expect_error(ledgerPowerLawShape(sa, 1L), "invalid")
expect_error(fireSenseUtils::ledgerPowerLawShape(sa, 1L), "invalid")
## the linear predictor mapped by the link with the handed shape reproduces p
ins$studyAreaWithSpreadParams[[shapeColPL]] <- 2
sim <- toyRun(ins)
Expand Down
24 changes: 12 additions & 12 deletions tests/testthat/test-smoothLinks.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ test_that("a richards row predicts the Richards link with no gamma in the ledger
expect_identical(sim$fireSense_SpreadPowerLaw,
c(pc = 0.25, lower = 0.02, nu = 2, B = 1 / fireSenseUtils::spreadPowerLawGamma))
expect_equal(terra::values(sim$fireSense_SpreadLinear, mat = FALSE)[cellsSL], xSL(), tolerance = 1e-9)
expect_null(ledgerPowerLawGamma(ins$studyAreaWithSpreadParams, 1L))
expect_null(fireSenseUtils::ledgerPowerLawGamma(ins$studyAreaWithSpreadParams, 1L))
## nu = 0 is Gompertz
g <- predVals(toyRun(inputsSL("richards", lower = 0.13, nu = 0)))
expect_equal(g[cellsSL], 0.13 + (0.25 - 0.13) * exp(-exp(-xSL() / fireSenseUtils::spreadPowerLawGamma)), tolerance = 1e-9)
Expand All @@ -75,23 +75,23 @@ test_that("with L = 0.13 a low-x pixel keeps 0.13 on both new links, and the old

test_that("rows without the columns their link needs stop; the helpers read the columns", {
sa <- inputsSL("richards")$studyAreaWithSpreadParams
expect_identical(ledgerLowerAsymptote(sa, 1L), 0.05)
expect_identical(ledgerRichardsNu(sa, 1L), 0.5)
expect_identical(ledgerLinkArgs(sa, 1L)$link, "richards")
expect_false("powerLawGamma" %in% names(ledgerLinkArgs(sa, 1L)))
expect_identical(fireSenseUtils::ledgerLowerAsymptote(sa, 1L), 0.05)
expect_identical(fireSenseUtils::ledgerRichardsNu(sa, 1L), 0.5)
expect_identical(fireSenseUtils::ledgerLinkArgs(sa, 1L)$link, "richards")
expect_false("powerLawGamma" %in% names(fireSenseUtils::ledgerLinkArgs(sa, 1L)))
sa[[nuColSL]] <- NULL
expect_error(ledgerRichardsNu(sa, 1L), "richardsNu must be")
expect_error(fireSenseUtils::ledgerRichardsNu(sa, 1L), "richardsNu must be")
sa <- inputsSL("powerLawSmooth")$studyAreaWithSpreadParams
sa[[lowerColSL]] <- 0.5
expect_error(ledgerLowerAsymptote(sa, 1L), "lowerAsymptote must be")
expect_error(fireSenseUtils::ledgerLowerAsymptote(sa, 1L), "lowerAsymptote must be")
sa[[lowerColSL]] <- 0 # a lower asymptote of 0 is never accepted
expect_error(ledgerLowerAsymptote(sa, 1L), "never 0")
expect_error(fireSenseUtils::ledgerLowerAsymptote(sa, 1L), "never 0")
sa[[lowerColSL]] <- NULL
expect_error(ledgerLowerAsymptote(sa, 1L), "lowerAsymptote must be")
expect_null(ledgerRichardsNu(inputsSL("powerLawSmooth")$studyAreaWithSpreadParams, 1L))
expect_error(fireSenseUtils::ledgerLowerAsymptote(sa, 1L), "lowerAsymptote must be")
expect_null(fireSenseUtils::ledgerRichardsNu(inputsSL("powerLawSmooth")$studyAreaWithSpreadParams, 1L))
## the other links have none
expect_null(ledgerLowerAsymptote(toyInputs()$studyAreaWithSpreadParams, 1L))
expect_identical(ledgerLinkArgs(toyInputs()$studyAreaWithSpreadParams, 1L), list(powerLawShape = 1))
expect_null(fireSenseUtils::ledgerLowerAsymptote(toyInputs()$studyAreaWithSpreadParams, 1L))
expect_identical(fireSenseUtils::ledgerLinkArgs(toyInputs()$studyAreaWithSpreadParams, 1L), list(powerLawShape = 1))
})

test_that("with several ELFs of one new link the parameters blend; with different links it stops", {
Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/test-spreadIntercept.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ test_that("a row without an intercept or centres predicts exactly as before", {
v <- predVals(toyRun(inputsI(intercept = FALSE, centre = FALSE)))
expect_equal(v[cells], handLogistic3(xHand(0)), tolerance = 1e-7)
expect_identical(v, predVals(toyRun()))
expect_null(ledgerCovCentre(toyInputs()$studyAreaWithSpreadParams, 1L))
expect_null(fireSenseUtils::ledgerCovCentre(toyInputs()$studyAreaWithSpreadParams, 1L))
})

test_that("an old ledger row's missing centre is NULL, also beside a row that has one", {
sa <- data.frame(polygonID = c("A", "B"))
sa[[centreCol]] <- list(centreI, NULL) # as rbindlist(fill = TRUE) leaves an older row
expect_identical(ledgerCovCentre(sa, 1L), centreI)
expect_null(ledgerCovCentre(sa, 2L))
expect_null(ledgerCovCentre(data.frame(polygonID = "A"), 1L))
expect_identical(fireSenseUtils::ledgerCovCentre(sa, 1L), centreI)
expect_null(fireSenseUtils::ledgerCovCentre(sa, 2L))
expect_null(fireSenseUtils::ledgerCovCentre(data.frame(polygonID = "A"), 1L))
})

test_that("with several ELFs each uses its own intercept and centre; an older ELF's row predicts as before", {
Expand Down
Loading