Skip to content
Open
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 @@
# fireSimulator_validation (development version)

* New parameter `scoreELFs`: the ELFs of `hindcastPath` to read and score (less `skipELFs`). The default, empty, reads every finished ELF as before. A named ELF that has not finished stops the job and is named in the message. In a campaign whose jobs each score one ELF, the jobs no longer all read every ELF (they waited on each other's cache locks).

* 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, `<outputPath>/seededFires/<ELF>/<year>_<start>/observed.tif` and `predicted.tif` (one layer per run, `set<k>_rep<r>`), 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.
Expand Down
17 changes: 14 additions & 3 deletions fireSimulator_validation.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defineModule(sim, list(
keywords = c("fire", "validation", "hindcast", "CRPS"),
authors = c(person("Eliot", "McIntire", email = "eliot.mcintire@nrcan-rncan.gc.ca", role = c("aut", "cre"))),
childModules = character(0),
version = list(fireSimulator_validation = "0.0.0.9031"),
version = list(fireSimulator_validation = "0.0.0.9032"),
timeframe = as.POSIXlt(c(NA, NA)),
timeunit = "year",
citation = list(),
Expand Down Expand Up @@ -86,6 +86,9 @@ defineModule(sim, list(
"Mode 'weights': bySet scores of other years to select on (to tune and report on the same ELFs). NA: `weightsBySet`.",
cacheKey = FALSE), # only .inputObjects (never cached) reads the files; what it read is in init's key
defineParameter("skipELFs", "character", character(0), NA, NA, "ELFs of hindcastPath left out."),
defineParameter("scoreELFs", "character", character(0), NA, NA,
"ELFs of hindcastPath to read and score, less skipELFs; each must have finished all hindcastYears. Empty: every finished ELF. One per job lets the jobs of a campaign run without reading each other's ELFs.",
cacheKey = FALSE), # only .inputObjects (never cached) reads it; the ELFs it gives are in init's key
defineParameter("inputsPath", "character", NA_character_, NA, NA,
"Folder of the ELF input folders: <inputsPath>/<ELF>/ has rtml_<ELF>.tif, the spread-fit ledger fireSenseParams_*<ledgerSuffix>.rds and the NFDB_point_*.shp and NBAC_merged_*.shp observations (the latest of each).",
cacheKey = FALSE), # as hindcastPath
Expand Down Expand Up @@ -125,8 +128,16 @@ scoredMinHa <- function(p) if (is.na(p$minHa)) p$escapeSizeHa else p$minHa
hc <- P(sim)$hindcastPath; yrs <- P(sim)$hindcastYears; sets <- P(sim)$hindcastSets; inp <- P(sim)$inputsPath
obsDir <- file.path(outputPath(sim), "obs"); dir.create(obsDir, recursive = TRUE, showWarnings = FALSE)
elfDir <- file.path(outputPath(sim), "elfs"); dir.create(elfDir, recursive = TRUE, showWarnings = FALSE)
done <- Filter(function(e) length(hindcastFinishedYears(hc, e, yrs, sets)) == length(yrs),
setdiff(list.files(hc), P(sim)$skipELFs))
isDone <- function(e) length(hindcastFinishedYears(hc, e, yrs, sets)) == length(yrs)
wanted <- setdiff(if (length(P(sim)$scoreELFs)) P(sim)$scoreELFs else list.files(hc), P(sim)$skipELFs)
if (length(P(sim)$scoreELFs)) {
if (!length(wanted)) stop("every ELF of scoreELFs is also in skipELFs: ", paste(P(sim)$scoreELFs, collapse = ", "))
if (length(missing <- setdiff(wanted, list.files(hc)))) stop("scoreELFs not in hindcastPath ", hc, ": ", paste(missing, collapse = ", "))
}
done <- Filter(isDone, wanted)
if (length(P(sim)$scoreELFs) && length(unfinished <- setdiff(wanted, done)))
stop("scoreELFs not finished in ", hc, " (no hindcast of every one of hindcastYears ", min(yrs), "-", max(yrs), " in hindcastSets ", paste(sets, collapse = ", "), "): ",
paste(unfinished, collapse = ", "))
sim$validationELFs <- readHindcastELFs(done, hc, years = yrs, sets = sets, inputsDir = inp, ledgerSuffix = P(sim)$ledgerSuffix,
covariateDir = if (is.na(P(sim)$covariatePath)) hc else P(sim)$covariatePath,
obsCacheDir = obsDir, elfCacheDir = elfDir, fireCauses = P(sim)$fireCauses,
Expand Down
15 changes: 15 additions & 0 deletions tests/testthat/test-module.R
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,18 @@ test_that("readHindcastELFs: the same hindcast under another root is a cache hit
do.call(readHindcastELFs, elfReadArgs(root, "1.1", dtReader))
expect_equal(length(readLines(counter)), 1L)
})

test_that("scoreELFs reads and scores only the named ELFs; a named ELF that has not finished stops the job naming it", {
skip_if_not_installed("SpaDES.core")
root <- withr::local_tempdir()
a <- toyElf(seed = 3, name = "9.9"); b <- toyElf(seed = 4, name = "7.7")
hc <- toyHindcastFiles(a, root); toyHindcastFiles(b, root)
dir.create(file.path(hc, "8.8")) # an ELF with no finished year
elfsOf <- function(sim) vapply(sim$validationELFs, `[[`, "", "elf")
expect_setequal(elfsOf(runToyModule(root, a, hc)), c("9.9", "7.7")) # default: every finished ELF
expect_equal(elfsOf(runToyModule(root, a, hc, scoreELFs = "7.7")), "7.7")
expect_equal(elfsOf(runToyModule(root, a, hc, scoreELFs = c("7.7", "9.9"), skipELFs = "9.9")), "7.7")
expect_error(runToyModule(root, a, hc, scoreELFs = c("7.7", "8.8")), "not finished.*8.8")
expect_error(runToyModule(root, a, hc, scoreELFs = c("7.7", "6.6")), "not in hindcastPath.*6.6") # no folder: a typo
expect_error(runToyModule(root, a, hc, scoreELFs = "7.7", skipELFs = "7.7"), "also in skipELFs")
})
Loading