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