Skip to content

Commit be50528

Browse files
mostly fix botched merge
1 parent 526a4ed commit be50528

File tree

10 files changed

+47
-473
lines changed

10 files changed

+47
-473
lines changed

.github/workflows/R-CMD-check.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
on:
44
push:
55
branches:
6+
- main
67
- master
7-
- 1-15-99
88
pull_request:
99
branches:
10+
- main
1011
- master
11-
- 1-15-99
1212

1313
name: R-CMD-check
1414

.github/workflows/test-coverage.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
on:
22
push:
33
branches:
4+
- main
45
- master
5-
- 1-15-99
66
pull_request:
77
branches:
8+
- main
89
- master
9-
- 1-15-99
1010

1111
name: test-coverage
1212

NAMESPACE

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ S3method(cube, data.table)
5151
S3method(rollup, data.table)
5252
export(frollmean)
5353
export(frollsum)
54-
export(frollmax)
5554
export(frollapply)
5655
export(nafill)
5756
export(setnafill)

R/froll.R

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,8 @@ froll = function(fun, x, n, fill=NA, algo=c("fast", "exact"), align=c("right", "
22
stopifnot(!missing(fun), is.character(fun), length(fun)==1L, !is.na(fun))
33
algo = match.arg(algo)
44
align = match.arg(align)
5-
leftadaptive = isTRUE(adaptive) && align=="left" ## support for left added in #5441
6-
if (leftadaptive) {
7-
rev2 = function(x) if (is.list(x)) sapply(x, rev, simplify=FALSE) else rev(x)
8-
verbose = getOption("datatable.verbose")
9-
if (verbose)
10-
catf("froll: adaptive=TRUE && align='left' pre-processing for align='right'\n")
11-
## TODO test atomic x but list of lists n (multiple windows)!
12-
x = rev2(x)
13-
n = rev2(n)
14-
align = "right"
15-
}
165
ans = .Call(CfrollfunR, fun, x, n, fill, algo, align, na.rm, hasNA, adaptive)
17-
if (!leftadaptive)
18-
ans
19-
else {
20-
if (verbose)
21-
catf("froll: adaptive=TRUE && align='left' post-processing from align='right'\n")
22-
rev2(ans)
23-
}
6+
ans
247
}
258

269
frollmean = function(x, n, fill=NA, algo=c("fast", "exact"), align=c("right", "left", "center"), na.rm=FALSE, hasNA=NA, adaptive=FALSE) {
@@ -29,14 +12,9 @@ frollmean = function(x, n, fill=NA, algo=c("fast", "exact"), align=c("right", "l
2912
frollsum = function(x, n, fill=NA, algo=c("fast","exact"), align=c("right", "left", "center"), na.rm=FALSE, hasNA=NA, adaptive=FALSE) {
3013
froll(fun="sum", x=x, n=n, fill=fill, algo=algo, align=align, na.rm=na.rm, hasNA=hasNA, adaptive=adaptive)
3114
}
32-
frollmax = function(x, n, fill=NA, algo=c("fast", "exact"), align=c("right", "left", "center"), na.rm=FALSE, hasNA=NA, adaptive=FALSE) {
33-
froll(fun="max", x=x, n=n, fill=fill, algo=algo, align=align, na.rm=na.rm, hasNA=hasNA, adaptive=adaptive)
34-
}
35-
frollapply = function(x, n, FUN, ..., fill=NA, align=c("right", "left", "center"), adaptive) {
15+
frollapply = function(x, n, FUN, ..., fill=NA, align=c("right", "left", "center")) {
3616
FUN = match.fun(FUN)
3717
align = match.arg(align)
38-
if (!missing(adaptive))
39-
stopf("frollapply does not support 'adaptive' argument")
4018
rho = new.env()
4119
ans = .Call(CfrollapplyR, FUN, x, n, fill, align, rho)
4220
ans

inst/tests/froll.Rraw

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -563,8 +563,8 @@ if (FALSE) {
563563

564564
#### adaptive limitations
565565
test(6000.145, frollmean(1:2, 1:2, adaptive=TRUE, align="right"), c(1, 1.5))
566-
test(6000.146, frollmean(1:2, 1:2, adaptive=TRUE, align="center"), error="using adaptive TRUE and align 'center' is not implemented")
567-
##test(6000.147, frollmean(1:2, 1:2, adaptive=TRUE, align="left"), error="using adaptive TRUE and align argument different than 'right' is not implemented") ## support added in #5441, TODO add tests
566+
test(6000.146, frollmean(1:2, 1:2, adaptive=TRUE, align="center"), error="using adaptive TRUE and align argument different than 'right' is not implemented")
567+
test(6000.147, frollmean(1:2, 1:2, adaptive=TRUE, align="left"), error="using adaptive TRUE and align argument different than 'right' is not implemented")
568568
test(6000.148, frollmean(list(1:2, 1:3), list(1:2), adaptive=TRUE), error="adaptive rolling function can only process 'x' having equal length of elements, like data.table or data.frame. If you want to call rolling function on list having variable length of elements call it for each field separately")
569569

570570
#### adaptive exact

man/froll.Rd

Lines changed: 34 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
\alias{rollmean}
88
\alias{frollmean}
99
\alias{rollsum}
10-
\alias{rollmax}
1110
\alias{frollsum}
12-
\alias{frollmax}
1311
\alias{rollapply}
1412
\alias{frollapply}
1513
\title{Rolling functions}
@@ -21,9 +19,7 @@ frollmean(x, n, fill=NA, algo=c("fast", "exact"),
2119
align=c("right", "left", "center"), na.rm=FALSE, hasNA=NA, adaptive=FALSE)
2220
frollsum(x, n, fill=NA, algo=c("fast","exact"),
2321
align=c("right", "left", "center"), na.rm=FALSE, hasNA=NA, adaptive=FALSE)
24-
frollmax(x, n, fill=NA, algo=c("fast","exact"),
25-
align=c("right", "left", "center"), na.rm=FALSE, hasNA=NA, adaptive=FALSE)
26-
frollapply(x, n, FUN, \dots, fill=NA, align=c("right", "left", "center"), adaptive)
22+
frollapply(x, n, FUN, \dots, fill=NA, align=c("right", "left", "center"))
2723
}
2824
\arguments{
2925
\item{x}{ Vector, \code{data.frame} or \code{data.table} of integer, numeric or logical columns over which to calculate the windowed aggregations. May also be a list, in which case the rolling function is applied to each of its elements. }
@@ -49,69 +45,36 @@ frollapply(x, n, FUN, \dots, fill=NA, align=c("right", "left", "center"), adapti
4945
conveniently within \code{data.table} syntax.
5046

5147
Argument \code{n} allows multiple values to apply rolling functions on
52-
multiple window sizes. If \code{adaptive=TRUE}, then \code{n} must be a list,
53-
see \emph{Adaptive rolling functions} section below for details.
48+
multiple window sizes. If \code{adaptive=TRUE}, then \code{n} must be a list.
49+
Each list element must be integer vector of window sizes corresponding
50+
to every single observation in each column; see Examples.
5451

55-
When multiple columns or multiple window widths are provided, then they
56-
are run in parallel. The exception is for \code{algo="exact"}, which runs in
57-
parallel already. See \code{\link{setDTthreads}} for defaults and further details on parallelism in data.table.
58-
}
59-
\section{\code{hasNA} argument}{
60-
\code{hasNA} can be used to speed up processing in cases when it is known
61-
whether \code{x} contains infinite values \code{NA, NaN, +Inf, -Inf}.
62-
\itemize{
63-
\item{ Default \code{hasNA=NA} will use faster \code{NA} agnostic implementation,
64-
but when \code{NA}s are detected it will re-run \code{NA} aware implementation. }
65-
\item{ \code{hasNA=TRUE} will use \code{NA} aware implementation straightaway. }
66-
\item{ \code{hasNA=FALSE} will use faster \code{NA} agnostic implementation.
67-
Then depending on the rolling function it will either
68-
\itemize{
69-
\item{ (\emph{mean, sum}) detect \code{NA}s, raise warning, re-run \code{NA} aware. }
70-
\item{ (\emph{max}) not detect \code{NA}s and may silently produce an incorrect
71-
answer. }}
72-
Therefore \code{hasNA=FALSE} should be used with care.
73-
}
74-
}
75-
}
76-
\section{Implementation}{
77-
\itemize{
78-
\item{ \code{algo="fast"} uses \emph{"on-line"} algorithm, and
79-
all of \code{NaN, +Inf, -Inf} are treated as \code{NA}. Not all functions
80-
have \emph{fast} implementation available. As of now
81-
\emph{max} and \code{adaptive=TRUE} do not have it, therefore it will
82-
automatically fall back to \emph{exact} implementation. \code{datatable.verbose}
83-
option can be used to check that. }
84-
\item{ \code{algo="exact"} will make rolling functions use a more
85-
computationally-intensive algorithm. For each observation from input vector
86-
it will compute a function on a window from scratch (complexity \eqn{O(n^2)}).
87-
Depending on the function, this algorithm may suffers less from
88-
floating point rounding error (the same consideration applies to base \code{\link[base]{mean}}).
89-
Algorithm also handles \code{NaN, +Inf, -Inf} consistently to
90-
base R, unless - for some functions (e.g. \emph{max}) - \code{hasNA} is \code{FALSE}
91-
but NAs are present. In case of some functions (like \emph{mean}), it will additionally
52+
When \code{algo="fast"} an \emph{"on-line"} algorithm is used, and
53+
all of \code{NaN, +Inf, -Inf} are treated as \code{NA}.
54+
Setting \code{algo="exact"} will make rolling functions to use a more
55+
computationally-intensive algorithm that suffers less from floating point
56+
rounding error (the same consideration applies to \code{\link[base]{mean}}).
57+
\code{algo="exact"} also handles \code{NaN, +Inf, -Inf} consistently to
58+
base R. In case of some functions (like \emph{mean}), it will additionally
9259
make extra pass to perform floating point error correction. Error
9360
corrections might not be truly exact on some platforms (like Windows)
94-
when using multiple threads. }
95-
}
96-
}
97-
\section{Adaptive rolling functions}{
61+
when using multiple threads.
62+
9863
Adaptive rolling functions are a special case where each
99-
observation has its own corresponding rolling window width. \code{n}
100-
argument must be a list, then each list element must be an integer vector
101-
of window sizes corresponding to every single observation in each column;
102-
see Examples. Due to the logic or implementation of adaptive rolling
103-
functions, the following restrictions apply:
64+
observation has its own corresponding rolling window width. Due to the logic
65+
of adaptive rolling functions, the following restrictions apply:
10466
\itemize{
105-
\item \code{align} does not support \code{"center"}.
67+
\item \code{align} only \code{"right"}.
10668
\item if list of vectors is passed to \code{x}, then all
10769
vectors within it must have equal length.
108-
\item functionality is not suported in \code{frollapply}.
10970
}
110-
}
111-
\section{\code{frollapply}}{
71+
72+
When multiple columns or multiple windows width are provided, then they
73+
are run in parallel. The exception is for \code{algo="exact"}, which runs in
74+
parallel already.
75+
11276
\code{frollapply} computes rolling aggregate on arbitrary R functions.
113-
\code{adaptive} argument is not supported. The input
114-
\code{x} (first argument) to the function \code{FUN}
77+
The input \code{x} (first argument) to the function \code{FUN}
11578
is coerced to \emph{numeric} beforehand and \code{FUN}
11679
has to return a scalar \emph{numeric} value. Checks for that are made only
11780
during the first iteration when \code{FUN} is evaluated. Edge cases can be
@@ -121,34 +84,32 @@ frollapply(x, n, FUN, \dots, fill=NA, align=c("right", "left", "center"), adapti
12184
because there is no thread-safe API to R's C \code{eval}. Nevertheless we've
12285
seen the computation speed up vis-a-vis versions implemented in base R.
12386
}
124-
\section{\code{zoo} package users notice}{
87+
\value{
88+
A list except when the input is a \code{vector} and
89+
\code{length(n)==1} in which case a \code{vector} is returned.
90+
}
91+
\note{
12592
Users coming from most popular package for rolling functions
12693
\code{zoo} might expect following differences in \code{data.table}
12794
implementation.
12895
\itemize{
129-
\item rolling functions will always return result of the same length
130-
as input.
131-
\item \code{fill} defaults to \code{NA}.
96+
\item rolling function will always return result of the same length as input.
97+
\item \code{fill} defaults to \code{NA}.
13298
\item \code{fill} accepts only constant values. It does not support
13399
for \emph{na.locf} or other functions.
134-
\item \code{align} defaults to \code{"right"}.
100+
\item \code{align} defaults to \code{"right"}.
135101
\item \code{na.rm} is respected, and other functions are not needed
136102
when input contains \code{NA}.
137-
\item integers and logical are always coerced to double.
103+
\item integers and logical are always coerced to double.
138104
\item when \code{adaptive=FALSE} (default), then \code{n} must be a
139105
numeric vector. List is not accepted.
140106
\item when \code{adaptive=TRUE}, then \code{n} must be vector of
141107
length equal to \code{nrow(x)}, or list of such vectors.
142108
\item \code{partial} window feature is not supported, although it can
143-
be accomplished by using \code{adaptive=TRUE}, see examples.
144-
\code{NA} is always returned for incomplete windows.
109+
be accomplished by using \code{adaptive=TRUE}, see
110+
examples. \code{NA} is always returned for incomplete windows.
145111
}
146-
}
147-
\value{
148-
A list except when the input is a \code{vector} and
149-
\code{length(n)==1} in which case a \code{vector} is returned.
150-
}
151-
\note{
112+
152113
Be aware that rolling functions operates on the physical order of input.
153114
If the intent is to roll values in a vector by a logical window, for
154115
example an hour, or a day, one has to ensure that there are no gaps in

src/data.table.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,6 @@ void frollmeanExact(double *x, uint64_t nx, ans_t *ans, int k, double fill, bool
206206
void frollsum(unsigned int algo, double *x, uint64_t nx, ans_t *ans, int k, int align, double fill, bool narm, int hasna, bool verbose);
207207
void frollsumFast(double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasna, bool verbose);
208208
void frollsumExact(double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasna, bool verbose);
209-
void frollmax(unsigned int algo, double *x, uint64_t nx, ans_t *ans, int k, int align, double fill, bool narm, int hasna, bool verbose);
210-
void frollmaxFast(double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasna, bool verbose);
211-
void frollmaxExact(double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasna, bool verbose);
212209
void frollapply(double *x, int64_t nx, double *w, int k, ans_t *ans, int align, double fill, SEXP call, SEXP rho, bool verbose);
213210

214211
// frolladaptive.c
@@ -218,9 +215,6 @@ void fadaptiverollmeanExact(double *x, uint64_t nx, ans_t *ans, int *k, double f
218215
void fadaptiverollsum(unsigned int algo, double *x, uint64_t nx, ans_t *ans, int *k, double fill, bool narm, int hasna, bool verbose);
219216
void fadaptiverollsumFast(double *x, uint64_t nx, ans_t *ans, int *k, double fill, bool narm, int hasna, bool verbose);
220217
void fadaptiverollsumExact(double *x, uint64_t nx, ans_t *ans, int *k, double fill, bool narm, int hasna, bool verbose);
221-
void fadaptiverollmax(unsigned int algo, double *x, uint64_t nx, ans_t *ans, int *k, double fill, bool narm, int hasna, bool verbose);
222-
//void fadaptiverollmaxFast(double *x, uint64_t nx, ans_t *ans, int *k, double fill, bool narm, int hasna, bool verbose); // does not exists as of now
223-
void fadaptiverollmaxExact(double *x, uint64_t nx, ans_t *ans, int *k, double fill, bool narm, int hasna, bool verbose);
224218

225219
// frollR.c
226220
SEXP frollfunR(SEXP fun, SEXP obj, SEXP k, SEXP fill, SEXP algo, SEXP align, SEXP narm, SEXP hasNA, SEXP adaptive);

0 commit comments

Comments
 (0)