Skip to content

Commit

Permalink
Improve validation and coverage in calc_MinDose().
Browse files Browse the repository at this point in the history
  • Loading branch information
mcol committed Feb 28, 2025
1 parent 9b86a24 commit 73e4241
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
8 changes: 8 additions & 0 deletions R/calc_MinDose.R
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ calc_MinDose <- function(
## check if this function is called by calc_MaxDose()
if ("invert" %in% names(extraArgs)) {
invert <- extraArgs$invert
.validate_logical_scalar(invert)
if (!log) {
log <- TRUE # overwrite user choice as max dose model currently only supports the logged version
cat(paste("\n[WARNING] The maximum dose model only supports the logged version.",
Expand All @@ -396,6 +397,7 @@ calc_MinDose <- function(
## console output
if ("verbose" %in% names(extraArgs)) {
verbose <- extraArgs$verbose
.validate_logical_scalar(verbose)
} else {
verbose <- TRUE
}
Expand All @@ -404,39 +406,45 @@ calc_MinDose <- function(
# first level bootstrap
if ("bs.M" %in% names(extraArgs)) {
M <- as.integer(extraArgs$bs.M)
.validate_positive_scalar(M, int = TRUE, name = "'bs.M'")
} else {
M <- 1000
}

# second level bootstrap
if ("bs.N" %in% names(extraArgs)) {
N <- as.integer(extraArgs$bs.N)
.validate_positive_scalar(N, int = TRUE, name= "'bs.N'")
} else {
N <- 3*M
}

# KDE bandwith
if ("bs.h" %in% names(extraArgs)) {
h <- extraArgs$bs.h
.validate_positive_scalar(h, name = "'bs.h'")
} else {
h <- (sd(data[ ,1])/sqrt(length(data[ ,1])))*2
}

# standard deviation of sigmab
if ("sigmab.sd" %in% names(extraArgs)) {
sigmab.sd <- extraArgs$sigmab.sd
.validate_positive_scalar(sigmab.sd)
} else {
sigmab.sd <- 0.04
}

if ("debug" %in% names(extraArgs)) {
debug <- extraArgs$debug
.validate_logical_scalar(debug)
} else {
debug <- FALSE
}

if ("cores" %in% names(extraArgs)) {
cores <- extraArgs$cores
.validate_positive_scalar(cores, int = TRUE)
} else {
cores <- parallel::detectCores()
if (multicore)
Expand Down
29 changes: 29 additions & 0 deletions tests/testthat/test_calc_MinDose.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,30 @@ test_that("input validation", {
"'par' should be a positive integer scalar")
expect_error(calc_MinDose(ExampleData.DeValues$CA1, par = 2),
"'par' can only be set to 3 or 4")
expect_error(calc_MinDose(ExampleData.DeValues$CA1, sigmab = 0.1,
invert = "error"),
"'invert' should be a single logical value")
expect_error(calc_MinDose(ExampleData.DeValues$CA1, sigmab = 0.1,
verbose = "error"),
"'verbose' should be a single logical value")
expect_error(calc_MinDose(ExampleData.DeValues$CA1, sigmab = 0.1,
debug = "error"),
"'debug' should be a single logical value")
expect_error(calc_MinDose(ExampleData.DeValues$CA1, sigmab = 0.1,
sigmab.sd = c(0.01, 0.02)),
"'sigmab.sd' should be a positive scalar")
expect_error(calc_MinDose(ExampleData.DeValues$CA1, sigmab = 0.1,
bootstrap = TRUE, bs.M = -1),
"'bs.M' should be a positive integer scalar")
expect_error(calc_MinDose(ExampleData.DeValues$CA1, sigmab = 0.1,
bootstrap = TRUE, bs.N = -1),
"'bs.N' should be a positive integer scalar")
expect_error(calc_MinDose(ExampleData.DeValues$CA1, sigmab = 0.1,
bootstrap = TRUE, bs.h = -1),
"'bs.h' should be a positive scalar")
expect_error(calc_MinDose(ExampleData.DeValues$CA1, sigmab = 0.1,
cores = -1),
"'cores' should be a positive integer scalar")
})

test_that("check class and length of output", {
Expand Down Expand Up @@ -81,6 +105,11 @@ test_that("check class and length of output", {
par = 4, gamma.lower = 2, log.output = TRUE,
bootstrap = TRUE, bs.M = 10, bs.N = 5, bs.h=100),
"Gamma is larger than mu, consider running the model with new")
expect_output(calc_MinDose(ExampleData.DeValues$CA1 / 100, sigmab = 0.1,
verbose = TRUE, log.output = TRUE, par = 4))
expect_silent(calc_MinDose(ExampleData.DeValues$CA1, sigmab = 0.1,
verbose = FALSE, invert = TRUE,
bootstrap = TRUE, bs.M = 10, bs.N = 5, bs.h = 10))
})
})

Expand Down

0 comments on commit 73e4241

Please sign in to comment.