add comments, fix some things. TODO: comment tests, finish computeWerDists, test it
[epclust.git] / epclust / tests / testthat / test.clustering.R
CommitLineData
56857861
BA
1context("clustering")
2
56857861
BA
3test_that("computeSynchrones behave as expected",
4{
8702eb86
BA
5 n = 300
6 x = seq(0,9.5,0.1)
7 L = length(x) #96 1/4h
8 K = 3
9 s1 = cos(x)
10 s2 = sin(x)
11 s3 = c( s1[1:(L%/%2)] , s2[(L%/%2+1):L] )
12 #sum((s1-s2)^2) == 96
13 #sum((s1-s3)^2) == 58
14 #sum((s2-s3)^2) == 38
15 s = list(s1, s2, s3)
37c82bba 16 series = matrix(nrow=L, ncol=n)
8702eb86 17 for (i in seq_len(n))
37c82bba 18 series[,i] = s[[I(i,K)]] + rnorm(L,sd=0.01)
8702eb86 19 getRefSeries = function(indices) {
492cd9e7 20 indices = indices[indices <= n]
37c82bba 21 if (length(indices)>0) series[,indices] else NULL
8702eb86 22 }
37c82bba 23 synchrones = computeSynchrones(bigmemory::as.big.matrix(cbind(s1,s2,s3)), getRefSeries,
d9bb53c5 24 n, 100, verbose=TRUE, parll=FALSE)
56857861 25
37c82bba 26 expect_equal(dim(synchrones), c(L,K))
8702eb86 27 for (i in 1:K)
d9bb53c5 28 expect_equal(synchrones[,i]/100, s[[i]], tolerance=0.01)
56857861
BA
29})
30
0486fbad
BA
31# Helper function to divide indices into balanced sets
32test_that("Helper function to spread indices work properly",
8702eb86 33{
0486fbad
BA
34 indices <- 1:400
35
36 # bigger nb_per_set than length(indices)
37 expect_equal(epclust:::.spreadIndices(indices,500), list(indices))
38
39 # nb_per_set == length(indices)
40 expect_equal(epclust:::.spreadIndices(indices,400), list(indices))
41
42 # length(indices) %% nb_per_set == 0
43 expect_equal(epclust:::.spreadIndices(indices,200),
44 c( list(indices[1:200]), list(indices[201:400]) ))
45 expect_equal(epclust:::.spreadIndices(indices,100),
46 c( list(indices[1:100]), list(indices[101:200]),
47 list(indices[201:300]), list(indices[301:400]) ))
48
49 # length(indices) / nb_per_set == 1, length(indices) %% nb_per_set == 100
50 expect_equal(epclust:::.spreadIndices(indices,300), list(indices))
51 # length(indices) / nb_per_set == 2, length(indices) %% nb_per_set == 42
52 repartition <- epclust:::.spreadIndices(indices,179)
53 expect_equal(length(repartition), 2)
54 expect_equal(length(repartition[[1]]), 179 + 21)
55 expect_equal(length(repartition[[1]]), 179 + 21)
56})
8702eb86 57
e161499b 58test_that("clusteringTask1 behave as expected",
56857861 59{
8702eb86
BA
60 n = 900
61 x = seq(0,9.5,0.1)
62 L = length(x) #96 1/4h
63 K1 = 60
8702eb86 64 s = lapply( seq_len(K1), function(i) x^(1+i/30)*cos(x+i) )
0fe757f7 65 series = matrix(nrow=L, ncol=n)
8702eb86 66 for (i in seq_len(n))
37c82bba 67 series[,i] = s[[I(i,K1)]] + rnorm(L,sd=0.01)
e161499b 68 getSeries = function(indices) {
492cd9e7 69 indices = indices[indices <= n]
37c82bba 70 if (length(indices)>0) series[,indices] else NULL
8702eb86 71 }
e161499b
BA
72 wf = "haar"
73 ctype = "absolute"
37c82bba 74 getContribs = function(indices) curvesToContribs(series[,indices],wf,ctype)
0fe757f7 75 require("cluster", quietly=TRUE)
9f05a4a0 76 algoClust1 = function(contribs,K) cluster::pam(t(contribs),K,diss=FALSE)$id.med
0fe757f7 77 indices1 = clusteringTask1(1:n, getContribs, K1, algoClust1, 75, verbose=TRUE, parll=FALSE)
e161499b 78 medoids_K1 = getSeries(indices1)
56857861 79
37c82bba 80 expect_equal(dim(medoids_K1), c(L,K1))
8702eb86 81 # Not easy to evaluate result: at least we expect it to be better than random selection of
e161499b
BA
82 # medoids within initial series
83 distorGood = computeDistortion(series, medoids_K1)
8702eb86 84 for (i in 1:3)
37c82bba 85 expect_lte( distorGood, computeDistortion(series,series[,sample(1:n, K1)]) )
56857861
BA
86})
87
e161499b 88test_that("clusteringTask2 behave as expected",
56857861 89{
8702eb86
BA
90 n = 900
91 x = seq(0,9.5,0.1)
92 L = length(x) #96 1/4h
93 K1 = 60
94 K2 = 3
e161499b 95 #for (i in 1:60) {plot(x^(1+i/30)*cos(x+i),type="l",col=i,ylim=c(-50,50)); par(new=TRUE)}
8702eb86 96 s = lapply( seq_len(K1), function(i) x^(1+i/30)*cos(x+i) )
0fe757f7 97 series = matrix(nrow=L, ncol=n)
8702eb86 98 for (i in seq_len(n))
9f05a4a0 99 series[,i] = s[[I(i,K1)]] + rnorm(L,sd=0.01)
e161499b 100 getRefSeries = function(indices) {
8702eb86 101 indices = indices[indices <= n]
37c82bba 102 if (length(indices)>0) series[,indices] else NULL
8702eb86 103 }
e161499b 104 # Artificially simulate 60 medoids - perfect situation, all equal to one of the refs
37c82bba 105 medoids_K1 = bigmemory::as.big.matrix( sapply( 1:K1, function(i) s[[I(i,K1)]] ) )
9f05a4a0
BA
106 algoClust2 = function(dists,K) cluster::pam(dists,K,diss=TRUE)$id.med
107 medoids_K2 = clusteringTask2(medoids_K1, K2, algoClust2, getRefSeries,
d9bb53c5 108 n, 75, verbose=TRUE, parll=FALSE)
56857861 109
37c82bba 110 expect_equal(dim(medoids_K2), c(L,K2))
8702eb86
BA
111 # Not easy to evaluate result: at least we expect it to be better than random selection of
112 # medoids within 1...K1 (among references)
113 distorGood = computeDistortion(series, medoids_K2)
114 for (i in 1:3)
37c82bba 115 expect_lte( distorGood, computeDistortion(series,medoids_K1[,sample(1:K1, K2)]) )
56857861 116})