Switch to unified view

a b/tests/testthat/test-tuneCluster.block.spls.R
1
context("tuneCluster.block.spls")
2
3
demo <- suppressMessages(get_demo_cluster())
4
X <- list(X = demo$X, Z = demo$Z)
5
Y <- demo$Y
6
test.list.keepX <- list("X" = c(5,10,15), "Z" = c(2,4,6))
7
test.keepY <- c(2:5)
8
9
#tune.block.spls <- tuneCluster.block.spls(X = X, Y = Y, test.list.keepX = test.list.keepX, test.keepY = test.keepY, mode = "canonical")
10
11
test_that("tuneCluster.block.spls failed on invalid input - X", {
12
    #-- X  // list of data.frame/matrix
13
    expect_error(tuneCluster.block.spls(X = demo$X), "X must be a list of matrix/data.frame", fixed = TRUE)
14
    expect_error(tuneCluster.block.spls(X = NA), "X must be a list of matrix/data.frame", fixed = TRUE)
15
    expect_error(tuneCluster.block.spls(X = NULL), "X must be a list of matrix/data.frame", fixed = TRUE)
16
    expect_error(tuneCluster.block.spls(X = "ah!"), "X must be a list of matrix/data.frame", fixed = TRUE)
17
}) 
18
19
test_that("tuneCluster.block.spls failed on invalid input - Y", {   
20
    #-- Y  // NULL or matrix
21
    expect_error(tuneCluster.block.spls(X = X, Y = ""), "Y must be a numeric matrix/data.frame", fixed = TRUE)
22
    expect_error(tuneCluster.block.spls(X = X, Y = NA), "Y must be a numeric matrix/data.frame", fixed = TRUE)
23
}) 
24
25
test_that("tuneCluster.block.spls failed on invalid input - indY", {   
26
    #-- indY  // if Y is NULL, numeric
27
    expect_error(tuneCluster.block.spls(X = X, Y = NULL), "'indY' must be a numeric value lower or equal to 2, the number of blocks in X.", fixed = TRUE)
28
    expect_error(tuneCluster.block.spls(X = X, indY = 3), "'indY' must be a numeric value lower or equal to 2, the number of blocks in X.", fixed = TRUE)
29
    expect_error(tuneCluster.block.spls(X = X, indY = ""), "'indY' must be a numeric value lower or equal to 2, the number of blocks in X.", fixed = TRUE)
30
    expect_error(tuneCluster.block.spls(X = X, indY = NULL), "'indY' must be a numeric value lower or equal to 2, the number of blocks in X.", fixed = TRUE)
31
}) 
32
33
test_that("tuneCluster.block.spls failed on invalid input - ncomp", {   
34
    #-- ncomp  // integer 
35
    expect_error(tuneCluster.block.spls(X = X, Y = Y, ncomp = NULL), "'ncomp' should be an integer between 1 and 10", fixed = TRUE)
36
    expect_error(tuneCluster.block.spls(X = X, Y = Y, ncomp = "abc"), "'ncomp' should be an integer between 1 and 10", fixed = TRUE)
37
    expect_error(tuneCluster.block.spls(X = X, Y = Y, ncomp = X), "'ncomp' should be an integer between 1 and 10", fixed = TRUE)
38
    expect_error(tuneCluster.block.spls(X = X, Y = Y, ncomp = 55), "'ncomp' should be an integer between 1 and 10", fixed = TRUE)
39
}) 
40
41
test_that("tuneCluster.block.spls failed on invalid input - test.list.keepX", {     
42
    #-- test.list.keepX  // list of integer of the same size as X
43
    expect_error(tuneCluster.block.spls(X = X, Y = Y), "'test.list.keepX' must be a list of numeric of size 2.", fixed = TRUE)
44
    expect_error(tuneCluster.block.spls(X = X, Y = Y, test.list.keepX = c()), "'test.list.keepX' must be a list of numeric of size 2.", fixed = TRUE)
45
    expect_error(tuneCluster.block.spls(X = X, Y = Y, test.list.keepX = c(1,2,3)), "'list.test.keepX' should have the same names as X", fixed = TRUE)
46
    expect_error(tuneCluster.block.spls(X = X, Y = Y, test.list.keepX = list()), "'test.list.keepX' must be a list of numeric of size 2.", fixed = TRUE)
47
    expect_error(tuneCluster.block.spls(X = X, Y = Y, test.list.keepX = list("A" = 1:3, "B"= c())), "'list.test.keepX' should have the same names as X", fixed = TRUE)
48
    expect_error(tuneCluster.block.spls(X = X, Y = Y, test.list.keepX = list("X" = 1:3, "Z"= c(4,100))), "'test.list.keepX[[2]] sould be lower than 50, ncol(X[[2]]).", fixed = TRUE)
49
}) 
50
51
test_that("tuneCluster.block.spls failed on invalid input - test.keepY", {      
52
    #-- test.keepY  // vector of size 
53
    expect_error(tuneCluster.block.spls(X = X, Y = Y, test.list.keepX = list("X" = 1:3, "Z"= c(4,5)), test.keepY = ""), "'test.keepY' should be numeric")
54
    expect_error(tuneCluster.block.spls(X = X, Y = Y, test.list.keepX = list("X" = 1:3, "Z"= c(4,5)), test.keepY = list()), "'test.keepY' should be numeric")
55
    expect_error(tuneCluster.block.spls(X = X, Y = Y, test.list.keepX = list("X" = 1:3, "Z"= c(4,5)), test.keepY = NA), "'test.keepY' should be numeric")
56
})
57
    
58
test_that("tuneCluster.block.spls works", {
59
    expect_is(suppressWarnings(tuneCluster.block.spls(X=X, Y=Y, test.list.keepX = list("X" = 1:3, "Z"= c(4,5)), test.keepY = c(5))),"block.spls.tune.silhouette")
60
    expect_is(suppressWarnings(tuneCluster.block.spls(X=X, Y=Y, ncomp = 3, test.list.keepX = list("X" = 3, "Z"= c(4,5)), test.keepY = c(5))),"block.spls.tune.silhouette")
61
    expect_is(suppressWarnings(tuneCluster.block.spls(X=list(X=demo$X, Y=demo$Y, Z=demo$Z), indY=2, ncomp = 2, test.list.keepX = list("X" = 1:3, "Z"= c(4,5), "Y"= 2))),"block.spls.tune.silhouette")
62
})