diff --git a/NEWS.md b/NEWS.md index 7417ae9..1a0d44e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,6 @@ # fireSense_dataPrepFit 1.3.0.9011 (development version) +- New parameter `fitFileSuffix` (default `""`), the same in `fireSense_spreadFit`, `fireSense_ignitionFit` and `fireSense_ELFs` (checked with `paramCheckOtherMods()`): with `spreadFitFilename = "latest"`, the SpreadFit ledger is read from the files with that suffix only (`fireSenseUtils::latestSpreadFits(suffix =)`), so a test run never reads the production fits. `""` is unchanged. Needs fireSenseUtils (>= 0.2.3.9104). - R CMD check of the module's package form has no notes: `NEWS.md` had two "(development version)" headings and none with a version, so it could not be parsed (one heading now, with the version), undefined names are declared (`R/globals.R`), and `methods`, `stats` and `utils` are in `reqdPkgs` for `is()`, `setNames()`, `var()`, `getFromNamespace()` and `modifyList()`. No change to results. One check warning remains: `SpaDES.core::convertToPackage()` writes `import()` for every `reqdPkgs` entry, so the whole-package imports clash ("replacing previous import"). - Fixed: `.inputObjects` (1-2 h of data preparation per ELF) was redone whenever the SpreadFit ledger changed, e.g. when any ELF's fit landed, because `spreadFitPreRun` was declared as an input and `fireSense_ELFs` puts the whole ledger in the simList before `.inputObjects` runs. It is now an output only. `.inputObjects` never reads it; `Init()` always assigns it (this study area's rows from `CacheGeo`, or `NULL`), and `dataPrepBuild`, `prepIgnitionFitData`, `prepEscapeFitData` and `prepSpreadFitData` key on it through `.useCacheArgs`, so a new fit of this study area still re-runs them and a cached event cannot restore an old row. Results are unchanged; the `.inputObjects` key changes once. diff --git a/R/readSpreadFitLedger.R b/R/readSpreadFitLedger.R index 7389704..4ef4ace 100644 --- a/R/readSpreadFitLedger.R +++ b/R/readSpreadFitLedger.R @@ -6,14 +6,16 @@ #' (`prepInputs()` would otherwise find its checksum changed and try to download it). #' #' @param spreadFitFilename The module's `spreadFitFilename`: a file in `cloudFolderID`, or `"latest"`. +#' @param fitFileSuffix With `"latest"`, only ledger files with this suffix are read; see +#' `fireSenseUtils::spreadFitFilenameFor()`. #' @param cloudFolderID The Google Drive folder holding the ledger files. #' @param domain The study area, an `sf` object. #' @param destinationPath Where the ledger files are downloaded. #' @return What `CacheGeo()` returns: the ledger rows within `domain`, or `NULL`. -readSpreadFitLedger <- function(spreadFitFilename, cloudFolderID, domain, destinationPath) { +readSpreadFitLedger <- function(spreadFitFilename, cloudFolderID, domain, destinationPath, fitFileSuffix = "") { purge <- 7 # download the named file again: it changes on Drive if (identical(spreadFitFilename, "latest")) { - latest <- fireSenseUtils::latestSpreadFits(cloudFolderID, destinationPath) + latest <- fireSenseUtils::latestSpreadFits(cloudFolderID, destinationPath, suffix = fitFileSuffix) if (is.null(latest)) return(NULL) tf <- tempfile(fileext = ".rds") diff --git a/fireSense_dataPrepFit.R b/fireSense_dataPrepFit.R index 101f4c9..41abb8d 100644 --- a/fireSense_dataPrepFit.R +++ b/fireSense_dataPrepFit.R @@ -17,7 +17,7 @@ defineModule(sim, list( reqdPkgs = list("data.table", "fastDummies", "Require", "PredictiveEcology/reproducible@development (>= 3.2.1.9042)", # CacheGeo re-reads a changed local file "PredictiveEcology/climateData@development (>= 2.2.3.9006)", - "PredictiveEcology/fireSenseUtils@development (>= 0.2.3.9100)", # dataYearBefore(), dataYearElement(before =); defaultFireYears() from 1986 + "PredictiveEcology/fireSenseUtils@development (>= 0.2.3.9104)", # dataYearBefore(), dataYearElement(before =); defaultFireYears() from 1986 "FOR-CAST/fireregimetools@main (>= 0.1.0.9008)", "ggplot2", "methods", "parallel", "purrr", "raster", "sf", "sp", "stats", "utils", "PredictiveEcology/LandR@development (>= 1.2.0.9015)", @@ -115,6 +115,12 @@ defineModule(sim, list( "the species, fuel and climate objects itself, as for an unfitted study area.", "Set it for every fireSense module at once with `.globals = list(heldOutFold = 1L)`;", "`Init()` stops if `fireSense_spreadFit` has a different value. Any other value is an error.")), + defineParameter("fitFileSuffix", "character", "", NA, NA, + paste("Text put after the model tag in the ledger file names, e.g. `\"_boundfix_rep81\"`, so a test run", + "writes and reads (`\"latest\"`) only its own files, never the production ones. `\"\"` (the", + "default) is the production ledger. The same parameter of `fireSense_spreadFit`, `fireSense_ignitionFit` and `fireSense_ELFs`;", + "set it for all with `.globals = list(fitFileSuffix = ...)`; modules that disagree stop", + "(`SpaDES.core::paramCheckOtherMods()`). Ignored when a file name is given.")), defineParameter("spreadFitGoogleDriveFolder", "character", fireSenseUtils::spreadFitLedgerFolder(), NA, NA, paste("URL of the Google Drive folder holding the ledger of previous SpreadFit results", @@ -415,7 +421,8 @@ Init <- function(sim) { paste(format(heldOutFold), collapse = ", ")) sim$spreadFitPreRun <- if (isTRUE(is.na(heldOutFold))) { readSpreadFitLedger(Par$spreadFitFilename, Par$spreadFitGoogleDriveFolder, - domain = sa, destinationPath = inputPath(sim)) + domain = sa, destinationPath = inputPath(sim), + fitFileSuffix = SpaDES.core::paramCheckOtherMods(sim, "fitFileSuffix")) } mod$haveSpreadFit <- is(sim$spreadFitPreRun, "sf") || is(sim$spreadFitPreRun, "data.frame") if (mod$haveSpreadFit) { diff --git a/tests/testthat/test-metadata.R b/tests/testthat/test-metadata.R index 90fe911..49578f7 100644 --- a/tests/testthat/test-metadata.R +++ b/tests/testthat/test-metadata.R @@ -99,7 +99,7 @@ test_that("parameters are the expected names", { "forestedLCC", "fuelClassCol", "fuelCovariates", "heldOutFold", "igAggFactor", "minBufferSize", "minCovariateProp", "nonflammableLCC", "nonForestCanBeYoungAge", "scanfiVersion", "sppEquivCol", "spreadFitFilename", - "spreadFitGoogleDriveFolder", "spreadIntercept", "targetFuelClasses", + "spreadFitGoogleDriveFolder", "fitFileSuffix", "spreadIntercept", "targetFuelClasses", "useRasterizedFireForSpread", "whichModulesToPrepare")) ) }) diff --git a/tests/testthat/test-readSpreadFitLedger.R b/tests/testthat/test-readSpreadFitLedger.R index c65ba10..d873a57 100644 --- a/tests/testthat/test-readSpreadFitLedger.R +++ b/tests/testthat/test-readSpreadFitLedger.R @@ -14,7 +14,7 @@ ledger <- function(ids, value) { test_that("'latest' reads the combined ledger and keeps only this study area's rows", { d <- withr::local_tempdir() - testthat::local_mocked_bindings(latestSpreadFits = function(cloudFolderID, destinationPath, polygonIDs = NULL) + testthat::local_mocked_bindings(latestSpreadFits = function(cloudFolderID, destinationPath, polygonIDs = NULL, suffix = "") ledger(c("4.1", "4.3"), c(1, 2)), .package = "fireSenseUtils") domain <- sf::st_as_sf(square(4e4)) # the second polygon, 4.3 out <- suppressMessages(readSpreadFitLedger("latest", "folder", domain, d)) @@ -26,7 +26,7 @@ test_that("a changed ledger is read again, not returned from an earlier read", { d <- withr::local_tempdir() domain <- sf::st_as_sf(square(4e4)) value <- 2 - testthat::local_mocked_bindings(latestSpreadFits = function(cloudFolderID, destinationPath, polygonIDs = NULL) + testthat::local_mocked_bindings(latestSpreadFits = function(cloudFolderID, destinationPath, polygonIDs = NULL, suffix = "") ledger(c("4.1", "4.3"), c(1, value)), .package = "fireSenseUtils") first <- suppressMessages(readSpreadFitLedger("latest", "folder", domain, d)) value <- 5 # a newer fit of 4.3 arrives @@ -48,3 +48,17 @@ test_that("a named file is read from the Drive folder, as before", { expect_identical(rec$args$cloudFolderID, "folderURL") expect_identical(rec$args$action, "nothing") }) + +test_that("'latest' reads only the files with this run's fitFileSuffix", { + d <- withr::local_tempdir() + asked <- NULL + testthat::local_mocked_bindings(latestSpreadFits = function(cloudFolderID, destinationPath, polygonIDs = NULL, suffix = "") { + asked <<- suffix + ledger("4.3", 2) + }, .package = "fireSenseUtils") + domain <- sf::st_as_sf(square(2e4 * 1)) + suppressMessages(readSpreadFitLedger("latest", "folder", domain, d, fitFileSuffix = "_boundfix_rep81")) + expect_identical(asked, "_boundfix_rep81") + suppressMessages(readSpreadFitLedger("latest", "folder", domain, d)) + expect_identical(asked, "") +})