Skip to content

Commit

Permalink
Merge pull request #53 from RLumSK/add_set_get_energy_calibration
Browse files Browse the repository at this point in the history
Add set and get methods for energy calibration
  • Loading branch information
RLumSK authored Sep 20, 2024
2 parents fb79bc1 + be39225 commit 6a5e80f
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ S3method(as.matrix,PeakPosition)
exportMethods("[")
exportMethods("[[")
exportMethods("set_energy<-")
exportMethods("set_energy_calibration<-")
exportMethods("set_method<-")
exportMethods("set_names<-")
exportMethods(Arith)
Expand All @@ -25,6 +26,7 @@ exportMethods(energy_calibrate)
exportMethods(get_channels)
exportMethods(get_counts)
exportMethods(get_energy)
exportMethods(get_energy_calibration)
exportMethods(get_hash)
exportMethods(get_livetime)
exportMethods(get_method)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ uncertainty calculation. (PR #42 by @RLumSK)
workflow once the equipment was calibrated (PR #49 by @RLumSK).
* Add new argument `use_MC` to `dose_predict()` method. The default is `FALSE` to maintain compatibility with old code and output exceptions. If set to `TRUE` the uncertainty on the gamma dose rate uses a Monte Carlo simulation approach for a more realistic error estimation (PR #46 by RLumSK)
* Add new function parameter `water_content` to `dose_predict()` to allow for an estimate of the dry gamma dose rate using the correction factor by Aitken (1985). The default is `NULL`, in this case nothing is corrected (PR #48 by @RLumSK)
* Add two more generics: `set_energy_calibration()` and `get_energy_calibration()` and corresponding methods for `GammaSpectrum` and `GammaSpectra` objects. They build on `energy_calibrate()` but enable a more comprehensible scripting
(PR XX by RLumSK).
* Extend `dose_predict()` to work with a `numeric` input for `background` as claimed in the documentary. This value can also be set to `c(0,0)` if no background
subtraction is wanted (PR #38 by @RLumSK)

Expand Down
14 changes: 14 additions & 0 deletions R/AllGenerics.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,20 @@ setGeneric(
def = function(x, value) standardGeneric("set_names<-")
)

#' @rdname mutator
#' @aliases get_energy_calibration-method
setGeneric(
name = "get_energy_calibration",
def = function(x) standardGeneric("get_energy_calibration")
)

#' @rdname mutator
#' @aliases set_energy_calibration-method
setGeneric(
name = "set_energy_calibration<-",
def = function(x, value) standardGeneric("set_energy_calibration<-")
)

#' @rdname mutator
#' @aliases get_livetime-method
setGeneric(
Expand Down
47 changes: 47 additions & 0 deletions R/mutators.R
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,29 @@ setMethod(
}
)

#' @export
#' @rdname mutator
#' @aliases set_energy_calibration,GammaSpectrum-method
setMethod(
f = "set_energy_calibration<-",
signature = "GammaSpectrum",
definition = function(x, value) {
x <- energy_calibrate(object = x, lines = value)
x
}
)

#' @export
#' @rdname mutator
#' @aliases get_energy_calibration,GammaSpectrum-method
setMethod(
f = "get_energy_calibration",
signature = "GammaSpectrum",
definition = function(x) {
x@calibration
}
)

## Baseline --------------------------------------------------------------------
#' @export
#' @rdname mutator
Expand Down Expand Up @@ -351,6 +374,30 @@ setMethod(
}
)

#' @export
#' @rdname mutator
#' @aliases set_energy_calibration,GammaSpectra-method
setMethod(
f = "set_energy_calibration<-",
signature = "GammaSpectra",
definition = function(x, value) {
x <- energy_calibrate(object = x, lines = value)
x
}
)

#' @export
#' @rdname mutator
#' @aliases get_energy_calibration,GammaSpectra-method
setMethod(
f = "get_energy_calibration",
signature = "GammaSpectra",
definition = function(x) {
lapply(x, function(y) y@calibration)
}
)


## PeakPosition ----------------------------------------------------------------
#' @export
#' @rdname mutator
Expand Down
22 changes: 22 additions & 0 deletions man/mutator.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-mutator.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,24 @@ test_that("GammaSpectrum", {
# energy
expect_length(get_energy(spectrum), 1024)
expect_equal(range_energy(spectrum), c(-7.004032, 3124.914528))
## set energy calibration method
lines <- data.frame(
channel = c(115, 496, 870),
energy = c(238, 1461, 2615)
)
set_energy_calibration(spectrum) <- lines
expect_s4_class(spectrum, class = "GammaSpectrum")
expect_true(has_calibration(spectrum))

spectra <- as(list(spectrum, spectrum), "GammaSpectra")
set_energy_calibration(spectra) <- lines
expect_s4_class(spectra, class = "GammaSpectra")
expect_true(all(has_calibration(spectra)))

## check the get method
expect_s3_class(get_energy_calibration(spectrum), "lm")
expect_s3_class(get_energy_calibration(spectra)[[2]], "lm")

})
# GammaSpectra =================================================================
test_that("GammaSpectra", {
Expand Down

0 comments on commit 6a5e80f

Please sign in to comment.