Skip to content

Commit b68278f

Browse files
committed
add include_self for movmean2
1 parent 42651b4 commit b68278f

8 files changed

Lines changed: 56 additions & 20 deletions

File tree

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Authors@R: c(
1818
License: MIT + file LICENSE
1919
Encoding: UTF-8
2020
Roxygen: list(markdown = TRUE)
21-
RoxygenNote: 7.2.3
21+
RoxygenNote: 7.3.3
2222
LinkingTo:
2323
Rcpp,
2424
RcppArmadillo

R/RcppExports.R

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,18 @@ movmean <- function(y, halfwin = 1L, SG_style = FALSE, w = NULL) {
7878
}
7979

8080
#' @param win_left,win_right windows size in the left and right
81+
#' @param include_self Whether to include the current point itself in the
82+
#' moving window. Default is `TRUE`.
8183
#' @rdname movmean
8284
#' @export
83-
movmean2 <- function(y, win_left = 1L, win_right = 0L, w = NULL) {
84-
.Call(`_rtrend_movmean2`, y, win_left, win_right, w)
85+
movmean2 <- function(y, win_left = 1L, win_right = 0L, w = NULL, include_self = TRUE) {
86+
.Call(`_rtrend_movmean2`, y, win_left, win_right, w, include_self)
8587
}
8688

8789
#' @param mat numeric matrix
8890
#' @rdname movmean
8991
#' @export
90-
movmean_2d <- function(mat, win_left = 3L, win_right = 0L) {
91-
.Call(`_rtrend_movmean_2d`, mat, win_left, win_right)
92+
movmean_2d <- function(mat, win_left = 3L, win_right = 0L, include_self = TRUE) {
93+
.Call(`_rtrend_movmean_2d`, mat, win_left, win_right, include_self)
9294
}
9395

man/acf.fft.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/movmean.Rd

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

man/set_dim.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/RcppExports.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,29 +124,31 @@ BEGIN_RCPP
124124
END_RCPP
125125
}
126126
// movmean2
127-
NumericVector movmean2(const arma::rowvec& y, int win_left, int win_right, Nullable<NumericVector> w);
128-
RcppExport SEXP _rtrend_movmean2(SEXP ySEXP, SEXP win_leftSEXP, SEXP win_rightSEXP, SEXP wSEXP) {
127+
NumericVector movmean2(const arma::rowvec& y, int win_left, int win_right, Nullable<NumericVector> w, bool include_self);
128+
RcppExport SEXP _rtrend_movmean2(SEXP ySEXP, SEXP win_leftSEXP, SEXP win_rightSEXP, SEXP wSEXP, SEXP include_selfSEXP) {
129129
BEGIN_RCPP
130130
Rcpp::RObject rcpp_result_gen;
131131
Rcpp::RNGScope rcpp_rngScope_gen;
132132
Rcpp::traits::input_parameter< const arma::rowvec& >::type y(ySEXP);
133133
Rcpp::traits::input_parameter< int >::type win_left(win_leftSEXP);
134134
Rcpp::traits::input_parameter< int >::type win_right(win_rightSEXP);
135135
Rcpp::traits::input_parameter< Nullable<NumericVector> >::type w(wSEXP);
136-
rcpp_result_gen = Rcpp::wrap(movmean2(y, win_left, win_right, w));
136+
Rcpp::traits::input_parameter< bool >::type include_self(include_selfSEXP);
137+
rcpp_result_gen = Rcpp::wrap(movmean2(y, win_left, win_right, w, include_self));
137138
return rcpp_result_gen;
138139
END_RCPP
139140
}
140141
// movmean_2d
141-
arma::mat movmean_2d(arma::mat& mat, int win_left, int win_right);
142-
RcppExport SEXP _rtrend_movmean_2d(SEXP matSEXP, SEXP win_leftSEXP, SEXP win_rightSEXP) {
142+
arma::mat movmean_2d(arma::mat& mat, int win_left, int win_right, bool include_self);
143+
RcppExport SEXP _rtrend_movmean_2d(SEXP matSEXP, SEXP win_leftSEXP, SEXP win_rightSEXP, SEXP include_selfSEXP) {
143144
BEGIN_RCPP
144145
Rcpp::RObject rcpp_result_gen;
145146
Rcpp::RNGScope rcpp_rngScope_gen;
146147
Rcpp::traits::input_parameter< arma::mat& >::type mat(matSEXP);
147148
Rcpp::traits::input_parameter< int >::type win_left(win_leftSEXP);
148149
Rcpp::traits::input_parameter< int >::type win_right(win_rightSEXP);
149-
rcpp_result_gen = Rcpp::wrap(movmean_2d(mat, win_left, win_right));
150+
Rcpp::traits::input_parameter< bool >::type include_self(include_selfSEXP);
151+
rcpp_result_gen = Rcpp::wrap(movmean_2d(mat, win_left, win_right, include_self));
150152
return rcpp_result_gen;
151153
END_RCPP
152154
}
@@ -161,8 +163,8 @@ static const R_CallMethodDef CallEntries[] = {
161163
{"_rtrend_smooth_wSG", (DL_FUNC) &_rtrend_smooth_wSG, 4},
162164
{"_rtrend_smooth_SG", (DL_FUNC) &_rtrend_smooth_SG, 3},
163165
{"_rtrend_movmean", (DL_FUNC) &_rtrend_movmean, 4},
164-
{"_rtrend_movmean2", (DL_FUNC) &_rtrend_movmean2, 4},
165-
{"_rtrend_movmean_2d", (DL_FUNC) &_rtrend_movmean_2d, 3},
166+
{"_rtrend_movmean2", (DL_FUNC) &_rtrend_movmean2, 5},
167+
{"_rtrend_movmean_2d", (DL_FUNC) &_rtrend_movmean_2d, 4},
166168
{NULL, NULL, 0}
167169
};
168170

src/movmean.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,14 +251,17 @@ NumericVector movmean(
251251
}
252252

253253
//' @param win_left,win_right windows size in the left and right
254+
//' @param include_self Whether to include the current point itself in the
255+
//' moving window. Default is `TRUE`.
254256
//' @rdname movmean
255257
//' @export
256258
// [[Rcpp::export]]
257259
NumericVector movmean2(
258260
const arma::rowvec &y,
259261
int win_left = 1,
260262
int win_right = 0,
261-
Nullable<NumericVector> w = R_NilValue)
263+
Nullable<NumericVector> w = R_NilValue,
264+
bool include_self = true)
262265
{
263266
int n = y.size();
264267
// arma::rowvec yy(y);
@@ -304,6 +307,7 @@ NumericVector movmean2(
304307

305308
for (int j = i_begin; j <= i_end; j++)
306309
{
310+
if (!include_self && j == i) continue;
307311
if (Rcpp::traits::is_finite<REALSXP>(y[j]))
308312
{
309313
n_i++;
@@ -324,7 +328,7 @@ NumericVector movmean2(
324328
//' @rdname movmean
325329
//' @export
326330
// [[Rcpp::export]]
327-
arma::mat movmean_2d(arma::mat& mat, int win_left = 3, int win_right = 0)
331+
arma::mat movmean_2d(arma::mat& mat, int win_left = 3, int win_right = 0, bool include_self = true)
328332
{
329333
int ntime = mat.n_cols;
330334
int nrow = mat.n_rows;
@@ -338,7 +342,7 @@ arma::mat movmean_2d(arma::mat& mat, int win_left = 3, int win_right = 0)
338342
// arma::colvec
339343
for (int i = 0; i < nrow; i++)
340344
{
341-
res.row(i) = Rcpp::as<arma::rowvec>(movmean2(mat.row(i), win_left, win_right));
345+
res.row(i) = Rcpp::as<arma::rowvec>(movmean2(mat.row(i), win_left, win_right, R_NilValue, include_self));
342346
}
343347
// Rcout << mat << std::endl;
344348
return res;

tests/testthat/test-movmean.R

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,31 @@ test_that("movmean works", {
2020
expect_true(all.equal(m, m2))
2121
})
2222

23+
test_that("movmean2 include_self works", {
24+
x <- c(4, 8, 6, -1, -2, -3, -1)
25+
26+
# include_self = TRUE (default): window [i-1, i, i+1]
27+
m_with <- movmean2(x, win_left = 1, win_right = 1, include_self = TRUE)
28+
# include_self = FALSE: window [i-1, i+1], excludes x[i]
29+
m_without <- movmean2(x, win_left = 1, win_right = 1, include_self = FALSE)
30+
31+
# For interior points they must differ (x is not constant)
32+
expect_false(isTRUE(all.equal(m_with[2:6], m_without[2:6])))
33+
34+
# Manual check for index 2 (1-based): neighbours are x[1]=4 and x[3]=6
35+
# include_self=FALSE => mean(4, 6) = 5
36+
expect_equal(m_without[2], mean(c(x[1], x[3])))
37+
38+
# include_self=TRUE => mean(4, 8, 6) = 6
39+
expect_equal(m_with[2], mean(c(x[1], x[2], x[3])))
40+
41+
# With NA: include_self=FALSE should still skip self even when self is NA
42+
x_na <- c(4, NA, 6, -1)
43+
m_na <- movmean2(x_na, win_left = 1, win_right = 1, include_self = FALSE)
44+
# index 2: neighbours are x[1]=4 and x[3]=6, self (NA) excluded -> mean=5
45+
expect_equal(m_na[2], mean(c(x_na[1], x_na[3]), na.rm = TRUE))
46+
})
47+
2348

2449
# test_that("movmean_R works", {
2550
# arr = rnorm(5 * 5 * 10) %>% array(dim = c(5, 5, 10))

0 commit comments

Comments
 (0)