Simplify plots: version OK with R6 classes
[talweg.git] / pkg / tests / testthat / test.computeFilaments.R
1 context("Check that computeFilaments behaves as expected")
2
3 #shorthand: map 1->1, 2->2, 3->3, 4->1, ..., 149->2, 150->3
4 I = function(i)
5 (i-1) %% 3 + 1
6
7 #MOCK data; NOTE: could be in inst/testdata as well
8 getDataTest = function(n)
9 {
10 data = Data$new()
11 x = seq(0,10,0.1)
12 L = length(x)
13 s1 = cos(x)
14 s2 = sin(x)
15 s3 = c( s1[1:(L%/%2)] , s2[(L%/%2+1):L] )
16 #sum((s1-s2)^2) == 97.59381
17 #sum((s1-s3)^2) == 57.03051
18 #sum((s2-s3)^2) == 40.5633
19 s = list( s1, s2, s3 )
20 series = list()
21 for (i in seq_len(n))
22 {
23 serie = s[[I(i)]] + rnorm(L,sd=0.01)
24 level = mean(serie)
25 serie = serie - level
26 # 10 series with NAs for index 2
27 if (I(i) == 2 && i >= 60 && i<= 90)
28 serie[sample(seq_len(L),1)] = NA
29 data$append(c(), serie, level, c(), c()) #no need for more
30 }
31 data
32 }
33
34 test_that("output is as expected on simulated series",
35 {
36 data = getDataTest(150)
37
38 # index 143 : serie type 2
39 f = computeFilaments(data, 143, limit=60, plot=FALSE)
40
41 # Expected output: 50-3-10 series of type 2, then 23 series of type 3 (closest next)
42 expect_identical(length(f$neighb_indices), as.integer(60))
43 expect_identical(length(f$colors), as.integer(60))
44 expect_equal(f$index, 143)
45 expect_true(all(I(f$neighb_indices) >= 2))
46 for (i in 1:37)
47 {
48 expect_equal(I(f$neighb_indices[i]), 2)
49 expect_match(f$colors[i], f$colors[1])
50 }
51 for (i in 38:60)
52 {
53 expect_equal(I(f$neighb_indices[i]), 3)
54 expect_match(f$colors[i], f$colors[38])
55 }
56 expect_match(f$colors[1], "#1*")
57 expect_match(f$colors[38], "#E*")
58
59 # index 142 : serie type 1
60 f = computeFilaments(data, 142, limit=50, plot=FALSE)
61
62 # Expected output: 50-10-3 series of type 1, then 13 series of type 3 (closest next)
63 # NOTE: -10 because only past days with no-NAs tomorrow => exclude type 1 in [60,90[
64 expect_identical(length(f$neighb_indices), as.integer(50))
65 expect_identical(length(f$colors), as.integer(50))
66 expect_equal(f$index, 142)
67 expect_true(all(I(f$neighb_indices) != 2))
68 for (i in 1:37)
69 {
70 expect_equal(I(f$neighb_indices[i]), 1)
71 expect_match(f$colors[i], f$colors[1])
72 }
73 for (i in 38:50)
74 {
75 expect_equal(I(f$neighb_indices[i]), 3)
76 expect_match(f$colors[i], f$colors[38])
77 }
78 expect_match(f$colors[1], "#1*")
79 expect_match(f$colors[38], "#E*")
80 })