Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hillalex committed Nov 1, 2024
1 parent 0561a36 commit cd3697c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
28 changes: 24 additions & 4 deletions tests/testthat/test-convert-log-scale.R
Original file line number Diff line number Diff line change
@@ -1,29 +1,49 @@
test_that("Can convert to and from log scale in R", {
inputs <- data.table::fread(test_path("testdata", "testdata.csv"))
log_inputs <- convert_log2_scale(inputs, "me", simplify_limits = FALSE)
unlog_inputs <- convert_log2_scale_inverse(log_inputs, "me")
log_inputs <- convert_log2_scale(inputs, "me", lower_limit = 2)
unlog_inputs <- convert_log2_scale_inverse(log_inputs, "me", lower_limit = 2)

expect_equal(inputs, unlog_inputs)
})

test_that("Can convert from log scale in R", {
inputs <- data.table::fread(test_path("testdata", "testdata.csv"))
res <- convert_log2_scale_inverse(
inputs, vars_to_transform = c("me", "lo"))
inputs,
vars_to_transform = c("me", "lo"),
lower_limit = 5)

expect_equal(res$me, 5 * 2^inputs$me)
expect_equal(res$lo, 5 * 2^inputs$lo)
expect_equal(res$hi, inputs$hi)
expect_equal(names(res), names(inputs))

# try with a different limit
res <- convert_log2_scale_inverse(
inputs,
vars_to_transform = "me",
lower_limit = 10)

expect_equal(res$me, 10 * 2^inputs$me)
})

test_that("Can convert from log scale in Cpp", {
inputs <- data.table::fread(test_path("testdata", "testdata.csv"))
rescpp <- convert_log2_scale_inverse_cpp(
inputs, vars_to_transform = c("me", "lo"))
inputs,
vars_to_transform = c("me", "lo"),
lower_limit = 5)

expect_equal(rescpp$me, 5 * 2^(inputs$me))
expect_equal(rescpp$lo, 5 * 2^(inputs$lo))
expect_equal(rescpp$hi, inputs$hi)
expect_equal(names(rescpp), names(inputs))

# try with a different limit
res <- convert_log2_scale_inverse_cpp(
inputs,
vars_to_transform = "me",
lower_limit = 10)

expect_equal(res$me, 10 * 2^inputs$me)
})
6 changes: 4 additions & 2 deletions tests/testthat/test-data.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ test_that("Can construct stan data", {
expect_equal(names(stan_data), c("N", "N_events", "id", "value", "censored",
"titre_type", "preds_sd", "K", "N_uncens", "N_lo",
"N_hi", "uncens_idx", "cens_lo_idx",
"cens_hi_idx", "t", "X", "P", "mu_t0",
"cens_hi_idx", "t", "X", "P", "upper_limit", "lower_limit", "mu_t0",
"mu_tp", "mu_ts", "mu_m1", "mu_m2", "mu_m3",
"sigma_t0", "sigma_tp", "sigma_ts", "sigma_m1", "sigma_m2",
"sigma_m3"))
Expand Down Expand Up @@ -53,7 +53,9 @@ test_that("Natural scale data is converted to log scale for stan", {
mod <- biokinetics$new(data = dat)
stan_data <- mod$get_stan_data()
expect_equal(stan_data$value,
convert_log2_scale(dat, "value")$value,
convert_log2_scale(dat,
lower_limit = 5,
vars_to_transform = "value")$value,
ignore_attr = TRUE)
})

Expand Down

0 comments on commit cd3697c

Please sign in to comment.