diff --git a/NAMESPACE b/NAMESPACE index 48d1c961..e22607a9 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -6,6 +6,9 @@ S3method(as_OpenSpecy,OpenSpecy) S3method(as_OpenSpecy,data.frame) S3method(as_OpenSpecy,default) S3method(as_OpenSpecy,list) +S3method(conform_spec,OpenSpecy) +S3method(conform_spec,default) +S3method(conform_spec,list) S3method(flatten_range,default) S3method(head,OpenSpecy) S3method(match_spec,data.frame) @@ -29,12 +32,8 @@ export(adj_res) export(as_OpenSpecy) export(c_spec) export(check_lib) -export(combine_OpenSpecy) export(conform_res) export(conform_spec) -export(conform_spec.OpenSpecy) -export(conform_spec.default) -export(conform_spectra) export(find_spec) export(flatten_range) export(gen_grid) diff --git a/R/conform_spec.R b/R/conform_spec.R index bce6d1ba..0fb3dbbb 100644 --- a/R/conform_spec.R +++ b/R/conform_spec.R @@ -1,32 +1,15 @@ #' @rdname conform_spec #' -#' @title Adjust spectral intensities to absorbance units +#' @title Conform spectra #' #' @description -#' Converts reflectance or transmittance intensity units to absorbance units. +#' #' #' @details -#' Many of the Open Specy functions will assume that the spectrum is in -#' absorbance units. For example, see \code{\link{match_spec}()} and -#' \code{\link{subtr_bg}()}. -#' To run those functions properly, you will need to first convert any spectra -#' from transmittance or reflectance to absorbance using this function. -#' The transmittance adjustment uses the \eqn{log10(1 / T)} -#' calculation which does not correct for system and particle characteristics. -#' The reflectance adjustment uses the Kubelka-Munk equation -#' \eqn{(1 - R)^2 / 2R}. We assume that the reflectance intensity -#' is a percent from 1-100 and first correct the intensity by dividing by 100 -#' so that it fits the form expected by the equation. +#' Many of #' -#' @param object a list object of class \code{OpenSpecy}. -#' @param type a character string specifying whether the input spectrum is -#' in absorbance units (\code{"none"}, default) or needs additional conversion -#' from \code{"reflectance"} or \code{"transmittance"} data. -#' @param make_rel logical; if \code{TRUE} spectra are automatically normalized -#' with \code{\link{make_rel}()}. -#' @param \ldots further arguments passed to submethods; this is -#' to \code{\link{adj_neg}()} for \code{adj_intens()} and -#' to \code{\link{conform_res}()} for \code{conform_intens()}. +#' @param x a list object of class \code{OpenSpecy}. +#' @param res spectral resolution adjusted to. #' #' @return #' \code{adj_intens()} returns a data frame containing two columns @@ -48,33 +31,33 @@ #' @importFrom magrittr %>% #' @importFrom data.table .SD #' @export -conform_spec <- function(object, ...) { +conform_spec <- function(x, res = 5) { UseMethod("conform_spec") } #' @rdname conform_spec #' #' @export -conform_spec.default <- function(object, ...) { +conform_spec.default <- function(x, res = 5) { stop("object 'x' needs to be of class 'OpenSpecy'", call. = F) } #' @rdname conform_spec #' #' @export -conform_spec.OpenSpecy <- function(object, type = "none", make_rel = TRUE, ...) { - wn <- conform_res(object$wavenumber, ...) +conform_spec.OpenSpecy <- function(x, res = 5) { + wn <- conform_res(x$wavenumber, res = res) - spec <- object$spectra[, lapply(.SD, .clean_spec, - x = object$wavenumber, - xout = wn)] + spec <- x$spectra[, lapply(.SD, .conform_intens, + x = x$wavenumber, + xout = wn)] - object$wavenumber <- wn - object$spectra <- spec + x$wavenumber <- wn + x$spectra <- spec - adj_intens(object, type = type, make_rel = make_rel, na.rm = T) + return(x) } -.clean_spec <- function(...) { +.conform_intens <- function(...) { approx(...)$y } diff --git a/R/manage_spec.R b/R/manage_spec.R index f6bc8827..2aca15b3 100644 --- a/R/manage_spec.R +++ b/R/manage_spec.R @@ -34,36 +34,13 @@ #' @importFrom data.table data.table as.data.table fread #' @export -#' @rdname data_norm -#' -#' @export -conform_spec <- function(spec, x, xout){ - c( - approx(x = x, y = spec, xout = xout)$y - ) -} - -#' @rdname data_norm -#' -#' @export -conform_spectra <- function(data, xout, coords = NULL){ - if(is_OpenSpecy(data)){ - as_OpenSpecy( - x = xout, - spectra = data$spectra[,lapply(.SD, function(x){ - conform_spec(x = data$wavenumber, spec = x, xout = xout)})], - metadata = data$metadata, - coords = coords - ) - } -} #' @rdname data_norm #' #' @export -combine_OpenSpecy <- function(files, wavenumbers = NULL, res = NULL, coords = NULL){ +c_spec <- function(..., wavenumbers = NULL, res = NULL, coords = NULL){ - if(!is.list(files)){ + if(!is.list(files)) { lof <- lapply(files, read_spec, coords = NULL) } else{ @@ -122,16 +99,43 @@ combine_OpenSpecy <- function(files, wavenumbers = NULL, res = NULL, coords = NU #' #' @importFrom data.table rbindlist #' @export +# c_spec <- function(...) { +# cin <- c(...) +# +# lst <- tapply(cin, names(cin), FUN = function(x) unname((x))) +# +# as_OpenSpecy( +# x = lst$wavenumber[[1]], +# # TODO: Probably should add a check to make sure all the wavenumbers are +# # aligned before doing this. +# spectra = as.data.table(lst$spectra), +# metadata = rbindlist(lst$metadata, fill = T) +# ) +# } + c_spec <- function(...) { - cin <- c(...) + UseMethod("conform_spec") +} + +#' @rdname conform_spec +#' +#' @export +conform_spec.default <- function(...) { + stop("'...' items need to be of class 'OpenSpecy'", call. = F) +} + +#' @rdname conform_spec +#' +#' @export +conform_spec.list <- function(...) { + +} - lst <- tapply(cin, names(cin), FUN = function(x) unname((x))) +#' @rdname conform_spec +#' +#' @export +conform_spec.OpenSpecy <- function(...) { + lst <- list(...) - as_OpenSpecy( - x = lst$wavenumber[[1]], - # TODO: Probably should add a check to make sure all the wavenumbers are - # aligned before doing this. - spectra = as.data.table(lst$spectra), - metadata = rbindlist(lst$metadata, fill = T) - ) + do.call } diff --git a/man/conform_spec.Rd b/man/conform_spec.Rd index 1b3fa54a..98aea325 100644 --- a/man/conform_spec.Rd +++ b/man/conform_spec.Rd @@ -1,50 +1,38 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/conform_spec.R +% Please edit documentation in R/conform_spec.R, R/manage_spec.R \name{conform_spec} \alias{conform_spec} \alias{conform_spec.default} \alias{conform_spec.OpenSpecy} -\title{Adjust spectral intensities to absorbance units} +\alias{conform_spec.list} +\title{Conform spectra} \usage{ -conform_spec(spec, x, xout) +conform_spec(x, res = 5) -conform_spec.default(object, ...) +\method{conform_spec}{default}(...) -conform_spec.OpenSpecy(object, type = "none", make_rel = TRUE, ...) -} -\arguments{ -\item{object}{a list object of class \code{OpenSpecy}.} +\method{conform_spec}{OpenSpecy}(...) + +\method{conform_spec}{default}(...) -\item{type}{a character string specifying whether the input spectrum is -in absorbance units (\code{"none"}, default) or needs additional conversion -from \code{"reflectance"} or \code{"transmittance"} data.} +\method{conform_spec}{list}(...) -\item{make_rel}{logical; if \code{TRUE} spectra are automatically normalized -with \code{\link{make_rel}()}.} +\method{conform_spec}{OpenSpecy}(...) +} +\arguments{ +\item{x}{a list object of class \code{OpenSpecy}.} -\item{\ldots}{further arguments passed to submethods; this is -to \code{\link{adj_neg}()} for \code{adj_intens()} and -to \code{\link{conform_res}()} for \code{conform_intens()}.} +\item{res}{spectral resolution adjusted to.} } \value{ \code{adj_intens()} returns a data frame containing two columns named \code{"wavenumber"} and \code{"intensity"}. } \description{ -Converts reflectance or transmittance intensity units to absorbance units. +Conform spectra } \details{ -Many of the Open Specy functions will assume that the spectrum is in -absorbance units. For example, see \code{\link{match_spec}()} and -\code{\link{subtr_bg}()}. -To run those functions properly, you will need to first convert any spectra -from transmittance or reflectance to absorbance using this function. -The transmittance adjustment uses the \eqn{log10(1 / T)} -calculation which does not correct for system and particle characteristics. -The reflectance adjustment uses the Kubelka-Munk equation -\eqn{(1 - R)^2 / 2R}. We assume that the reflectance intensity -is a percent from 1-100 and first correct the intensity by dividing by 100 -so that it fits the form expected by the equation. +Many of } \examples{ data("raman_hdpe") diff --git a/man/data_norm.Rd b/man/data_norm.Rd index 44feb310..4a344fca 100644 --- a/man/data_norm.Rd +++ b/man/data_norm.Rd @@ -1,14 +1,12 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/data_norm.R, R/flatten_range.R, -% R/manage_spec.R, R/restric_range.R +% R/restric_range.R \name{adj_res} \alias{adj_res} \alias{conform_res} \alias{adj_neg} \alias{make_rel} \alias{flatten_range} -\alias{conform_spectra} -\alias{combine_OpenSpecy} \alias{restrict_range} \title{Normalization and conversion of spectral data} \usage{ @@ -22,10 +20,6 @@ make_rel(y, na.rm = FALSE) flatten_range(x, ...) -conform_spectra(data, xout, coords = NULL) - -combine_OpenSpecy(files, wavenumbers = NULL, res = NULL, coords = NULL) - restrict_range(x, ...) } \arguments{ diff --git a/man/manage_spec.Rd b/man/manage_spec.Rd index 12cbc8db..97889b96 100644 --- a/man/manage_spec.Rd +++ b/man/manage_spec.Rd @@ -1,11 +1,10 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/manage_spec.R -\name{conform_spec} -\alias{conform_spec} +\name{c_spec} \alias{c_spec} \title{Manage spectral objects} \usage{ -conform_spec(spec, x, xout) +c_spec(...) c_spec(...) }