Skip to content

Commit f2e3fb3

Browse files
committed
Make simple hops more consistent, working
Consistent in that they output the output cols
1 parent 131671c commit f2e3fb3

File tree

4 files changed

+89
-40
lines changed

4 files changed

+89
-40
lines changed

R/slide-refactor.R

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#' 3:4
1010
#' )
1111
#'
12+
#' @keywords internal
1213
time_slide_to_simple_hop <- function(.slide_comp, ..., .before_n_steps, .after_n_steps) {
1314
function(grp_data, grp_key, ref_inds) {
1415
available_ref_time_values <- vec_slice(grp_data$time_value, ref_inds)
@@ -81,29 +82,46 @@ time_slide_to_simple_hop <- function(.slide_comp, ..., .before_n_steps, .after_n
8182
}
8283
}
8384

84-
# TODO simplify to just trailing and put shift elsewhere?
85+
#' Convert upstream specialized slide function to a simple hop function
8586
#'
8687
#' upstream_slide_to_simple_hop(frollmean, .in_colnames = "value", .out_colnames = "slide_value", .before_n_steps = 1L, .after_n_steps = 0L)(
8788
#' tibble(time_value = 1:5, value = 1:5),
8889
#' tibble(geo_value = 1),
8990
#' 3:4
9091
#' )
92+
#' upstream_slide_to_simple_hop(slide_mean, .in_colnames = "value", .out_colnames = "slide_value", .before_n_steps = 1L, .after_n_steps = 0L)(
93+
#' tibble(time_value = 1:5, value = 1:5),
94+
#' tibble(geo_value = 1),
95+
#' 3:4
96+
#' )
97+
#'
98+
#' upstream_slide_to_simple_hop(frollmean, .in_colnames = "value", .out_colnames = "slide_value", .before_n_steps = Inf, .after_n_steps = 0L)(
99+
#' tibble(time_value = 1:5, value = 1:5),
100+
#' tibble(geo_value = 1),
101+
#' 3:4
102+
#' )
103+
#'
104+
#' @keywords internal
91105
upstream_slide_to_simple_hop <- function(.f, ..., .in_colnames, .out_colnames, .before_n_steps, .after_n_steps) {
92106
f_info <- upstream_slide_f_info(.f, ...)
93107
in_colnames <- .in_colnames
94108
out_colnames <- .out_colnames
95109
f_from_package <- f_info$from_package
96-
# TODO move .before_n_steps, .after_n_steps to args of this function?
110+
f_dots_baked <-
111+
if (rlang::dots_n(...) == 0L) {
112+
# Leaving `.f` unchanged slightly improves computation speed and trims
113+
# debug stack traces:
114+
.f
115+
} else {
116+
purrr::partial(.f, ... = , ...) # `... =` stands in for future args
117+
}
97118
switch(f_from_package,
98119
data.table = if (.before_n_steps == Inf) {
99120
if (.after_n_steps != 0L) {
100121
stop(".before_n_steps only supported with .after_n_steps = 0")
101122
}
102123
function(grp_data, grp_key, ref_inds) {
103-
grp_data[, out_colnames] <-
104-
f_dots_baked(grp_data[, in_colnames], seq_len(nrow(grp_data)), adaptive = TRUE)
105-
grp_data[, out_colnames] <- out_cols
106-
grp_data
124+
f_dots_baked(grp_data[, in_colnames], seq_len(nrow(grp_data)), adaptive = TRUE)
107125
}
108126
} else {
109127
function(grp_data, grp_key, ref_inds) {
@@ -116,24 +134,24 @@ upstream_slide_to_simple_hop <- function(.f, ..., .in_colnames, .out_colnames, .
116134
c(out_col[(.after_n_steps + 1L):length(out_col)], rep(NA, .after_n_steps))
117135
})
118136
}
119-
grp_data[, out_colnames] <- out_cols
120-
grp_data
137+
out_cols
121138
}
122139
},
123140
slider =
124141
# TODO Inf checks?
125142
function(grp_data, grp_key, ref_inds) {
126-
for (col_i in seq_along(in_colnames)) {
127-
grp_data[[out_colnames[[col_i]]]] <- f_dots_baked(grp_data[[in_colnames[[col_i]]]], before = .before_n_steps, after = .after_n_steps)
128-
}
129-
grp_data
143+
names(in_colnames) <- in_colnames
144+
lapply(in_colnames, function(in_colname) {
145+
f_dots_baked(grp_data[[in_colname]], before = .before_n_steps, after = .after_n_steps)
146+
})
130147
},
148+
# TODO improve message
131149
stop("unsupported package")
132150
)
133151
}
134152

135-
# TODO maybe make ref_inds optional or have special handling if it's the whole sequence?
136-
#
153+
# TODO maybe make ref_inds optional or have special handling if it's the whole sequence? But can it ever be the full sequence in the common fixed-width window case? Should be some truncation of it.
154+
137155
# TODO decide whether/where to put time range stuff
138156

139157
# TODO grp_ -> ek_ ?

man/time_slide_to_simple_hop.Rd

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

man/upstream_slide_to_simple_hop.Rd

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

man/upstream_slide_to_simple_hopper.Rd

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)