From 610e5d4459610217fa461576f2a87338592525e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=98topepo=E2=80=99?= <‘mxkuhn@gmail.com’> Date: Wed, 3 Jan 2024 12:58:49 -0500 Subject: [PATCH 01/13] initial work on Savitzky-Golay filter --- DESCRIPTION | 2 +- NAMESPACE | 7 ++ R/baseline.R | 2 + R/helpers.R | 36 ++++++ R/savitzky_golay.R | 161 +++++++++++++++++++++++++++ man/step_measure_savitzky_golay.Rd | 62 +++++++++++ tests/testthat/test_savitzky_golay.R | 0 7 files changed, 269 insertions(+), 1 deletion(-) create mode 100644 R/savitzky_golay.R create mode 100644 man/step_measure_savitzky_golay.Rd create mode 100644 tests/testthat/test_savitzky_golay.R diff --git a/DESCRIPTION b/DESCRIPTION index 007401c..14e8677 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -31,7 +31,7 @@ Suggests: Config/testthat/edition: 3 Encoding: UTF-8 Roxygen: list(markdown = TRUE) -RoxygenNote: 7.2.3 +RoxygenNote: 7.2.3.9000 VignetteBuilder: knitr Depends: R (>= 3.5.0), diff --git a/NAMESPACE b/NAMESPACE index df7da00..732fdf2 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,16 +1,23 @@ # Generated by roxygen2: do not edit by hand +S3method(bake,step_baseline) S3method(bake,step_measure_input_long) S3method(bake,step_measure_input_wide) +S3method(bake,step_measure_savitzky_golay) +S3method(prep,step_baseline) S3method(prep,step_measure_input_long) S3method(prep,step_measure_input_wide) +S3method(prep,step_measure_savitzky_golay) S3method(print,step_baseline) S3method(print,step_measure_input_long) S3method(print,step_measure_input_wide) +S3method(print,step_measure_savitzky_golay) S3method(tidy,step_measure_input_long) S3method(tidy,step_measure_input_wide) +S3method(tidy,step_measure_savitzky_golay) export(step_measure_input_long) export(step_measure_input_wide) +export(step_measure_savitzky_golay) export(subtract_rf_baseline) import(recipes) import(rlang) diff --git a/R/baseline.R b/R/baseline.R index 235bdfd..57a12bf 100644 --- a/R/baseline.R +++ b/R/baseline.R @@ -41,6 +41,7 @@ step_baseline_new <- ) } +#' @export prep.step_baseline <- function(x, training, info = NULL, ...) { col_names <- recipes::recipes_eval_select(x$terms, training, info) recipes::check_type(x, quant = TRUE) @@ -84,6 +85,7 @@ subtract_rf_baseline <- function(data, yvar, span = 2/3, maxit = c(5, 5)){ ) } +#' @export bake.step_baseline <- function(object, new_data, ...) { cli::cli_alert_danger("Not yet implemented.") } diff --git a/R/helpers.R b/R/helpers.R index 78e9924..4945063 100644 --- a/R/helpers.R +++ b/R/helpers.R @@ -12,3 +12,39 @@ add_location <- function(.data, loc) { .measures = purrr::map(.measures, add_location_col, loc = loc) ) } + +# ------------------------------------------------------------------------------ +# Move between lists of tibbles and matrices (and back) + +# Assumes identical locations +measure_to_matrix <- function(x) { + n <- length(x) + loc <- x[[1]]$location + res <- do.call("rbind", purrr::map(x, ~ .x[["value"]])) + res +} + +matrix_to_measure <- function(x, loc) { + # x is {num_samples} x {num_features} + # We need to convert this to a list of length {num_samples}. + # Each list element is a tibble that is {num_features} x 2 + if (!is.matrix(x)) { + cli::cli_abort("Input should be a matrix.") + } + if (length(loc) != ncol(x)) { + cli::cli_abort("# locations should be the same at the number of columns in the source matrix.") + } + + x <- t(x) + x <- tibble::as_tibble(x, .name_repair = "minimal") + + res <- purrr::map(x, ~ tibble::new_tibble(list(value = .x, location = loc))) + unname(res) +} + +measure_to_tibble <- function(x) { + x <- + tibble::tibble(x = sg, sample_num = seq_along(x)) %>% + tidyr::unnest(cols = x) + x +} diff --git a/R/savitzky_golay.R b/R/savitzky_golay.R new file mode 100644 index 0000000..c43794c --- /dev/null +++ b/R/savitzky_golay.R @@ -0,0 +1,161 @@ +#' Savitzky-Golay Pre-Processing +#' +#' `step_measure_savitzky_golay` creates a *specification* of a recipe +#' step that +#' +#' @param recipe A recipe object. The step will be added to the +#' sequence of operations for this recipe. +#' @param ... One or more selector functions to choose which +#' variables are affected by the step. See [selections()] +#' for more details. For the `tidy` method, these are not +#' currently used. +#' @param role Not used by this step since no new variables are +#' created. +#' @param trained A logical to indicate if the quantities for +#' preprocessing have been estimated. +#' @param degree The polynomial degree to use for smoothing. +#' @param window_size The window size to use for smoothing. +#' @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 +#' recipe is baked by [bake()]? While all operations are baked +#' when [prep()] is run, some operations may not be able to be +#' conducted on new data (e.g. processing the outcome variable(s)). +#' Care should be taken when using `skip = TRUE` as it may affect +#' the computations for subsequent operations +#' @param id A character string that is unique to this step to identify it. +#' @return An updated version of `recipe` with the new step added to the +#' sequence of any existing operations. +#' +#' @export +#' @details +#' +#' # Tidying +#' +#' When you [`tidy()`][tidy.recipe()] this step, a tibble with columns +#' is returned. +#' +# @examples + +step_measure_savitzky_golay <- + function(recipe, + ..., + role = NA, + trained = FALSE, + degree = 3, + window_size = 11, + differentiation_order = 0, + skip = FALSE, + id = rand_id("measure_savitzky_golay")) { + recipes::add_step( + recipe, + step_measure_savitzky_golay_new( + terms = enquos(...), + trained = trained, + role = role, + degree = degree, + window_size = window_size, + differentiation_order = differentiation_order, + skip = FALSE, + id = id + ) + ) + } + +step_measure_savitzky_golay_new <- + function(terms, role, trained, degree, window_size, differentiation_order, + na_rm, skip, id) { + recipes::step( + subclass = "measure_savitzky_golay", + terms = terms, + role = role, + trained = trained, + degree = degree, + window_size = window_size, + differentiation_order = differentiation_order, + skip = skip, + id = id + ) + } + +#' @export +prep.step_measure_savitzky_golay <- function(x, training, info = NULL, ...) { + col_names <- recipes::recipes_eval_select(x$terms, training, info) + recipes::check_type(training[, col_names]) + + step_measure_savitzky_golay_new( + terms = x$terms, + role = x$role, + trained = TRUE, + degree = x$degree, + window_size = x$window_size, + differentiation_order = x$differentiation_order, + skip = x$skip, + id = x$id + ) +} + +#' @export +bake.step_measure_savitzky_golay <- function(object, new_data, ...) { + + res <- + .comp_savitzky_golay( + new_data$.measures, + diffs = object$differentiation_order, + degree = object$degree, + window = object$window_size + ) + new_data$.measures <- res + tibble::as_tibble(new_data) +} + +#' @export +print.step_measure_savitzky_golay <- + function(x, width = max(20, options()$width - 30), ...) { + title <- "Savitzky-Golay preprocessing " + recipes::print_step(names(x$means), x$terms, x$trained, title, width) + invisible(x) + } + +#' @rdname measure-tidy.recipe +#' @export +tidy.step_measure_savitzky_golay <- function(x, ...) { + if (is_trained(x)) { + res <- tibble::tibble(terms = ".measure", + value = na_dbl) + } else { + term_names <- recipes::sel2char(x$terms) + res <- tibble::tibble(terms = term_names, + value = na_dbl) + } + res$id <- x$id + res +} + +# ------------------------------------------------------------------------------ + +.comp_savitzky_golay <- function(dat, diffs = 1, degree = 2, window = 3, ...) { + rlang::check_installed("prospectr") + loc <- dat[[1]]$location + dat <- measure_to_matrix(dat) + + cl <- + rlang::call2( + "savitzkyGolay", + .ns = "prospectr", + X = expr(dat), + m = diffs, + p = degree, + w = window, + ... + ) + res <- rlang::eval_tidy(cl) + + if (ncol(res) != ncol(dat)) { + # Prob can do better + loc <- 1:ncol(res) + } + + matrix_to_measure(res, loc) +} + diff --git a/man/step_measure_savitzky_golay.Rd b/man/step_measure_savitzky_golay.Rd new file mode 100644 index 0000000..045e988 --- /dev/null +++ b/man/step_measure_savitzky_golay.Rd @@ -0,0 +1,62 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/savitzky_golay.R +\name{step_measure_savitzky_golay} +\alias{step_measure_savitzky_golay} +\title{Savitzky-Golay Pre-Processing} +\usage{ +step_measure_savitzky_golay( + recipe, + ..., + role = NA, + trained = FALSE, + degree = 3, + window_size = 11, + differentiation_order = 0, + skip = FALSE, + id = rand_id("measure_savitzky_golay") +) +} +\arguments{ +\item{recipe}{A recipe object. The step will be added to the +sequence of operations for this recipe.} + +\item{...}{One or more selector functions to choose which +variables are affected by the step. See \code{\link[=selections]{selections()}} +for more details. For the \code{tidy} method, these are not +currently used.} + +\item{role}{Not used by this step since no new variables are +created. \if{html}{\out{}}} + +\item{trained}{A logical to indicate if the quantities for +preprocessing have been estimated.} + +\item{degree}{The polynomial degree to use for smoothing.} + +\item{window_size}{The window size to use for smoothing.} + +\item{differentiation_order}{An integer for the degree of filtering (zero +indicates no differentiation).} + +\item{skip}{A logical. Should the step be skipped when the +recipe is baked by \code{\link[=bake]{bake()}}? While all operations are baked +when \code{\link[=prep]{prep()}} is run, some operations may not be able to be +conducted on new data (e.g. processing the outcome variable(s)). +Care should be taken when using \code{skip = TRUE} as it may affect +the computations for subsequent operations} + +\item{id}{A character string that is unique to this step to identify it.} +} +\value{ +An updated version of \code{recipe} with the new step added to the +sequence of any existing operations. +} +\description{ +\code{step_measure_savitzky_golay} creates a \emph{specification} of a recipe +step that \if{html}{\out{}} +} +\section{Tidying}{ +When you \code{\link[=tidy.recipe]{tidy()}} this step, a tibble with columns +\if{html}{\out{}} is returned. +} + diff --git a/tests/testthat/test_savitzky_golay.R b/tests/testthat/test_savitzky_golay.R new file mode 100644 index 0000000..e69de29 From 65d522714610cfe1e639c27b2f5f399fdcc5df8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=98topepo=E2=80=99?= <‘mxkuhn@gmail.com’> Date: Wed, 3 Jan 2024 17:05:40 -0500 Subject: [PATCH 02/13] remove terms, more scaffolding --- NAMESPACE | 1 + R/helpers.R | 15 ++++++-- R/savitzky_golay.R | 58 +++++++++++++++++++++--------- man/step_measure_savitzky_golay.Rd | 20 ++++++----- 4 files changed, 67 insertions(+), 27 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 732fdf2..ad90683 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -12,6 +12,7 @@ S3method(print,step_baseline) S3method(print,step_measure_input_long) S3method(print,step_measure_input_wide) S3method(print,step_measure_savitzky_golay) +S3method(required_pkgs,step_isomap) S3method(tidy,step_measure_input_long) S3method(tidy,step_measure_input_wide) S3method(tidy,step_measure_savitzky_golay) diff --git a/R/helpers.R b/R/helpers.R index 4945063..0218aa7 100644 --- a/R/helpers.R +++ b/R/helpers.R @@ -18,8 +18,6 @@ add_location <- function(.data, loc) { # Assumes identical locations measure_to_matrix <- function(x) { - n <- length(x) - loc <- x[[1]]$location res <- do.call("rbind", purrr::map(x, ~ .x[["value"]])) res } @@ -44,7 +42,18 @@ matrix_to_measure <- function(x, loc) { measure_to_tibble <- function(x) { x <- - tibble::tibble(x = sg, sample_num = seq_along(x)) %>% + tibble::tibble(x = x, sample_num = seq_along(x)) %>% tidyr::unnest(cols = x) x } + +# ------------------------------------------------------------------------------ + +check_for_measure <- function(x) { + if (!any(names(x) == ".measures")) { + cli::cli_abort("A column called {.code .measures} in the data. See + {.fn step_measure_input_wide} and + {.fn step_measure_input_long}.") + } + invisible(NULL) +} diff --git a/R/savitzky_golay.R b/R/savitzky_golay.R index c43794c..b2a36d8 100644 --- a/R/savitzky_golay.R +++ b/R/savitzky_golay.R @@ -1,16 +1,12 @@ #' Savitzky-Golay Pre-Processing #' #' `step_measure_savitzky_golay` creates a *specification* of a recipe -#' step that +#' step that smooths and filters the measurement sequence. #' #' @param recipe A recipe object. The step will be added to the #' sequence of operations for this recipe. -#' @param ... One or more selector functions to choose which -#' variables are affected by the step. See [selections()] -#' for more details. For the `tidy` method, these are not -#' currently used. #' @param role Not used by this step since no new variables are -#' created. +#' created. #' @param trained A logical to indicate if the quantities for #' preprocessing have been estimated. #' @param degree The polynomial degree to use for smoothing. @@ -29,6 +25,14 @@ #' #' @export #' @details +#' No selectors should be supplied to this step function. The data should be in +#' a special internal format produced by [step_measure_input_wide()] or +#' [step_measure_input_long()]. +#' +#' Measurements are assumed to be equally spaced. +#' +#' The step will produce fewer predictor values (i.e., fewer measurements) than +#' the input. #' #' # Tidying #' @@ -39,7 +43,6 @@ step_measure_savitzky_golay <- function(recipe, - ..., role = NA, trained = FALSE, degree = 3, @@ -50,7 +53,6 @@ step_measure_savitzky_golay <- recipes::add_step( recipe, step_measure_savitzky_golay_new( - terms = enquos(...), trained = trained, role = role, degree = degree, @@ -63,11 +65,10 @@ step_measure_savitzky_golay <- } step_measure_savitzky_golay_new <- - function(terms, role, trained, degree, window_size, differentiation_order, + function(role, trained, degree, window_size, differentiation_order, na_rm, skip, id) { recipes::step( subclass = "measure_savitzky_golay", - terms = terms, role = role, trained = trained, degree = degree, @@ -80,11 +81,23 @@ step_measure_savitzky_golay_new <- #' @export prep.step_measure_savitzky_golay <- function(x, training, info = NULL, ...) { - col_names <- recipes::recipes_eval_select(x$terms, training, info) - recipes::check_type(training[, col_names]) + 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.") + } + if (!is.numeric(x$differentiation_order) | length(x$differentiation_order) != 1 + | x$differentiation_order < 0) { + cli::cli_abort("{.arg differentiation_order} 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("{.arg window_size} to {.fn step_measure_savitzky_golay} should + be a single odd integer greater than 0.") + } step_measure_savitzky_golay_new( - terms = x$terms, role = x$role, trained = TRUE, degree = x$degree, @@ -105,6 +118,7 @@ bake.step_measure_savitzky_golay <- function(object, new_data, ...) { degree = object$degree, window = object$window_size ) + # TODO try to approximate the wave numbers that were input. new_data$.measures <- res tibble::as_tibble(new_data) } @@ -113,7 +127,8 @@ bake.step_measure_savitzky_golay <- function(object, new_data, ...) { print.step_measure_savitzky_golay <- function(x, width = max(20, options()$width - 30), ...) { title <- "Savitzky-Golay preprocessing " - recipes::print_step(names(x$means), x$terms, x$trained, title, width) + recipes::print_step("", "", + x$trained, title, width) invisible(x) } @@ -132,6 +147,13 @@ tidy.step_measure_savitzky_golay <- function(x, ...) { res } +#' @rdname measure-required_pkgs.recipe +#' @export +required_pkgs.step_isomap <- function(x, ...) { + c("measure", "prospectr") +} + + # ------------------------------------------------------------------------------ .comp_savitzky_golay <- function(dat, diffs = 1, degree = 2, window = 3, ...) { @@ -149,10 +171,14 @@ tidy.step_measure_savitzky_golay <- function(x, ...) { w = window, ... ) - res <- rlang::eval_tidy(cl) + res <- try(rlang::eval_tidy(cl), silent = TRUE) + if (inherits(res, "try-error")) { + msg <- as.character(res) + cli::cli_abort("Savitzky-Golay computations failed with error: {msg}") + } if (ncol(res) != ncol(dat)) { - # Prob can do better + # TODO Prob can do better; can we appriximate what the wave numbers should be? loc <- 1:ncol(res) } diff --git a/man/step_measure_savitzky_golay.Rd b/man/step_measure_savitzky_golay.Rd index 045e988..89fef6f 100644 --- a/man/step_measure_savitzky_golay.Rd +++ b/man/step_measure_savitzky_golay.Rd @@ -6,7 +6,6 @@ \usage{ step_measure_savitzky_golay( recipe, - ..., role = NA, trained = FALSE, degree = 3, @@ -20,13 +19,8 @@ step_measure_savitzky_golay( \item{recipe}{A recipe object. The step will be added to the sequence of operations for this recipe.} -\item{...}{One or more selector functions to choose which -variables are affected by the step. See \code{\link[=selections]{selections()}} -for more details. For the \code{tidy} method, these are not -currently used.} - \item{role}{Not used by this step since no new variables are -created. \if{html}{\out{}}} +created.} \item{trained}{A logical to indicate if the quantities for preprocessing have been estimated.} @@ -53,7 +47,17 @@ sequence of any existing operations. } \description{ \code{step_measure_savitzky_golay} creates a \emph{specification} of a recipe -step that \if{html}{\out{}} +step that smooths and filters the measurement sequence. +} +\details{ +No selectors should be supplied to this step function. The data should be in +a special internal format produced by \code{\link[=step_measure_input_wide]{step_measure_input_wide()}} or +\code{\link[=step_measure_input_long]{step_measure_input_long()}}. + +Measurements are assumed to be equally spaced. + +The step will produce fewer predictor values (i.e., fewer measurements) than +the input. } \section{Tidying}{ When you \code{\link[=tidy.recipe]{tidy()}} this step, a tibble with columns From b7fe7be94c8fe84f88e4151fa0d425c9f1126df6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=98topepo=E2=80=99?= <‘mxkuhn@gmail.com’> Date: Wed, 3 Jan 2024 21:57:52 -0500 Subject: [PATCH 03/13] small changes --- R/savitzky_golay.R | 46 +++++++++++++++++++++++------- man/step_measure_savitzky_golay.Rd | 33 ++++++++++++++++----- 2 files changed, 61 insertions(+), 18 deletions(-) diff --git a/R/savitzky_golay.R b/R/savitzky_golay.R index b2a36d8..a3dcf76 100644 --- a/R/savitzky_golay.R +++ b/R/savitzky_golay.R @@ -9,8 +9,8 @@ #' created. #' @param trained A logical to indicate if the quantities for #' preprocessing have been estimated. -#' @param degree The polynomial degree to use for smoothing. -#' @param window_size The window size to use for smoothing. +#' @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 differentiation_order An integer for the degree of filtering (zero #' indicates no differentiation). #' @param skip A logical. Should the step be skipped when the @@ -24,22 +24,40 @@ #' sequence of any existing operations. #' #' @export +#' @family measure-smoothing +#' @family measure-differencing #' @details -#' No selectors should be supplied to this step function. The data should be in +#' This method can both smooth out random noise and reduce between-predictor +#' correlation. It fits a polynomial to a window of measurements and this results +#' in fewer measurements than the input. Measurements are assumed to be equally +#' spaced. +#' +#' The polynomial degree should be less than the window size. +#' +#' **No selectors should be supplied to this step function**. The data should be in #' a special internal format produced by [step_measure_input_wide()] or #' [step_measure_input_long()]. #' -#' Measurements are assumed to be equally spaced. -#' -#' The step will produce fewer predictor values (i.e., fewer measurements) than -#' the input. +#' The measurement locations are reset to integer indices starting at one. #' #' # Tidying #' #' When you [`tidy()`][tidy.recipe()] this step, a tibble with columns #' is returned. #' -# @examples +#' @examples +#' if (rlang::is_installed("prospectr")) { +#' rec <- +#' 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( +#' differentiation_order = 1, +#' degree = 3, +#' window_size = 5 +#' ) %>% +#' prep() +#' } step_measure_savitzky_golay <- function(recipe, @@ -88,14 +106,20 @@ prep.step_measure_savitzky_golay <- function(x, training, info = NULL, ...) { } if (!is.numeric(x$differentiation_order) | length(x$differentiation_order) != 1 | x$differentiation_order < 0) { - cli::cli_abort("{.arg differentiation_order} to {.fn step_measure_savitzky_golay} should - be a single integer greater than -1.") + cli::cli_abort("{.arg differentiation_order} 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("{.arg window_size} to {.fn step_measure_savitzky_golay} should + cli::cli_abort("{.arg window_size} to {.fn step_measure_savitzky_golay} should \\ be a single odd integer greater than 0.") } + if (x$window_size <= x$degree) { + cli::cli_abort("The {.arg window_size} value of {x$window_size} for \\ + {.fn step_measure_savitzky_golay} should \\ + be greater or equal to the {.arg degree} value of {x$degree}.") + } step_measure_savitzky_golay_new( role = x$role, diff --git a/man/step_measure_savitzky_golay.Rd b/man/step_measure_savitzky_golay.Rd index 89fef6f..2b40761 100644 --- a/man/step_measure_savitzky_golay.Rd +++ b/man/step_measure_savitzky_golay.Rd @@ -25,9 +25,9 @@ created.} \item{trained}{A logical to indicate if the quantities for preprocessing have been estimated.} -\item{degree}{The polynomial degree to use for smoothing.} +\item{degree}{An integer for the polynomial degree to use for smoothing.} -\item{window_size}{The window size to use for smoothing.} +\item{window_size}{An odd integer for the window size to use for smoothing.} \item{differentiation_order}{An integer for the degree of filtering (zero indicates no differentiation).} @@ -50,17 +50,36 @@ sequence of any existing operations. step that smooths and filters the measurement sequence. } \details{ -No selectors should be supplied to this step function. The data should be in +This method can both smooth out random noise and reduce between-predictor +correlation. It fits a polynomial to a window of measurements and this results +in fewer measurements than the input. Measurements are assumed to be equally +spaced. + +The polynomial degree should be less than the window size. + +\strong{No selectors should be supplied to this step function}. The data should be in a special internal format produced by \code{\link[=step_measure_input_wide]{step_measure_input_wide()}} or \code{\link[=step_measure_input_long]{step_measure_input_long()}}. -Measurements are assumed to be equally spaced. - -The step will produce fewer predictor values (i.e., fewer measurements) than -the input. +The measurement locations are reset to integer indices starting at one. } \section{Tidying}{ When you \code{\link[=tidy.recipe]{tidy()}} this step, a tibble with columns \if{html}{\out{}} is returned. } +\examples{ +if (rlang::is_installed("prospectr")) { + rec <- + 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( + differentiation_order = 1, + degree = 3, + window_size = 5 + ) \%>\% + prep() +} +} +\concept{measure-smoothing} From 367327e78c8d38b24ee82e269c70aa73ea11f651 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=98topepo=E2=80=99?= <‘mxkuhn@gmail.com’> Date: Wed, 3 Jan 2024 21:59:12 -0500 Subject: [PATCH 04/13] better error --- R/helpers.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/helpers.R b/R/helpers.R index 0218aa7..48cd0bd 100644 --- a/R/helpers.R +++ b/R/helpers.R @@ -51,7 +51,7 @@ measure_to_tibble <- function(x) { check_for_measure <- function(x) { if (!any(names(x) == ".measures")) { - cli::cli_abort("A column called {.code .measures} in the data. See + cli::cli_abort("A column called {.code .measures} should be in the data. See {.fn step_measure_input_wide} and {.fn step_measure_input_long}.") } From c8afcc4602bca0e085cbe76575f242281fd920cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=98topepo=E2=80=99?= <‘mxkuhn@gmail.com’> Date: Thu, 4 Jan 2024 07:14:15 -0500 Subject: [PATCH 05/13] small updates --- R/input_wide.R | 1 + R/savitzky_golay.R | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/R/input_wide.R b/R/input_wide.R index cf7cc20..87a2567 100644 --- a/R/input_wide.R +++ b/R/input_wide.R @@ -172,6 +172,7 @@ wide_to_list <- function(x, ind, selections) { ) %>% dplyr::select(-temp) %>% # TODO convert some of this to use vctrs + # https://www.tidyverse.org/blog/2023/04/performant-packages/#nest tidyr::nest(.by = c(-value), .key = ".measures") %>% dplyr::select(-..row) %>% add_location(ind) diff --git a/R/savitzky_golay.R b/R/savitzky_golay.R index a3dcf76..e95d119 100644 --- a/R/savitzky_golay.R +++ b/R/savitzky_golay.R @@ -107,17 +107,17 @@ prep.step_measure_savitzky_golay <- function(x, training, info = NULL, ...) { if (!is.numeric(x$differentiation_order) | length(x$differentiation_order) != 1 | x$differentiation_order < 0) { cli::cli_abort("{.arg differentiation_order} to - {.fn step_measure_savitzky_golay} should be a single \\ + {.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("{.arg window_size} to {.fn step_measure_savitzky_golay} should \\ + cli::cli_abort("{.arg window_size} to {.fn step_measure_savitzky_golay} should be a single odd integer greater than 0.") } if (x$window_size <= x$degree) { - cli::cli_abort("The {.arg window_size} value of {x$window_size} for \\ - {.fn step_measure_savitzky_golay} should \\ + cli::cli_abort("The {.arg window_size} value of {x$window_size} for + {.fn step_measure_savitzky_golay} should be greater or equal to the {.arg degree} value of {x$degree}.") } From daa075ff6fa2b4fca53459c6bfa6157f9621f355 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=98topepo=E2=80=99?= <‘mxkuhn@gmail.com’> Date: Thu, 4 Jan 2024 08:47:15 -0500 Subject: [PATCH 06/13] fail if missing data per sample --- R/checks.R | 15 +++++++++++++++ R/input_long.R | 2 ++ tests/testthat/test_measure_input_long.R | 8 +++++--- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/R/checks.R b/R/checks.R index f8d6bf5..97f4e4c 100644 --- a/R/checks.R +++ b/R/checks.R @@ -16,3 +16,18 @@ check_single_selector <- function(res, arg) { rlang::abort(msg) } } + +check_measure_dims <- function(x) { + num_rows <- purrr::map_int(x$.measures, nrow) + num_unique <- sort(table(num_rows), decreasing = TRUE) + most_freq <- as.integer(names(num_unique)[1]) + if (length(num_unique) != 1) { + which_rows <- which(num_rows != most_freq) + n_bad <- length(which_rows) + chr_rows <- paste(which_rows, collapse = ", ") + cli::cli_abort("The number of rows in each measure should be the same. + Most samples have {most_freq} rows and these do not: + {chr_rows}. Please pad the input with missing values.") + } + invisible(NULL) +} diff --git a/R/input_long.R b/R/input_long.R index ebbf917..8f3829c 100644 --- a/R/input_long.R +++ b/R/input_long.R @@ -117,6 +117,8 @@ bake.step_measure_input_long <- function(object, new_data, ...) { tidyr::nest(.by = c(-value), .key = ".measures") } + check_measure_dims(new_data) + new_data } diff --git a/tests/testthat/test_measure_input_long.R b/tests/testthat/test_measure_input_long.R index b7d3330..1aa20c1 100644 --- a/tests/testthat/test_measure_input_long.R +++ b/tests/testthat/test_measure_input_long.R @@ -57,10 +57,12 @@ test_that("ingest long format data", { ### missing rows - prep_2 <- + expect_snapshot( recipe(water + fat + protein ~ ., data = miss_train) %>% - step_measure_input_long(absorp, location = vars(ind)) %>% - prep() + step_measure_input_long(absorp, location = vars(ind)) %>% + prep(), + error = TRUE + ) ## missing values From fa9ef375b9c6411053133bbbb05b74728acf0139 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=98topepo=E2=80=99?= <‘mxkuhn@gmail.com’> Date: Thu, 4 Jan 2024 08:47:26 -0500 Subject: [PATCH 07/13] re-generate tests and snapshots --- DESCRIPTION | 1 + R/helpers.R | 1 + R/savitzky_golay.R | 10 +- man/step_measure_savitzky_golay.Rd | 1 + tests/testthat/_snaps/measure_collect.md | 120 ------- tests/testthat/_snaps/measure_input_long.md | 22 +- tests/testthat/_snaps/measure_input_wide.md | 6 +- tests/testthat/_snaps/savitzky_golay.md | 380 ++++++++++++++++++++ tests/testthat/test-data-transformations.R | 10 + tests/testthat/test_savitzky_golay.R | 110 ++++++ 10 files changed, 526 insertions(+), 135 deletions(-) delete mode 100644 tests/testthat/_snaps/measure_collect.md create mode 100644 tests/testthat/_snaps/savitzky_golay.md create mode 100644 tests/testthat/test-data-transformations.R diff --git a/DESCRIPTION b/DESCRIPTION index 14e8677..efeadd9 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -22,6 +22,7 @@ Suggests: covr, knitr, modeldata, + prospectr, rmarkdown, roxygen2, testthat (>= 3.0.0), diff --git a/R/helpers.R b/R/helpers.R index 48cd0bd..abaf028 100644 --- a/R/helpers.R +++ b/R/helpers.R @@ -18,6 +18,7 @@ add_location <- function(.data, loc) { # Assumes identical locations measure_to_matrix <- function(x) { + # silently recycles :-O res <- do.call("rbind", purrr::map(x, ~ .x[["value"]])) res } diff --git a/R/savitzky_golay.R b/R/savitzky_golay.R index e95d119..d8c4bdc 100644 --- a/R/savitzky_golay.R +++ b/R/savitzky_golay.R @@ -115,11 +115,6 @@ prep.step_measure_savitzky_golay <- function(x, training, info = NULL, ...) { cli::cli_abort("{.arg window_size} to {.fn step_measure_savitzky_golay} should be a single odd integer greater than 0.") } - if (x$window_size <= x$degree) { - cli::cli_abort("The {.arg window_size} value of {x$window_size} for - {.fn step_measure_savitzky_golay} should - be greater or equal to the {.arg degree} value of {x$degree}.") - } step_measure_savitzky_golay_new( role = x$role, @@ -196,13 +191,16 @@ required_pkgs.step_isomap <- function(x, ...) { ... ) res <- try(rlang::eval_tidy(cl), silent = TRUE) + if (inherits(res, "try-error")) { msg <- as.character(res) + msg <- strsplit(msg, " : ")[[1]][2] + msg <- gsub("\\n", "", msg) cli::cli_abort("Savitzky-Golay computations failed with error: {msg}") } if (ncol(res) != ncol(dat)) { - # TODO Prob can do better; can we appriximate what the wave numbers should be? + # TODO Prob can do better; can we approximate what the wave numbers should be? loc <- 1:ncol(res) } diff --git a/man/step_measure_savitzky_golay.Rd b/man/step_measure_savitzky_golay.Rd index 2b40761..42bdc8f 100644 --- a/man/step_measure_savitzky_golay.Rd +++ b/man/step_measure_savitzky_golay.Rd @@ -82,4 +82,5 @@ if (rlang::is_installed("prospectr")) { prep() } } +\concept{measure-differencing} \concept{measure-smoothing} diff --git a/tests/testthat/_snaps/measure_collect.md b/tests/testthat/_snaps/measure_collect.md deleted file mode 100644 index 518b671..0000000 --- a/tests/testthat/_snaps/measure_collect.md +++ /dev/null @@ -1,120 +0,0 @@ -# Print recipe with step_measure_collect works - - Code - meats_rec - Message - - -- Recipe ---------------------------------------------------------------------- - - -- Inputs - Number of variables by role - descriptor: 3 - undeclared role: 100 - - -- Operations - * Collect measurements: starts_with("x_") - ---- - - Code - meats_long_rec - Message - - -- Recipe ---------------------------------------------------------------------- - - -- Inputs - Number of variables by role - descriptor: 4 - undeclared role: 2 - - -- Operations - * Collect measurements: channel, transmittance - -# Prep recipe with step_measure_collect works - - Code - meats_prep - Message - - -- Recipe ---------------------------------------------------------------------- - - -- Inputs - Number of variables by role - descriptor: 3 - undeclared role: 100 - - -- Training information - Training data contained 215 data points and no incomplete rows. - - -- Operations - * Collect measurements: x_001, x_002, x_003, x_004, x_005, x_006, ... | Trained - ---- - - Code - meats_long_prep - Message - - -- Recipe ---------------------------------------------------------------------- - - -- Inputs - Number of variables by role - descriptor: 4 - undeclared role: 2 - - -- Training information - Training data contained 21500 data points and no incomplete rows. - - -- Operations - * Collect measurements: channel, transmittance | Trained - -# Bake recipe with step_measure_collect works - - Code - meats_bake - Output - # A tibble: 215 x 2 - .index .measures - > - 1 1 [100 x 5] - 2 2 [100 x 5] - 3 3 [100 x 5] - 4 4 [100 x 5] - 5 5 [100 x 5] - 6 6 [100 x 5] - 7 7 [100 x 5] - 8 8 [100 x 5] - 9 9 [100 x 5] - 10 10 [100 x 5] - # i 205 more rows - ---- - - Code - meats_long_bake - Output - # A tibble: 1 x 2 - .index .measures - - 1 1 - -# Tidy recipe with step_measure_collect works - - Code - tidy(meats_rec) - Output - # A tibble: 1 x 6 - number operation type trained skip id - - 1 1 step measure_collect FALSE FALSE measure_collect_Bp5vK - ---- - - Code - tidy(meats_long_rec) - Output - # A tibble: 1 x 6 - number operation type trained skip id - - 1 1 step measure_collect FALSE FALSE measure_collect_RUieL - diff --git a/tests/testthat/_snaps/measure_input_long.md b/tests/testthat/_snaps/measure_input_long.md index 6ad90b0..e115358 100644 --- a/tests/testthat/_snaps/measure_input_long.md +++ b/tests/testthat/_snaps/measure_input_long.md @@ -2,7 +2,7 @@ Code print(rec_1) - Message + Message -- Recipe ---------------------------------------------------------------------- @@ -43,7 +43,7 @@ Code print(prep_1) - Message + Message -- Recipe ---------------------------------------------------------------------- @@ -56,7 +56,7 @@ Training data contained 20000 data points and no incomplete rows. -- Operations - * Collate long analytical measurements: absorp, ind | Trained + * Collate long analytical measurements: absorp and ind | Trained --- @@ -87,17 +87,27 @@ Code recipe(water + fat + protein ~ absorp, data = na_train) %>% step_measure_input_long(absorp) %>% prep() - Error + Condition Error in `step_measure_input_long()`: Caused by error in `prep()`: ! 'location' is required for long input data +--- + + Code + recipe(water + fat + protein ~ ., data = miss_train) %>% + step_measure_input_long(absorp, location = vars(ind)) %>% prep() + Condition + Error in `step_measure_input_long()`: + Caused by error in `check_measure_dims()`: + ! The number of rows in each measure should be the same. Most samples have 100 rows and these do not: 1. Please pad the input with missing values. + --- Code recipe(water + fat + protein ~ ., data = na_train) %>% step_measure_input_long( dplyr::everything(), location = vars(ind)) %>% prep() - Error + Condition Error in `step_measure_input_long()`: Caused by error in `check_single_selector()`: ! The selection for `...` should only select a single column (6 columns were selected). @@ -107,7 +117,7 @@ Code recipe(water + fat + protein ~ ., data = na_train) %>% step_measure_input_long( absorp, location = vars(dplyr::everything())) %>% prep() - Error + Condition Error in `step_measure_input_long()`: Caused by error in `check_single_selector()`: ! The selection for `location` should only select a single column (6 columns were selected). diff --git a/tests/testthat/_snaps/measure_input_wide.md b/tests/testthat/_snaps/measure_input_wide.md index 5c84336..98d5e98 100644 --- a/tests/testthat/_snaps/measure_input_wide.md +++ b/tests/testthat/_snaps/measure_input_wide.md @@ -2,7 +2,7 @@ Code print(rec_1) - Message + Message -- Recipe ---------------------------------------------------------------------- @@ -48,7 +48,7 @@ Code print(prep_1) - Message + Message -- Recipe ---------------------------------------------------------------------- @@ -92,7 +92,7 @@ Code recipe(water + fat + protein ~ ., data = na_train) %>% step_measure_input_wide( x_001:x_100, location_values = 1:2) %>% prep() - Error + Condition Error in `step_measure_input_wide()`: Caused by error in `prep()`: ! 100 columns were selected as inputs but `location_values` has 2 values. diff --git a/tests/testthat/_snaps/savitzky_golay.md b/tests/testthat/_snaps/savitzky_golay.md new file mode 100644 index 0000000..73308c9 --- /dev/null +++ b/tests/testthat/_snaps/savitzky_golay.md @@ -0,0 +1,380 @@ +# savitzky-golay inputs + + 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 `.comp_savitzky_golay()`: + ! Savitzky-Golay computations failed with error: polynomial order p must be geater or equal to differentiation order m + +--- + + 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 `.comp_savitzky_golay()`: + ! Savitzky-Golay computations failed with error: polynomial order p must be geater or equal to differentiation order m + +--- + + 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 `.comp_savitzky_golay()`: + ! Savitzky-Golay computations failed with error: polynomial order p must be geater or equal to differentiation order m + +--- + + 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 `.comp_savitzky_golay()`: + ! Savitzky-Golay computations failed with error: filter length w mus be greater than polynomial order p + +--- + + 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 `.comp_savitzky_golay()`: + ! Savitzky-Golay computations failed with error: filter length w mus be greater than polynomial order p + +--- + + 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 `.comp_savitzky_golay()`: + ! Savitzky-Golay computations failed with error: filter length w mus be greater than polynomial order p + +--- + + 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 `.comp_savitzky_golay()`: + ! Savitzky-Golay computations failed with error: filter length w mus be greater than polynomial order p + +--- + + 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 `.comp_savitzky_golay()`: + ! Savitzky-Golay computations failed with error: filter length w mus be greater than polynomial order p + +--- + + 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 `.comp_savitzky_golay()`: + ! Savitzky-Golay computations failed with error: filter length w mus be greater than polynomial order p + +--- + + 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 `.comp_savitzky_golay()`: + ! Savitzky-Golay computations failed with error: filter length w mus be greater than polynomial order p + +--- + + 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 `.comp_savitzky_golay()`: + ! Savitzky-Golay computations failed with error: filter length w mus be greater than polynomial order p + +--- + + 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()`: + ! `window_size` 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()`: + ! `window_size` 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()`: + ! `window_size` 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()`: + ! `window_size` 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()`: + ! `window_size` 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()`: + ! `window_size` 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()`: + ! `window_size` 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()`: + ! `window_size` 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()`: + ! `window_size` 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()`: + ! `window_size` 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()`: + ! `window_size` 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()`: + ! `window_size` 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()`: + ! `window_size` 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()`: + ! `window_size` 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()`: + ! `window_size` 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()`: + ! `window_size` 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()`: + ! `window_size` 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()`: + ! `window_size` 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()`: + ! `window_size` 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()`: + ! `window_size` 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()`: + ! `window_size` 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()`: + ! `window_size` 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()`: + ! `window_size` 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()`: + ! `window_size` 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 `.comp_savitzky_golay()`: + ! Savitzky-Golay computations failed with error: polynomial order p must be geater or equal to differentiation order m + +--- + + 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 `.comp_savitzky_golay()`: + ! Savitzky-Golay computations failed with error: polynomial order p must be geater or equal to differentiation order m + +--- + + 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 `.comp_savitzky_golay()`: + ! Savitzky-Golay computations failed with error: polynomial order p must be geater or equal to differentiation order m + diff --git a/tests/testthat/test-data-transformations.R b/tests/testthat/test-data-transformations.R new file mode 100644 index 0000000..e645356 --- /dev/null +++ b/tests/testthat/test-data-transformations.R @@ -0,0 +1,10 @@ +test_that("transform measure to matrix", { + rec <- + recipe(water + fat + protein ~ ., data = meats_long) %>% + update_role(id, new_role = "id") %>% + step_measure_input_long(transmittance, location = vars(channel)) %>% + prep() + + spect_start <- measure:::measure_to_matrix(rec$template$.measures) + +}) diff --git a/tests/testthat/test_savitzky_golay.R b/tests/testthat/test_savitzky_golay.R index e69de29..91104df 100644 --- a/tests/testthat/test_savitzky_golay.R +++ b/tests/testthat/test_savitzky_golay.R @@ -0,0 +1,110 @@ +test_that("savitzky-golay computations", { + skip_if_not_installed("prospectr") + + # ------------------------------------------------------------------------------ + + rec <- + recipe(water + fat + protein ~ ., data = meats_long) %>% + update_role(id, new_role = "id") %>% + step_measure_input_long(transmittance, location = vars(channel)) %>% + prep() + + spect_start <- measure:::measure_to_matrix(rec$template$.measures) + + # ------------------------------------------------------------------------------ + + grid <- expand.grid(diffs = 0:3, deg = 4:6, wn = c(15, 21, 25)) + + for (i in 1:nrow(grid)) { + meas_res <- + measure:::.comp_savitzky_golay( + rec$template$.measures, + diffs = grid$diffs[i], + degree = grid$deg[i], + window = grid$wn[i] + ) %>% + measure:::measure_to_matrix() + prosp_res <- + prospectr::savitzkyGolay(spect_start, + m = grid$diffs[i], + p = grid$deg[i], + w = grid$wn[i]) + expect_equal(meas_res, prosp_res) + } + +}) + +test_that("savitzky-golay inputs", { + skip_if_not_installed("prospectr") + + # ------------------------------------------------------------------------------ + + rec <- + recipe(water + fat + protein ~ ., data = meats_long) %>% + update_role(id, new_role = "id") %>% + step_measure_input_long(transmittance, location = vars(channel)) %>% + prep() + + # ------------------------------------------------------------------------------ + + 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 + ) + + lst <- NULL + for (i in 1:nrow(bad_inputs)) { + + expect_snapshot({ + rec %>% + step_measure_savitzky_golay( + differentiation_order = bad_inputs$diffs[i], + window_size = bad_inputs$wn[i], + degree = bad_inputs$deg[i] + ) %>% + prep() + }, + error = TRUE + ) + } + +}) + From d6bcf0c5e774a850270c7ca07bd03bc1d5125e31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=98topepo=E2=80=99?= <‘mxkuhn@gmail.com’> Date: Thu, 4 Jan 2024 09:11:11 -0500 Subject: [PATCH 08/13] clean up some documentation --- R/helpers.R | 6 +-- R/input_long.R | 9 ++++ R/savitzky_golay.R | 6 ++- man/required_pkgs.recipe.Rd | 15 ++++++ man/step_measure_input_long.Rd | 7 +++ man/tidy.recipe.Rd | 21 +++++++++ tests/testthat/test-data-transformations.R | 53 +++++++++++++++++++++- 7 files changed, 111 insertions(+), 6 deletions(-) create mode 100644 man/required_pkgs.recipe.Rd create mode 100644 man/tidy.recipe.Rd diff --git a/R/helpers.R b/R/helpers.R index abaf028..72a0158 100644 --- a/R/helpers.R +++ b/R/helpers.R @@ -15,10 +15,10 @@ add_location <- function(.data, loc) { # ------------------------------------------------------------------------------ # Move between lists of tibbles and matrices (and back) +# Assumes identical locations. We have code to check that the per-sample +# dimensions are equal but nothing yet for identical locations. -# Assumes identical locations measure_to_matrix <- function(x) { - # silently recycles :-O res <- do.call("rbind", purrr::map(x, ~ .x[["value"]])) res } @@ -37,7 +37,7 @@ matrix_to_measure <- function(x, loc) { x <- t(x) x <- tibble::as_tibble(x, .name_repair = "minimal") - res <- purrr::map(x, ~ tibble::new_tibble(list(value = .x, location = loc))) + res <- purrr::map(x, ~ tibble::new_tibble(list(location = loc, value = .x))) unname(res) } diff --git a/R/input_long.R b/R/input_long.R index 8f3829c..c7cd75b 100644 --- a/R/input_long.R +++ b/R/input_long.R @@ -30,6 +30,13 @@ #' rows. We advise having a column with 7 unique values indicating which of the #' rows correspond to each sample. #' +#' # Missing Data +#' +#' Currently, \pkg{measure} assumes that there are equal numbers of values +#' within a sample. If there are missing values in the measurements, you'll +#' need to pad them with missing values (as opposed to an absent row in the +#' long format). If not, an error will occur. +#' #' # Tidying #' #' When you [`tidy()`][tidy.recipe()] this step, a tibble indicating which of @@ -130,6 +137,8 @@ print.step_measure_input_long <- invisible(x) } +#' Tidiers for measure steps +#' @param x A recipe step. #' @rdname tidy.recipe #' @export tidy.step_measure_input_long <- function(x, ...) { diff --git a/R/savitzky_golay.R b/R/savitzky_golay.R index d8c4bdc..6c710aa 100644 --- a/R/savitzky_golay.R +++ b/R/savitzky_golay.R @@ -151,7 +151,7 @@ print.step_measure_savitzky_golay <- invisible(x) } -#' @rdname measure-tidy.recipe +#' @rdname tidy.recipe #' @export tidy.step_measure_savitzky_golay <- function(x, ...) { if (is_trained(x)) { @@ -166,7 +166,9 @@ tidy.step_measure_savitzky_golay <- function(x, ...) { res } -#' @rdname measure-required_pkgs.recipe +#' Set package dependencies +#' @param x A step object. +#' @name required_pkgs.recipe #' @export required_pkgs.step_isomap <- function(x, ...) { c("measure", "prospectr") diff --git a/man/required_pkgs.recipe.Rd b/man/required_pkgs.recipe.Rd new file mode 100644 index 0000000..75c46e7 --- /dev/null +++ b/man/required_pkgs.recipe.Rd @@ -0,0 +1,15 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/savitzky_golay.R +\name{required_pkgs.recipe} +\alias{required_pkgs.recipe} +\alias{required_pkgs.step_isomap} +\title{Set package dependencies} +\usage{ +\method{required_pkgs}{step_isomap}(x, ...) +} +\arguments{ +\item{x}{A step object.} +} +\description{ +Set package dependencies +} diff --git a/man/step_measure_input_long.Rd b/man/step_measure_input_long.Rd index 2185aa3..37c4e9b 100644 --- a/man/step_measure_input_long.Rd +++ b/man/step_measure_input_long.Rd @@ -68,6 +68,13 @@ measure and 7 samples, the input data (in long format) should have 1,400 rows. We advise having a column with 7 unique values indicating which of the rows correspond to each sample. } +\section{Missing Data}{ +Currently, \pkg{measure} assumes that there are equal numbers of values +within a sample. If there are missing values in the measurements, you'll +need to pad them with missing values (as opposed to an absent row in the +long format). If not, an error will occur. +} + \section{Tidying}{ When you \code{\link[=tidy.recipe]{tidy()}} this step, a tibble indicating which of the original columns were used to reformat the data. diff --git a/man/tidy.recipe.Rd b/man/tidy.recipe.Rd new file mode 100644 index 0000000..9efaf39 --- /dev/null +++ b/man/tidy.recipe.Rd @@ -0,0 +1,21 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/input_long.R, R/input_wide.R, +% R/savitzky_golay.R +\name{tidy.step_measure_input_long} +\alias{tidy.step_measure_input_long} +\alias{tidy.step_measure_input_wide} +\alias{tidy.step_measure_savitzky_golay} +\title{Tidiers for measure steps} +\usage{ +\method{tidy}{step_measure_input_long}(x, ...) + +\method{tidy}{step_measure_input_wide}(x, ...) + +\method{tidy}{step_measure_savitzky_golay}(x, ...) +} +\arguments{ +\item{x}{A recipe step.} +} +\description{ +Tidiers for measure steps +} diff --git a/tests/testthat/test-data-transformations.R b/tests/testthat/test-data-transformations.R index e645356..6ff2a6c 100644 --- a/tests/testthat/test-data-transformations.R +++ b/tests/testthat/test-data-transformations.R @@ -1,10 +1,61 @@ test_that("transform measure to matrix", { + + rec <- + recipe(water + fat + protein ~ ., data = meats_long) %>% + update_role(id, new_role = "id") %>% + step_measure_input_long(transmittance, location = vars(channel)) %>% + prep() + + spect_mat <- measure:::measure_to_matrix(rec$template$.measures) + + expect_equal(nrow(spect_mat), length(unique(meats_long$id))) + expect_equal(ncol(spect_mat), length(unique(meats_long$channel))) + + ids <- unique(meats_long$id) + for (i in seq_along(ids)) { + expect_equal( + spect_mat[i, ], + meats_long[meats_long$id == ids[i], "transmittance"][[1]] + ) + } + +}) + +test_that("transform matrix to measure", { + rec <- recipe(water + fat + protein ~ ., data = meats_long) %>% update_role(id, new_role = "id") %>% step_measure_input_long(transmittance, location = vars(channel)) %>% prep() - spect_start <- measure:::measure_to_matrix(rec$template$.measures) + spect_mat <- measure:::measure_to_matrix(rec$template$.measures) + + locs <- unique(meats_long$channel) + + spect_list <- measure:::matrix_to_measure(spect_mat, locs) + + for (i in seq_along(spect_list)) { + expect_equal( + spect_list[[i]], + rec$template$.measures[[i]] + ) + } + +}) + +test_that("transform tidy format", { + + rec <- + recipe(water + fat + protein ~ ., data = meats_long) %>% + update_role(id, new_role = "id") %>% + step_measure_input_long(transmittance, location = vars(channel)) %>% + prep() + + spect_df <- measure:::measure_to_tibble(rec$template$.measures) + exp_df <- meats_long[, c("channel", "transmittance", "id")] + names(exp_df) <- c("location", "value", "sample_num") + + expect_equal(spect_df, exp_df) }) From 51c57ec98fa6874a61d06f7aa485cf4b6a67a88f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=98topepo=E2=80=99?= <‘mxkuhn@gmail.com’> Date: Sat, 6 Jan 2024 17:47:56 -0500 Subject: [PATCH 09/13] adjust inputs if needed --- R/savitzky_golay.R | 32 ++++- man/step_measure_savitzky_golay.Rd | 5 +- tests/testthat/_snaps/savitzky_golay.md | 160 ++++++------------------ tests/testthat/test_savitzky_golay.R | 36 ++++-- 4 files changed, 94 insertions(+), 139 deletions(-) diff --git a/R/savitzky_golay.R b/R/savitzky_golay.R index 6c710aa..d63f0da 100644 --- a/R/savitzky_golay.R +++ b/R/savitzky_golay.R @@ -32,7 +32,10 @@ #' in fewer measurements than the input. Measurements are assumed to be equally #' spaced. #' -#' The polynomial degree should be less than the window size. +#' The polynomial degree should be less than the window size. Also, window +#' size must be greater than polynomial degree. If either case is true, the +#' original argument values are increased to satisfy these conditions (with a +#' warning). #' #' **No selectors should be supplied to this step function**. The data should be in #' a special internal format produced by [step_measure_input_wide()] or @@ -106,14 +109,35 @@ prep.step_measure_savitzky_golay <- function(x, training, info = NULL, ...) { } if (!is.numeric(x$differentiation_order) | length(x$differentiation_order) != 1 | x$differentiation_order < 0) { - cli::cli_abort("{.arg differentiation_order} to + 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("{.arg window_size} to {.fn step_measure_savitzky_golay} should - be a single odd integer greater than 0.") + cli::cli_abort("The {.arg window_size} argument to + {.fn step_measure_savitzky_golay} should be a single odd + integer greater than 0.") + } + + # 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 + 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}.") } step_measure_savitzky_golay_new( diff --git a/man/step_measure_savitzky_golay.Rd b/man/step_measure_savitzky_golay.Rd index 42bdc8f..b84bb0a 100644 --- a/man/step_measure_savitzky_golay.Rd +++ b/man/step_measure_savitzky_golay.Rd @@ -55,7 +55,10 @@ correlation. It fits a polynomial to a window of measurements and this results in fewer measurements than the input. Measurements are assumed to be equally spaced. -The polynomial degree should be less than the window size. +The polynomial degree should be less than the window size. Also, window +size must be greater than polynomial degree. If either case is true, the +original argument values are increased to satisfy these conditions (with a +warning). \strong{No selectors should be supplied to this step function}. The data should be in a special internal format produced by \code{\link[=step_measure_input_wide]{step_measure_input_wide()}} or diff --git a/tests/testthat/_snaps/savitzky_golay.md b/tests/testthat/_snaps/savitzky_golay.md index 73308c9..b664c7c 100644 --- a/tests/testthat/_snaps/savitzky_golay.md +++ b/tests/testthat/_snaps/savitzky_golay.md @@ -1,112 +1,46 @@ # savitzky-golay inputs - 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 `.comp_savitzky_golay()`: - ! Savitzky-Golay computations failed with error: polynomial order p must be geater or equal to differentiation order m + The `degree` argument to `step_measure_savitzky_golay()` should be greater than or equal to `differentiation_order`. 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 `.comp_savitzky_golay()`: - ! Savitzky-Golay computations failed with error: polynomial order p must be geater or equal to differentiation order m + The `degree` argument to `step_measure_savitzky_golay()` should be greater than or equal to `differentiation_order`. 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 `.comp_savitzky_golay()`: - ! Savitzky-Golay computations failed with error: polynomial order p must be geater or equal to differentiation order m + The `degree` argument to `step_measure_savitzky_golay()` should be greater than or equal to `differentiation_order`. 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 `.comp_savitzky_golay()`: - ! Savitzky-Golay computations failed with error: filter length w mus be greater than polynomial order p + The `window_size` argument to `step_measure_savitzky_golay()` should be greater than or equal to `degree`. The polynomial degree was increased to 7. --- - 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 `.comp_savitzky_golay()`: - ! Savitzky-Golay computations failed with error: filter length w mus be greater than polynomial order p + The `window_size` argument to `step_measure_savitzky_golay()` should be greater than or equal to `degree`. The polynomial degree was increased to 7. --- - 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 `.comp_savitzky_golay()`: - ! Savitzky-Golay computations failed with error: filter length w mus be greater than polynomial order p + The `window_size` argument to `step_measure_savitzky_golay()` should be greater than or equal to `degree`. The polynomial degree was increased to 7. --- - 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 `.comp_savitzky_golay()`: - ! Savitzky-Golay computations failed with error: filter length w mus be greater than polynomial order p + The `window_size` argument to `step_measure_savitzky_golay()` should be greater than or equal to `degree`. The polynomial degree was increased to 7. --- - 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 `.comp_savitzky_golay()`: - ! Savitzky-Golay computations failed with error: filter length w mus be greater than polynomial order p + The `window_size` argument to `step_measure_savitzky_golay()` should be greater than or equal to `degree`. The polynomial degree was increased to 7. --- - 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 `.comp_savitzky_golay()`: - ! Savitzky-Golay computations failed with error: filter length w mus be greater than polynomial order p + The `window_size` argument to `step_measure_savitzky_golay()` should be greater than or equal to `degree`. The polynomial degree was increased to 7. --- - 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 `.comp_savitzky_golay()`: - ! Savitzky-Golay computations failed with error: filter length w mus be greater than polynomial order p + The `window_size` argument to `step_measure_savitzky_golay()` should be greater than or equal to `degree`. The polynomial degree was increased to 7. --- - 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 `.comp_savitzky_golay()`: - ! Savitzky-Golay computations failed with error: filter length w mus be greater than polynomial order p + The `window_size` argument to `step_measure_savitzky_golay()` should be greater than or equal to `degree`. The polynomial degree was increased to 7. --- @@ -116,7 +50,7 @@ Condition Error in `step_measure_savitzky_golay()`: Caused by error in `prep()`: - ! `window_size` to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. + ! The `window_size` argument to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. --- @@ -126,7 +60,7 @@ Condition Error in `step_measure_savitzky_golay()`: Caused by error in `prep()`: - ! `window_size` to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. + ! The `window_size` argument to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. --- @@ -136,7 +70,7 @@ Condition Error in `step_measure_savitzky_golay()`: Caused by error in `prep()`: - ! `window_size` to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. + ! The `window_size` argument to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. --- @@ -146,7 +80,7 @@ Condition Error in `step_measure_savitzky_golay()`: Caused by error in `prep()`: - ! `window_size` to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. + ! The `window_size` argument to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. --- @@ -156,7 +90,7 @@ Condition Error in `step_measure_savitzky_golay()`: Caused by error in `prep()`: - ! `window_size` to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. + ! The `window_size` argument to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. --- @@ -166,7 +100,7 @@ Condition Error in `step_measure_savitzky_golay()`: Caused by error in `prep()`: - ! `window_size` to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. + ! The `window_size` argument to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. --- @@ -176,7 +110,7 @@ Condition Error in `step_measure_savitzky_golay()`: Caused by error in `prep()`: - ! `window_size` to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. + ! The `window_size` argument to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. --- @@ -186,7 +120,7 @@ Condition Error in `step_measure_savitzky_golay()`: Caused by error in `prep()`: - ! `window_size` to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. + ! The `window_size` argument to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. --- @@ -196,7 +130,7 @@ Condition Error in `step_measure_savitzky_golay()`: Caused by error in `prep()`: - ! `window_size` to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. + ! The `window_size` argument to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. --- @@ -206,7 +140,7 @@ Condition Error in `step_measure_savitzky_golay()`: Caused by error in `prep()`: - ! `window_size` to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. + ! The `window_size` argument to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. --- @@ -216,7 +150,7 @@ Condition Error in `step_measure_savitzky_golay()`: Caused by error in `prep()`: - ! `window_size` to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. + ! The `window_size` argument to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. --- @@ -226,7 +160,7 @@ Condition Error in `step_measure_savitzky_golay()`: Caused by error in `prep()`: - ! `window_size` to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. + ! The `window_size` argument to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. --- @@ -236,7 +170,7 @@ Condition Error in `step_measure_savitzky_golay()`: Caused by error in `prep()`: - ! `window_size` to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. + ! The `window_size` argument to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. --- @@ -246,7 +180,7 @@ Condition Error in `step_measure_savitzky_golay()`: Caused by error in `prep()`: - ! `window_size` to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. + ! The `window_size` argument to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. --- @@ -256,7 +190,7 @@ Condition Error in `step_measure_savitzky_golay()`: Caused by error in `prep()`: - ! `window_size` to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. + ! The `window_size` argument to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. --- @@ -266,7 +200,7 @@ Condition Error in `step_measure_savitzky_golay()`: Caused by error in `prep()`: - ! `window_size` to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. + ! The `window_size` argument to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. --- @@ -276,7 +210,7 @@ Condition Error in `step_measure_savitzky_golay()`: Caused by error in `prep()`: - ! `window_size` to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. + ! The `window_size` argument to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. --- @@ -286,7 +220,7 @@ Condition Error in `step_measure_savitzky_golay()`: Caused by error in `prep()`: - ! `window_size` to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. + ! The `window_size` argument to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. --- @@ -296,7 +230,7 @@ Condition Error in `step_measure_savitzky_golay()`: Caused by error in `prep()`: - ! `window_size` to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. + ! The `window_size` argument to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. --- @@ -306,7 +240,7 @@ Condition Error in `step_measure_savitzky_golay()`: Caused by error in `prep()`: - ! `window_size` to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. + ! The `window_size` argument to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. --- @@ -316,7 +250,7 @@ Condition Error in `step_measure_savitzky_golay()`: Caused by error in `prep()`: - ! `window_size` to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. + ! The `window_size` argument to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. --- @@ -326,7 +260,7 @@ Condition Error in `step_measure_savitzky_golay()`: Caused by error in `prep()`: - ! `window_size` to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. + ! The `window_size` argument to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. --- @@ -336,7 +270,7 @@ Condition Error in `step_measure_savitzky_golay()`: Caused by error in `prep()`: - ! `window_size` to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. + ! The `window_size` argument to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. --- @@ -346,35 +280,17 @@ Condition Error in `step_measure_savitzky_golay()`: Caused by error in `prep()`: - ! `window_size` to `step_measure_savitzky_golay()` should be a single odd integer greater than 0. + ! 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 `.comp_savitzky_golay()`: - ! Savitzky-Golay computations failed with error: polynomial order p must be geater or equal to differentiation order m + The `degree` argument to `step_measure_savitzky_golay()` should be greater than or equal to `differentiation_order`. 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 `.comp_savitzky_golay()`: - ! Savitzky-Golay computations failed with error: polynomial order p must be geater or equal to differentiation order m + The `degree` argument to `step_measure_savitzky_golay()` should be greater than or equal to `differentiation_order`. 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 `.comp_savitzky_golay()`: - ! Savitzky-Golay computations failed with error: polynomial order p must be geater or equal to differentiation order m + The `degree` argument to `step_measure_savitzky_golay()` should be greater than or equal to `differentiation_order`. The polynomial degree was increased to 4. diff --git a/tests/testthat/test_savitzky_golay.R b/tests/testthat/test_savitzky_golay.R index 91104df..eaf6a11 100644 --- a/tests/testthat/test_savitzky_golay.R +++ b/tests/testthat/test_savitzky_golay.R @@ -90,20 +90,32 @@ test_that("savitzky-golay inputs", { 3L, 2L, 15 ) - lst <- NULL for (i in 1:nrow(bad_inputs)) { - expect_snapshot({ - rec %>% - step_measure_savitzky_golay( - differentiation_order = bad_inputs$diffs[i], - window_size = bad_inputs$wn[i], - degree = bad_inputs$deg[i] - ) %>% - prep() - }, - error = TRUE - ) + if (bad_inputs$wn[i] == 10) { + expect_snapshot({ + rec %>% + step_measure_savitzky_golay( + differentiation_order = bad_inputs$diffs[i], + window_size = bad_inputs$wn[i], + degree = bad_inputs$deg[i] + ) %>% + prep() + }, + error = TRUE) + + } else { + expect_snapshot_warning({ + rec %>% + step_measure_savitzky_golay( + differentiation_order = bad_inputs$diffs[i], + window_size = bad_inputs$wn[i], + degree = bad_inputs$deg[i] + ) %>% + prep() + }) + } + } }) From e9af9156b417a2b1b40027f2f41c3396412be392 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=98topepo=E2=80=99?= <‘mxkuhn@gmail.com’> Date: Sat, 6 Jan 2024 22:03:18 -0500 Subject: [PATCH 10/13] missing arg docs --- R/savitzky_golay.R | 3 +++ man/required_pkgs.recipe.Rd | 2 ++ man/tidy.recipe.Rd | 4 +++- 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/R/savitzky_golay.R b/R/savitzky_golay.R index d63f0da..65e475b 100644 --- a/R/savitzky_golay.R +++ b/R/savitzky_golay.R @@ -176,6 +176,8 @@ print.step_measure_savitzky_golay <- } #' @rdname tidy.recipe +#' @param x A step object. +#' @param ... Not used. #' @export tidy.step_measure_savitzky_golay <- function(x, ...) { if (is_trained(x)) { @@ -192,6 +194,7 @@ tidy.step_measure_savitzky_golay <- function(x, ...) { #' Set package dependencies #' @param x A step object. +#' @param ... Not used. #' @name required_pkgs.recipe #' @export required_pkgs.step_isomap <- function(x, ...) { diff --git a/man/required_pkgs.recipe.Rd b/man/required_pkgs.recipe.Rd index 75c46e7..be5122e 100644 --- a/man/required_pkgs.recipe.Rd +++ b/man/required_pkgs.recipe.Rd @@ -9,6 +9,8 @@ } \arguments{ \item{x}{A step object.} + +\item{...}{Not used.} } \description{ Set package dependencies diff --git a/man/tidy.recipe.Rd b/man/tidy.recipe.Rd index 9efaf39..1c016b2 100644 --- a/man/tidy.recipe.Rd +++ b/man/tidy.recipe.Rd @@ -14,7 +14,9 @@ \method{tidy}{step_measure_savitzky_golay}(x, ...) } \arguments{ -\item{x}{A recipe step.} +\item{x}{A step object.} + +\item{...}{Not used.} } \description{ Tidiers for measure steps From 740f225944f26cc76b9e75d771fdec0359d3494a Mon Sep 17 00:00:00 2001 From: James Wade Date: Mon, 8 Jan 2024 12:47:34 -0500 Subject: [PATCH 11/13] update test snapshots --- tests/testthat/_snaps/measure_input_long.md | 12 +++--- tests/testthat/_snaps/measure_input_wide.md | 6 +-- tests/testthat/_snaps/savitzky_golay.md | 48 ++++++++++----------- 3 files changed, 33 insertions(+), 33 deletions(-) diff --git a/tests/testthat/_snaps/measure_input_long.md b/tests/testthat/_snaps/measure_input_long.md index e115358..3e1f939 100644 --- a/tests/testthat/_snaps/measure_input_long.md +++ b/tests/testthat/_snaps/measure_input_long.md @@ -2,7 +2,7 @@ Code print(rec_1) - Message + Message -- Recipe ---------------------------------------------------------------------- @@ -43,7 +43,7 @@ Code print(prep_1) - Message + Message -- Recipe ---------------------------------------------------------------------- @@ -87,7 +87,7 @@ Code recipe(water + fat + protein ~ absorp, data = na_train) %>% step_measure_input_long(absorp) %>% prep() - Condition + Error Error in `step_measure_input_long()`: Caused by error in `prep()`: ! 'location' is required for long input data @@ -97,7 +97,7 @@ Code recipe(water + fat + protein ~ ., data = miss_train) %>% step_measure_input_long(absorp, location = vars(ind)) %>% prep() - Condition + Error Error in `step_measure_input_long()`: Caused by error in `check_measure_dims()`: ! The number of rows in each measure should be the same. Most samples have 100 rows and these do not: 1. Please pad the input with missing values. @@ -107,7 +107,7 @@ Code recipe(water + fat + protein ~ ., data = na_train) %>% step_measure_input_long( dplyr::everything(), location = vars(ind)) %>% prep() - Condition + Error Error in `step_measure_input_long()`: Caused by error in `check_single_selector()`: ! The selection for `...` should only select a single column (6 columns were selected). @@ -117,7 +117,7 @@ Code recipe(water + fat + protein ~ ., data = na_train) %>% step_measure_input_long( absorp, location = vars(dplyr::everything())) %>% prep() - Condition + Error Error in `step_measure_input_long()`: Caused by error in `check_single_selector()`: ! The selection for `location` should only select a single column (6 columns were selected). diff --git a/tests/testthat/_snaps/measure_input_wide.md b/tests/testthat/_snaps/measure_input_wide.md index 98d5e98..5c84336 100644 --- a/tests/testthat/_snaps/measure_input_wide.md +++ b/tests/testthat/_snaps/measure_input_wide.md @@ -2,7 +2,7 @@ Code print(rec_1) - Message + Message -- Recipe ---------------------------------------------------------------------- @@ -48,7 +48,7 @@ Code print(prep_1) - Message + Message -- Recipe ---------------------------------------------------------------------- @@ -92,7 +92,7 @@ Code recipe(water + fat + protein ~ ., data = na_train) %>% step_measure_input_wide( x_001:x_100, location_values = 1:2) %>% prep() - Condition + Error Error in `step_measure_input_wide()`: Caused by error in `prep()`: ! 100 columns were selected as inputs but `location_values` has 2 values. diff --git a/tests/testthat/_snaps/savitzky_golay.md b/tests/testthat/_snaps/savitzky_golay.md index b664c7c..cf647a4 100644 --- a/tests/testthat/_snaps/savitzky_golay.md +++ b/tests/testthat/_snaps/savitzky_golay.md @@ -47,7 +47,7 @@ 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 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. @@ -57,7 +57,7 @@ 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 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. @@ -67,7 +67,7 @@ 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 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. @@ -77,7 +77,7 @@ 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 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. @@ -87,7 +87,7 @@ 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 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. @@ -97,7 +97,7 @@ 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 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. @@ -107,7 +107,7 @@ 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 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. @@ -117,7 +117,7 @@ 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 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. @@ -127,7 +127,7 @@ 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 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. @@ -137,7 +137,7 @@ 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 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. @@ -147,7 +147,7 @@ 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 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. @@ -157,7 +157,7 @@ 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 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. @@ -167,7 +167,7 @@ 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 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. @@ -177,7 +177,7 @@ 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 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. @@ -187,7 +187,7 @@ 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 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. @@ -197,7 +197,7 @@ 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 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. @@ -207,7 +207,7 @@ 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 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. @@ -217,7 +217,7 @@ 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 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. @@ -227,7 +227,7 @@ 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 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. @@ -237,7 +237,7 @@ 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 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. @@ -247,7 +247,7 @@ 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 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. @@ -257,7 +257,7 @@ 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 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. @@ -267,7 +267,7 @@ 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 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. @@ -277,7 +277,7 @@ 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 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. From a96d42c01b1d0d39838ef67680652d2ecee1786d Mon Sep 17 00:00:00 2001 From: James Wade Date: Mon, 8 Jan 2024 13:21:49 -0500 Subject: [PATCH 12/13] update snapshops, cleanup description and namespace --- DESCRIPTION | 5 ----- NAMESPACE | 10 ++++++++++ R/helpers.R | 21 +++++++++++++++++++- man/tidy.recipe.Rd | 10 ++++++++-- tests/testthat/_snaps/measure_input_long.md | 10 +++++----- tests/testthat/_snaps/measure_input_wide.md | 6 +++--- tests/testthat/_snaps/measure_output_long.md | 16 +++------------ 7 files changed, 49 insertions(+), 29 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index f36d4c2..fc96346 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -40,8 +40,3 @@ Encoding: UTF-8 LazyData: true Roxygen: list(markdown = TRUE) RoxygenNote: 7.2.3.9000 -VignetteBuilder: knitr -Depends: - R (>= 3.5.0), - recipes -LazyData: true \ No newline at end of file diff --git a/NAMESPACE b/NAMESPACE index ad90683..0b98d43 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -3,21 +3,31 @@ S3method(bake,step_baseline) S3method(bake,step_measure_input_long) S3method(bake,step_measure_input_wide) +S3method(bake,step_measure_output_long) +S3method(bake,step_measure_output_wide) S3method(bake,step_measure_savitzky_golay) S3method(prep,step_baseline) S3method(prep,step_measure_input_long) S3method(prep,step_measure_input_wide) +S3method(prep,step_measure_output_long) +S3method(prep,step_measure_output_wide) S3method(prep,step_measure_savitzky_golay) S3method(print,step_baseline) S3method(print,step_measure_input_long) S3method(print,step_measure_input_wide) +S3method(print,step_measure_output_long) +S3method(print,step_measure_output_wide) S3method(print,step_measure_savitzky_golay) S3method(required_pkgs,step_isomap) S3method(tidy,step_measure_input_long) S3method(tidy,step_measure_input_wide) +S3method(tidy,step_measure_output_long) +S3method(tidy,step_measure_output_wide) S3method(tidy,step_measure_savitzky_golay) 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) import(recipes) diff --git a/R/helpers.R b/R/helpers.R index 128c3bd..9a8c979 100644 --- a/R/helpers.R +++ b/R/helpers.R @@ -57,4 +57,23 @@ check_for_measure <- function(x) { {.fn step_measure_input_long}.") } invisible(NULL) -} \ No newline at end of file +} + +# ------------------------------------------------------------------------------ +# Make sue that data are in the correct format. + +check_has_measure <- function(x, cl) { + step_fn <- as.character(cl[[1]]) + step_fn <- gsub("prep\\.", "", step_fn) + step_fn <- paste0("`", step_fn, "()`.") + + + if (!any(names(x) == ".measures")) { + msg <- + paste0("It appears that the measurements have not been converted ", + "for the inernal format. See `step_measure_input_long()` ", + "and `step_measure_input_wide()` and use these prior to ", + step_fn) + rlang::abort(msg) + } +} diff --git a/man/tidy.recipe.Rd b/man/tidy.recipe.Rd index 1c016b2..771f663 100644 --- a/man/tidy.recipe.Rd +++ b/man/tidy.recipe.Rd @@ -1,9 +1,11 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/input_long.R, R/input_wide.R, -% R/savitzky_golay.R +% Please edit documentation in R/input_long.R, R/input_wide.R, R/output_long.R, +% R/output_wide.R, R/savitzky_golay.R \name{tidy.step_measure_input_long} \alias{tidy.step_measure_input_long} \alias{tidy.step_measure_input_wide} +\alias{tidy.step_measure_output_long} +\alias{tidy.step_measure_output_wide} \alias{tidy.step_measure_savitzky_golay} \title{Tidiers for measure steps} \usage{ @@ -11,6 +13,10 @@ \method{tidy}{step_measure_input_wide}(x, ...) +\method{tidy}{step_measure_output_long}(x, ...) + +\method{tidy}{step_measure_output_wide}(x, ...) + \method{tidy}{step_measure_savitzky_golay}(x, ...) } \arguments{ diff --git a/tests/testthat/_snaps/measure_input_long.md b/tests/testthat/_snaps/measure_input_long.md index 284193a..3e1f939 100644 --- a/tests/testthat/_snaps/measure_input_long.md +++ b/tests/testthat/_snaps/measure_input_long.md @@ -2,7 +2,7 @@ Code print(rec_1) - Message + Message -- Recipe ---------------------------------------------------------------------- @@ -43,7 +43,7 @@ Code print(prep_1) - Message + Message -- Recipe ---------------------------------------------------------------------- @@ -87,7 +87,7 @@ Code recipe(water + fat + protein ~ absorp, data = na_train) %>% step_measure_input_long(absorp) %>% prep() - Condition + Error Error in `step_measure_input_long()`: Caused by error in `prep()`: ! 'location' is required for long input data @@ -107,7 +107,7 @@ Code recipe(water + fat + protein ~ ., data = na_train) %>% step_measure_input_long( dplyr::everything(), location = vars(ind)) %>% prep() - Condition + Error Error in `step_measure_input_long()`: Caused by error in `check_single_selector()`: ! The selection for `...` should only select a single column (6 columns were selected). @@ -117,7 +117,7 @@ Code recipe(water + fat + protein ~ ., data = na_train) %>% step_measure_input_long( absorp, location = vars(dplyr::everything())) %>% prep() - Condition + Error Error in `step_measure_input_long()`: Caused by error in `check_single_selector()`: ! The selection for `location` should only select a single column (6 columns were selected). diff --git a/tests/testthat/_snaps/measure_input_wide.md b/tests/testthat/_snaps/measure_input_wide.md index 98d5e98..5c84336 100644 --- a/tests/testthat/_snaps/measure_input_wide.md +++ b/tests/testthat/_snaps/measure_input_wide.md @@ -2,7 +2,7 @@ Code print(rec_1) - Message + Message -- Recipe ---------------------------------------------------------------------- @@ -48,7 +48,7 @@ Code print(prep_1) - Message + Message -- Recipe ---------------------------------------------------------------------- @@ -92,7 +92,7 @@ Code recipe(water + fat + protein ~ ., data = na_train) %>% step_measure_input_wide( x_001:x_100, location_values = 1:2) %>% prep() - Condition + Error Error in `step_measure_input_wide()`: Caused by error in `prep()`: ! 100 columns were selected as inputs but `location_values` has 2 values. diff --git a/tests/testthat/_snaps/measure_output_long.md b/tests/testthat/_snaps/measure_output_long.md index 21bb78b..639e464 100644 --- a/tests/testthat/_snaps/measure_output_long.md +++ b/tests/testthat/_snaps/measure_output_long.md @@ -2,7 +2,7 @@ Code print(rec_1) - Message + Message -- Recipe ---------------------------------------------------------------------- @@ -45,7 +45,7 @@ Code print(prep_1) - Message + Message -- Recipe ---------------------------------------------------------------------- @@ -58,7 +58,7 @@ Training data contained 400 data points and no incomplete rows. -- Operations - * Collate long analytical measurements: absorp, ind | Trained + * Collate long analytical measurements: absorp and ind | Trained * Restructure analytical measurements to long format: ~"" | Trained @@ -88,13 +88,3 @@ 1 1 step measure_input_long TRUE FALSE potato 2 2 step measure_output_long TRUE FALSE turnip ---- - - Code - recipe(water + fat + protein ~ ., data = meats_train) %>% - step_measure_output_long() %>% prep() - Condition - Error in `step_measure_output_long()`: - Caused by error in `check_has_measure()`: - ! It appears that the measurements have not been converted for the inernal format. See `step_measure_input_long()` and `step_measure_input_wide()` and use these prior to `step_measure_output_long()`. - From f2967c7b8e04ad426bad7d07a6b442bd6e355f1f Mon Sep 17 00:00:00 2001 From: James Wade Date: Mon, 8 Jan 2024 13:23:40 -0500 Subject: [PATCH 13/13] update snapshots --- tests/testthat/_snaps/measure_output_wide.md | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/tests/testthat/_snaps/measure_output_wide.md b/tests/testthat/_snaps/measure_output_wide.md index 56631da..eb6c68c 100644 --- a/tests/testthat/_snaps/measure_output_wide.md +++ b/tests/testthat/_snaps/measure_output_wide.md @@ -2,7 +2,7 @@ Code print(rec_1) - Message + Message -- Recipe ---------------------------------------------------------------------- @@ -45,7 +45,7 @@ Code print(prep_1) - Message + Message -- Recipe ---------------------------------------------------------------------- @@ -58,7 +58,7 @@ Training data contained 400 data points and no incomplete rows. -- Operations - * Collate long analytical measurements: absorp, ind | Trained + * Collate long analytical measurements: absorp and ind | Trained * Restructure analytical measurements to wide format: ~"" | Trained @@ -88,13 +88,3 @@ 1 1 step measure_input_long TRUE FALSE potato 2 2 step measure_output_wide TRUE FALSE turnip ---- - - Code - recipe(water + fat + protein ~ ., data = meats_train) %>% - step_measure_output_wide() %>% prep() - Condition - Error in `step_measure_output_wide()`: - Caused by error in `check_has_measure()`: - ! It appears that the measurements have not been converted for the inernal format. See `step_measure_input_long()` and `step_measure_input_wide()` and use these prior to `step_measure_output_wide()`. -