Skip to content

Commit

Permalink
removed nplr warnings (#190)
Browse files Browse the repository at this point in the history
* Change warnings to message for handle_datetime

* Fix warnings with bottom asymptote for plots

* Manually handle values outside the asymptotes

* Add test covering the warning
  • Loading branch information
ZetrextJG authored Oct 24, 2024
1 parent a21b3a3 commit 5ae107c
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 11 deletions.
31 changes: 24 additions & 7 deletions R/classes-model.R
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,37 @@ Model <- R6::R6Class(
#' seen in standard curve samples \eqn{RAU_{max}}. Defaults to 0.
#' If the value of the predicted RAU is above \eqn{RAU_{max} + \text{over_max_extrapolation}},
#' the value is censored to the value of that sum.
#' @param eps (`numeric(1)`)\cr
#' A small value used to avoid numerical issues close to the asymptotes
#'
#' @return (`data.frame()`)\cr
#' Dataframe with the predicted RAU values for given MFI values
#' The columns are named as follows:
#' - `RAU` - the Relative Antibody Units (RAU) value
#' - `MFI` - the predicted MFI value
#'
predict = function(mfi, over_max_extrapolation = 0) {
predict = function(mfi, over_max_extrapolation = 0, eps = 1e-6) {
private$assert_model_fitted()
original_mfi <- mfi
# Extrapolation maximum
max_sc_rau <- max(dilution_to_rau(self$dilutions), na.rm = TRUE)
rau_threshold <- max_sc_rau + over_max_extrapolation
top_asymptote_transformed <- private$mfi_transform(self$top_asymptote - eps)
rau_at_top_asymptote <- nplr::getEstimates(self$model,
targets = top_asymptote_transformed
)[1, "x"]
if (rau_threshold >= rau_at_top_asymptote) {
warning(
"Extrapolation above the top asymptote is not allowed.
Samples with MFI values above the top asymptote will be censored to the top asymptote.
Consider using a smaller value for `over_max_extrapolation`."
)
}
# Handle mfi outside asymptotes
mfi <- clamp(mfi,
lower = self$bottom_asymptote + eps,
upper = self$top_asymptote - eps
)
mfi <- private$mfi_transform(mfi)
# Example columns: y, x,
df <- nplr::getEstimates(self$model, mfi)
Expand All @@ -170,11 +191,7 @@ Model <- R6::R6Class(
# Convert to RAU
df[, "x"] <- dilution_to_rau(df[, "x"])
# Censor values or extrapolate
max_sc_rau <- max(dilution_to_rau(self$dilutions), na.rm = TRUE)
max_allowed_value <- max_sc_rau + over_max_extrapolation
df[, "x"] <- ifelse(
df[, "x"] > max_allowed_value, max_allowed_value, df[, "x"]
)
df[, "x"] <- ifelse(df[, "x"] > rau_threshold, rau_threshold, df[, "x"])
# Rename columns before returning
colnames(df) <- sub("x", "RAU", colnames(df))
colnames(df) <- sub("y", "MFI", colnames(df))
Expand All @@ -191,7 +208,7 @@ Model <- R6::R6Class(
private$assert_model_fitted()
upper_bound <- nplr::getPar(self$model)$params$top
upper_bound <- (upper_bound + 1) / 2
lower_bound <- private$mfi_transform(5) # Scaled MFI for MFI = 5
lower_bound <- private$mfi_transform(10) # Scaled MFI for MFI = 10

uniform_targets <- seq(lower_bound, upper_bound, length.out = 100)
df <- nplr::getEstimates(self$model, targets = uniform_targets)
Expand Down
4 changes: 2 additions & 2 deletions R/parser.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ handle_datetime <- function(datetime_str, file_format = "xPONENT") {
if (!is.na(first_attempt)) {
return(first_attempt)
} else {
warning("Could not parse datetime string using default datetime format. Trying other possibilies.")
message("Could not parse datetime string using default datetime format. Trying other possibilies.")
for (order in possible_orders[-1]) {
datetime <- lubridate::parse_date_time2(datetime_str, orders = order, tz = "")
if (!is.na(datetime)) {
warning("Successfully parsed datetime string using order: ", order)
message("Successfully parsed datetime string using order: ", order)
return(datetime)
}
}
Expand Down
5 changes: 4 additions & 1 deletion man/Model.Rd

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

2 changes: 1 addition & 1 deletion tests/testthat/test-parser.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ test_that("Test handle_datetime with seen date formats", {
)

# Automatic recovery with xPONENT datetime format DD/MM/YYYY HH:MM
expect_warning(dt <- handle_datetime("26/02/2014 16:07", "xPONENT"))
expect_message(dt <- handle_datetime("26/02/2014 16:07", "xPONENT"))
expect_equal(dt, as.POSIXct("2014-02-26 16:07:00", tz = ""))

# Default INTELLIFLEX datetime format YYYY-MM-DD HH:MM:SS AM/PM
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/test-standard_curve.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,12 @@ test_that("Test plotting the full plot", {
model <- create_standard_curve_model_analyte(plate, "Spike_6P_IPP")
expect_no_error(plot_standard_curve_analyte_with_model(plate, model))
})

test_that("Test over max extrapolation", {
plate <- get_test_plate()
model <- create_standard_curve_model_analyte(plate, "Spike_6P_IPP")
expect_warning(plot_standard_curve_analyte_with_model(
plate, model,
over_max_extrapolation = Inf
))
})

0 comments on commit 5ae107c

Please sign in to comment.