e8f87521e13d14dcb3108e6197b99dfe8a040dd2
[talweg.git] / pkg / tests / testthat / test.computeFilaments.R
1 context("Check that computeFilaments behaves as expected")
2
3 getDataTest = function(n, shift)
4 {
5 x = seq(0,10,0.1)
6 L = length(x)
7 s1 = cos(x)
8 s2 = sin(x)
9 s3 = c( s1[1:(L%/%2)] , s2[(L%/%2+1):L] )
10 #sum((s1-s2)^2) == 97.59381
11 #sum((s1-s3)^2) == 57.03051
12 #sum((s2-s3)^2) == 40.5633
13 s = list( s1, s2, s3 )
14 series = list()
15 for (i in seq_len(n))
16 {
17 index = (i%%3) + 1
18 level = mean(s[[index]])
19 serie = s[[index]] - level + rnorm(L,sd=0.05)
20 # 10 series with NAs for index 2
21 if (index == 2 && i >= 60 && i<= 90)
22 serie[sample(seq_len(L),1)] = NA
23 series[[i]] = list("level"=level,"serie"=serie) #no need for more
24 }
25 if (shift)
26 {
27 # Simulate shift at origin when predict_at > 0
28 series[2:(n+1)] = series[1:n]
29 series[[1]] = list("level"=0, "serie"=s[[1]][1:(L%/%2)])
30 }
31 new("Data", data=series)
32 }
33
34 test_that("output is as expected on simulated series",
35 {
36 data = getDataTest(150, FALSE)
37
38 # index 142 : serie type 2
39 f = computeFilaments(data, 142, limit=60, plot=FALSE)
40 # Expected output: 22 series of type 3 (closer), then 50-2-10 series of type 2
41 expect_identical(length(f$indices), 60)
42 expect_identical(length(f$colors), 60)
43 for (i in 1:22)
44 {
45 expect_identical((f$indices[i] %% 3) + 1, 3)
46 expect_match(f2$colors[i], f$colors[1])
47 }
48 for (i in 23:60)
49 {
50 expect_identical((f$indices[i] %% 3) + 1, 2)
51 expect_match(f2$colors[i], f$colors[23])
52 }
53 expect_match(colors[1], "...")
54 expect_match(colors[23], "...")
55 })
56
57 test_that("output is as expected on simulated series",
58 {
59 data = getDataTest(150, TRUE)
60
61 # index 143 : serie type 3
62 f = computeFilaments(data, 143, limit=70, plot=FALSE)
63 # Expected output: 22 series of type 2 (closer) then 50-2 series of type 3
64 expect_identical(length(f$indices), 70)
65 expect_identical(length(f$colors), 70)
66 for (i in 1:22)
67 {
68 # -1 because of the initial shift
69 expect_identical(( (f$indices[i]-1) %% 3 ) + 1, 2)
70 expect_match(f$colors[i], f$colors[1])
71 }
72 for (i in 23:70)
73 {
74 expect_identical(( (f$indices[i]-1) %% 3 ) + 1, 3)
75 expect_match(f$colors[i], f$colors[23])
76 }
77 expect_match(colors[1], "...")
78 expect_match(colors[23], "...")
79 })
80
81 test_that("output is as expected on simulated series",
82 {
83 data = getDataTest(150, TRUE)
84
85 # index 144 : serie type 1
86 f = computeFilaments(data, 144, limit=50, plot=FALSE)
87 # Expected output: 2 series of type 3 (closer), then 50-2 series of type 1
88 expect_identical(length(f$indices), 50)
89 expect_identical(length(f$colors), 50)
90 for (i in 1:2)
91 {
92 # -1 because of the initial shift
93 expect_identical(( (f$indices[i]-1) %% 3 ) + 1, 3)
94 expect_match(f$colors[i], f$colors[1])
95 }
96 for (i in 3:50)
97 {
98 expect_identical(( (f$indices[i]-1) %% 3 ) + 1, 1)
99 expect_match(f$colors[i], f$colors[3])
100 }
101 expect_match(colors[1], "...")
102 expect_match(colors[3], "...")
103 })