fixes: TODO, debug test.clustering.R and finish writing clustering.R
[epclust.git] / epclust / tests / testthat / test.computeMedoidsIndices.R
index be5b2b4..efd6af9 100644 (file)
@@ -1,42 +1,24 @@
 context("computeMedoidsIndices")
 
-test_that("serialization + getDataInFile retrieve original data / from matrix",
+test_that("computeMedoidsIndices behave as expected",
 {
-       data_bin_file = "/tmp/epclust_test_m.bin"
-       unlink(data_bin_file)
+       # Generate a gaussian mixture
+       n = 999
+       L = 7
+       medoids = cbind( rep(0,L), rep(-5,L), rep(5,L) )
+       # short series...
+       series = t( rbind( MASS::mvrnorm(n/3, medoids[,1], diag(L)),
+               MASS::mvrnorm(n/3, medoids[,2], diag(L),
+               MASS::mvrnorm(n/3, medoids[,3], diag(L))) ) )
 
-       #dataset 200 lignes / 30 columns
-       data_ascii = matrix(runif(200*30,-10,10),ncol=30)
-       nbytes = 4 #lead to a precision of 1e-7 / 1e-8
-       endian = "little"
+       # With high probability, medoids indices should resemble 1,1,1,...,2,2,2,...,3,3,3,...
+       mi = epclust:::.computeMedoidsIndices(medoids, series)
+       mi_ref = rep(1:3, each=n/3)
+       expect_that( mean(mi != mi_ref) < 0.01 )
 
-       #Simulate serialization in one single call
-       binarize(data_ascii, data_bin_file, 500, ",", nbytes, endian)
-       expect_equal(file.info(data_bin_file)$size, length(data_ascii)*nbytes+8)
-       for (indices in list(c(1,3,5), 3:13, c(5,20,50), c(75,130:135), 196:200))
-       {
-               data_lines = getDataInFile(indices, data_bin_file, nbytes, endian)
-               expect_equal(data_lines, data_ascii[indices,], tolerance=1e-6)
-       }
-       unlink(data_bin_file)
-
-       #...in several calls (last call complete, next call NULL)
-       for (i in 1:20)
-               binarize(data_ascii[((i-1)*10+1):(i*10),], data_bin_file, 20, ",", nbytes, endian)
-       expect_equal(file.info(data_bin_file)$size, length(data_ascii)*nbytes+8)
-       for (indices in list(c(1,3,5), 3:13, c(5,20,50), c(75,130:135), 196:200))
-       {
-               data_lines = getDataInFile(indices, data_bin_file, nbytes, endian)
-               expect_equal(data_lines, data_ascii[indices,], tolerance=1e-6)
-       }
-       unlink(data_bin_file)
+       # Now with a random matrix, compare with (trusted) R version
+       series = matrix(runif(n*L, min=-7, max=7), nrow=L)
+       mi = epclust:::.computeMedoidsIndices(medoids, series)
+       mi_ref = R_computeMedoidsIndices(medoids, series)
+       expect_equal(mi, mi_ref)
 })
-
-TODO: test computeMedoids + filter
-#              #R-equivalent, requiring a matrix (thus potentially breaking "fit-in-memory" hope)
-#              mat_meds = medoids[,]
-#              mi = rep(NA,nb_series)
-#              for (i in 1:nb_series)
-#                      mi[i] <- which.min( rowSums( sweep(mat_meds, 2, ref_series[i,], '-')^2 ) )
-#              rm(mat_meds); gc()
-