Skip to content

Commit c568774

Browse files
committed
refactor: handle time +/- Inf in helper function
1 parent 5295f1b commit c568774

File tree

6 files changed

+86
-24
lines changed

6 files changed

+86
-24
lines changed

R/epi_slide_opt_archive.R

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,11 @@ epi_slide_opt_archive_one_epikey <- function(
6464
# Time inp_update_max_t + before should have an output update, since it
6565
# depends on inp_update_max_t + before - before = inp_update_max_t, which
6666
# has an input update. Similarly, we could have updates beginning with
67-
# inp_update_min_t - after, or anything in between these two bounds.
68-
out_update_min_t <- time_minus_n_steps(inp_update_min_t, after, time_type)
69-
out_update_max_t <- time_plus_n_steps(inp_update_max_t, before, time_type)
67+
# inp_update_min_t - after, or anything in between these two bounds. If
68+
# before == Inf, we need to update outputs all the way to the end of the
69+
# input *snapshot*.
70+
out_update_min_t <- time_minus_slide_window_arg(inp_update_min_t, after, time_type)
71+
out_update_max_t <- time_plus_slide_window_arg(inp_update_max_t, before, time_type, max(inp_snapshot$time_value))
7072
out_update_ts <- vec_slice(inp_snapshot$time_value, between(inp_snapshot$time_value, out_update_min_t, out_update_max_t))
7173
out_update <- epi_slide_opt_one_epikey(inp_snapshot, f_dots_baked, f_from_package, before, after, unit_step, time_type, out_update_ts, in_colnames, out_colnames)
7274
out_diff <- tbl_diff2(prev_out_snapshot, out_update, "time_value", "update")

R/epi_slide_opt_edf.R

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -172,20 +172,10 @@ epi_slide_opt_one_epikey <- function(inp_tbl,
172172
out_time_values,
173173
in_colnames, out_colnames) {
174174
# TODO try converting time values to reals, do work on reals, convert back at very end?
175-
if (before == Inf) {
176-
if (after != 0L) {
177-
cli_abort('.window_size = Inf is only supported with .align = "right"',
178-
class = "epiprocess__epi_slide_opt_archive__inf_window_invalid_align"
179-
)
180-
}
181-
# We need to use the entire input range, filling in time gaps. We shouldn't
182-
# pad the ends.
183-
slide_t_min <- min(inp_tbl$time_value) # FIXME match existing behavior, or complete changeover
184-
slide_t_max <- max(out_time_values)
185-
} else {
186-
slide_t_min <- min(out_time_values) - before
187-
slide_t_max <- max(out_time_values) + after
188-
}
175+
#
176+
# FIXME min time_value for this epikey vs. entire edf; match existing behavior, or complete changeover
177+
slide_t_min <- time_minus_slide_window_arg(min(out_time_values), before, time_type, min(inp_tbl$time_value))
178+
slide_t_max <- time_plus_slide_window_arg(max(out_time_values), after, time_type)
189179
slide_nrow <- time_delta_to_n_steps(slide_t_max - slide_t_min, time_type) + 1L
190180
slide_time_values <- slide_t_min + 0L:(slide_nrow - 1L) * unit_step
191181
slide_inp_backrefs <- vec_match(slide_time_values, inp_tbl$time_value)

R/time-utils.R

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ difftime_approx_ceiling_time_delta <- function(difftime, time_type) {
339339
)
340340
}
341341

342-
#' Difference between two time value vectors in terms of number of time "steps"
342+
#' Difference between two finite `time_value` vectors in terms of number of time "steps"
343343
#'
344344
#' @param x a time_value (vector) of time type `time_type`
345345
#' @param y a time_value (vector) of time type `time_type`
@@ -352,15 +352,18 @@ time_minus_time_in_n_steps <- function(x, y, time_type) {
352352
time_delta_to_n_steps(x - y, time_type)
353353
}
354354

355-
#' Advance/retreat time_values by specified number of time "steps"
355+
#' Advance/retreat time_value(s) by bare-integerish number(s) of time "steps"
356356
#'
357357
#' Here, a "step" is based on the `time_type`, not just the class of `x`.
358358
#'
359359
#' @param x a time_value (vector) of time type `time_type`
360-
#' @param y integerish (vector)
360+
#' @param y bare integerish (vector)
361361
#' @param time_type as in [`validate_slide_window_arg()`]
362362
#' @return a time_value (vector) of time type `time_type`
363363
#'
364+
#' @seealso [`time_plus_slide_window_arg`] if you're working with a `y` that is
365+
#' a slide window arg, which is scalar but otherwise more general (class-wise,
366+
#' Inf-wise) than an integerish vector.
364367
#' @keywords internal
365368
time_plus_n_steps <- function(x, y, time_type) {
366369
x + y * unit_time_delta(time_type, "fast")
@@ -370,3 +373,32 @@ time_plus_n_steps <- function(x, y, time_type) {
370373
time_minus_n_steps <- function(x, y, time_type) {
371374
x - y * unit_time_delta(time_type, "fast")
372375
}
376+
377+
#' Advance/retreat time_value(s) by specified amount (slide window arg)
378+
#'
379+
#' @param x a time_value (vector) of time type `time_type`
380+
#' @param y a (scalar) slide window arg; should pass [`validate_slide_window_arg()`]
381+
#' @param time_type as in [`validate_slide_window_arg()`]
382+
#' @param max_time_value when `y == Inf`, what should be the result of adding `y`?
383+
#' @param min_time_value when `y == Inf`, what should be the result of subtracting `y`?
384+
#' @return a time_value (vector) of time type `time_type`
385+
#'
386+
#' @keywords internal
387+
#' @seealso [`time_plus_n_steps`], if you're working with an integerish vector
388+
#' number of time steps `y` (output from other `*n_steps` functions) instead.
389+
time_plus_slide_window_arg <- function(x, y, time_type, max_time_value) {
390+
if (y == Inf) {
391+
rep(max_time_value, vec_size(x))
392+
} else {
393+
time_plus_n_steps(x, y, time_type)
394+
}
395+
}
396+
397+
#' @rdname time_plus_slide_window_arg
398+
time_minus_slide_window_arg <- function(x, y, time_type, min_time_value) {
399+
if (y == Inf) {
400+
rep(min_time_value, vec_size(x))
401+
} else {
402+
time_minus_n_steps(x, y, time_type)
403+
}
404+
}

man/time_minus_time_in_n_steps.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/time_plus_n_steps.Rd

Lines changed: 7 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/time_plus_slide_window_arg.Rd

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)