Skip to content

Commit

Permalink
Fix to issue #182 (#183)
Browse files Browse the repository at this point in the history
refactor: cleaned output of `test_that` tests to increase ease of debugging
  • Loading branch information
Max-Bladen authored Sep 15, 2022
1 parent 2a828e3 commit cf4f6a5
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 9 deletions.
11 changes: 11 additions & 0 deletions tests/testthat/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,14 @@
require(testthat)
expect_equal(round(numeric_value, digits = digits), round(expected, digits = digits))
}

#' Prevent calls to cat() and print() from showing
#'
#' @param x an R object
#' @keywords internal
#' @return x, the inputted R object
.quiet <- function(x) {
sink(tempfile())
on.exit(sink())
invisible(force(x))
}
3 changes: 2 additions & 1 deletion tests/testthat/test-auroc.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
context("auroc")

test_that("auroc works", {

data(breast.tumors)
set.seed(1)
test=sample(1:47,5,replace=FALSE)
Expand All @@ -11,7 +12,7 @@ test_that("auroc works", {

res.plsda <- plsda(X, Y, ncomp = 2)

auc.plsda=auroc(res.plsda,plot = TRUE,roc.comp = 1)
auc.plsda=.quiet(auroc(res.plsda,plot = TRUE,roc.comp = 1))

expect_equal(matrix(auc.plsda$Comp1),
rbind(0.863, 2.473e-05))
Expand Down
6 changes: 4 additions & 2 deletions tests/testthat/test-circsPlot.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ test_that("circosPlot works with similar feature names in different blocks", cod
x
})
}



data("breast.TCGA")
data = list(mrna = breast.TCGA$data.train$mrna,
mirna = breast.TCGA$data.train$mirna,
Expand All @@ -37,8 +40,7 @@ test_that("circosPlot works with similar feature names in different blocks", cod
list.keepX = list(mrna = rep(20, 2), mirna = rep(10,2), protein = rep(10, 2))
TCGA.block.splsda = block.splsda(X = data, Y = breast.TCGA$data.train$subtype,
ncomp = 2, keepX = list.keepX, design = 'full')
cp_res <- circosPlot(TCGA.block.splsda, cutoff = 0.7)

cp_res <- .quiet(circosPlot(TCGA.block.splsda, cutoff = 0.7))
expect_is(cp_res, "matrix")
})

Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-perf.diablo.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ test_that("perf.diablo works with and without parallel processing and with auroc

RNGversion(.mixo_rng()) ## in case RNG changes!
set.seed(100)
perf.res12 = perf.sgccda(nutrimouse.sgccda, folds = folds, nrepeat = nrep, auc = TRUE, progressBar = TRUE)
perf.res12 = perf.sgccda(nutrimouse.sgccda, folds = folds, nrepeat = nrep, auc = TRUE, progressBar = FALSE)
choices <- unname(perf.res12$choice.ncomp$AveragedPredict[,1])
expect_equal(choices, c(2,2))
aucs <- round(unname(perf.res12$auc$comp1[,1]), 2)
Expand All @@ -38,7 +38,7 @@ test_that("perf.diablo works with and without parallel processing and with auroc
# by listening to ... in perf for seed. Results are different even with seeds but reproducible with same cpus
# the hassle of making it fully reproducible is a bit too arduous

perf.res42 = perf(nutrimouse.sgccda, folds = folds, nrepeat = nrep, auc = TRUE, cpus = 2, seed = 100, progressBar = TRUE)
perf.res42 = perf(nutrimouse.sgccda, folds = folds, nrepeat = nrep, auc = TRUE, cpus = 2, seed = 100, progressBar = FALSE)
choices <- unname(perf.res42$choice.ncomp$AveragedPredict[,1])
expect_equal(choices, c(1,1))
aucs <- round(unname(perf.res42$auc$comp1[,1]), 2)
Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/test-tune.spls.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ test_that("tune.spls works with and without parallel", code = {
RNGversion(.mixo_rng()) ## in case RNG changes!

## ----------- tune.spls works fine -----------
set.seed(42)
set.seed(12)
tune.spls11 <-
tune.spls(
X = X,
Expand All @@ -25,11 +25,11 @@ test_that("tune.spls works with and without parallel", code = {
nrepeat = nrepeat
)
expect_is(tune.spls11, "tune.spls")
expect_equal(c(comp1 = 5, comp2 = 10), tune.spls11$choice.keepX)
expect_equal(c(comp1 = 5, comp2 = 10), tune.spls11$choice.keepY)
expect_equal(c(comp1 = 10, comp2 = 5), tune.spls11$choice.keepX)
expect_equal(c(comp1 = 5, comp2 = 5), tune.spls11$choice.keepY)

## ----------- tune(method="spls") same as tune.spls -----------
set.seed(42)
set.seed(12)
tune.spls12 <-
tune(
method = "spls",
Expand Down

0 comments on commit cf4f6a5

Please sign in to comment.