save state: wrong idea for indices repartition
[epclust.git] / epclust / tests / testthat / test.clustering.R
index 6c94f92..7116e73 100644 (file)
@@ -19,15 +19,14 @@ test_that("computeClusters1&2 behave as expected",
                n = ndK[1] ; d = ndK[2] ; K = ndK[3]
                cs = n/K #cluster size
                Id = diag(d)
-               coefs = do.call(rbind,
-                       lapply(1:K, function(i) MASS::mvrnorm(cs, c(rep(0,(i-1)),5,rep(0,d-i)), Id)))
+               coefs = sapply(1:K, function(i) MASS::mvrnorm(cs, c(rep(0,(i-1)),5,rep(0,d-i)), Id))
                indices_medoids1 = computeClusters1(coefs, K, verbose=TRUE)
                indices_medoids2 = computeClusters2(dist(coefs), K, verbose=TRUE)
                # Get coefs assignments (to medoids)
                assignment1 = sapply(seq_len(n), function(i)
-                       which.min( rowSums( sweep(coefs[indices_medoids1,],2,coefs[i,],'-')^2 ) ) )
+                       which.min( colSums( sweep(coefs[,indices_medoids1],1,coefs[,i],'-')^2 ) ) )
                assignment2 = sapply(seq_len(n), function(i)
-                       which.min( rowSums( sweep(coefs[indices_medoids2,],2,coefs[i,],'-')^2 ) ) )
+                       which.min( colSums( sweep(coefs[,indices_medoids2],1,coefs[,i],'-')^2 ) ) )
                for (i in 1:K)
                {
                        expect_equal(sum(assignment1==i), cs, tolerance=5)
@@ -70,19 +69,19 @@ test_that("computeSynchrones behave as expected",
        #sum((s1-s3)^2) == 58
        #sum((s2-s3)^2) == 38
        s = list(s1, s2, s3)
-       series = matrix(nrow=n, ncol=L)
+       series = matrix(nrow=L, ncol=n)
        for (i in seq_len(n))
-               series[i,] = s[[I(i,K)]] + rnorm(L,sd=0.01)
+               series[,i] = s[[I(i,K)]] + rnorm(L,sd=0.01)
        getRefSeries = function(indices) {
                indices = indices[indices <= n]
-               if (length(indices)>0) series[indices,] else NULL
+               if (length(indices)>0) series[,indices] else NULL
        }
-       synchrones = computeSynchrones(bigmemory::as.big.matrix(rbind(s1,s2,s3)), getRefSeries,
+       synchrones = computeSynchrones(bigmemory::as.big.matrix(cbind(s1,s2,s3)), getRefSeries,
                n, 100, verbose=TRUE, parll=FALSE)
 
-       expect_equal(dim(synchrones), c(K,L))
+       expect_equal(dim(synchrones), c(L,K))
        for (i in 1:K)
-               expect_equal(synchrones[i,], s[[i]], tolerance=0.01)
+               expect_equal(synchrones[,i], s[[i]], tolerance=0.01)
 })
 
 # NOTE: medoids can be a big.matrix
@@ -93,7 +92,7 @@ computeDistortion = function(series, medoids)
        if (bigmemory::is.big.matrix(medoids))
                medoids = medoids[,]
        for (i in seq_len(n))
-               distortion = distortion + min( rowSums( sweep(medoids,2,series[i,],'-')^2 ) / L )
+               distortion = distortion + min( colSums( sweep(medoids,1,series[,i],'-')^2 ) / L )
        distortion / n
 }
 
@@ -106,23 +105,23 @@ test_that("clusteringTask1 behave as expected",
        s = lapply( seq_len(K1), function(i) x^(1+i/30)*cos(x+i) )
        series = matrix(nrow=n, ncol=L)
        for (i in seq_len(n))
-               series[i,] = s[[I(i,K1)]] + rnorm(L,sd=0.01)
+               series[,i] = s[[I(i,K1)]] + rnorm(L,sd=0.01)
        getSeries = function(indices) {
                indices = indices[indices <= n]
-               if (length(indices)>0) series[indices,] else NULL
+               if (length(indices)>0) series[,indices] else NULL
        }
        wf = "haar"
        ctype = "absolute"
-       getContribs = function(indices) curvesToContribs(series[indices,],wf,ctype)
+       getContribs = function(indices) curvesToContribs(series[,indices],wf,ctype)
        indices1 = clusteringTask1(1:n, getContribs, K1, 75, verbose=TRUE, parll=FALSE)
        medoids_K1 = getSeries(indices1)
 
-       expect_equal(dim(medoids_K1), c(K1,L))
+       expect_equal(dim(medoids_K1), c(L,K1))
        # Not easy to evaluate result: at least we expect it to be better than random selection of
        # medoids within initial series
        distorGood = computeDistortion(series, medoids_K1)
        for (i in 1:3)
-               expect_lte( distorGood, computeDistortion(series,series[sample(1:n, K1),]) )
+               expect_lte( distorGood, computeDistortion(series,series[,sample(1:n, K1)]) )
 })
 
 test_that("clusteringTask2 behave as expected",
@@ -139,19 +138,18 @@ test_that("clusteringTask2 behave as expected",
                series[i,] = s[[I(i,K1)]] + rnorm(L,sd=0.01)
        getRefSeries = function(indices) {
                indices = indices[indices <= n]
-               if (length(indices)>0) series[indices,] else NULL
+               if (length(indices)>0) series[,indices] else NULL
        }
        # Artificially simulate 60 medoids - perfect situation, all equal to one of the refs
-       medoids_K1 = bigmemory::as.big.matrix(
-               do.call(rbind, lapply( 1:K1, function(i) s[[I(i,K1)]] ) ) )
+       medoids_K1 = bigmemory::as.big.matrix( sapply( 1:K1, function(i) s[[I(i,K1)]] ) )
        medoids_K2 = clusteringTask2(medoids_K1, K2, getRefSeries, n, 75, verbose=TRUE, parll=FALSE)
 
-       expect_equal(dim(medoids_K2), c(K2,L))
+       expect_equal(dim(medoids_K2), c(L,K2))
        # Not easy to evaluate result: at least we expect it to be better than random selection of
        # medoids within 1...K1 (among references)
        distorGood = computeDistortion(series, medoids_K2)
        for (i in 1:3)
-               expect_lte( distorGood, computeDistortion(series,medoids_K1[sample(1:K1, K2),]) )
+               expect_lte( distorGood, computeDistortion(series,medoids_K1[,sample(1:K1, K2)]) )
 })
 
 #NOTE: rather redundant test