diff --git a/DESCRIPTION b/DESCRIPTION index c1f5de7..8dee28f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -14,7 +14,9 @@ Depends: recipes Imports: cli, + dials, dplyr, + generics, glue, IDPmisc, purrr, diff --git a/NAMESPACE b/NAMESPACE index 0b98d43..7684ffa 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -24,17 +24,22 @@ S3method(tidy,step_measure_input_wide) S3method(tidy,step_measure_output_long) S3method(tidy,step_measure_output_wide) S3method(tidy,step_measure_savitzky_golay) +S3method(tunable,step_measure_savitzky_golay) +export(differentiation_order) export(step_measure_input_long) export(step_measure_input_wide) export(step_measure_output_long) export(step_measure_output_wide) export(step_measure_savitzky_golay) export(subtract_rf_baseline) +export(tunable) +export(window_side) import(recipes) import(rlang) importFrom(dplyr,arrange) importFrom(dplyr,mutate) importFrom(dplyr,select) +importFrom(generics,tunable) importFrom(glue,glue) importFrom(tibble,tibble) importFrom(utils,globalVariables) diff --git a/R/parameters.R b/R/parameters.R new file mode 100644 index 0000000..756e5f8 --- /dev/null +++ b/R/parameters.R @@ -0,0 +1,81 @@ +#' Parameter for measure steps +#' +#' `window_side()` and `differentiation_order()` are used with Savitzky-Golay +#' processing. +#' +#' @param range A two-element vector holding the _defaults_ for the smallest and +#' largest possible values, respectively. If a transformation is specified, +#' these values should be in the _transformed units_. +#' +#' @param trans A `trans` object from the `scales` package, such as +#' `scales::transform_log10()` or `scales::transform_reciprocal()`. If not provided, +#' the default is used which matches the units used in `range`. If no +#' transformation, `NULL`. +#' +#' @details +#' This parameter is often used to correct for zero-count data in tables or +#' proportions. +#' +#' @return A function with classes `"quant_param"` and `"param"`. +#' @examples +#' window_side() +#' differentiation_order() +#' @export +window_side <- function(range = c(1L, 5L), trans = NULL) { + dials::new_quant_param( + type = "integer", + range = range, + inclusive = c(TRUE, TRUE), + trans = trans, + label = c(window_side = "Window Size (one side)"), + finalize = NULL + ) +} + +#' @rdname window_side +#' @export +differentiation_order <- function(range = c(0L, 4L), trans = NULL) { + dials::new_quant_param( + type = "integer", + range = range, + inclusive = c(TRUE, TRUE), + trans = trans, + label = c(differentiation_order = "Differentiation Order"), + finalize = NULL + ) +} + + + + +# ------------------------------------------------------------------------------ +# Tunable methods + +#' @importFrom generics tunable +#' @export +generics::tunable + +#' tunable methods for measure +#' +#' These functions define what parameters _can_ be tuned for specific steps. +#' They also define the recommended objects from the `dials` package that can be +#' used to generate new parameter values and other characteristics. +#' @param x A recipe step object +#' @param ... Not used. +#' @name tunable_measure +#' @return A tibble object. +#' @keywords internal +#' @export +tunable.step_measure_savitzky_golay <- function(x, ...) { + tibble::tibble( + name = c("window_side", "differentiation_order", "degree"), + call_info = list( + list(pkg = "measure", fun = "window_side"), + list(pkg = "measure", fun = "differentiation_order"), + list(pkg = "dials", fun = "degree_int", range = c(1L, 5L)) + ), + source = "recipe", + component = "step_measure_savitzky_golay", + component_id = x$id + ) +} diff --git a/R/savitzky_golay.R b/R/savitzky_golay.R index 153bbb4..17a6515 100644 --- a/R/savitzky_golay.R +++ b/R/savitzky_golay.R @@ -10,7 +10,9 @@ #' @param trained A logical to indicate if the quantities for #' preprocessing have been estimated. #' @param degree An integer for the polynomial degree to use for smoothing. -#' @param window_size An odd integer for the window size to use for smoothing. +#' @param window_side An integer for how many units there are on each side of +#' the window. This means that `window_side = 1` has a total window width of +#' 3 (e.g., width is `2 * window_side + 1`). #' @param differentiation_order An integer for the degree of filtering (zero #' indicates no differentiation). #' @param skip A logical. Should the step be skipped when the @@ -57,7 +59,7 @@ #' step_measure_savitzky_golay( #' differentiation_order = 1, #' degree = 3, -#' window_size = 5 +#' window_side = 5 #' ) %>% #' prep() #' } @@ -66,7 +68,7 @@ step_measure_savitzky_golay <- role = NA, trained = FALSE, degree = 3, - window_size = 11, + window_side = 11, differentiation_order = 0, skip = FALSE, id = rand_id("measure_savitzky_golay")) { @@ -76,7 +78,7 @@ step_measure_savitzky_golay <- trained = trained, role = role, degree = degree, - window_size = window_size, + window_side = window_side, differentiation_order = differentiation_order, skip = FALSE, id = id @@ -85,14 +87,14 @@ step_measure_savitzky_golay <- } step_measure_savitzky_golay_new <- - function(role, trained, degree, window_size, differentiation_order, + function(role, trained, degree, window_side, differentiation_order, na_rm, skip, id) { recipes::step( subclass = "measure_savitzky_golay", role = role, trained = trained, degree = degree, - window_size = window_size, + window_side = window_side, differentiation_order = differentiation_order, skip = skip, id = id @@ -103,47 +105,53 @@ step_measure_savitzky_golay_new <- prep.step_measure_savitzky_golay <- function(x, training, info = NULL, ...) { check_for_measure(training) if (!is.numeric(x$degree) | length(x$degree) != 1 | x$degree < 1) { - cli::cli_abort("{.arg degree} to {.fn step_measure_savitzky_golay} should - be a single integer greater than zero.") + cli::cli_abort("The {.arg degree} argument to \\ + {.fn step_measure_savitzky_golay} was {x$degree} and \\ + should be a single integer greater than zero.") } if (!is.numeric(x$differentiation_order) | length(x$differentiation_order) != 1 | x$differentiation_order < 0) { - cli::cli_abort("The {.arg differentiation_order} argument to - {.fn step_measure_savitzky_golay} should be a single + cli::cli_abort("The {.arg differentiation_order} argument to \\ + {.fn step_measure_savitzky_golay} should be a single \\ integer greater than -1.") } - if (!is.numeric(x$window_size) | length(x$window_size) != 1 | - x$window_size < 1 | x$window_size %% 2 != 1) { - cli::cli_abort("The {.arg window_size} argument to - {.fn step_measure_savitzky_golay} should be a single odd + if (!is.numeric(x$window_side) | length(x$window_side) != 1 | + x$window_side < 1) { + cli::cli_abort("The {.arg window_side} argument to \\ + {.fn step_measure_savitzky_golay} should be an \\ integer greater than 0.") } + window_size = 2 * x$window_side + 1 + # polynomial order p must be geater or equal to differentiation order m if (x$degree <= x$differentiation_order) { x$degree <- x$differentiation_order + 1 cli::cli_warn("The {.arg degree} argument to - {.fn step_measure_savitzky_golay} should be greater than or - equal to {.arg differentiation_order}. The polynomial degree + {.fn step_measure_savitzky_golay} should be greater than or \\ + equal to {.arg differentiation_order} \\ + ({x$differentiation_order}). The polynomial degree \\ was increased to {x$degree}.") } # filter length w must be greater than polynomial order p - if (x$window_size <= x$degree) { - x$window_size <- x$degree + 1 - if (x$window_size %% 2 == 0) { - x$window_size <- x$window_size + 1 - } - cli::cli_warn("The {.arg window_size} argument to - {.fn step_measure_savitzky_golay} should be greater than or - equal to {.arg degree}. The polynomial degree was increased - to {x$window_size}.") + if (window_size <= x$degree) { + old_val <- x$window_side + old_size <- 2 * old_val + 1 + x$window_side <- ceiling(x$degree/2) + cli::cli_warn("The window size ({old_size}) should be greater than or \\ + equal to {.arg degree} ({x$degree}). {.arg window_side} was \\ + increased from {old_val} to {x$window_side}.") } +# 2*wd + 1 > d +# 2*wd < d - 1 +# wd > (d-1)/2 + step_measure_savitzky_golay_new( role = x$role, trained = TRUE, degree = x$degree, - window_size = x$window_size, + window_side = x$window_side, differentiation_order = x$differentiation_order, skip = x$skip, id = x$id @@ -157,7 +165,7 @@ bake.step_measure_savitzky_golay <- function(object, new_data, ...) { new_data$.measures, diffs = object$differentiation_order, degree = object$degree, - window = object$window_size + window = 2 * object$window_side + 1 ) # TODO try to approximate the wave numbers that were input. new_data$.measures <- res diff --git a/man/reexports.Rd b/man/reexports.Rd new file mode 100644 index 0000000..c86bc7a --- /dev/null +++ b/man/reexports.Rd @@ -0,0 +1,16 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/parameters.R +\docType{import} +\name{reexports} +\alias{reexports} +\alias{tunable} +\title{Objects exported from other packages} +\keyword{internal} +\description{ +These objects are imported from other packages. Follow the links +below to see their documentation. + +\describe{ + \item{generics}{\code{\link[generics]{tunable}}} +}} + diff --git a/man/step_measure_savitzky_golay.Rd b/man/step_measure_savitzky_golay.Rd index b84bb0a..f7bc10d 100644 --- a/man/step_measure_savitzky_golay.Rd +++ b/man/step_measure_savitzky_golay.Rd @@ -9,7 +9,7 @@ step_measure_savitzky_golay( role = NA, trained = FALSE, degree = 3, - window_size = 11, + window_side = 11, differentiation_order = 0, skip = FALSE, id = rand_id("measure_savitzky_golay") @@ -27,7 +27,9 @@ preprocessing have been estimated.} \item{degree}{An integer for the polynomial degree to use for smoothing.} -\item{window_size}{An odd integer for the window size to use for smoothing.} +\item{window_side}{An integer for how many units there are on each side of +the window. This means that \code{window_side = 1} has a total window width of +3 (e.g., width is \code{2 * window_side + 1}).} \item{differentiation_order}{An integer for the degree of filtering (zero indicates no differentiation).} @@ -80,7 +82,7 @@ if (rlang::is_installed("prospectr")) { step_measure_savitzky_golay( differentiation_order = 1, degree = 3, - window_size = 5 + window_side = 5 ) \%>\% prep() } diff --git a/man/tunable_measure.Rd b/man/tunable_measure.Rd new file mode 100644 index 0000000..c0da6b4 --- /dev/null +++ b/man/tunable_measure.Rd @@ -0,0 +1,23 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/parameters.R +\name{tunable_measure} +\alias{tunable_measure} +\alias{tunable.step_measure_savitzky_golay} +\title{tunable methods for measure} +\usage{ +\method{tunable}{step_measure_savitzky_golay}(x, ...) +} +\arguments{ +\item{x}{A recipe step object} + +\item{...}{Not used.} +} +\value{ +A tibble object. +} +\description{ +These functions define what parameters \emph{can} be tuned for specific steps. +They also define the recommended objects from the \code{dials} package that can be +used to generate new parameter values and other characteristics. +} +\keyword{internal} diff --git a/man/window_side.Rd b/man/window_side.Rd new file mode 100644 index 0000000..5381fa2 --- /dev/null +++ b/man/window_side.Rd @@ -0,0 +1,36 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/parameters.R +\name{window_side} +\alias{window_side} +\alias{differentiation_order} +\title{Parameter for measure steps} +\usage{ +window_side(range = c(1L, 5L), trans = NULL) + +differentiation_order(range = c(0L, 4L), trans = NULL) +} +\arguments{ +\item{range}{A two-element vector holding the \emph{defaults} for the smallest and +largest possible values, respectively. If a transformation is specified, +these values should be in the \emph{transformed units}.} + +\item{trans}{A \code{trans} object from the \code{scales} package, such as +\code{scales::transform_log10()} or \code{scales::transform_reciprocal()}. If not provided, +the default is used which matches the units used in \code{range}. If no +transformation, \code{NULL}.} +} +\value{ +A function with classes \code{"quant_param"} and \code{"param"}. +} +\description{ +\code{window_side()} and \code{differentiation_order()} are used with Savitzky-Golay +processing. +} +\details{ +This parameter is often used to correct for zero-count data in tables or +proportions. +} +\examples{ +window_side() +differentiation_order() +} diff --git a/tests/testthat/_snaps/savitzky_golay.md b/tests/testthat/_snaps/savitzky_golay.md index b664c7c..1111007 100644 --- a/tests/testthat/_snaps/savitzky_golay.md +++ b/tests/testthat/_snaps/savitzky_golay.md @@ -1,296 +1,124 @@ # savitzky-golay inputs - The `degree` argument to `step_measure_savitzky_golay()` should be greater than or equal to `differentiation_order`. The polynomial degree was increased to 3. + The window size (3) should be greater than or equal to `degree` (5). `window_side` was increased from 1 to 3. --- - The `degree` argument to `step_measure_savitzky_golay()` should be greater than or equal to `differentiation_order`. The polynomial degree was increased to 4. + The window size (3) should be greater than or equal to `degree` (5). `window_side` was increased from 1 to 3. --- - The `degree` argument to `step_measure_savitzky_golay()` should be greater than or equal to `differentiation_order`. The polynomial degree was increased to 4. + The window size (3) should be greater than or equal to `degree` (5). `window_side` was increased from 1 to 3. --- - The `window_size` argument to `step_measure_savitzky_golay()` should be greater than or equal to `degree`. The polynomial degree was increased to 7. + The window size (3) should be greater than or equal to `degree` (5). `window_side` was increased from 1 to 3. --- - The `window_size` argument to `step_measure_savitzky_golay()` should be greater than or equal to `degree`. The polynomial degree was increased to 7. + The window size (3) should be greater than or equal to `degree` (6). `window_side` was increased from 1 to 3. --- - The `window_size` argument to `step_measure_savitzky_golay()` should be greater than or equal to `degree`. The polynomial degree was increased to 7. + The window size (3) should be greater than or equal to `degree` (6). `window_side` was increased from 1 to 3. --- - The `window_size` argument to `step_measure_savitzky_golay()` should be greater than or equal to `degree`. The polynomial degree was increased to 7. + The window size (3) should be greater than or equal to `degree` (6). `window_side` was increased from 1 to 3. --- - The `window_size` argument to `step_measure_savitzky_golay()` should be greater than or equal to `degree`. The polynomial degree was increased to 7. + The window size (3) should be greater than or equal to `degree` (6). `window_side` was increased from 1 to 3. --- - The `window_size` argument to `step_measure_savitzky_golay()` should be greater than or equal to `degree`. The polynomial degree was increased to 7. + The `degree` argument to `step_measure_savitzky_golay()` should be greater than or equal to `differentiation_order` (1). The polynomial degree was increased to 2. --- - The `window_size` argument to `step_measure_savitzky_golay()` should be greater than or equal to `degree`. The polynomial degree was increased to 7. + The `degree` argument to `step_measure_savitzky_golay()` should be greater than or equal to `differentiation_order` (2). The polynomial degree was increased to 3. --- - The `window_size` argument to `step_measure_savitzky_golay()` should be greater than or equal to `degree`. The polynomial degree was increased to 7. + The `degree` argument to `step_measure_savitzky_golay()` should be greater than or equal to `differentiation_order` (3). The polynomial degree was increased to 4. --- - Code - rec %>% step_measure_savitzky_golay(differentiation_order = bad_inputs$diffs[i], - window_size = bad_inputs$wn[i], degree = bad_inputs$deg[i]) %>% prep() - Condition - Error in `step_measure_savitzky_golay()`: - Caused by error in `prep()`: - ! The `window_size` argument to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. + The `degree` argument to `step_measure_savitzky_golay()` should be greater than or equal to `differentiation_order` (2). The polynomial degree was increased to 3. --- - Code - rec %>% step_measure_savitzky_golay(differentiation_order = bad_inputs$diffs[i], - window_size = bad_inputs$wn[i], degree = bad_inputs$deg[i]) %>% prep() - Condition - Error in `step_measure_savitzky_golay()`: - Caused by error in `prep()`: - ! The `window_size` argument to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. + The `degree` argument to `step_measure_savitzky_golay()` should be greater than or equal to `differentiation_order` (3). The polynomial degree was increased to 4. --- - Code - rec %>% step_measure_savitzky_golay(differentiation_order = bad_inputs$diffs[i], - window_size = bad_inputs$wn[i], degree = bad_inputs$deg[i]) %>% prep() - Condition - Error in `step_measure_savitzky_golay()`: - Caused by error in `prep()`: - ! The `window_size` argument to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. + The `degree` argument to `step_measure_savitzky_golay()` should be greater than or equal to `differentiation_order` (3). The polynomial degree was increased to 4. --- Code rec %>% step_measure_savitzky_golay(differentiation_order = bad_inputs$diffs[i], - window_size = bad_inputs$wn[i], degree = bad_inputs$deg[i]) %>% prep() + window_side = bad_inputs$wn[i], degree = bad_inputs$deg[i]) %>% prep() Condition Error in `step_measure_savitzky_golay()`: Caused by error in `prep()`: - ! The `window_size` argument to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. + ! The `differentiation_order` argument to `step_measure_savitzky_golay()` should be a single integer greater than -1. --- Code rec %>% step_measure_savitzky_golay(differentiation_order = bad_inputs$diffs[i], - window_size = bad_inputs$wn[i], degree = bad_inputs$deg[i]) %>% prep() + window_side = bad_inputs$wn[i], degree = bad_inputs$deg[i]) %>% prep() Condition Error in `step_measure_savitzky_golay()`: Caused by error in `prep()`: - ! The `window_size` argument to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. + ! The `degree` argument to `step_measure_savitzky_golay()` was 0 and should be a single integer greater than zero. --- Code rec %>% step_measure_savitzky_golay(differentiation_order = bad_inputs$diffs[i], - window_size = bad_inputs$wn[i], degree = bad_inputs$deg[i]) %>% prep() + window_side = bad_inputs$wn[i], degree = bad_inputs$deg[i]) %>% prep() Condition Error in `step_measure_savitzky_golay()`: Caused by error in `prep()`: - ! The `window_size` argument to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. + ! The `window_side` argument to `step_measure_savitzky_golay()` should be an integer greater than 0. ---- +# savitzky-golay tuning parameters Code - rec %>% step_measure_savitzky_golay(differentiation_order = bad_inputs$diffs[i], - window_size = bad_inputs$wn[i], degree = bad_inputs$deg[i]) %>% prep() - Condition - Error in `step_measure_savitzky_golay()`: - Caused by error in `prep()`: - ! The `window_size` argument to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. + window_side() + Output + Window Size (one side) (quantitative) + Range: [1, 5] --- Code - rec %>% step_measure_savitzky_golay(differentiation_order = bad_inputs$diffs[i], - window_size = bad_inputs$wn[i], degree = bad_inputs$deg[i]) %>% prep() - Condition - Error in `step_measure_savitzky_golay()`: - Caused by error in `prep()`: - ! The `window_size` argument to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. + window_side(c(2, 10)) + Output + Window Size (one side) (quantitative) + Range: [2, 10] --- Code - rec %>% step_measure_savitzky_golay(differentiation_order = bad_inputs$diffs[i], - window_size = bad_inputs$wn[i], degree = bad_inputs$deg[i]) %>% prep() - Condition - Error in `step_measure_savitzky_golay()`: - Caused by error in `prep()`: - ! The `window_size` argument to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. + differentiation_order() + Output + Differentiation Order (quantitative) + Range: [0, 4] --- Code - rec %>% step_measure_savitzky_golay(differentiation_order = bad_inputs$diffs[i], - window_size = bad_inputs$wn[i], degree = bad_inputs$deg[i]) %>% prep() - Condition - Error in `step_measure_savitzky_golay()`: - Caused by error in `prep()`: - ! The `window_size` argument to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. - ---- - - Code - rec %>% step_measure_savitzky_golay(differentiation_order = bad_inputs$diffs[i], - window_size = bad_inputs$wn[i], degree = bad_inputs$deg[i]) %>% prep() - Condition - Error in `step_measure_savitzky_golay()`: - Caused by error in `prep()`: - ! The `window_size` argument to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. - ---- - - Code - rec %>% step_measure_savitzky_golay(differentiation_order = bad_inputs$diffs[i], - window_size = bad_inputs$wn[i], degree = bad_inputs$deg[i]) %>% prep() - Condition - Error in `step_measure_savitzky_golay()`: - Caused by error in `prep()`: - ! The `window_size` argument to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. - ---- - - Code - rec %>% step_measure_savitzky_golay(differentiation_order = bad_inputs$diffs[i], - window_size = bad_inputs$wn[i], degree = bad_inputs$deg[i]) %>% prep() - Condition - Error in `step_measure_savitzky_golay()`: - Caused by error in `prep()`: - ! The `window_size` argument to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. - ---- - - Code - rec %>% step_measure_savitzky_golay(differentiation_order = bad_inputs$diffs[i], - window_size = bad_inputs$wn[i], degree = bad_inputs$deg[i]) %>% prep() - Condition - Error in `step_measure_savitzky_golay()`: - Caused by error in `prep()`: - ! The `window_size` argument to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. - ---- - - Code - rec %>% step_measure_savitzky_golay(differentiation_order = bad_inputs$diffs[i], - window_size = bad_inputs$wn[i], degree = bad_inputs$deg[i]) %>% prep() - Condition - Error in `step_measure_savitzky_golay()`: - Caused by error in `prep()`: - ! The `window_size` argument to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. - ---- - - Code - rec %>% step_measure_savitzky_golay(differentiation_order = bad_inputs$diffs[i], - window_size = bad_inputs$wn[i], degree = bad_inputs$deg[i]) %>% prep() - Condition - Error in `step_measure_savitzky_golay()`: - Caused by error in `prep()`: - ! The `window_size` argument to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. - ---- - - Code - rec %>% step_measure_savitzky_golay(differentiation_order = bad_inputs$diffs[i], - window_size = bad_inputs$wn[i], degree = bad_inputs$deg[i]) %>% prep() - Condition - Error in `step_measure_savitzky_golay()`: - Caused by error in `prep()`: - ! The `window_size` argument to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. - ---- - - Code - rec %>% step_measure_savitzky_golay(differentiation_order = bad_inputs$diffs[i], - window_size = bad_inputs$wn[i], degree = bad_inputs$deg[i]) %>% prep() - Condition - Error in `step_measure_savitzky_golay()`: - Caused by error in `prep()`: - ! The `window_size` argument to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. - ---- - - Code - rec %>% step_measure_savitzky_golay(differentiation_order = bad_inputs$diffs[i], - window_size = bad_inputs$wn[i], degree = bad_inputs$deg[i]) %>% prep() - Condition - Error in `step_measure_savitzky_golay()`: - Caused by error in `prep()`: - ! The `window_size` argument to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. - ---- - - Code - rec %>% step_measure_savitzky_golay(differentiation_order = bad_inputs$diffs[i], - window_size = bad_inputs$wn[i], degree = bad_inputs$deg[i]) %>% prep() - Condition - Error in `step_measure_savitzky_golay()`: - Caused by error in `prep()`: - ! The `window_size` argument to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. - ---- - - Code - rec %>% step_measure_savitzky_golay(differentiation_order = bad_inputs$diffs[i], - window_size = bad_inputs$wn[i], degree = bad_inputs$deg[i]) %>% prep() - Condition - Error in `step_measure_savitzky_golay()`: - Caused by error in `prep()`: - ! The `window_size` argument to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. - ---- - - Code - rec %>% step_measure_savitzky_golay(differentiation_order = bad_inputs$diffs[i], - window_size = bad_inputs$wn[i], degree = bad_inputs$deg[i]) %>% prep() - Condition - Error in `step_measure_savitzky_golay()`: - Caused by error in `prep()`: - ! The `window_size` argument to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. - ---- - - Code - rec %>% step_measure_savitzky_golay(differentiation_order = bad_inputs$diffs[i], - window_size = bad_inputs$wn[i], degree = bad_inputs$deg[i]) %>% prep() - Condition - Error in `step_measure_savitzky_golay()`: - Caused by error in `prep()`: - ! The `window_size` argument to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. - ---- - - Code - rec %>% step_measure_savitzky_golay(differentiation_order = bad_inputs$diffs[i], - window_size = bad_inputs$wn[i], degree = bad_inputs$deg[i]) %>% prep() - Condition - Error in `step_measure_savitzky_golay()`: - Caused by error in `prep()`: - ! The `window_size` argument to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. - ---- - - The `degree` argument to `step_measure_savitzky_golay()` should be greater than or equal to `differentiation_order`. The polynomial degree was increased to 3. - ---- - - The `degree` argument to `step_measure_savitzky_golay()` should be greater than or equal to `differentiation_order`. The polynomial degree was increased to 4. - ---- - - The `degree` argument to `step_measure_savitzky_golay()` should be greater than or equal to `differentiation_order`. The polynomial degree was increased to 4. + recipe(water + fat + protein ~ ., data = meats_long) %>% update_role(id, + new_role = "id") %>% step_measure_input_long(transmittance, location = vars( + channel)) %>% step_measure_savitzky_golay() %>% tunable() + Output + # A tibble: 3 x 5 + name call_info source component component_id + + 1 window_side recipe step_measure_savit~ measure_sav~ + 2 differentiation_order recipe step_measure_savit~ measure_sav~ + 3 degree recipe step_measure_savit~ measure_sav~ diff --git a/tests/testthat/test_savitzky_golay.R b/tests/testthat/test_savitzky_golay.R index 837585e..7792508 100644 --- a/tests/testthat/test_savitzky_golay.R +++ b/tests/testthat/test_savitzky_golay.R @@ -50,54 +50,36 @@ test_that("savitzky-golay inputs", { bad_inputs <- tibble::tribble( ~diffs, ~deg, ~wn, - 2L, 1L, 5, - 3L, 1L, 5, - 3L, 2L, 5, - 0L, 5L, 5, - 1L, 5L, 5, - 2L, 5L, 5, - 3L, 5L, 5, - 0L, 6L, 5, - 1L, 6L, 5, - 2L, 6L, 5, - 3L, 6L, 5, - 0L, 1L, 10, - 1L, 1L, 10, - 2L, 1L, 10, - 3L, 1L, 10, - 0L, 2L, 10, - 1L, 2L, 10, - 2L, 2L, 10, - 3L, 2L, 10, - 0L, 3L, 10, - 1L, 3L, 10, - 2L, 3L, 10, - 3L, 3L, 10, - 0L, 4L, 10, - 1L, 4L, 10, - 2L, 4L, 10, - 3L, 4L, 10, - 0L, 5L, 10, - 1L, 5L, 10, - 2L, 5L, 10, - 3L, 5L, 10, - 0L, 6L, 10, - 1L, 6L, 10, - 2L, 6L, 10, - 3L, 6L, 10, - 2L, 1L, 15, - 3L, 1L, 15, - 3L, 2L, 15 + 0L, 5L, 1L, + 1L, 5L, 1L, + 2L, 5L, 1L, + 3L, 5L, 1L, + 0L, 6L, 1L, + 1L, 6L, 1L, + 2L, 6L, 1L, + 3L, 6L, 1L, + 1L, 1L, 10L, + 2L, 1L, 10L, + 3L, 1L, 10L, + 2L, 2L, 10L, + 3L, 2L, 10L, + 3L, 3L, 10L, + -1L, 1L, 21L, + 3L, 0L, 21L, + 3L, 2L, 0L ) + for (i in 1:nrow(bad_inputs)) { - if (bad_inputs$wn[i] == 10) { + bad_rng <- bad_inputs$diffs[i] < 0 | bad_inputs$deg[i] < 1 | bad_inputs$wn[i] < 1 + + if (bad_rng) { expect_snapshot( { rec %>% step_measure_savitzky_golay( differentiation_order = bad_inputs$diffs[i], - window_size = bad_inputs$wn[i], + window_side = bad_inputs$wn[i], degree = bad_inputs$deg[i] ) %>% prep() @@ -109,7 +91,7 @@ test_that("savitzky-golay inputs", { rec %>% step_measure_savitzky_golay( differentiation_order = bad_inputs$diffs[i], - window_size = bad_inputs$wn[i], + window_side = bad_inputs$wn[i], degree = bad_inputs$deg[i] ) %>% prep() @@ -117,3 +99,18 @@ test_that("savitzky-golay inputs", { } } }) + + +test_that("savitzky-golay tuning parameters", { + expect_snapshot(window_side()) + expect_snapshot(window_side(c(2, 10))) + expect_snapshot(differentiation_order()) + + expect_snapshot( + recipe(water + fat + protein ~ ., data = meats_long) %>% + update_role(id, new_role = "id") %>% + step_measure_input_long(transmittance, location = vars(channel)) %>% + step_measure_savitzky_golay() %>% + tunable() + ) +})