Revert to previous x_init settings in optimParams (keeping the initial one)
[morpheus.git] / pkg / tests / testthat / test-alignMatrices.R
CommitLineData
cbd88fe5
BA
1context("alignMatrices")
2
3# Helper to generate a random series of matrices to align
4.generateMatrices = function(d, K, N, noise)
5{
6dd5c2ac
BA
6 matrices = list( matrix(runif(d*K, min=-1, max=1),ncol=K) ) #reference
7 for (i in 2:N)
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
cbd88fe5
BA
14}
15
16test_that("labelSwitchingAlign correctly aligns de-noised parameters",
17{
6dd5c2ac
BA
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]
cbd88fe5 24
6dd5c2ac
BA
25 # 1] Generate matrix series
26 matrices_permut = .generateMatrices(d,K,N,noise=FALSE)
cbd88fe5 27
6dd5c2ac
BA
28 # 2] Call align function with mode=approx1
29 matrices_aligned =
30 alignMatrices(matrices_permut[2:N], ref=matrices_permut[[1]], ls_mode="approx1")
cbd88fe5 31
6dd5c2ac
BA
32 # 3] Check alignment
33 for (j in 2:N)
34 expect_equal(matrices_aligned[[j-1]], matrices_permut[[1]])
cbd88fe5 35
6dd5c2ac
BA
36 # 2bis] Call align function with mode=approx2
37 matrices_aligned =
38 alignMatrices(matrices_permut[2:N], ref=matrices_permut[[1]], ls_mode="approx2")
cbd88fe5 39
6dd5c2ac
BA
40 # 3bis] Check alignment
41 for (j in 2:N)
42 expect_equal(matrices_aligned[[j-1]], matrices_permut[[1]])
43 }
cbd88fe5
BA
44})
45
46test_that("labelSwitchingAlign correctly aligns noisy parameters",
47{
6dd5c2ac
BA
48 N = 30 #number of matrices
49 d_K_list = list(c(2,2), c(5,3))
50 for (i in 1:2)
51 {
52 d = d_K_list[[i]][1]
53 K = d_K_list[[i]][2]
54 max_error = d * 0.2 #TODO: what value to choose ?
cbd88fe5 55
6dd5c2ac
BA
56 # 1] Generate matrix series
57 matrices_permut = .generateMatrices(d,K,N,noise=TRUE)
cbd88fe5 58
6dd5c2ac
BA
59 # 2] Call align function
60 matrices_aligned = alignMatrices(matrices_permut, ref="mean", ls_mode="exact")
cbd88fe5 61
6dd5c2ac
BA
62 # 3] Check alignment
63 for (j in 2:N)
64 {
65 expect_that( norm(matrices_aligned[[j]] - matrices_permut[[1]]),
66 is_less_than(max_error) )
67 }
68 }
cbd88fe5 69})