diff --git a/R/derivoce.R b/R/derivoce.R index 8fea5d7..038a02e 100644 --- a/R/derivoce.R +++ b/R/derivoce.R @@ -88,7 +88,7 @@ derivoce_covariates <- function() { ), temporal_gradient = list( label = "Temporal gradient", - units = "source units per step, day, or month", + units = "source units per `by` (default per month)", description = paste( "Rate of change between consecutive time steps at each location: how fast", "conditions are shifting, as distinct from what they currently are." @@ -101,13 +101,16 @@ derivoce_covariates <- function() { label = "Lagged covariate", units = "source units", description = paste( - "The value n steps back. Populations respond with a delay: a bloom feeds", - "the animals sampled a month later, not those sampled during it." + "The value n calendar units (`by`, default month) back. Populations", + "respond with a delay: a bloom feeds the animals sampled a month later,", + "not those sampled during it." ), inputs = c(vars = ""), neighbourhood = FALSE, outputs = function(spec) { - paste0(spec$vars, spec$suffix %||% paste0("_lag", spec$n %||% 1)) + # derivoce names the unit: SST_lag1month, not SST_lag1. + paste0(spec$vars, spec$suffix %||% + paste0("_lag", spec$n %||% 1, spec$by %||% "month")) } ), integrate_covariate = list( @@ -338,6 +341,13 @@ normalize_derivoce_spec <- function(spec) { # YAML gives a list rather than a vector for any sequence it cannot simplify; # derivoce takes vectors. spec[] <- lapply(spec, function(value) if (is.list(value)) unlist(value) else value) + + # derivoce counts back in calendar units and has no default unit, since a + # daily record and a monthly one differ. Every covariate here is a monthly + # mean (see covariate_catalog.R), so a step that does not say is monthly. + if (spec$type %in% c("lag_covariate", "temporal_gradient") && is.null(spec$by)) { + spec$by <- "month" + } spec } @@ -556,16 +566,18 @@ derivoce_choices <- function(selected, bathymetry = character(), list(type = "temporal_gradient", suffix = "_tgrad", group = "Temporal", label = "Rate of change in %s (per month)", expensive = FALSE, step = function(v) list(type = "temporal_gradient", vars = v, - per = "month")), - list(type = "lag_covariate", suffix = "_lag1", group = "Temporal", + by = "month")), + list(type = "lag_covariate", suffix = "_lag1month", group = "Temporal", label = "%s one month earlier", expensive = FALSE, - step = function(v) list(type = "lag_covariate", vars = v, n = 1)), + step = function(v) list(type = "lag_covariate", vars = v, n = 1, + by = "month")), # Twelve monthly steps back is the same month a year earlier, which asks a # different question from last month: whether conditions a full cycle ago # set up this year's population, rather than whether last month fed it. - list(type = "lag_covariate", suffix = "_lag12", group = "Temporal", + list(type = "lag_covariate", suffix = "_lag12month", group = "Temporal", label = "%s one year earlier", expensive = FALSE, - step = function(v) list(type = "lag_covariate", vars = v, n = 12)), + step = function(v) list(type = "lag_covariate", vars = v, n = 12, + by = "month")), list(type = "integrate_covariate", suffix = "_int", group = "Temporal", label = "%s accumulated since January", expensive = FALSE, step = function(v) list(type = "integrate_covariate", vars = v, diff --git a/README.md b/README.md index 7f89191..9a72e2b 100644 --- a/README.md +++ b/README.md @@ -545,7 +545,7 @@ covariates: bottom: BOTT # SST_BOTT_vgrad, the stratification index - type: lag_covariate vars: [CHL] - "n": 1 # CHL_lag1 - see the note on quoting below + "n": 1 # CHL_lag1month - see the note on quoting below - type: integrate_covariate vars: [CHL] window: year # CHL_int, the original pipeline's int_chl diff --git a/inst/configs/cfin_gom.yaml b/inst/configs/cfin_gom.yaml index 909b023..a1ac2ec 100644 --- a/inst/configs/cfin_gom.yaml +++ b/inst/configs/cfin_gom.yaml @@ -135,7 +135,7 @@ covariates: # false, so `n: 1` would name this key FALSE and the step would silently # fall back to a one-month lag. taupatch recovers the spelling on read, # but quoting keeps the file correct for anything else that opens it. - "n": 1 # CHL_lag1; last month's bloom feeds this month + "n": 1 # CHL_lag1month; last month's bloom feeds this month - type: integrate_covariate vars: [CHL] window: year # CHL_int; the original pipeline's int_chl diff --git a/tests/testthat/test-derivoce.R b/tests/testthat/test-derivoce.R index 183bf2a..56bf790 100644 --- a/tests/testthat/test-derivoce.R +++ b/tests/testthat/test-derivoce.R @@ -56,7 +56,7 @@ test_that("predicted column names match the columns derivoce produces", { expect_setequal(setdiff(names(derived), names(env)), derivoce_names(config)) expect_setequal( derivoce_names(config), - c("SST_grad", "SSS_grad", "strat", "SST_tgrad", "CHL_lag2", "CHL_int", + c("SST_grad", "SSS_grad", "strat", "SST_tgrad", "CHL_lag2month", "CHL_int", "speed", "EKE") ) }) @@ -76,9 +76,9 @@ test_that("derived covariates carry real values, not just columns", { # The lag is undefined in the first step and equals the previous step's value # after it, matched by location rather than row order. - expect_true(all(is.na(flat$SST_lag1[flat$MONTH == 6]))) - expect_false(any(is.na(flat$SST_lag1[flat$MONTH == 7]))) - expect_equal(sort(flat$SST_lag1[flat$MONTH == 7]), sort(flat$SST[flat$MONTH == 6])) + expect_true(all(is.na(flat$SST_lag1month[flat$MONTH == 6]))) + expect_false(any(is.na(flat$SST_lag1month[flat$MONTH == 7]))) + expect_equal(sort(flat$SST_lag1month[flat$MONTH == 7]), sort(flat$SST[flat$MONTH == 6])) }) test_that("a lag written 'n: 2' in a config file lags by two steps", { @@ -98,7 +98,7 @@ test_that("a lag written 'n: 2' in a config file lags by two steps", { spec <- config$covariates$derivoce[[1]] expect_equal(spec$n, 2) expect_null(spec[["FALSE"]]) - expect_equal(derivoce_names(config), "SST_lag2") + expect_equal(derivoce_names(config), "SST_lag2month") env <- fetch_covariates(config, years = 2018, months = 6:8) derived <- suppressMessages(add_derivoce_covariates(env, config)) @@ -106,9 +106,9 @@ test_that("a lag written 'n: 2' in a config file lags by two steps", { # Two steps back: undefined for the first two months rather than the first, # and August carries June's value. - expect_true(all(is.na(flat$SST_lag2[flat$MONTH %in% c(6, 7)]))) - expect_false(any(is.na(flat$SST_lag2[flat$MONTH == 8]))) - expect_equal(sort(flat$SST_lag2[flat$MONTH == 8]), sort(flat$SST[flat$MONTH == 6])) + expect_true(all(is.na(flat$SST_lag2month[flat$MONTH %in% c(6, 7)]))) + expect_false(any(is.na(flat$SST_lag2month[flat$MONTH == 8]))) + expect_equal(sort(flat$SST_lag2month[flat$MONTH == 8]), sort(flat$SST[flat$MONTH == 6])) }) test_that("steps chain, so a step can read what an earlier one produced", { @@ -200,7 +200,7 @@ test_that("a neighbourhood step warns when its input was upsampled", { # source grid; the lag of the same variable is unaffected, so only one warns. expect_warning(derived <- suppressMessages(add_derivoce_covariates(env, config)), "upsampled from a coarser product") - expect_true(all(c("CHL_grad", "CHL_lag1") %in% names(derived))) + expect_true(all(c("CHL_grad", "CHL_lag1month") %in% names(derived))) # The record of which variables were upsampled survives for later stages. expect_equal(attr(derived, "upsampled"), "CHL") }) @@ -253,7 +253,7 @@ test_that("a derivoce block survives being read from a config file", { config <- load_config(path) expect_gt(length(config$covariates$derivoce), 0) - expect_true(all(c("SST_grad", "CHL_lag1") %in% derivoce_names(config))) + expect_true(all(c("SST_grad", "CHL_lag1month") %in% derivoce_names(config))) # And the same through a config the generator wrote. dir <- tempfile("configs"); dir.create(dir) @@ -262,7 +262,7 @@ test_that("a derivoce block survives being read from a config file", { selected = c("SST", "BOTT"), bathymetry = character(), transform = list(), derivoce = list(list(type = "lag_covariate", vars = "SST", n = 2)) ) - expect_equal(derivoce_names(load_config(generated)), "SST_lag2") + expect_equal(derivoce_names(load_config(generated)), "SST_lag2month") }) test_that("a run with no derivoce block is untouched", { @@ -274,3 +274,16 @@ test_that("a run with no derivoce block is untouched", { env <- fetch_covariates(config, years = 2018, months = 6) expect_identical(add_derivoce_covariates(env, config), env) }) + +test_that("a lag or temporal gradient counts in months unless told otherwise", { + expect_equal(normalize_derivoce_spec(list(type = "lag_covariate", vars = "SST"))$by, + "month") + expect_equal(normalize_derivoce_spec(list(type = "temporal_gradient", vars = "SST"))$by, + "month") + expect_null(normalize_derivoce_spec(list(type = "horizontal_gradient", vars = "SST"))$by) + + daily <- normalize_derivoce_spec(list(type = "lag_covariate", vars = "SST", n = 7, + by = "day")) + expect_equal(daily$by, "day") + expect_equal(derivoce_covariates()$lag_covariate$outputs(daily), "SST_lag7day") +})