From 336582af457a9f345a6ef0c6e91a4af6b29010fb Mon Sep 17 00:00:00 2001 From: Eliot McIntire Date: Wed, 18 Nov 2026 16:16:34 -0700 Subject: [PATCH] Plot the spread probability each run (.plots, default "screen") Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Lx3DXgg187sAPQZwQT3Mf4 --- NEWS.md | 2 ++ fireSense_spreadPredict.R | 7 +++++++ tests/testthat/setup-toySpread.R | 2 ++ tests/testthat/test-events.R | 9 +++++++++ tests/testthat/test-metadata.R | 6 +++--- 5 files changed, 23 insertions(+), 3 deletions(-) diff --git a/NEWS.md b/NEWS.md index cab60f1..487d5c8 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # fireSense_spreadPredict (development version) +- New parameter `.plots` (default `"screen"`): the spread probability map (`fireSense_SpreadPredicted`) is plotted each `run` (`SpreadProbability_yr`). `NA` or `NULL` for none. + - A ledger row with an interaction coefficient (`a:b`, e.g. `CMD:dom_agb_Pice_mar`, from a `fireSense_spreadFit` fitted with `fireSense_dataPrepFit`'s `spreadInteractions = "climate:fuel"`) predicts with it: `spreadProbOneELF()` needs the covariates `a` and `b` (and their ranges in `covMinMax_spread`), and `fireSenseUtils::spreadProbFromIntegerCovs()` forms their product after each is rescaled and centred with the row's `covCentre_spread` entry for it (0 where `youngAge` cleared the fuel), and multiplies it by the coefficient, as the fit does. A row without interactions predicts exactly as before. Needs fireSenseUtils >= 0.2.3.9177 (#242). - With `paramSets`: the cache of a parameter set's surface no longer reads all the values of `flammableRTM` and `rasterToMatchLargeELF` every replicate (a new raster object or a change at 1000 sampled cells is a change) and returns the stored surface without copying it (a changed stored surface is recomputed). On a 20-million-cell raster a replicate that reuses a surface takes 0.35 s instead of 0.9 s, most of it SpaDES. `paramSets` stops if the ledger rows have the simulated ELF twice, or if a file of ledger rows has none for it. diff --git a/fireSense_spreadPredict.R b/fireSense_spreadPredict.R index 1977ea3..c67d07e 100644 --- a/fireSense_spreadPredict.R +++ b/fireSense_spreadPredict.R @@ -66,6 +66,9 @@ defineModule(sim, list( desc = "Time of the first prediction."), defineParameter(name = ".runInterval", class = "numeric", default = 1, desc = "Interval between predictions, in years. `NA` predicts once."), + defineParameter(".plots", "character", "screen", NA, NA, + desc = paste("Passed to `types` in `Plots()`, e.g. \"screen\" (default), \"png\": the spread probability", + "map is plotted each `run`. `NA` or `NULL`: no plots.")), defineParameter(name = ".saveInitialTime", class = "numeric", default = NA, desc = "Time of the `save` event, which does nothing. `NA` means never."), defineParameter(".studyAreaName", "character", NA, NA, NA, @@ -165,6 +168,10 @@ doEvent.fireSense_spreadPredict <- function(sim, eventTime, eventType, debug = F }, run = { sim <- spreadPredictRun(sim) + ## the year's spread probability + if (anyPlotting(P(sim)$.plots)) + Plots(sim$fireSense_SpreadPredicted, types = P(sim)$.plots, + filename = paste0("SpreadProbability_yr", time(sim))) if (!is.na(P(sim)$.runInterval)) { sim <- scheduleEvent(sim, time(sim) + P(sim)$.runInterval, moduleName, "run", diff --git a/tests/testthat/setup-toySpread.R b/tests/testthat/setup-toySpread.R index 60dcba4..47d14c6 100644 --- a/tests/testthat/setup-toySpread.R +++ b/tests/testthat/setup-toySpread.R @@ -44,7 +44,9 @@ toyInputs <- function(covs = toyCovariates(), params = toyParams(), studyAreaWithSpreadParams = sa) } +## `.plots = NA` unless given: the default "screen" would plot the spread probability in every toy run toySim <- function(objects = toyInputs(), params = list(), times = list(start = 1, end = 1)) { + if (!".plots" %in% names(params)) params$.plots <- NA SpaDES.core::simInit( times = c(times, timeunit = "year"), modules = moduleName, diff --git a/tests/testthat/test-events.R b/tests/testthat/test-events.R index 78d1a47..1bf9d6b 100644 --- a/tests/testthat/test-events.R +++ b/tests/testthat/test-events.R @@ -67,3 +67,12 @@ test_that("the prediction follows the covariates from year to year", { expect_equal(predVals(sim)[4], 0.1622730, tolerance = 1e-6) expect_equal(predVals(sim)[1], 0.19, tolerance = 1e-7) }) + +test_that("`.plots` saves the spread probability map each run (default \"screen\"), and NA plots nothing", { + sim <- toyRun(params = list(.plots = "png"), times = list(start = 1, end = 2)) + f <- SpaDES.core::outputs(sim)$file + expect_setequal(basename(f), c("SpreadProbability_yr1.png", "SpreadProbability_yr2.png")) + expect_true(all(file.exists(f))) + expect_length(SpaDES.core::outputs(toyRun())$file, 0L) # toySim() sets NA +}) + diff --git a/tests/testthat/test-metadata.R b/tests/testthat/test-metadata.R index 1766244..b371ff7 100644 --- a/tests/testthat/test-metadata.R +++ b/tests/testthat/test-metadata.R @@ -50,10 +50,10 @@ paramTable <- function(md) { test_that("parameters have the expected names, classes and defaults", { md <- SpaDES.core::moduleMetadata(module = moduleName, path = modulePath) expected <- data.frame( - class = c("integer", "numeric", "numeric", "numeric", "character", "logical", "numeric", "numeric", "character", "numeric", "numeric", "logical", "character", "integer"), + class = c("character", "integer", "numeric", "numeric", "numeric", "character", "logical", "numeric", "numeric", "character", "numeric", "numeric", "logical", "character", "integer"), ## .runInitialTime defaults to start(sim), which is 0 when only metadata is parsed - default = c("1L", "0", "1", "NA", "NA", "FALSE", "20000", "50", "c(\"natural\", \"unknown\")", "0.13", "0.276", "FALSE", "NA", "1L"), - row.names = c(".rep", ".runInitialTime", ".runInterval", ".saveInitialTime", ".studyAreaName", ".useCache", + default = c("\"screen\"", "1L", "0", "1", "NA", "NA", "FALSE", "20000", "50", "c(\"natural\", \"unknown\")", "0.13", "0.276", "FALSE", "NA", "1L"), + row.names = c(".plots", ".rep", ".runInitialTime", ".runInterval", ".saveInitialTime", ".studyAreaName", ".useCache", "ELFblendWidth", "escapeSizeHa", "fireCauses", "lowerSpreadProb", "maxFireSpread", "paramSetByReplicate", "paramSets", "repsPerSet") ) expect_identical(paramTable(md), expected)