Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add input checking and improve test coverage #131

Merged
merged 32 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
4c83347
Improved the description of the mu parameter
jamesmbaazam Jul 21, 2023
5dfce95
Added input checking of the mu argument
jamesmbaazam Jul 21, 2023
1d75784
Linting
jamesmbaazam Jul 21, 2023
e9e6056
Improved the error messages
jamesmbaazam Jul 24, 2023
526344f
Added tests for the borel functions
jamesmbaazam Jul 24, 2023
20a2616
Added input checking to rnbinom_mean_disp()
jamesmbaazam Jul 24, 2023
3818eb3
Added tests for the utils functions
jamesmbaazam Jul 24, 2023
7fcbb54
Regenerated the doc files for the the borel functions
jamesmbaazam Jul 24, 2023
a05957e
Linting
jamesmbaazam Jul 24, 2023
c426ae2
Linting
jamesmbaazam Jul 24, 2023
d665889
Added a test for chain_sim()
jamesmbaazam Jul 24, 2023
b38b3e3
Linted all the test scripts
jamesmbaazam Jul 24, 2023
caae30f
Incremented the minor version
jamesmbaazam Jul 24, 2023
b97fe0b
Added news items to describe new changes
jamesmbaazam Jul 24, 2023
577d1b5
Update CITATION.cff
actions-user Jul 24, 2023
61333cf
Check all elements of x
jamesmbaazam Nov 6, 2023
1234b39
Import checkmate
jamesmbaazam Nov 6, 2023
f8b988a
Add checks on x, n, and mu
jamesmbaazam Nov 6, 2023
48e36cc
Update docs to reflect input types
jamesmbaazam Nov 6, 2023
6195f3d
Add input checks for args of rnbinom_mean_disp()
jamesmbaazam Nov 6, 2023
b4cc22d
Fix error message regex
jamesmbaazam Nov 6, 2023
b439f36
Fix test file name
jamesmbaazam Nov 6, 2023
efa41ed
Fix lower value of x
jamesmbaazam Nov 6, 2023
944ffc2
Explicitly assign arguments
jamesmbaazam Nov 6, 2023
039062a
Restructure test block for readability
jamesmbaazam Nov 6, 2023
add81c8
Style the tests for readability
jamesmbaazam Nov 6, 2023
0db7607
Check that x is below 0
jamesmbaazam Nov 6, 2023
5e2790a
Add checks and tests for all arguments of rnbinom_mean_disp()
jamesmbaazam Nov 6, 2023
3435d12
Generate docs
jamesmbaazam Nov 6, 2023
3d58f99
Linting: Fix indentation
jamesmbaazam Nov 6, 2023
78bd84a
Finalise NEWS item
jamesmbaazam Nov 6, 2023
309fa34
Update CITATION.cff
actions-user Nov 6, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type: software
license: MIT
title: 'bpmodels: Simulating and Analysing Transmission Chain Statistics using Branching
Process Models'
version: 0.3.0
version: 0.3.1
abstract: Provides methods to simulate and analyse the size and length of branching
processes with an arbitrary offspring distribution. These can be used, for example,
to analyse the distribution of chain sizes or length of infectious disease outbreaks,
Expand Down Expand Up @@ -249,3 +249,15 @@ references:
given-names: Saralees
email: [email protected]
year: '2023'
- type: software
title: checkmate
abstract: 'checkmate: Fast and Versatile Argument Checks'
notes: Imports
url: https://mllg.github.io/checkmate/
repository: https://CRAN.R-project.org/package=checkmate
authors:
- family-names: Lang
given-names: Michel
email: [email protected]
orcid: https://orcid.org/0000-0001-9754-0393
year: '2023'
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: bpmodels
Title: Simulating and Analysing Transmission Chain Statistics using Branching Process
Models
Version: 0.3.0
Version: 0.3.1
Authors@R: c(
person("James M.", "Azam", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "https://orcid.org/0000-0001-5782-7330")),
Expand Down Expand Up @@ -48,3 +48,5 @@ LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
Language: en-GB
Imports:
checkmate
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# bpmodels 0.3.1

## Unit tests and input validation

* The following internal functions now have input validation: `rborel()`, `dborel()`, `complementary_logprob()`, and `rnbinom_mean_disp()`.
* Code coverage has been improved with more tests on the following functions: `rborel()`, `dborel()`, `chain_sim()`, `rnbinom_mean_disp()`, `complementary_logprob()`, `rgen_length()`, and `rbinom_size()`.

# bpmodels 0.3.0

