diff --git a/NEWS.md b/NEWS.md index 21b7f93..6b1de7c 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # fireSense_spreadPredict (development version) +- 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. + - New parameters `paramSets` (path to an `.rds` or `.csv` of spread parameter vectors for the simulated ELF; default `NA`, behaviour unchanged) and `repsPerSet`. The sets replace the simulated ELF's ledger rows (neighbour ELFs keep theirs); replicate r uses set `ceiling(r / repsPerSet)`, calls `set.seed(k)` with k its number within the set (common random numbers across sets), and the surface is recomputed only when the set changes. The link, lower asymptote, `gamma` and covariate centres come from the ledger row; a set lacking a coefficient of the row stops. The set index is output in `fireSense_SpreadParamSet`. New input `.ELFind`, as in `fireSense_dataPrepPredict`. - A ledger row with `spreadLink` `"powerLawSmooth"` or `"richards"` (and its `lowerAsymptote_spread`, `richardsNu_spread`) is predicted with that link (`fireSenseUtils::spreadProbFromCovs()`); `"richards"` has no `gamma`. New output `fireSense_SpreadLink` names the link, and `fireSense_SpreadPowerLaw` and `fireSense_SpreadLinear` are given for all three linear-predictor links, with the link's own parameter names (`fireSenseUtils::spreadLinkParNames`). Fitted ELFs with different such links cannot be blended (an error). Needs fireSenseUtils with the links. diff --git a/fireSense_spreadPredict.R b/fireSense_spreadPredict.R index c9242d7..9a961af 100644 --- a/fireSense_spreadPredict.R +++ b/fireSense_spreadPredict.R @@ -231,11 +231,12 @@ spreadPredictRun <- function(sim) { set.seed(paramSetsSeed(P(sim)$.runInitialTime, ps$k, ps$id)) ## everything the outputs depend on; `time(sim)` is not in it: with one covariate table it does not change them key <- list(P(sim)$paramSets, ps$set, sim$fireSense_SpreadParamSet, covs, sa[setdiff(names(sa), "params")], - ps$params, terra::values(sim$flammableRTM, mat = FALSE), - if (!is.null(sim$rasterToMatchLargeELF)) terra::values(sim$rasterToMatchLargeELF, mat = FALSE), + ps$params, rasterVersion("flammableRTM", sim$flammableRTM), + rasterVersion("rasterToMatchLargeELF", sim$rasterToMatchLargeELF), P(sim)$ELFblendWidth, P(sim)$lowerSpreadProb, P(sim)$maxFireSpread) - if (identical(mod$surface$key, key)) { - for (nm in surfaceOutputs) sim[[nm]] <- unwrapIfRaster(mod$surface$outputs[[nm]]) + if (identical(mod$surface$key, key) && + identical(mod$surface$samples, lapply(mod$surface$outputs, rasterSample))) { ## unedited since stored + for (nm in surfaceOutputs) sim[[nm]] <- mod$surface$outputs[[nm]] return(invisible(sim)) } } @@ -315,7 +316,11 @@ spreadPredictRun <- function(sim) { sim$fireSense_SpreadPredicted[pred$pixelID] <- pred$spreadProb if (!is.na(P(sim)$paramSets)) - mod$surface <- list(key = key, outputs = lapply(stats::setNames(surfaceOutputs, surfaceOutputs), function(nm) wrapIfRaster(sim[[nm]]))) + { + outs <- lapply(stats::setNames(surfaceOutputs, surfaceOutputs), + function(nm) if (inherits(sim[[nm]], "SpatRaster")) terra::deepcopy(sim[[nm]]) else sim[[nm]]) + mod$surface <- list(key = key, outputs = outs, samples = lapply(outs, rasterSample)) + } invisible(sim) } @@ -333,18 +338,26 @@ spreadPredictRun <- function(sim) { #' within the set) and `params` (the one-row `data.frame`). paramSetsForRep <- function(sim, sa, rep) { idCol <- fireSenseUtils::polygonIDTxt - own <- if (NROW(sa) == 1L) 1L else match(as.character(sim$.ELFind), as.character(sa[[idCol]])) - if (length(own) != 1L || is.na(own)) + elf <- as.character(sim$.ELFind) + own <- if (NROW(sa) == 1L) 1L else if (length(elf) == 1L && !is.na(elf)) which(as.character(sa[[idCol]]) == elf) else integer() + if (length(own) > 1L) + stop("fireSense_spreadPredict: sim$studyAreaWithSpreadParams has ", length(own), " rows for ELF ", elf, + "; paramSets needs exactly one", call. = FALSE) + if (length(own) != 1L) stop("fireSense_spreadPredict: paramSets with several fitted ELFs needs sim$.ELFind to be ONE of the ELFs of ", "sim$studyAreaWithSpreadParams (", paste(sa[[idCol]], collapse = ", "), "); it is ", if (is.null(sim$.ELFind)) "missing" else paste(sim$.ELFind, collapse = ", "), call. = FALSE) path <- P(sim)$paramSets if (!identical(mod$paramSetsFile$path, path)) { sets <- if (grepl("[.]csv$", path, ignore.case = TRUE)) data.table::fread(path, data.table = FALSE) else readRDS(path) - if (is.data.frame(sets) && "params" %in% names(sets)) { ## ledger rows + isLedger <- is.data.frame(sets) && "params" %in% names(sets) + if (isLedger) { ## ledger rows if (idCol %in% names(sets)) sets <- sets[as.character(sets[[idCol]]) %in% as.character(sa[[idCol]][own]), , drop = FALSE] sets <- do.call(rbind, lapply(sets$params, as.data.frame)) } + if (!NROW(sets)) + stop("fireSense_spreadPredict: the paramSets file ", path, " has no parameter set", + if (isLedger) paste0(" (the ledger rows have none for ELF ", sa[[idCol]][own], ")"), call. = FALSE) mod$paramSetsFile <- list(path = path, sets = as.data.frame(sets)) } sets <- mod$paramSetsFile$sets @@ -376,9 +389,37 @@ paramSetsSeed <- function(year, k, id) { as.integer((round(year) * 1000003 + k * 7919 + idHash * 104729) %% .Machine$integer.max) } -## a SpatRaster is held by a pointer, so what `mod$surface` keeps is a copy -wrapIfRaster <- function(x) if (inherits(x, "SpatRaster")) terra::wrap(x) else x -unwrapIfRaster <- function(x) if (inherits(x, "PackedSpatRaster")) terra::unwrap(x) else x +#' Values of a raster at up to 1000 evenly spaced cells +#' +#' A cheap check that a raster has not been edited in place (`terra::set.values()`): reading all the values of +#' a 20-million-cell raster takes about as long as predicting. +#' +#' @param r A `SpatRaster`, or anything else (returned as `NULL`). +#' @return The values, or `NULL`. +rasterSample <- function(r) { + if (!inherits(r, "SpatRaster")) return(NULL) + n <- terra::ncell(r) + r[unique(round(seq(1, n, length.out = min(n, 1000L))))][[1]] +} + +#' A number that changes when a raster input changes +#' +#' The version of `r` only changes with a new `SpatRaster` object (terra's `[<-` and `values<-` make one) or +#' different sampled values (`rasterSample()`); `mod$rasterStamp` keeps the last object, so its address cannot be +#' reused by another. It is for the key of `mod$surface`, so the full raster is not read every replicate. +#' +#' @param name character; `mod$rasterStamp` entry. +#' @param r A `SpatRaster`, or `NULL`. +#' @return An integer; `NULL` for `NULL`. +rasterVersion <- function(name, r) { + if (is.null(r)) return(NULL) + cur <- mod$rasterStamp[[name]] + smp <- rasterSample(r) + if (is.null(cur) || !identical(cur$obj@pntr, r@pntr) || !identical(cur$sample, smp)) + cur <- list(obj = r, sample = smp, version = (if (is.null(cur)) 0L else cur$version) + 1L) + mod$rasterStamp[[name]] <- cur + cur$version +} #' Each ELF's weight at each pixel #' diff --git a/tests/testthat/test-paramSets.R b/tests/testthat/test-paramSets.R index b4b8fbb..bcf3609 100644 --- a/tests/testthat/test-paramSets.R +++ b/tests/testthat/test-paramSets.R @@ -57,8 +57,9 @@ test_that("reps of one set reuse its surface: it is computed once per set", { surf <- sim@.xData$.modObjs[[moduleName]]$surface expect_identical(surf$key[[2]], 1L) mo <- sim@.xData$.modObjs[[moduleName]] - r <- terra::unwrap(mo$surface$outputs$fireSense_SpreadPredicted); r[1] <- 99 - mo$surface$outputs$fireSense_SpreadPredicted <- terra::wrap(r) + r <- mo$surface$outputs$fireSense_SpreadPredicted; r[1] <- 99 + mo$surface$outputs$fireSense_SpreadPredicted <- r + mo$surface$samples <- lapply(mo$surface$outputs, rasterSample) end(sim) <- 2 sim <- SpaDES.core::spades(sim, debug = FALSE) expect_identical(predVals(sim)[1], 99) @@ -136,3 +137,45 @@ test_that("a neighbour ELF with 2 rows still cycles with the replicate: the cach expect_equal(vals[[1]], 0.13 + (0.25 - 0.13) / 2, tolerance = 1e-9) expect_equal(vals[[2]], 0.13 + (0.35 - 0.13) / 2, tolerance = 1e-9) ## ELF A's row 2, not rep 1's surface }) + +test_that("a duplicate ELF id in the ledger rows stops", { + ins <- multiInputs(); ins$.ELFind <- "B" + sa <- ins$studyAreaWithSpreadParams; sa$polygonID <- c("B", "B"); ins$studyAreaWithSpreadParams <- sa + expect_error(toyRun(ins, params = list(ELFblendWidth = 6000, paramSets = setsFile(sa$params[[2]]))), "2 rows for ELF B") +}) + +test_that("a file of ledger rows with none for this ELF stops with a clear message", { + ins <- multiInputs(); ins$.ELFind <- "B" + led <- data.frame(polygonID = "A"); led$params <- list(ins$studyAreaWithSpreadParams$params[[1]]) + expect_error(toyRun(ins, params = list(ELFblendWidth = 6000, paramSets = setsFile(led))), + "ledger rows have none for ELF B") + ## and with rows for it, they are the sets + led <- data.frame(polygonID = c("A", "B")); led$params <- ins$studyAreaWithSpreadParams$params + expect_identical(unname(toyRun(ins, params = list(ELFblendWidth = 6000, paramSets = setsFile(led)))$fireSense_SpreadParamSet), c(1L, 1L)) +}) + +test_that("a raster input changes the surface key only when it changes, without reading all its values", { + r <- terra::rast(nrows = 100, ncols = 100, vals = 1) + e <- new.env(); e$rasterStamp <- NULL + v <- function(x) { mod <- e; environment(rasterVersion) <- environment(); rasterVersion("r", x) } + v1 <- v(r) + expect_identical(v(r), v1) ## the same object: the same version, no re-read + r2 <- r; r2[1] <- 0 + expect_gt(v(r2), v1) ## a new object + v2 <- v(r2) + terra::set.values(r2, 1L, 5) ## an in-place edit, same pointer + expect_gt(v(r2), v2) + expect_null(v(NULL)) +}) + +test_that("a changed flammableRTM recomputes the surface of the same set", { + p <- twoSets() + sim <- toySim(toyInputs(), params = list(paramSets = setsFile(p), repsPerSet = 2L, paramSetByReplicate = TRUE)) + sim <- SpaDES.core::spades(sim, debug = FALSE) + v1 <- predVals(sim) + fl <- sim$flammableRTM; fl[1] <- 0; sim$flammableRTM <- fl ## cell 1 stops being flammable ... + covs <- sim$fireSense_SpreadCovariates; sim$fireSense_SpreadCovariates <- covs[covs$pixelID != 1L, ] + end(sim) <- 2; sim <- SpaDES.core::spades(sim, debug = FALSE) + expect_identical(unname(sim$fireSense_SpreadParamSet), 1L) + expect_true(is.na(predVals(sim)[1])); expect_false(is.na(v1[1])) +})