update
[morpheus.git] / pkg / tests / testthat / test-alignMatrices.R
CommitLineData
cbd88fe5
BA
1# Helper to generate a random series of matrices to align
2.generateMatrices = function(d, K, N, noise)
3{
6dd5c2ac 4 matrices = list( matrix(runif(d*K, min=-1, max=1),ncol=K) ) #reference
2b3a6af5 5 for (i in 2:(N+1))
6dd5c2ac
BA
6 {
7 matrices[[i]] <- matrices[[1]][,sample(1:K)]
8 if (noise)
9 matrices[[i]] = matrices[[i]] + matrix(rnorm(d*K, sd=0.05), ncol=K)
10 }
11 matrices
cbd88fe5
BA
12}
13
14test_that("labelSwitchingAlign correctly aligns de-noised parameters",
15{
2b3a6af5
BA
16 N <- 30 #number of matrices
17 d_K_list <- list(c(2,2), c(5,3))
6dd5c2ac
BA
18 for (i in 1:2)
19 {
2b3a6af5
BA
20 d <- d_K_list[[i]][1]
21 K <- d_K_list[[i]][2]
cbd88fe5 22
6dd5c2ac 23 # 1] Generate matrix series
2b3a6af5 24 Ms <- .generateMatrices(d, K, N, noise=FALSE)
cbd88fe5 25
6dd5c2ac 26 # 2] Call align function with mode=approx1
2b3a6af5 27 aligned <- alignMatrices(Ms[2:(N+1)], ref=Ms[[1]], ls_mode="approx1")
cbd88fe5 28
6dd5c2ac 29 # 3] Check alignment
2b3a6af5
BA
30 for (j in 1:N)
31 expect_equal(aligned[[j]], Ms[[1]])
cbd88fe5 32
6dd5c2ac 33 # 2bis] Call align function with mode=approx2
2b3a6af5 34 aligned <- alignMatrices(Ms[2:(N+1)], ref=Ms[[1]], ls_mode="approx2")
cbd88fe5 35
6dd5c2ac 36 # 3bis] Check alignment
2b3a6af5
BA
37 for (j in 1:N)
38 expect_equal(aligned[[j]], Ms[[1]])
6dd5c2ac 39 }
cbd88fe5
BA
40})
41
42test_that("labelSwitchingAlign correctly aligns noisy parameters",
43{
2b3a6af5
BA
44 N <- 30 #number of matrices
45 d_K_list <- list(c(2,2), c(5,3))
6dd5c2ac
BA
46 for (i in 1:2)
47 {
2b3a6af5
BA
48 d <- d_K_list[[i]][1]
49 K <- d_K_list[[i]][2]
50 max_error <- d * 0.2 #TODO: what value to choose ?
cbd88fe5 51
6dd5c2ac 52 # 1] Generate matrix series
2b3a6af5 53 Ms <- .generateMatrices(d, K, N, noise=TRUE)
cbd88fe5 54
6dd5c2ac 55 # 2] Call align function
2b3a6af5 56 aligned <- alignMatrices(Ms[2:(N+1)], ref=Ms[[1]], ls_mode="exact")
cbd88fe5 57
6dd5c2ac
BA
58 # 3] Check alignment
59 for (j in 2:N)
ab35f610 60 expect_lt( norm(aligned[[j]] - Ms[[1]]), max_error )
6dd5c2ac 61 }
cbd88fe5 62})