JamesHWade / measure

The goal of measure is to be a recipes-like interface to tidymodels for analytical characterization data.
https://jameshwade.github.io/measure/
Other
5 stars 2 forks source link

sample-wise mapping infrastructure #9

Open topepo opened 1 year ago

topepo commented 1 year ago

There are a fair number of operations that happen within-sample (like most smoothing or baseline correction methods).

We should make some infrastructure that makes it easy to implement and test those operations (with less code). We could also include fault tolerance (or at least better error messages) and capturing logging created by the underlying package functions.

Here's a prototype of something that I'm thinking about:

library(tidymodels)

tidymodels_prefer()
options(pillar.sigfig = 6)

Put the meat data into a format where each sample is a row and the measurements are in list columns (as discussed previously)


meats_2 <-
  meats %>%
  add_rowindex() %>% 
  pivot_longer(
    cols = c(x_001:x_100),
    names_to = "index",
    values_to = "value"
  ) %>%
  mutate(
    index = gsub("x_", "", index),
    index = as.numeric(index)
  ) %>%
  nest(.by = c(-value, -index), .key = ".spectra") 

A simple example of smoothing:

# Smoothing example

smooth_loess <- function(dat, span = 0.75, degree = 2, ...) {
  mod <- loess(value ~ index, data = dat, span = span, degree = degree, ...)
  dat$value <- mod$fitted
  dat
}
# a sample-wise mapping function

map_spectra <- function(.data, ...) {
  dplyr::mutate(
    .data,
    .spectra = purrr::map(.spectra, ...)
  )
}

Example syntax for bake() methods.

call_by_fn   <- map_spectra(meats_2,   smooth_loess)
call_by_form <- map_spectra(meats_2, ~ smooth_loess(.x, span = .1))

call_by_fn$.spectra[[1]] %>% slice(1:3)
#> # A tibble: 3 × 2
#>   index   value
#>   <dbl>   <dbl>
#> 1     1 2.63437
#> 2     2 2.63125
#> 3     3 2.62864
call_by_form$.spectra[[1]] %>% slice(1:3)
#> # A tibble: 3 × 2
#>   index   value
#>   <dbl>   <dbl>
#> 1     1 2.61783
#> 2     2 2.61809
#> 3     3 2.61850

Created on 2023-09-12 with reprex v2.0.2