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

feat: Add ChemoSpec_to_hyperSpec and inverse

parent 1da377b0
Branches
Tags
No related merge requests found
......@@ -163,3 +163,41 @@ matrix_to_ChemoSpec <- function(
...
)
}
#' {hyperSpec} 🔵 ➡️ 🟠 {ChemoSpec}
#'
#' {ChemoSpec} requires that the {hyperSpec} object
#' meets the following conditions:
#' - Wavelengths must be unique.
#' - Spectra must not contain `NA`s.
#'
#' @param hySpc [`hyperSpec::hyperSpec-class`] object.
#' @inheritParams to_ChemoSpec
#' @inheritDotParams to_ChemoSpec groups desc colors_set sym_set alt.sym_set .strict_extra_data_names
#'
#' @examples
#' suppressPackageStartupMessages(library(hyperSpec))
#' data(flu)
#' hyperSpec_to_ChemoSpec(
#' flu,
#' names = 1:6 |> as.character(),
#' groups = LETTERS[1:6] |> as.factor()
#' ) |> str()
#'
#' @export
#' @keywords from_hyperSpec to_ChemoSpec
#' @seealso `to_ChemoSpec()`, `ChemoSpec_to_hyperSpec()`
hyperSpec_to_ChemoSpec <- function(hySpc, names, groups, ...) {
to_ChemoSpec(
data = hySpc[[]],
freq = hySpc |> hyperSpec::wl(),
names = names,
# ^ Unique filenames are not enforced by hyperSpec
groups = groups,
# ^ No class factor is required for hyperSpec
unit_frequency = hySpc@label$.wavelength |> base::as.character(),
unit_intensity = hySpc@label$spc |> base::as.character(),
extra_data = hySpc$..,
...
)
}
......@@ -12,3 +12,58 @@ to_hyperSpec <- function(...) {
rlang::check_installed("hyperSpec", version = "0.200.0.9000")
hyperSpec::new_hyperSpec(...)
}
#' {ChemoSpec} 🟠 ➡️ 🔵 {hyperSpec}
#'
#' @param Spectra `ChemoSpec::Spectra()` object.
#' @inheritDotParams hyperSpec::new_hyperSpec gc
#'
#' @returns A [`hyperSpec::hyperSpec-class`] object.
#'
#' @examples
#' suppressPackageStartupMessages(library(ChemoSpec))
#' data("alignMUD")
#' ChemoSpec_to_hyperSpec(alignMUD) |> str()
#'
#' @export
#' @keywords from_ChemoSpec to_hyperSpec
#' @seealso `to_hyperSpec()`, `hyperSpec_to_ChemoSpec()`
ChemoSpec_to_hyperSpec <- function(Spectra, ...) {
rlang::check_installed("ChemoSpecUtils")
ChemoSpecUtils::chkSpectra(Spectra)
spc <- Spectra$data
extra_names <- base::names(Spectra) |>
base::setdiff(base::c(
"freq", "names", "data", "groups", "colors", "sym", "alt.sym", "unit", "desc"
))
data <- data.frame(
groups = Spectra$groups,
colors = Spectra$colors,
sym = Spectra$sym,
alt.sym = Spectra$alt.sym
) |> cbind(
Spectra[extra_names] |>
base::as.data.frame(row.names = Spectra$names)
)
wavelength <- Spectra$freq
labels <- base::list(
.wavelength = Spectra$unit[1],
spc = Spectra$unit[2]
)
for (extra_name in extra_names) {
labels[[extra_name]] <- extra_name
}
to_hyperSpec(
spc = spc,
data = data,
wavelength = wavelength,
labels = labels,
...
)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment