From 6fd4dccc0ba21afc678d7ff45a634ed49dc32a59 Mon Sep 17 00:00:00 2001 From: RLumSK Date: Thu, 22 Aug 2024 12:47:37 +0200 Subject: [PATCH 1/2] Implement request from #22 for GammaSpectra-class objects + ad method in energy_calibrate() + up docu + ad tests + ad NEWS --- NEWS.md | 1 + R/energy_calibrate.R | 22 ++++++++++++++++++++++ man/energy.Rd | 3 +++ tests/testthat/test-calibrate.R | 18 ++++++++++++++++++ 4 files changed, 44 insertions(+) diff --git a/NEWS.md b/NEWS.md index 1f51e238..e0e259a5 100644 --- a/NEWS.md +++ b/NEWS.md @@ -3,6 +3,7 @@ # gamma 1.0.5 ## Bugfixes & changes * Add support for Kromek SPE files to `read()`(#28 by @RLumSK) +* Add support for `GammaSpectra-class` objects for `energy_calibrate()`(issue: #22) ## Internals * Fix unicode character in plot axis labels. diff --git a/R/energy_calibrate.R b/R/energy_calibrate.R index 6cbeceef..cfee9f74 100644 --- a/R/energy_calibrate.R +++ b/R/energy_calibrate.R @@ -61,6 +61,28 @@ setMethod( } ) +#' @export +#' @rdname energy +#' @aliases energy_calibrate,GammaSpectra,PeakPosition-method +setMethod( + f = "energy_calibrate", + signature = signature(object = "GammaSpectra", lines = "PeakPosition"), + definition = function(object, lines, ...) { + # Get data + peaks <- as.data.frame(lines) + peaks$energy <- peaks$energy_expected + + # Adjust spectrum for energy shift using the same set of peask for + # the entire set of gamma spectra + # Return a new gamma spectrum with adjusted energy + spc <- lapply(unlist(object), energy_calibrate, peaks) + + ## make create GammaSpectra class + methods::as(spc, "GammaSpectra") + + } +) + # Predicates =================================================================== #' @export #' @rdname energy diff --git a/man/energy.Rd b/man/energy.Rd index 99948892..833139b1 100644 --- a/man/energy.Rd +++ b/man/energy.Rd @@ -9,6 +9,7 @@ \alias{has_calibration} \alias{energy_calibrate,GammaSpectrum,list-method} \alias{energy_calibrate,GammaSpectrum,PeakPosition-method} +\alias{energy_calibrate,GammaSpectra,PeakPosition-method} \alias{has_energy,GammaSpectrum-method} \alias{has_energy,GammaSpectra-method} \alias{has_calibration,GammaSpectrum-method} @@ -25,6 +26,8 @@ has_calibration(object) \S4method{energy_calibrate}{GammaSpectrum,PeakPosition}(object, lines, ...) +\S4method{energy_calibrate}{GammaSpectra,PeakPosition}(object, lines, ...) + \S4method{has_energy}{GammaSpectrum}(object) \S4method{has_energy}{GammaSpectra}(object) diff --git a/tests/testthat/test-calibrate.R b/tests/testthat/test-calibrate.R index d8c2fef5..06e5fdfd 100644 --- a/tests/testthat/test-calibrate.R +++ b/tests/testthat/test-calibrate.R @@ -58,6 +58,24 @@ test_that("Calibrate a GammaSpectrum object with a PeakPosition object", { expect_length(spectrum@energy, 0) expect_length(calib@energy, 1024) }) +test_that("Calibrate a GammaSpectra object with a PeakPosition object", { + spc_file <- system.file("extdata/LaBr.TKA", package = "gamma") + spectrum_1 <- spectrum_2 <- read(spc_file) + spectra <- methods::as(list(spectrum_1, spectrum_2), "GammaSpectra") + + peaks <- .PeakPosition( + hash = spectrum_1@hash, + channel = c(76L, 459L, 816L), + energy_expected = c(NA_real_, NA_real_, NA_real_) + ) + + set_energy(peaks) <- c(238, 1461, 2614.5) + calib <- energy_calibrate(spectra, lines = peaks) + + expect_s4_class(calib, "GammaSpectra") + +}) + test_that("the energy scale of a GammaSpectrum is set", { cnf_file <- system.file("extdata/LaBr.CNF", package = "gamma") cnf_spc <- read(cnf_file) From 5378febc07ec533edb55e8c6ac8289b55ad37a96 Mon Sep 17 00:00:00 2001 From: RLumSK Date: Thu, 22 Aug 2024 12:50:20 +0200 Subject: [PATCH 2/2] Add PR ID --- NEWS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index e0e259a5..19130c27 100644 --- a/NEWS.md +++ b/NEWS.md @@ -3,7 +3,7 @@ # gamma 1.0.5 ## Bugfixes & changes * Add support for Kromek SPE files to `read()`(#28 by @RLumSK) -* Add support for `GammaSpectra-class` objects for `energy_calibrate()`(issue: #22) +* Add support for `GammaSpectra-class` objects for `energy_calibrate()`(issue: #22, PR #31 by @RLumSK) ## Internals * Fix unicode character in plot axis labels.