## Website
Expand Down
19 changes: 15 additions & 4 deletions R/borel.r
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
##' Density of the Borel distribution
##'
##' @param x vector of integers.
##' @param mu mu parameter.
##' @param x vector of quantiles; integer.
##' @param mu mu parameter (the poisson mean); non-negative.
##' @param log logical; if TRUE, probabilities p are given as log(p).
##' @return probability mass.
##' @author Sebastian Funk
dborel <- function(x, mu, log = FALSE) {
if (x < 1) stop("'x' must be greater than 0")
checkmate::assert_numeric(
x, lower = 1, upper = Inf
)
checkmate::assert_number(
mu, lower = 0, finite = TRUE, na.ok = FALSE
)
ld <- -mu * x + (x - 1) * log(mu * x) - lgamma(x + 1)
if (!log) ld <- exp(ld)
return(ld)
Expand All @@ -16,11 +21,17 @@ dborel <- function(x, mu, log = FALSE) {
##'
##' Random numbers are generated by simulating from a Poisson branching process
##' @param n number of random variates to generate.
##' @param mu mu parameter.
##' @param mu mu parameter (the Poisson mean).
##' @param infinite any number to treat as infinite; simulations will be stopped
##' if this number is reached
##' @return vector of random numbers
##' @author Sebastian Funk
rborel <- function(n, mu, infinite = Inf) {
jamesmbaazam marked this conversation as resolved.
Show resolved Hide resolved
checkmate::assert_number(
n, lower = 1, finite = TRUE, na.ok = FALSE
)
checkmate::assert_number(
mu, lower = 0, finite = TRUE, na.ok = FALSE
)
chain_sim(n, "pois", "size", infinite = infinite, lambda = mu)
}
16 changes: 14 additions & 2 deletions R/utils.r
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#' @author Sebastian Funk
#' @keywords internal
complementary_logprob <- function(x) {
checkmate::assert_numeric(
x, lower = -Inf, upper = 0
)
tryCatch(log1p(-sum(exp(x))), error = function(e) -Inf)
}

Expand Down Expand Up @@ -43,14 +46,23 @@ rgen_length <- function(n, x, prob) {
#' Negative binomial random numbers parametrized
#' in terms of mean and dispersion coefficient
#' @param n number of samples to draw
#' @param mn mean of distribution
#' @param disp dispersion coefficient (var/mean)
#' @param mn mean of distribution; Must be > 0.
#' @param disp dispersion coefficient (var/mean); Must be > 1.
#' @return vector containing the random numbers
#' @author Flavio Finger
#' @export
#' @examples
#' rnbinom_mean_disp(n = 5, mn = 4, disp = 2)
rnbinom_mean_disp <- function(n, mn, disp) {
jamesmbaazam marked this conversation as resolved.
Show resolved Hide resolved
checkmate::assert_number(
n, lower = 1, finite = TRUE, na.ok = FALSE
)
checkmate::assert_number(
disp, lower = 1, finite = TRUE, na.ok = FALSE
)
checkmate::assert_number(
mn, lower = 1E-100, finite = TRUE, na.ok = FALSE
)
size <- mn / (disp - 1)
stats::rnbinom(n, size = size, mu = mn)
}
4 changes: 2 additions & 2 deletions man/dborel.Rd

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

2 changes: 1 addition & 1 deletion man/rborel.Rd

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

4 changes: 2 additions & 2 deletions man/rnbinom_mean_disp.Rd

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

28 changes: 28 additions & 0 deletions tests/testthat/test-borel.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
test_that("We can calculate probabilities and sample", {
expect_gt(dborel(1, 0.5), 0)
expect_identical(dborel(1, 0.5, log = TRUE), -0.5)
expect_length(rborel(2, 0.9), 2)
})

test_that("Errors are thrown", {
expect_error(
dborel(x = 0, mu = 0.5),
"is not >= 1"
)
expect_error(
dborel(x = 1, mu = -0.5),
"is not >= 0"
)
expect_error(
dborel(x = 1, mu = Inf),
"Must be finite"
)
expect_error(
rborel(n = 0, mu = -0.5),
"is not >= 1"
)
expect_error(
rborel(n = 0, mu = Inf),
"is not >= 1"
)
})
60 changes: 60 additions & 0 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
test_that("Util functions work", {
expect_length(rnbinom_mean_disp(n = 5, mn = 4, disp = 2), 5)
expect_length(rgen_length(n = 1, x = c(1, 2, 3), prob = 0.3), 3)
expect_length(rbinom_size(n = 1, x = c(1, 2, 3), prob = 0.3), 3)
jamesmbaazam marked this conversation as resolved.
Show resolved Hide resolved
expect_identical(complementary_logprob(x = 0), -Inf)
expect_identical(complementary_logprob(x = -Inf), 0)
expect_lt(complementary_logprob(x = -0.1), 0)
jamesmbaazam marked this conversation as resolved.
Show resolved Hide resolved
})

test_that("Errors are thrown", {
# Checks on 'disp' argument
expect_error(
rnbinom_mean_disp(n = 5, mn = 4, disp = 0.9),
"is not >= 1"
)
expect_error(
rnbinom_mean_disp(n = 5, mn = 4, disp = NA),
"May not be NA"
)
expect_error(
rnbinom_mean_disp(n = 5, mn = 4, disp = Inf),
"Must be finite"
)
# Checks on 'n' argument
expect_error(
rnbinom_mean_disp(n = 0, mn = 4, disp = 2),
"is not >= 1"
)
expect_error(
rnbinom_mean_disp(n = NA, mn = 4, disp = 2),
"May not be NA"
)
expect_error(
rnbinom_mean_disp(n = Inf, mn = 4, disp = 2),
"Must be finite"
)
# Checks on 'mn' argument
expect_error(
rnbinom_mean_disp(n = 2, mn = 0, disp = 2)
)
expect_error(
rnbinom_mean_disp(n = 2, mn = NA, disp = 2),
"May not be NA"
)
expect_error(
rnbinom_mean_disp(n = 2, mn = Inf, disp = 2),
"Must be finite"
)
})

test_that("Errors are thrown", {
expect_error(
complementary_logprob(0.1),
"is not <= 0"
)
expect_error(
complementary_logprob(Inf),
"is not <= 0"
)
})
9 changes: 0 additions & 9 deletions tests/testthat/tests-borel.r

This file was deleted.

Loading