Commit | Line | Data |
---|---|---|
cbd88fe5 BA |
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) | |
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 | matrices_permut = .generateMatrices(d,K,N,noise=FALSE) | |
27 | ||
28 | # 2] Call align function with mode=approx1 | |
29 | matrices_aligned = | |
30 | alignMatrices(matrices_permut[2:N], ref=matrices_permut[[1]], ls_mode="approx1") | |
31 | ||
32 | # 3] Check alignment | |
33 | for (j in 2:N) | |
34 | expect_equal(matrices_aligned[[j-1]], matrices_permut[[1]]) | |
35 | ||
36 | # 2bis] Call align function with mode=approx2 | |
37 | matrices_aligned = | |
38 | alignMatrices(matrices_permut[2:N], ref=matrices_permut[[1]], ls_mode="approx2") | |
39 | ||
40 | # 3bis] Check alignment | |
41 | for (j in 2:N) | |
42 | expect_equal(matrices_aligned[[j-1]], matrices_permut[[1]]) | |
43 | } | |
44 | }) | |
45 | ||
46 | test_that("labelSwitchingAlign correctly aligns noisy parameters", | |
47 | { | |
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 ? | |
55 | ||
56 | # 1] Generate matrix series | |
57 | matrices_permut = .generateMatrices(d,K,N,noise=TRUE) | |
58 | ||
59 | # 2] Call align function | |
60 | matrices_aligned = alignMatrices(matrices_permut, ref="mean", ls_mode="exact") | |
61 | ||
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 | } | |
69 | }) |