Skip to content

Commit

Permalink
Merge pull request #52 from RLumSK/add_methods_energy_calibrate
Browse files Browse the repository at this point in the history
Add more methodsw for the energy calibration
  • Loading branch information
RLumSK authored Sep 20, 2024
2 parents d530166 + 957d15d commit fb79bc1
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 1 deletion.
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ uncertainty calculation. (PR #42 by @RLumSK)
* Add support for `GammaSpectra-class` objects for `energy_calibrate()`(issue: #22, PR #31 by @RLumSK).
* Add coercion method for `PeakPosition-class` to `list` (exported as `as.list()`) and from `list` to `PeakPosition-class`. This enables better plotting functionality if the peak positions for where provided manually as `list` and not via, e.g., `peak_find()` (PR #37 by @RLumSK).
* Add additional columns to the output object of `dose_predict()` and calculate a "final" dose based on the mean of the findings from the count and the energy threshold (PR #43 by @RLumSK)
* Add new methods for signatures `lm` and `GammaSpectrum-class` to`energy_calibrate()` for `GammaSpectrum-class` and `GammaSpectra-class()` objects for the argument `lines`. In simple words, instead of providing data for an energy/channel calibration such calibration can be copied over from another already calibrated spectrum (PR #49 by @RLumSK).
* Add new methods for signatures `lm`, `CalibrationCurve-class`, and `GammaSpectrum-class` to`energy_calibrate()` for `GammaSpectrum-class` and `GammaSpectra-class()` objects for the argument `lines`. In simple words, instead of providing data for an energy/channel calibration such calibration can be copied over from another already calibrated spectrum (PR #49, #52 by @RLumSK).
* Add support for the energy calibration model to `dose_fit()` and `dose_predict()`. What does it mean? (1) If an energy calibration was performed on the spectra used for the dose rate model fitting, the model information is forwarded to the info slot of the model. (2) The function `dose_fit()` can read this information and double-check whether the user tries to predict the dose with calibrated or uncalibrated data. If the calibration has data but the spectrum does not, the function tries to use the available calibration. Given that the energy calibration often does not change considerably, this should dramatically simplify the
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)
Expand Down
39 changes: 39 additions & 0 deletions R/energy_calibrate.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,30 @@ setMethod(

})

#' @export
#' @rdname energy
#' @aliases energy_calibrate,GammaSpectrum,CalibrationCurve-method
setMethod(
f = "energy_calibrate",
signature = signature(object = "GammaSpectrum", lines = "CalibrationCurve"),
definition = function(object, lines, ...) {
if (is.null(lines@details$energy_calibration) || any(is.na(lines@details$energy_calibration)))
stop("The CalibrationCurve-class object provided via 'lines' does not have any energy calibration!",
call. = FALSE)

## check for multiple calibrations
if (length(lines@details$energy_calibration) > 1)
stop("Found more than one energy calibration in the CalibrationCurve-class object! Could not decide which one to take!",
call. = FALSE)

## get calibration
lines <- lines@details$energy_calibration[[1]]

## call the regular function
energy_calibrate(object, lines)

})

#' @export
#' @rdname energy
#' @aliases energy_calibrate,GammaSpectrum,list-method
Expand Down Expand Up @@ -169,6 +193,21 @@ setMethod(
}
)

#' @export
#' @rdname energy
#' @aliases energy_calibrate,GammaSpectra,CalibrationCurve-method
setMethod(
f = "energy_calibrate",
signature = signature(object = "GammaSpectra", lines = "CalibrationCurve"),
definition = function(object, lines, ...) {
spc <- lapply(unlist(object), energy_calibrate, lines)

## make create GammaSpectra class
methods::as(spc, "GammaSpectra")

}
)

# Predicates ===================================================================
#' @export
#' @rdname energy
Expand Down
6 changes: 6 additions & 0 deletions man/energy.Rd

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

52 changes: 52 additions & 0 deletions tests/testthat/test-calibrate.R
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,55 @@ test_that("Calibrate a GammaSpectrum and GammaSpectra object with a lm object",
regexp = "The spectrum provided via 'lines' does not have any calibration!")

})

test_that("Calibrate a GammaSpectrum/GammaSpectra object with a CalibrationCurve object", {
##load calibration dataset
data("BDX_LaBr_1")

## load spectrum
cnf_file <- system.file("extdata/LaBr.CNF", package = "gamma")
cnf_spc <- read(cnf_file)
cnf_spectra <- as(list(cnf_spc, cnf_spc), "GammaSpectra")

## make an energy calibration
lines <- list(
channel = c(76, 459, 816),
energy = c(238, 1461, 2614.5)
)
calib <- energy_calibrate(cnf_spc, lines = lines)

## manually add energy calibration
BDX_LaBr_1@details[["energy_calibration"]] <- list(calib@calibration)

## assign energy calibration
t <- expect_s4_class(energy_calibrate(cnf_spectra, BDX_LaBr_1), "GammaSpectra")
expect_true(all(has_calibration(t)))
t <- expect_s4_class(energy_calibrate(cnf_spc, BDX_LaBr_1), "GammaSpectrum")
expect_true(has_calibration(t))

## make sure we get an error for no calibration
BDX_LaBr_1@details[["energy_calibration"]] <- NULL
expect_error(
object = energy_calibrate(cnf_spc, BDX_LaBr_1),
regexp = "The CalibrationCurve-class object provided via 'lines' does not have any energy calibration!")

BDX_LaBr_1@details[["energy_calibration"]] <- NULL
expect_error(
object = energy_calibrate(cnf_spectra, BDX_LaBr_1),
regexp = "The CalibrationCurve-class object provided via 'lines' does not have any energy calibration!")

## for NA
BDX_LaBr_1@details[["energy_calibration"]] <- NA
expect_error(
object = energy_calibrate(cnf_spc, BDX_LaBr_1),
regexp = "The CalibrationCurve-class object provided via 'lines' does not have any energy calibration!")

## for NA
BDX_LaBr_1@details[["energy_calibration"]] <- list(t@calibration, t@calibration)
expect_error(
object = energy_calibrate(cnf_spc, BDX_LaBr_1),
regexp = "Found more than one energy calibration in the CalibrationCurve-class object! Could not decide which one to take!")


})

0 comments on commit fb79bc1

Please sign in to comment.