Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add PipeOpFDASmooth #71

Merged
merged 9 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ Imports:
R6,
tf
Suggests:
testthat (>= 3.0.0)
rpart,
testthat (>= 3.0.0),
pracma,
zoo
Remotes:
tidyfun/tf
Config/testthat/edition: 3
Expand All @@ -36,6 +39,7 @@ Collate:
'PipeOpFDAExtract.R'
'PipeOpFDAFlatten.R'
'PipeOpFDAInterpol.R'
'PipeOpFDASmooth.R'
'TaskClassif_phoneme.R'
'TaskRegr_dti.R'
'TaskRegr_fuel.R'
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
export(PipeOpFDAExtract)
export(PipeOpFDAFlatten)
export(PipeOpFDAInterpol)
export(PipeOpFDASmooth)
import(R6)
import(checkmate)
import(data.table)
Expand Down
81 changes: 81 additions & 0 deletions R/PipeOpFDASmooth.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#' @title Smoothing Functional Data
#' @name mlr_pipeops_fda.smooth
#'
#' @description
#' Smoothes functional data using [`tf::tf_smooth()`].
#' This preprocessing operator is similar to [`PipeOpFDAInterpol`], however it does not interpolate to unobserved
#' x-values, but rather smooths the observed values.
#'
#' @section Parameters:
#' The parameters are the parameters inherited from [`PipeOpTaskPreprocSimple`], as well as the following
#' parameters:
#' * `method` :: `character(1)`\cr
#' One of:
#' * "lowess": locally weighted scatterplot smoothing (default)
#' * "rollmean": rolling mean
#' * "rollmedian": rolling meadian
#' * "savgol": Savitzky-Golay filtering
#'
#' All methods but "lowess" ignore non-equidistant arg values.
#' * `args` :: named `list()`\cr
#' List of named arguments that is passed to `tf_smooth()`. See the help page of `tf_smooth()` for
#' default values.
#' * `verbose` :: `logical(1)`\cr
#' Whether to print messages during the transformation.
#' Is initialized to `FALSE`.
#'
#' @export
#' @examples
#' library(mlr3pipelines)
#' task = tsk("fuel")
#' po_smooth = po("fda.smooth", method = "rollmean", args = list(k = 5))
#' task_smooth = po_smooth$train(list(task))[[1L]]
#' task_smooth
#' task_smooth$data(cols = c("NIR", "UVVIS"))
PipeOpFDASmooth = R6Class("PipeOpFDASmooth",
inherit = mlr3pipelines::PipeOpTaskPreprocSimple,
public = list(
#' @description Initializes a new instance of this Class.
#' @param id (`character(1)`)\cr
#' Identifier of resulting object, default `"fda.smooth"`.
#' @param param_vals (named `list`)\cr
#' List of hyperparameter settings, overwriting the hyperparameter settings that would
#' otherwise be set during construction. Default `list()`.
initialize = function(id = "fda.smooth", param_vals = list()) {
param_set = ps(
method = p_fct(default = "lowess", c("lowess", "rollmean", "rollmedian", "savgol"), tags = c("train", "predict")), # nolint
args = p_uty(tags = c("train", "predict", "required"),
custom_check = crate(function(x) check_list(x, names = "unique"))),
verbose = p_lgl(tags = c("train", "predict", "required"))
)

param_set$set_values(args = list(), verbose = FALSE)

super$initialize(
id = id,
param_set = param_set,
param_vals = param_vals,
packages = c("mlr3fda", "mlr3pipelines", "tf", "stats"),
feature_types = c("tfd_reg", "tfd_irreg")
)
}
),
private = list(
.transform_dt = function(dt, levels) {
pars = self$param_set$get_values()

if (pars$verbose) {
map_dtc(dt, function(x) {
invoke(tf::tf_smooth, x = x, method = pars$method, .args = pars$args)
})
} else {
map_dtc(dt, function(x) {
suppressMessages(invoke(tf::tf_smooth, x = x, method = pars$method, .args = pars$args))
})
}

}
)
)
#' @include zzz.R
register_po("fda.smooth", PipeOpFDASmooth)
2 changes: 1 addition & 1 deletion R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#' @importFrom tf tf_approx_linear tf_approx_spline tf_approx_fill_extend tf_approx_locf tf_approx_nocb
#'
#' @section Data types:
#' To extend mlr3 to functional data, two data types from the {tf} package are added:
#' To extend mlr3 to functional data, two data types from the tf package are added:
#' * `tfd_irreg` - Irregular functional data, i.e. the functions are observed for
#' potentiall different inputs for each observation.
#' * `tfd_reg` - Regular functional data, i.e. the functions are observed for the same input
Expand Down
2 changes: 1 addition & 1 deletion man/mlr3fda-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

103 changes: 103 additions & 0 deletions man/mlr_pipeops_fda.smooth.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading