From 79a6ab23ec7487428cd601311e71170a88e98af4 Mon Sep 17 00:00:00 2001 From: Eliot McIntire Date: Fri, 4 Dec 2026 11:00:17 -0700 Subject: [PATCH] Fix: burn$grid is the burn maps' grid (core), not the ignition maps'; S9 and writeSeededFires used the wrong cell coordinates Co-Authored-By: Claude Opus 5.5 --- NEWS.md | 2 ++ R/readFireSenseHindcast.R | 4 ++-- tests/testthat/test-reader.R | 17 +++++++++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/NEWS.md b/NEWS.md index 0f91f82..a688e63 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # fireSimulator_validation (development version) +* Fix: S9 placed every fire's circle with the wrong grid. `readHindcastLocation()` stored the ignition maps' grid (960 m in the fireSense hindcasts) as `burn$grid`, but the burn cells are numbered on the burn maps' 240 m grid (`core`), so `gridXY()` put the cells and starts in the wrong places. `burn$grid` is now `gridOf(core)`. On ELF 14.3 (lp05 in-sample, 141 fires) the median AUC goes from 0.94 to 0.72, AUC minus the distance-only baseline from +0.28 to -0.08, and the share of a fire inside its circle from 0.42 to 0.93. Every S9 value scored before this fix is wrong. `writeSeededFires()` failed on the same grid ("no valid cells"). The cached ELF reads are rebuilt once. + * S8 and S9 per parameter set, and the seeded fires as maps. `scoreBySet()` now also scores S8 (size given escape) and S9 (shape given escape) of each set of an in-sample ELF: columns `S8_rho`, `S8_log2ratio`, `S9_aucDiff`, `S9_auc`, `S9_aucDist`, `S9_shareInside`, `S8_nFires`, `S9_nFires`, with `_runLo`/`_runHi` (the ELF rows of `escapeResultRows()`). `elfOfSet()` now keeps only the set's runs in `location$seeded$sizes` and `location$burn` too; before, it subset only `simYears` and `simSizes`. To split the burns by set, `readHindcastLocation()` records the run of each burn map (`burn$runIds`, from `hindcast_reps.csv`, new `burnRunIds()`) and keeps each burned cell's fire (`fire`, its burn ID), and `readHindcastSeeded()` keeps `fire` in `sizes`; the grid also carries its `crs`. New `writeSeededFires()`: `writeOutputs` writes, for each in-sample ELF, `/seededFires//_/observed.tif` and `predicted.tif` (one layer per run, `set_rep`), holding exactly the cells S8 and S9 score (1 burned, 0 burnable and not burned, NA not scored, so lakes are NA), with `fires.csv`; a predicted layer holds only the fire grown from that start. `bySet` defaults to `NA`: score by set when some ELF's runs are of more than one set. The cached ELF reads are rebuilt once. * In-sample hindcasts (they spread the observed escapes from their real starts and years, `FS_HC_IGNITIONS=observedEscapes`): S7 is no longer scored, because its whole-ELF AUC (about 0.9) only shows that the fires start where the observed ones did. New tests S8 and S9 score what a fire does once it has escaped (`R/escapeTests.R`). S8, size given escape: for each seeded observed escape, the median over runs of the final size of the fire that grew from it against the fire's observed size; per ELF the Spearman correlation `rho` (single-run range), the median `log2ratio` of simulated over observed size and `nFires`. S9, shape given escape: for each seeded fire with N observed cells, the circle around its start that holds 2N burnable core cells; the AUC of the share of runs that burned each cell for the cells the fire burned against the others (`aucOf()`), reported as the median over fires with a single-run range, beside a distance-only baseline `aucDist` (each cell of the circle scored by its closeness to the start) and the headline `aucDiff` (model AUC minus baseline AUC), `shareInside` (the share of the fire's cells inside the circle, since cells outside are not counted) and `nFires`. Both are pooled over ELFs with `bootstrapPooled()` and are rows of `wave1_results.csv` (tests `S8`, `S9`); the page has a "Given an escape" table, shown only when there are such rows. A run is in-sample when every run of every year with a fire started the same fires from the same cells (`hindcastInSample()`, read from `fireSense_burnSummary.csv`: the hindcast files record no setting, only the folder name does); `readFireSenseHindcast()` has a new argument `inSample` (default `NA`: detect) and puts `inSample` and `location$seeded` in the ELF input. A fire is tied to its observed escape by its start cell (`igLoc` of `fireSense_burnSummary.csv`, in the order of `hindcast_sizes.csv`), and the start to the observed polygon that holds it. S8 and S9 are not scored for normal runs: their fires do not start where the observed ones did. diff --git a/R/readFireSenseHindcast.R b/R/readFireSenseHindcast.R index 1e12162..8926763 100644 --- a/R/readFireSenseHindcast.R +++ b/R/readFireSenseHindcast.R @@ -151,7 +151,7 @@ toScoredIndex <- function(scoredCells, n) { i <- integer(n); i[scoredCells] <- s #' `hindcast_observedPoints.gpkg` and `hindcast_observedPolys.gpkg`; the other years' observed points and polygons come from the record `rec` (as [readHindcastObserved()]). Each year is reduced as #' it is read ([ignitionYear()], the burned cells of each run), so no stack of maps is held. Burns are on the cells of `core` (the burnable core, as the other tests count them). #' @param rec [observedRecord()]. @param hindcastDir,elf,scored,sets As [readHindcastSim()]. @param core [hindcastCore()]. -#' @return `list(ign, burn)`, see the top of `R/spatialTests.R` (`burn` also has `grid` and `gridCell`, the grid and the grid cell of each scored cell, `runIds`, the run of [readHindcastSim()] of each burn run, +#' @return `list(ign, burn)`, see the top of `R/spatialTests.R` (`burn` also has `grid` and `gridCell`, the grid of `core` and the grid cell of each scored cell: the burn maps' grid, which can be finer than the ignition maps', `runIds`, the run of [readHindcastSim()] of each burn run, #' and in `sim` a `fire` column, the fire's number in its run: its burn ID); `NULL` when the hindcast has no ignition maps. readHindcastLocation <- function(rec, hindcastDir, elf, scored, sets, core) { dirs <- function(y) file.path(hindcastDir, elf, y, sets) @@ -180,7 +180,7 @@ readHindcastLocation <- function(rec, hindcastDir, elf, scored, sets, core) { runIds <- perYear[[1]]$runIds if (!all(vapply(perYear, function(x) identical(x$runIds, runIds), TRUE))) stop(elf, ": the burn maps of the scored years are not of the same runs") list(ign = list(grid = grid, points = pointsOf, sim = lapply(perYear, `[[`, "ign")), - burn = list(nCells = length(scoredCells), nRuns = perYear[[1]]$nRuns, runIds = runIds, obs = obsBurn, sim = lapply(perYear, `[[`, "burn"), grid = grid, gridCell = scoredCells)) + burn = list(nCells = length(scoredCells), nRuns = perYear[[1]]$nRuns, runIds = runIds, obs = obsBurn, sim = lapply(perYear, `[[`, "burn"), grid = gridOf(core), gridCell = scoredCells)) } ## The run (as `run` of [readHindcastSim()]) of each `hindcast_burnID_rep.tif` of `files`: rep NN of the `hindcast_reps.csv` beside it diff --git a/tests/testthat/test-reader.R b/tests/testthat/test-reader.R index 250855f..d1d2c13 100644 --- a/tests/testthat/test-reader.R +++ b/tests/testthat/test-reader.R @@ -181,3 +181,20 @@ test_that("observedRecord counts the fires that start in the core at their whole big <- observedRecord(core, flammable, "nfdb", "nbac", years = 2000L, escapeSizeHa = 600) # escapeSizeHa is the parameter, not a constant expect_equal(big$counts$escapes, 0); expect_equal(big$escapeSizeHa, 600) }) + +test_that("readHindcastLocation: burn$grid is the burn maps' grid (core), not the coarser ignition maps' grid", { + root <- withr::local_tempdir(); d <- file.path(root, "9.9", 1986, 1); dir.create(d, recursive = TRUE) + core <- terra::rast(nrows = 8, ncols = 8, xmin = 0, xmax = 800, ymin = 0, ymax = 800, crs = "EPSG:3978", vals = 1) + core[c(1, 2)] <- NA # two cells not burnable + ign <- terra::rast(nrows = 2, ncols = 2, xmin = 0, xmax = 800, ymin = 0, ymax = 800, crs = "EPSG:3978", vals = 0.25) # 4x coarser, as the real hindcasts + terra::writeRaster(ign, file.path(d, "hindcast_ignitionProb_rep01.tif")) + burned <- c(20L, 21L, 60L) + b <- terra::setValues(core, NA); b[burned] <- 1L + terra::writeRaster(b, file.path(d, "hindcast_burnID_rep01.tif"), datatype = "INT2U") + write.csv(data.frame(rep = 1L, paramSet = 1L, year = 1986L, time = 1986), file.path(d, "hindcast_reps.csv"), row.names = FALSE) + loc <- readHindcastLocation(list(polys = list(), points = list()), root, "9.9", 1986L, 1L, core) + expect_equal(loc$burn$grid[c("nrow", "ncol")], list(nrow = 8, ncol = 8)); expect_equal(loc$ign$grid[c("nrow", "ncol")], list(nrow = 2, ncol = 2)) + cells <- loc$burn$gridCell[loc$burn$sim[["1986"]]$cell] + expect_equal(sort(cells), burned) + xy <- gridXY(loc$burn$grid, cells); expect_equal(cbind(xy$x, xy$y), unname(terra::xyFromCell(core, cells))) # S9's circle is placed with these coordinates +})