Skip to content
Snippets Groups Projects
Verified Commit ada7631a authored by Gärber, Florian's avatar Gärber, Florian
Browse files

feat: Add simplerspec_to_hyperSpec

parent 5076670d
Branches
Tags
No related merge requests found
......@@ -4,5 +4,6 @@ export(ChemoSpec_to_hyperSpec)
export(hyperSpec_to_ChemoSpec)
export(matrix_to_ChemoSpec)
export(simplerspec_to_ChemoSpec)
export(simplerspec_to_hyperSpec)
export(to_ChemoSpec)
export(to_hyperSpec)
......@@ -67,3 +67,84 @@ ChemoSpec_to_hyperSpec <- function(Spectra, ...) {
...
)
}
#' {simplerspec} ⚪ ➡️ 🔵 {hyperSpec}
#'
#' Some data will not be converted:
#' - Spectra columns except the `spc_column`
#' - Wavenumber columns except the `wavelength_column`
#'
#' @param spc_tbl [`simplerspec::gather_spc`],
#' [`simplerspec::resample_spc`], [`simplerspec::average_spc`],
#' or [`simplerspec::preprocess_spc`] spectra tibble.
#' @param spc_column Column name of the data to use.
#' Must be one of `"spc_pre", "spc_mean", "spc_rs", "spc"`, or
#' any column name if `strict_column_check` is set to FALSE.
#' @param wavelength_column Column name of the data to use.
#' Must be one of `"xvalues_pre", "wavenumbers_rs", "wavenumbers"`, or
#' any column name if `strict_column_check` is set to FALSE.
#' @param unit_wavelength,unit_spc **Optional** labels for the x- and y-axis.
#' @param .strict_column_check (**Default**: TRUE) Set this to FALSE to allow
#' using any valid column name on `spc_tbl` in `spc_column` and
#' `wavelength_column`.
#' @inheritParams to_hyperSpec
#' @inheritDotParams hyperSpec::new_hyperSpec gc
#'
#' @returns A `ChemoSpec::Spectra()` object.
#'
#' @examples
#' data("simplerspec_opus")
#' spectra <- simplerspec_to_hyperSpec(simplerspec_opus) |> str()
#'
#' @export
#' @keywords from_simplerspec to_hyperSpec
#' @seealso `to_hyperSpec()`, `simplerspec_to_ChemoSpec()`
simplerspec_to_hyperSpec <- function(
spc_tbl,
spc_column = base::c("spc_pre", "spc_mean", "spc_rs", "spc"),
wavelength_column = base::c("xvalues_pre", "wavenumbers_rs", "wavenumbers"),
unit_spc = "{missing spectra unit}",
unit_wavelength = "{missing wavelength unit}",
.strict_column_check = TRUE,
...) {
rlang::check_installed("tibble")
spc_tbl <- tibble::as_tibble(spc_tbl)
checkmate::assert_class(spc_tbl, base::c("tbl_df", "tbl", "data.frame"))
if (.strict_column_check) {
spc_column <- base::match.arg(spc_column, c("spc_pre", "spc_mean", "spc_rs", "spc"))
wavelength_column <- base::match.arg(wavelength_column, c("xvalues_pre", "wavenumbers_rs", "wavenumbers"))
} else {
spc_column <- base::match.arg(spc_column, colnames(spc_tbl))
wavelength_column <- base::match.arg(wavelength_column, colnames(spc_tbl))
}
spc <- base::do.call(rbind, spc_tbl[, spc_column][[1]]) |> as.matrix()
data <- base::do.call(rbind, spc_tbl$metadata) |> base::as.data.frame()
wavelength <- spc_tbl[, wavelength_column][[1]][[1]]
labels <- base::list(
.wavelength = unit_wavelength,
spc = unit_spc
)
for (data_name in colnames(data)) {
labels[[data_name]] <- data_name
}
# Because simplerspec contains a metadata field beginning with "spc"
# (spc_in_file), the check in hyperSpec for is.null(data$spc) will
# return that metadata column instead of NULL.
# To avoid causing an unnecessary warning message for package users,
# the spectra data is instead passed as part of data.
data$spc <- spc
to_hyperSpec(
data = data,
wavelength = wavelength,
labels = labels # ,
# ...
)
}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/to_hyperSpec.R
\name{simplerspec_to_hyperSpec}
\alias{simplerspec_to_hyperSpec}
\title{{simplerspec} ⚪ ➡️ 🔵 {hyperSpec}}
\usage{
simplerspec_to_hyperSpec(
spc_tbl,
spc_column = base::c("spc_pre", "spc_mean", "spc_rs", "spc"),
wavelength_column = base::c("xvalues_pre", "wavenumbers_rs", "wavenumbers"),
unit_spc = "{missing spectra unit}",
unit_wavelength = "{missing wavelength unit}",
.strict_column_check = TRUE,
...
)
}
\arguments{
\item{spc_tbl}{\code{\link[simplerspec:gather_spc]{simplerspec::gather_spc}},
\code{\link[simplerspec:resample_spc]{simplerspec::resample_spc}}, \code{\link[simplerspec:average_spc]{simplerspec::average_spc}},
or \code{\link[simplerspec:preprocess_spc]{simplerspec::preprocess_spc}} spectra tibble.}
\item{spc_column}{Column name of the data to use.
Must be one of \verb{"spc_pre", "spc_mean", "spc_rs", "spc"}, or
any column name if \code{strict_column_check} is set to FALSE.}
\item{wavelength_column}{Column name of the data to use.
Must be one of \verb{"xvalues_pre", "wavenumbers_rs", "wavenumbers"}, or
any column name if \code{strict_column_check} is set to FALSE.}
\item{unit_wavelength, unit_spc}{\strong{Optional} labels for the x- and y-axis.}
\item{.strict_column_check}{(\strong{Default}: TRUE) Set this to FALSE to allow
using any valid column name on \code{spc_tbl} in \code{spc_column} and
\code{wavelength_column}.}
\item{...}{
Arguments passed on to \code{\link[hyperSpec:initialize]{hyperSpec::new_hyperSpec}}
\describe{
\item{\code{gc}}{(logical) \cr Use garbage collection.
If option \code{gc} is \code{TRUE}, the initialization will have frequent calls
to \code{\link[base:gc]{base::gc()}}, which can help to avoid swapping or running out of
memory. The default value of \code{gc} can be set via \code{\link[hyperSpec:hy_set_options]{hy_set_options()}}.}
}}
}
\value{
A \code{ChemoSpec::Spectra()} object.
}
\description{
Some data will not be converted:
\itemize{
\item Spectra columns except the \code{spc_column}
\item Wavenumber columns except the \code{wavelength_column}
}
}
\examples{
data("simplerspec_opus")
spectra <- simplerspec_to_hyperSpec(simplerspec_opus) |> str()
}
\seealso{
\code{to_hyperSpec()}, \code{simplerspec_to_ChemoSpec()}
}
\keyword{from_simplerspec}
\keyword{to_hyperSpec}
test_that("simplerspec ➡️ hyperSpec: opus", {
skip_if_not_installed("simplerspec")
skip_if_not_installed("tibble")
skip_if_not_installed("hyperSpec")
data(simplerspec_opus)
skip_if_not(exists("simplerspec_opus"), "Example data set required for test")
hy <- simplerspec_to_hyperSpec(simplerspec_opus)
expect_s4_class(hy, "hyperSpec")
})
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment