From 53f7bc930d0bf70434581ff9fbcad0cc5eca7d38 Mon Sep 17 00:00:00 2001 From: "alex.hill@gmail.com" Date: Mon, 4 Nov 2024 15:19:15 +0000 Subject: [PATCH] test limits --- R/biokinetics.R | 2 +- tests/testthat/test-data.R | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/R/biokinetics.R b/R/biokinetics.R index 305154d..9868c77 100644 --- a/R/biokinetics.R +++ b/R/biokinetics.R @@ -45,7 +45,7 @@ biokinetics <- R6::R6Class( private$lower_limit <- min(private$data$value) } else { min_value <- min(private$data$value) - if (max_value > upper_limit) { + if (min_value < lower_limit) { warning(sprintf("Data contains a value of %s which is smaller than the lower detection limit %s", min_value, lower_limit)) } diff --git a/tests/testthat/test-data.R b/tests/testthat/test-data.R index 6f3afb8..668b9f0 100644 --- a/tests/testthat/test-data.R +++ b/tests/testthat/test-data.R @@ -65,3 +65,39 @@ test_that("Log scale data is passed directly to stan", { stan_data <- mod$get_stan_data() expect_equal(stan_data$value, dat$value, ignore_attr = TRUE) }) + +test_that("Highest value is used as default upper limit", { + dat <- data.table::fread(system.file("delta_full.rds", package = "epikinetics")) + mod <- biokinetics$new(data = dat, lower_detection_limit = 2) + stan_data <- mod$get_stan_data() + expect_equal(stan_data$upper_limit, log2(max(dat$value)/2)) + expect_equal(stan_data$lower_limit, 0) +}) + +test_that("Warns if data contains values above the upper limit", { + dat <- data.table::fread(system.file("delta_full.rds", package = "epikinetics")) + expect_warning({mod <- biokinetics$new(data = dat, upper_detection_limit = 10)}, + "Data contains a value of 2560 which is greater than the upper detection limit 10") + stan_data <- mod$get_stan_data() + expect_equal(stan_data$upper_limit, log2(10/5)) + expect_equal(stan_data$lower_limit, 0) +}) + +test_that("Smallest value is used as default lower limit", { + dat <- data.table::fread(system.file("delta_full.rds", package = "epikinetics")) + mod <- biokinetics$new(data = dat, upper_detection_limit = 3000) + stan_data <- mod$get_stan_data() + expect_equal(stan_data$upper_limit, log2(3000/min(dat$value))) + expect_equal(stan_data$lower_limit, 0) +}) + +test_that("Warns if data contains values below the lower limit", { + dat <- data.table::fread(system.file("delta_full.rds", package = "epikinetics")) + expect_warning({mod <- biokinetics$new(data = dat, + upper_detection_limit = 2560, + lower_detection_limit = 10)}, + "Data contains a value of 5 which is smaller than the lower detection limit 10") + stan_data <- mod$get_stan_data() + expect_equal(stan_data$upper_limit, log2(2560/10)) + expect_equal(stan_data$lower_limit, 0) +})