Skip to content

Commit

Permalink
Merge pull request #31 from RLumSK/issue_22
Browse files Browse the repository at this point in the history
Implement request from #22 for GammaSpectra-class objects
  • Loading branch information
nfrerebeau authored Aug 22, 2024
2 parents 2f29143 + 322f0b7 commit a9e689e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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, PR #31 by @RLumSK)
* Update vignette about the dose rate calibration curve determination to make it more
intelligible (#30 by @RLumSK).

Expand Down
22 changes: 22 additions & 0 deletions R/energy_calibrate.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions man/energy.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions tests/testthat/test-calibrate.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit a9e689e

Please sign in to comment.