From 3a69dfc26fb0b5bb9683cf494df30c651ba21dd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vilmantas=20G=C4=97g=C5=BEna?= Date: Wed, 4 Aug 2021 00:55:55 +0300 Subject: [PATCH 1/7] Convert wl_convert_units() to generic --- R/wl_convert_units.R | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/R/wl_convert_units.R b/R/wl_convert_units.R index 42445eda..7c90e8a6 100644 --- a/R/wl_convert_units.R +++ b/R/wl_convert_units.R @@ -1,15 +1,16 @@ +# Function ------------------------------------------------------------------- #' Convert between different wavelength units #' #' The following units can be converted into each other: #' *nm*, \emph{\eqn{cm^{-1}}{inverse cm}}, *eV*, *THz* and *Raman shift*. #' -#' @param x data for conversion -#' @param from source unit -#' @param to destination unit -#' @param ref_wl laser wavelength (required for work with Raman shift) +#' @param x Data for conversion. +#' @param from Source unit. +#' @param to Destination unit. +#' @param ref_wl Laser/Reference wavelength (required for work with Raman shift). #' -#' @author R. Kiselev +#' @author R. Kiselev, V. Gegzna #' #' @concept wavelengths #' @@ -19,6 +20,14 @@ #' wl_convert_units(3200, "Raman shift", "nm", ref_wl = 785.04) #' wl_convert_units(785, "nm", "invcm") wl_convert_units <- function(x, from, to, ref_wl = NULL) { + UseMethod("wl_convert_units", x) +} + +# Method --------------------------------------------------------------------- + +#' @rdname wl_convert_units +#' @export +wl_convert_units.default <- function(x, from, to, ref_wl = NULL) { src <- .wl_fix_unit_name(from) dest <- .wl_fix_unit_name(to) @@ -35,6 +44,9 @@ wl_convert_units <- function(x, from, to, ref_wl = NULL) { return(f(x, ref_wl)) } + +# Helper functions ----------------------------------------------------------- + wl_ev2freq <- function(x, ...) wl_nm2freq(wl_ev2nm(x)) wl_ev2invcm <- function(x, ...) q * x / (100 * h * c) wl_ev2nm <- function(x, ...) 1e9 * h * c / (q * x) From 24abcffcd4661eeb264a55d0682b9316f04be377 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vilmantas=20G=C4=97g=C5=BEna?= Date: Wed, 4 Aug 2021 00:57:21 +0300 Subject: [PATCH 2/7] Add method wl_convert_units.hyperSpec() --- R/wl_convert_units.R | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/R/wl_convert_units.R b/R/wl_convert_units.R index 7c90e8a6..c7ab7e52 100644 --- a/R/wl_convert_units.R +++ b/R/wl_convert_units.R @@ -45,6 +45,28 @@ wl_convert_units.default <- function(x, from, to, ref_wl = NULL) { } +# Method --------------------------------------------------------------------- + +#' @rdname wl_convert_units +#' @export +wl_convert_units.hyperSpec <- function(x, from, to, ref_wl = NULL) { + wl_old <- wl(x) + wl_new <- wl_convert_units(wl_old, from, to, ref_wl) + wl(x) <- wl_new + + x@label$.wavelength <- + switch(.wl_fix_unit_name(to), + nm = expression("Wavelength, nm"), + invcm = expression(tilde(nu) / cm^-1), + ev = expression("Energy / eV"), + freq = expression(nu / THz), + raman = expression(Raman ~ shift / cm^-1), + to + ) + + x +} + # Helper functions ----------------------------------------------------------- wl_ev2freq <- function(x, ...) wl_nm2freq(wl_ev2nm(x)) @@ -219,4 +241,20 @@ hySpc.testthat::test(wl_convert_units) <- function() { # # ... # # }) + + + test_that("wl_convert_units.hyperSpec works", { + local_edition(3) + + # hyperSpec: + expect_silent(spc <- wl_convert_units(flu, from = "nm", to = "1/cm")) + + expect_s4_class(spc, "hyperSpec") + expect_equal(as.character(labels(spc, ".wavelength")), "tilde(nu)/cm^-1") + + # Integer vector: + expect_silent(wls <- wl_convert_units(wl(flu), from = "nm", to = "1/cm")) + expect_equal(wls, wl(spc)) + + }) } From 2ddde7f15d8a43a70aac34aa5e1892b89447aa94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vilmantas=20G=C4=97g=C5=BEna?= Date: Wed, 4 Aug 2021 01:04:21 +0300 Subject: [PATCH 3/7] Convert wl_convert_units() to generic --- NEWS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/NEWS.md b/NEWS.md index f72767d9..fa06e2f3 100644 --- a/NEWS.md +++ b/NEWS.md @@ -45,6 +45,7 @@ `wl.eval()` | `wl_eval()` | cbeleites/hyperSpec#208, cbeleites/hyperSpec#309 `wlconv()` | `wl_convert_units()` | cbeleites/hyperSpec#208, cbeleites/hyperSpec#309 - Wavelength unit conversion functions `ev2freq()`, `ev2invcm()`, `ev2nm()`, `ev2raman()`, `freq2ev()`, `freq2invcm()`, `freq2nm()`, `freq2raman()`, `invcm2ev()`, `invcm2freq()`, `invcm2nm()`, `invcm2raman()`, `nm2ev()`, `nm2freq()`, `nm2invcm()`, `nm2raman()`, `raman2ev()`, `raman2freq()`, `raman2invcm()`, `raman2nm()` are deprecated in favor of `wl_convert_units()` (cbeleites/hyperSpec#300). +* Function `wl_convert_units()` converted to S3 generic. Default and hyperSpec methods were added (#29). * Function `spc.NA.linapprox()`, which was deprecated for long time, is now completely removed (cbeleites/hyperSpec#239). * Column names in spectra matrix (`$spc` column of `hyperSpec` object) are now returned correctly by functions `spc.bin()` (cbeleites/hyperSpec#237), and `spc.loess()` (cbeleites/hyperSpec#245). * New function `hy_list_available_hySpc_packages()` lists packages, that are available in GitHub organization `r-hyperSpec`. From 54f60e19aa797053ad879e86b80649c054c64dba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vilmantas=20G=C4=97g=C5=BEna?= Date: Wed, 4 Aug 2021 01:15:05 +0300 Subject: [PATCH 4/7] Update documentation --- R/wl_convert_units.R | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/R/wl_convert_units.R b/R/wl_convert_units.R index c7ab7e52..893e1855 100644 --- a/R/wl_convert_units.R +++ b/R/wl_convert_units.R @@ -5,13 +5,21 @@ #' The following units can be converted into each other: #' *nm*, \emph{\eqn{cm^{-1}}{inverse cm}}, *eV*, *THz* and *Raman shift*. #' +#' For `hyperSpec` objects, values of `@wavelength` and label of wavelengths +#' (`@label$.wavelength`) are changed. +#' +#' #' @param x Data for conversion. -#' @param from Source unit. -#' @param to Destination unit. -#' @param ref_wl Laser/Reference wavelength (required for work with Raman shift). +#' @param from (character): Source units. E.g. "nm", "1/cm", "eV", "tHz", +#' "Raman shift". +#' @param to (character): Destination units. +#' @param ref_wl (numeric): Laser/Reference wavelength +#' (required for work with Raman shift). #' #' @author R. Kiselev, V. Gegzna #' +#' @return Object of the same class as input `x`. +#' #' @concept wavelengths #' #' @export @@ -106,6 +114,7 @@ wl_raman2nm <- function(x, ref_wl) 1e7 / (1e7 / ref_wl - x) } } + unit <- gsub(" .*$", "", tolower(unit)) if (unit %in% c("raman", "stokes", "rel", "rel.", "relative", "rel.cm-1", "rel.cm", "rel.1/cm", "raman shift")) { return("raman") From a51bb7d9931b3895fd321ba6557b380004d16e2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vilmantas=20G=C4=97g=C5=BEna?= Date: Wed, 4 Aug 2021 01:15:27 +0300 Subject: [PATCH 5/7] Update list of units --- R/wl_convert_units.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/wl_convert_units.R b/R/wl_convert_units.R index 893e1855..913293c9 100644 --- a/R/wl_convert_units.R +++ b/R/wl_convert_units.R @@ -119,7 +119,7 @@ wl_raman2nm <- function(x, ref_wl) 1e7 / (1e7 / ref_wl - x) if (unit %in% c("raman", "stokes", "rel", "rel.", "relative", "rel.cm-1", "rel.cm", "rel.1/cm", "raman shift")) { return("raman") } - if (unit %in% c("invcm", "energy", "wavenumber", "cm-1", "inverted", "cm", "1/cm")) { + if (unit %in% c("invcm", "energy", "wavenumber", "cm-1", "cm^-1", "cm^{-1}", "inverted", "cm", "1/cm")) { return("invcm") } if (unit %in% c("nm", "nanometer", "wavelength")) { From fde94780f6dab0ab545f11ce08ad70f0484416ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vilmantas=20G=C4=97g=C5=BEna?= Date: Thu, 12 Aug 2021 18:53:09 +0300 Subject: [PATCH 6/7] Update documentation Addresses: https://github.com/r-hyperspec/hyperSpec/pull/30#pullrequestreview-728792504 --- R/wl_convert_units.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/R/wl_convert_units.R b/R/wl_convert_units.R index 913293c9..036bcf23 100644 --- a/R/wl_convert_units.R +++ b/R/wl_convert_units.R @@ -9,7 +9,8 @@ #' (`@label$.wavelength`) are changed. #' #' -#' @param x Data for conversion. +#' @param x Data to be converted. +#' Either a `hyperSpec` object or a numeric or integer vector. #' @param from (character): Source units. E.g. "nm", "1/cm", "eV", "tHz", #' "Raman shift". #' @param to (character): Destination units. From 1fff829bb86b1ebb47e207668309def83f4ef477 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vilmantas=20G=C4=97g=C5=BEna?= Date: Thu, 12 Aug 2021 18:59:19 +0300 Subject: [PATCH 7/7] Update unit tests Addresses: https://github.com/r-hyperspec/hyperSpec/pull/30#pullrequestreview-728792504 --- R/wl_convert_units.R | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/R/wl_convert_units.R b/R/wl_convert_units.R index 036bcf23..799c6fd9 100644 --- a/R/wl_convert_units.R +++ b/R/wl_convert_units.R @@ -245,14 +245,14 @@ hySpc.testthat::test(wl_convert_units) <- function() { }) - # TODO (tests): Add expected results to the conversion grid and check against them. + # TODO (tests): Add expected results to the conversion grid and + # check against them. # test_that("wl_convert_units() performs conversion correctly", { # # ... # # }) - test_that("wl_convert_units.hyperSpec works", { local_edition(3) @@ -261,10 +261,16 @@ hySpc.testthat::test(wl_convert_units) <- function() { expect_s4_class(spc, "hyperSpec") expect_equal(as.character(labels(spc, ".wavelength")), "tilde(nu)/cm^-1") + }) + + test_that("wl_convert_units.default works", { + local_edition(3) + + spc <- wl_convert_units(flu, from = "nm", to = "1/cm") # Integer vector: - expect_silent(wls <- wl_convert_units(wl(flu), from = "nm", to = "1/cm")) + x <- wl(flu) + expect_silent(wls <- wl_convert_units(x, from = "nm", to = "1/cm")) expect_equal(wls, wl(spc)) - }) }