Skip to content

Commit df61845

Browse files
Merge pull request #1421 from M3nin0/feat/impute-methods
inclusion of new impute methods
2 parents 513aa15 + 5cacd19 commit df61845

13 files changed

+706
-94
lines changed

NAMESPACE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,9 @@ S3method(summary,variance_cube)
515515
export("sits_bands<-")
516516
export("sits_labels<-")
517517
export(impute_linear)
518+
export(impute_mean)
519+
export(impute_mean_window)
520+
export(impute_median)
518521
export(sits_accuracy)
519522
export(sits_accuracy_summary)
520523
export(sits_add_base_cube)

R/RcppExports.R

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,46 @@ C_glcm_correlation <- function(x, angles, nrows, ncols, n_grey, window_size) {
6565
.Call(`_sits_C_glcm_correlation`, x, angles, nrows, ncols, n_grey, window_size)
6666
}
6767

68+
C_interp_mean_window_vec <- function(data, k, weighting) {
69+
.Call(`_sits_C_interp_mean_window_vec`, data, k, weighting)
70+
}
71+
72+
C_interp_mean_window_mat <- function(data, k, weighting) {
73+
.Call(`_sits_C_interp_mean_window_mat`, data, k, weighting)
74+
}
75+
76+
linear_interp <- function(mtx) {
77+
.Call(`_sits_linear_interp`, mtx)
78+
}
79+
80+
linear_interp_vec <- function(vec) {
81+
.Call(`_sits_linear_interp_vec`, vec)
82+
}
83+
84+
C_mask_na <- function(x) {
85+
.Call(`_sits_C_mask_na`, x)
86+
}
87+
88+
C_fill_na <- function(x, fill) {
89+
.Call(`_sits_C_fill_na`, x, fill)
90+
}
91+
92+
C_interp_mean_vec <- function(data) {
93+
.Call(`_sits_C_interp_mean_vec`, data)
94+
}
95+
96+
C_interp_mean_mat <- function(data) {
97+
.Call(`_sits_C_interp_mean_mat`, data)
98+
}
99+
100+
C_interp_median_vec <- function(data) {
101+
.Call(`_sits_C_interp_median_vec`, data)
102+
}
103+
104+
C_interp_median_mat <- function(data) {
105+
.Call(`_sits_C_interp_median_mat`, data)
106+
}
107+
68108
C_kernel_median <- function(x, ncols, nrows, band, window_size) {
69109
.Call(`_sits_C_kernel_median`, x, ncols, nrows, band, window_size)
70110
}
@@ -125,22 +165,6 @@ C_label_max_prob <- function(x) {
125165
.Call(`_sits_C_label_max_prob`, x)
126166
}
127167

128-
linear_interp <- function(mtx) {
129-
.Call(`_sits_linear_interp`, mtx)
130-
}
131-
132-
linear_interp_vec <- function(vec) {
133-
.Call(`_sits_linear_interp_vec`, vec)
134-
}
135-
136-
C_mask_na <- function(x) {
137-
.Call(`_sits_C_mask_na`, x)
138-
}
139-
140-
C_fill_na <- function(x, fill) {
141-
.Call(`_sits_C_fill_na`, x, fill)
142-
}
143-
144168
batch_calc <- function(n_pixels, max_lines_per_batch) {
145169
.Call(`_sits_batch_calc`, n_pixels, max_lines_per_batch)
146170
}

