X-Git-Url: https://git.auder.net/game/current/gitweb.css?a=blobdiff_plain;f=epclust%2Ftests%2Ftestthat%2Ftest-clustering.R;fp=epclust%2Ftests%2Ftestthat%2Ftest.clustering.R;h=fa22dff3cae04d3f605f6f2d5c3a01fff76e6f34;hb=40f12a2f66d06fd77183ea02b996f5c66f90761c;hp=2f24d0844695aa6f65fdfb3bf6a7b44be9ffab32;hpb=a52836b23adb4bfa6722642ec6426fb7b5f39650;p=epclust.git diff --git a/epclust/tests/testthat/test.clustering.R b/epclust/tests/testthat/test-clustering.R similarity index 55% rename from epclust/tests/testthat/test.clustering.R rename to epclust/tests/testthat/test-clustering.R index 2f24d08..fa22dff 100644 --- a/epclust/tests/testthat/test.clustering.R +++ b/epclust/tests/testthat/test-clustering.R @@ -1,68 +1,5 @@ context("clustering") -test_that("computeSynchrones behave as expected", -{ - # Generate 300 sinusoïdal series of 3 kinds: all series of indices == 0 mod 3 are the same - # (plus noise), all series of indices == 1 mod 3 are the same (plus noise) ... - n = 300 - x = seq(0,9.5,0.1) - L = length(x) #96 1/4h - K = 3 - s1 = cos(x) - s2 = sin(x) - s3 = c( s1[1:(L%/%2)] , s2[(L%/%2+1):L] ) - #sum((s1-s2)^2) == 96 - #sum((s1-s3)^2) == 58 - #sum((s2-s3)^2) == 38 - s = list(s1, s2, s3) - series = matrix(nrow=L, ncol=n) - for (i in seq_len(n)) - series[,i] = s[[I(i,K)]] + rnorm(L,sd=0.01) - - getRefSeries = function(indices) { - indices = indices[indices <= n] - if (length(indices)>0) as.matrix(series[,indices]) else NULL - } - - synchrones = computeSynchrones(bigmemory::as.big.matrix(cbind(s1,s2,s3)), getRefSeries, - n, 100, verbose=TRUE, parll=FALSE) - - expect_equal(dim(synchrones), c(L,K)) - for (i in 1:K) - { - # Synchrones are (for each medoid) sums of closest curves. - # Here, we expect exactly 100 curves of each kind to be assigned respectively to - # synchrone 1, 2 and 3 => division by 100 should be very close to the ref curve - expect_equal(synchrones[,i]/100, s[[i]], tolerance=0.01) - } -}) - -test_that("Helper function to spread indices work properly", -{ - indices <- 1:400 - - # bigger nb_per_set than length(indices) - expect_equal(epclust:::.spreadIndices(indices,500), list(indices)) - - # nb_per_set == length(indices) - expect_equal(epclust:::.spreadIndices(indices,400), list(indices)) - - # length(indices) %% nb_per_set == 0 - expect_equal(epclust:::.spreadIndices(indices,200), - c( list(indices[1:200]), list(indices[201:400]) )) - expect_equal(epclust:::.spreadIndices(indices,100), - c( list(indices[1:100]), list(indices[101:200]), - list(indices[201:300]), list(indices[301:400]) )) - - # length(indices) / nb_per_set == 1, length(indices) %% nb_per_set == 100 - expect_equal(epclust:::.spreadIndices(indices,300), list(indices)) - # length(indices) / nb_per_set == 2, length(indices) %% nb_per_set == 42 - repartition <- epclust:::.spreadIndices(indices,179) - expect_equal(length(repartition), 2) - expect_equal(length(repartition[[1]]), 179 + 21) - expect_equal(length(repartition[[1]]), 179 + 21) -}) - test_that("clusteringTask1 behave as expected", { # Generate 60 reference sinusoïdal series (medoids to be found), @@ -83,7 +20,7 @@ test_that("clusteringTask1 behave as expected", wf = "haar" ctype = "absolute" - getContribs = function(indices) curvesToContribs(series[,indices],wf,ctype) + getContribs = function(indices) curvesToContribs(as.matrix(series[,indices]),wf,ctype) require("cluster", quietly=TRUE) algoClust1 = function(contribs,K) cluster::pam(t(contribs),K,diss=FALSE)$id.med @@ -135,3 +72,18 @@ test_that("clusteringTask2 behave as expected", for (i in 1:3) expect_lte( distor_good, computeDistortion(synchrones, synchrones[,sample(1:K1,3)]) ) }) + +# Compute the sum of (normalized) sum of squares of closest distances to a medoid. +# Note: medoids can be a big.matrix +computeDistortion = function(series, medoids) +{ + if (bigmemory::is.big.matrix(medoids)) + medoids = medoids[,] #extract standard matrix + + n = ncol(series) ; L = nrow(series) + distortion = 0. + for (i in seq_len(n)) + distortion = distortion + min( colSums( sweep(medoids,1,series[,i],'-')^2 ) / L ) + + sqrt( distortion / n ) +}