db139edefa31db83bc67c02f00110df4f8209ec9
[epclust.git] / epclust / tests / testthat / test-computeSynchrones.R
1 context("computeSynchrones")
2
3 test_that("computeSynchrones behave as expected",
4 {
5 # Generate 300 sinusoïdal series of 3 kinds: all series of indices == 0 mod 3 are the same
6 # (plus noise), all series of indices == 1 mod 3 are the same (plus noise) ...
7 n = 300
8 x = seq(0,9.5,0.1)
9 L = length(x) #96 1/4h
10 K = 3
11 s1 = cos(x)
12 s2 = sin(x)
13 s3 = c( s1[1:(L%/%2)] , s2[(L%/%2+1):L] )
14 #sum((s1-s2)^2) == 96
15 #sum((s1-s3)^2) == 58
16 #sum((s2-s3)^2) == 38
17 s = list(s1, s2, s3)
18 series = matrix(nrow=L, ncol=n)
19 for (i in seq_len(n))
20 series[,i] = s[[I(i,K)]] + rnorm(L,sd=0.01)
21
22 getRefSeries = function(indices) {
23 indices = indices[indices <= n]
24 if (length(indices)>0) as.matrix(series[,indices]) else NULL
25 }
26
27 synchrones = computeSynchrones(bigmemory::as.big.matrix(cbind(s1,s2,s3)), getRefSeries,
28 n, 100, verbose=TRUE, parll=FALSE)
29
30 expect_equal(dim(synchrones), c(L,K))
31 for (i in 1:K)
32 {
33 # Synchrones are (for each medoid) sums of closest curves.
34 # Here, we expect exactly 100 curves of each kind to be assigned respectively to
35 # synchrone 1, 2 and 3 => division by 100 should be very close to the ref curve
36 expect_equal(synchrones[,i]/100, s[[i]], tolerance=0.01)
37 }
38 })