R/api_classify.R

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -541,18 +541,18 @@
541541
bands = bands
542542
)
543543
}
544-
# Apply time series filter
545-
if (.has(filter_fn)) {
544+
# Apply imputation filter
545+
if (.has(impute_fn)) {
546546
samples <- .apply_across(
547547
data = samples,
548-
fn = filter_fn
548+
fn = impute_fn
549549
)
550550
}
551-
# Apply imputation filter
552-
if (.has(impute_fn)) {
551+
# Apply time series filter
552+
if (.has(filter_fn)) {
553553
samples <- .apply_across(
554554
data = samples,
555-
fn = impute_fn
555+
fn = filter_fn
556556
)
557557
}
558558
# Compute the breaks in time for multiyear classification

R/sits_imputation.R

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,126 @@ impute_linear <- function(data = NULL) {
1919
.factory_function(data, impute_fun)
2020
}
2121

22+
#' @title Remove NA using median
23+
#' @name impute_median
24+
#' @author Felipe Carlos, \email{efelipecarlos@@gmail.com}
25+
#' @description Remove NA using median
26+
#'
27+
#' @param data A time series vector or matrix
28+
#' @return A set of filtered time series using
29+
#' the imputation function.
30+
#'
31+
#' @export
32+
impute_median <- function(data = NULL) {
33+
# Define impute function
34+
impute_fun <- function(data) {
35+
# Matrix
36+
if (inherits(data, "matrix")) {
37+
C_interp_median_mat(data)
38+
}
39+
40+
# Vector implementation
41+
else {
42+
as.vector(
43+
C_interp_median_vec(data)
44+
)
45+
}
46+
}
47+
48+
.factory_function(data, impute_fun)
49+
}
50+
51+
#' @title Remove NA using mean
52+
#' @name impute_mean
53+
#' @author Felipe Carlos, \email{efelipecarlos@@gmail.com}
54+
#' @description Remove NA using mean
55+
#'
56+
#' @param data A time series vector or matrix
57+
#' @return A set of filtered time series using
58+
#' the imputation function.
59+
#'
60+
#' @export
61+
impute_mean <- function(data = NULL) {
62+
# Define impute function
63+
impute_fun <- function(data) {
64+
# Matrix
65+
if (inherits(data, "matrix")) {
66+
C_interp_mean_mat(data)
67+
}
68+
69+
# Vector implementation
70+
else {
71+
as.vector(
72+
C_interp_mean_vec(data)
73+
)
74+
}
75+
}
76+
77+
.factory_function(data, impute_fun)
78+
}
79+
80+
#' @title Remove NA using weighted moving average
81+
#' @name impute_mean_window
82+
#' @author Felipe Carlos, \email{efelipecarlos@@gmail.com}
83+
#' @description Remove NA using weighted moving average
84+
#'
85+
#' @param data A time series vector or matrix
86+
#' @param k A integer width of the moving average window. Expands
87+
#' to both sides of the center element e.g. k = 2 means 4
88+
#' observations (2 left, 2 right) are taken into account.
89+
#' If all observations in the current window are NA, the
90+
#' window size is automatically increased until there are
91+
#' at least 2 non-NA values present
92+
#' @param weighting A string with the weighting strategy to be used. More
93+
#' details below (default is "simple").
94+
#' @return A set of filtered time series using
95+
#' the imputation function.
96+
#'
97+
#' @note
98+
#' The \code{weighting} parameter defines the weighting strategy used
99+
#' in the moving window. The strategies available are:
100+
#'
101+
#' \itemize{
102+
#' \item{\code{simple} - Simple Moving Average (SMA) (default option)}
103+
#' \item{\code{linear} - Linear Weighted Moving Average (LWMA)}
104+
#' \item{\code{exponential} - Exponential Weighted Moving Average (EWMA)}
105+
#' }
106+
#'
107+
#' @references
108+
#' The implementation of this function was adapted from the \code{imputeTS} R
109+
#' Package. The code is open-source, under the GPL license, and is available on
110+
#' GitHub \url{https://github.com/SteffenMoritz/imputeTS}.
111+
#'
112+
#' @export
113+
impute_mean_window <- function(data = NULL, k = 2, weighting = "simple") {
114+
# Check parameters
115+
.check_int_parameter(k, min = 2)
116+
.check_chr_within(
117+
x = weighting,
118+
within = c("simple", "linear", "exponential")
119+
)
120+
121+
# Define impute function
122+
impute_fun <- function(data) {
123+
# By default, use the vector implementation
124+
fnc <- C_interp_mean_window_vec
125+
126+
# If data is a matrix, use matrix implementation
127+
if (inherits(data, "matrix")) {
128+
fnc = C_interp_mean_window_mat
129+
}
130+
131+
# Impute!
132+
fnc(
133+
data = data,
134+
k = k,
135+
weighting = weighting
136+
)
137+
}
138+
139+
.factory_function(data, impute_fun)
140+
}
141+
22142
#' @title Replace NA values in time series with imputation function
23143
#' @name sits_impute
24144
#' @author Gilberto Camara, \email{gilberto.camara@@inpe.br}
@@ -37,7 +157,7 @@ sits_impute <- function(samples, impute_fn = impute_linear()) {
37157
.check_samples_ts(samples)
38158
.samples_foreach_ts(samples, function(row) {
39159
.ts_values(row) <- tibble::as_tibble(
40-
purrr::map_df(.ts_bands(row), function(band) {
160+
purrr::map_dfc(.ts_bands(row), function(band) {
41161
# get band values
42162
band_value <- as.vector(as.matrix(row[[band]]))
43163
# impute data

man/impute_mean.Rd

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

man/impute_mean_window.Rd

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

man/impute_median.Rd

Lines changed: 21 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)