From 47128a49ae1f9e900032ad7851a11fab7b089dfe Mon Sep 17 00:00:00 2001 From: Eliot McIntire Date: Mon, 16 Nov 2026 12:43:17 -0700 Subject: [PATCH 1/2] Leave outputDir out of the event cache keys (defineParameter cacheKey = FALSE) A path or core count that differs between machines made the same run a cache miss on another machine. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_014G18Mik3fH4z5M5CyMSUS6 --- NEWS.md | 1 + canClimateData.R | 5 +++-- tests/testthat/test-metadata.R | 8 ++++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/NEWS.md b/NEWS.md index ffc2cbf..9f60f67 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,6 @@ # canClimateData (development version) +- `outputDir` is no longer part of the module's event cache keys (`defineParameter(cacheKey = FALSE)`; SpaDES.core >= 3.2.1.9047). It only says where the prepared climate files go, and `Init()` already left it out of the `prepClimateLayers()` key, so a cache built on one machine can match on another with a different folder. The event cache keys of this module change once. - Rerunning this module on a finished run whose years are not calendar years (NRV), with that run's `climateYearRecord` (from the climateYear module), now prepares the climate years the record holds for the simulation years the rerun serves (the load times of its inputs, `inputs(sim)`, which `alreadyRun` sets to the finished run's save times), not the years in `climateVariables` or `historicalClimateYears`/`projectedClimateYears`. Variables by period, and `calcCumMDC` ones (which need consecutive years), keep their years. `times` are not checked as calendar years then. `climateYearRecord` is a new optional input; in a normal run climateYear writes it after this module's `init`, and `loadOrder` keeps that order. - `climateData` is now required from its modsForFireSense branch (>= 2.2.3.9011). `checkCalendarYears()` is only there; climateData development reached 2.2.3.9011 without it, so the old floor installed a climateData whose `init` failed with "could not find function". - `projectedClimateYears` now defaults to 2017:2100 instead of 2011:2100. The projected archive for tile 39 (CNRM-ESM2-1 ssp370) has no 2013-2016 files, and observed climate now covers 1901-2024, so projections before 2017 are not needed. diff --git a/canClimateData.R b/canClimateData.R index fc077a8..87e7ab0 100644 --- a/canClimateData.R +++ b/canClimateData.R @@ -25,7 +25,7 @@ defineModule(sim, list( "PredictiveEcology/fireSenseUtils@development (>= 0.0.5.9046)", "PredictiveEcology/LandR@development (>= 1.1.0.9064)", "PredictiveEcology/reproducible@development (>= 3.2.1.9060)", - "PredictiveEcology/SpaDES.core@development (>= 2.1.5)", + "PredictiveEcology/SpaDES.core@development (>= 3.2.1.9047)", # defineParameter(cacheKey = FALSE) "PredictiveEcology/SpaDES.tools@development (>= 2.0.4.9002)"), parameters = rbind( defineParameter("bufferDist", "numeric", 20000, NA, NA, @@ -48,7 +48,8 @@ defineModule(sim, list( "(e.g., when running multiple simulation replicates)", "to avoid multiple copies of the climate data.", "If not specified by the user, a subdirectory of the simulation output", - "directory will be used (i.e., `file.path(outputPath(sim), 'climate')`.")), + "directory will be used (i.e., `file.path(outputPath(sim), 'climate')`."), + cacheKey = FALSE), # only where the files go: `prepClimateLayers()` leaves `dstdir` out of its key too defineParameter("projectedClimatePeriod", "character", NULL, NA, NA, paste("period to use for projected climate normals", "if `climateVariables` not supplied.")), diff --git a/tests/testthat/test-metadata.R b/tests/testthat/test-metadata.R index 0fe5b54..c5b78df 100644 --- a/tests/testthat/test-metadata.R +++ b/tests/testthat/test-metadata.R @@ -51,3 +51,11 @@ test_that("projected climate years start in 2017", { pars <- stats::setNames(md$parameters$default, md$parameters$paramName) expect_identical(pars[["projectedClimateYears"]], 2017:2100) }) + +test_that("`outputDir` is not part of the module's cache keys", { + ## it only says where the prepared climate files go (`Init()` leaves `dstdir` out of the + ## `prepClimateLayers()` key too), so a cache built on one machine can match on another + p <- SpaDES.core::moduleMetadata(module = moduleName, path = modulePath)$parameters + expect_false(p$cacheKey[p$paramName == "outputDir"]) + expect_true(all(p$cacheKey[p$paramName != "outputDir"])) +}) From af7472bc797c6f4f0ef32e41138b51bdbae14374 Mon Sep 17 00:00:00 2001 From: Eliot McIntire Date: Mon, 16 Nov 2026 13:04:10 -0700 Subject: [PATCH 2/2] outputDir: pass asPath(dir, nParentDirs) to key the event on the folder's last components; drop cacheKey = FALSE An outputDir that is a shared hindcast climate folder must still miss for another folder, as a hit writes nothing there. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_014G18Mik3fH4z5M5CyMSUS6 --- NEWS.md | 2 +- canClimateData.R | 11 +++++-- tests/testthat/test-metadata.R | 8 ----- tests/testthat/test-outputDirKey.R | 49 ++++++++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 12 deletions(-) create mode 100644 tests/testthat/test-outputDirKey.R diff --git a/NEWS.md b/NEWS.md index 9f60f67..ded1241 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,6 @@ # canClimateData (development version) -- `outputDir` is no longer part of the module's event cache keys (`defineParameter(cacheKey = FALSE)`; SpaDES.core >= 3.2.1.9047). It only says where the prepared climate files go, and `Init()` already left it out of the `prepClimateLayers()` key, so a cache built on one machine can match on another with a different folder. The event cache keys of this module change once. +- `outputDir` may be a `reproducible::asPath(dir, nParentDirs)`: the event cache keys then hold only the last `nParentDirs + 1` components of the folder, so a cache built on one machine matches on another where the folder has a different prefix, while a different folder (e.g. another hindcast) still gives a different key. A plain path is keyed in full, as before. Nothing in the module changed; the parameter description says how to pass it. - Rerunning this module on a finished run whose years are not calendar years (NRV), with that run's `climateYearRecord` (from the climateYear module), now prepares the climate years the record holds for the simulation years the rerun serves (the load times of its inputs, `inputs(sim)`, which `alreadyRun` sets to the finished run's save times), not the years in `climateVariables` or `historicalClimateYears`/`projectedClimateYears`. Variables by period, and `calcCumMDC` ones (which need consecutive years), keep their years. `times` are not checked as calendar years then. `climateYearRecord` is a new optional input; in a normal run climateYear writes it after this module's `init`, and `loadOrder` keeps that order. - `climateData` is now required from its modsForFireSense branch (>= 2.2.3.9011). `checkCalendarYears()` is only there; climateData development reached 2.2.3.9011 without it, so the old floor installed a climateData whose `init` failed with "could not find function". - `projectedClimateYears` now defaults to 2017:2100 instead of 2011:2100. The projected archive for tile 39 (CNRM-ESM2-1 ssp370) has no 2013-2016 files, and observed climate now covers 1901-2024, so projections before 2017 are not needed. diff --git a/canClimateData.R b/canClimateData.R index 87e7ab0..64628a6 100644 --- a/canClimateData.R +++ b/canClimateData.R @@ -25,7 +25,7 @@ defineModule(sim, list( "PredictiveEcology/fireSenseUtils@development (>= 0.0.5.9046)", "PredictiveEcology/LandR@development (>= 1.1.0.9064)", "PredictiveEcology/reproducible@development (>= 3.2.1.9060)", - "PredictiveEcology/SpaDES.core@development (>= 3.2.1.9047)", # defineParameter(cacheKey = FALSE) + "PredictiveEcology/SpaDES.core@development (>= 2.1.5)", "PredictiveEcology/SpaDES.tools@development (>= 2.0.4.9002)"), parameters = rbind( defineParameter("bufferDist", "numeric", 20000, NA, NA, @@ -48,8 +48,13 @@ defineModule(sim, list( "(e.g., when running multiple simulation replicates)", "to avoid multiple copies of the climate data.", "If not specified by the user, a subdirectory of the simulation output", - "directory will be used (i.e., `file.path(outputPath(sim), 'climate')`."), - cacheKey = FALSE), # only where the files go: `prepClimateLayers()` leaves `dstdir` out of its key too + "directory will be used (i.e., `file.path(outputPath(sim), 'climate')`.", + "Event cache keys hold this value as given, so a different folder", + "is a different key, and a machine-specific prefix is part of it.", + "To let the same folder match on another machine, pass", + "`reproducible::asPath(dir, nParentDirs)`: the key then holds only", + "`dir`'s last `nParentDirs + 1` components, e.g.", + "`asPath(dir, 2)` for `//climate`.")), defineParameter("projectedClimatePeriod", "character", NULL, NA, NA, paste("period to use for projected climate normals", "if `climateVariables` not supplied.")), diff --git a/tests/testthat/test-metadata.R b/tests/testthat/test-metadata.R index c5b78df..0fe5b54 100644 --- a/tests/testthat/test-metadata.R +++ b/tests/testthat/test-metadata.R @@ -51,11 +51,3 @@ test_that("projected climate years start in 2017", { pars <- stats::setNames(md$parameters$default, md$parameters$paramName) expect_identical(pars[["projectedClimateYears"]], 2017:2100) }) - -test_that("`outputDir` is not part of the module's cache keys", { - ## it only says where the prepared climate files go (`Init()` leaves `dstdir` out of the - ## `prepClimateLayers()` key too), so a cache built on one machine can match on another - p <- SpaDES.core::moduleMetadata(module = moduleName, path = modulePath)$parameters - expect_false(p$cacheKey[p$paramName == "outputDir"]) - expect_true(all(p$cacheKey[p$paramName != "outputDir"])) -}) diff --git a/tests/testthat/test-outputDirKey.R b/tests/testthat/test-outputDirKey.R new file mode 100644 index 0000000..8bb8bf9 --- /dev/null +++ b/tests/testthat/test-outputDirKey.R @@ -0,0 +1,49 @@ +## `outputDir` is part of the event cache keys, so an absolute path with a machine-specific prefix +## (the hindcast climate folder, /outputs/hindcast//climate) made a cache built +## on one machine miss on another. `outputDir` may be `reproducible::asPath(dir, nParentDirs)`, which +## the key holds as only `dir`'s last `nParentDirs + 1` components. A different folder must still miss: +## a hit writes nothing into the new folder. + +test_that("Init() uses a Path outputDir as the folder the climate files go to", { + withr::local_options(reproducible.useCache = FALSE, reproducible.useNewDigestAlgorithm = 2) + root <- withr::local_tempdir() + dir <- file.path(root, "outputs", "hindcastX", "9.1", "climate") + sim <- runInit(path = withr::local_tempdir(), params = list(outputDir = reproducible::asPath(dir, 2))) + expect_true(all(startsWith(normalizePath(terra::sources(sim$historicalClimateRasters[[1]])), + normalizePath(dir)))) +}) + +## a module with this module's parameter and a cached init, to read the key from the cache +writeKeyModule <- function(modulePath) { + dir.create(file.path(modulePath, "m"), recursive = TRUE) + cat(file = file.path(modulePath, "m", "m.R"), sep = "", ' +defineModule(sim, list(name = "m", description = "", keywords = "", authors = person("A", "B"), + childModules = character(0), version = list(m = "0.0.1"), timeframe = as.POSIXlt(c(NA, NA)), + timeunit = "year", citation = list(), documentation = list(), reqdPkgs = list(), + parameters = rbind( + defineParameter("outputDir", "character", NA_character_, NA, NA, "d"), + defineParameter(".useCache", "character", "init", NA, NA, "c")), + inputObjects = bindrows(), outputObjects = bindrows())) +doEvent.m <- function(sim, eventTime, eventType, debug = FALSE) invisible(sim) +') +} + +test_that("a Path outputDir keys the event on its last components: same under another base, different for another folder", { + skip_if_not_installed("SpaDES.core") + withr::local_options(spades.loadReqdPkgs = FALSE, spades.moduleCodeChecks = FALSE, spades.useRequire = FALSE, + reproducible.useMemoise = FALSE, spades.saveSimOnExit = FALSE) + modulePath <- withr::local_tempdir(); cachePath <- withr::local_tempdir() + writeKeyModule(modulePath) + nKeys <- function(outputDir) { + s <- suppressMessages(SpaDES.core::simInit(modules = "m", times = list(start = 0, end = 1), + params = list(m = list(outputDir = outputDir)), + paths = list(modulePath = modulePath, cachePath = cachePath))) + suppressMessages(SpaDES.core::spades(s, debug = FALSE)) + length(unique(reproducible::showCache(cachePath, verbose = -2)$cacheId)) + } + asP <- function(base, hindcast) reproducible::asPath(file.path(base, "outputs", hindcast, "9.1", "climate"), 2) + expect_identical(nKeys(asP("/machineA", "hindcastX")), 1L) + expect_identical(nKeys(asP("/other/machineB", "hindcastX")), 1L) # same relative folder: a hit + expect_identical(nKeys(asP("/machineA", "hindcastY")), 2L) # another folder: a miss + expect_identical(nKeys(file.path("/machineA", "outputs", "hindcastX", "9.1", "climate")), 3L) # a plain path is keyed in full +})