Skip to content

Commit

Permalink
less repetition in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bcjaeger committed Oct 21, 2023
1 parent d476280 commit 7df5f53
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 23 deletions.
6 changes: 3 additions & 3 deletions tests/testthat/test-find_cutpoints.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ test_that(
y <- mat_list_surv[[i]]$y
w <- mat_list_surv[[i]]$w

if(nrow(y) > 250) {y <- y[1:250, ]; w <- w[1:250]}
if(nrow(y) > 100) {y <- y[1:100, ]; w <- w[1:100]}

for(cp_type in c("ctns", "bnry", "catg")){

Expand All @@ -22,9 +22,9 @@ test_that(
xb_uni <- unique(xb)
# leaf_min_events <- 5
# leaf_min_obs <- 10
for(leaf_min_events in c(1, 5, 10)){
for(leaf_min_events in c(1, 5)){

for(leaf_min_obs in c(leaf_min_events + c(0, 5, 10))){
for(leaf_min_obs in c(leaf_min_events + c(0, 5))){

cp_stats <- cp_find_bounds_R(y, w, xb, xb_uni, leaf_min_events, leaf_min_obs)

Expand Down
33 changes: 13 additions & 20 deletions tests/testthat/test-orsf.R
Original file line number Diff line number Diff line change
Expand Up @@ -375,31 +375,24 @@ test_that(
# testing the seed behavior when no_fit is TRUE. You should get the same
# forest whether you train with orsf() or with orsf_train().

for(.n_tree in c(100, 250, 1000)){
object <- orsf(pbc, Surv(time, status) ~ .,
n_tree = n_tree_test, no_fit = TRUE,
importance = 'anova')
set.seed(89)
time_estimated <- orsf_time_to_train(object, n_tree_subset = 1)

object <- orsf(pbc_orsf, Surv(time, status) ~ . - id,
n_tree = .n_tree, no_fit = TRUE,
importance = 'anova')
set.seed(89)
time_estimated <- orsf_time_to_train(object, n_tree_subset = 50)
set.seed(89)
time_true_start <- Sys.time()
fit_orsf_3 <- orsf_train(object)
time_true_stop <- Sys.time()

set.seed(89)
time_true_start <- Sys.time()
fit_orsf_3 <- orsf_train(object)
time_true_stop <- Sys.time()
time_true <- time_true_stop - time_true_start

time_true <- time_true_stop - time_true_start
diff_abs <- abs(as.numeric(time_true - time_estimated))

diff_abs <- abs(as.numeric(time_true - time_estimated))
diff_rel <- diff_abs / as.numeric(time_true)
# estimated time is within 5 seconds of true time.
expect_lt(diff_abs, 5)

# expect the difference between estimated and true time is < 5 second.
expect_lt(diff_abs, 5)
# expect that the difference is not greater than 5x the
# magnitude of the actual time it took to fit the forest
expect_lt(diff_rel, 5)

}
}
)

Expand Down

0 comments on commit 7df5f53

Please sign in to comment.