TODO: unit tests for simil days
[talweg.git] / pkg / tests / testthat / test-computeFilaments.R
CommitLineData
3ddf1c12 1context("computeFilaments")
1e20780e 2
af3b84f4
BA
3#shorthand: map 1->1, 2->2, 3->3, 4->1, ..., 149->2, 150->3
4I = function(i)
5 (i-1) %% 3 + 1
6
44a9990b 7#MOCK data; NOTE: could be in inst/testdata as well
af3b84f4 8getDataTest = function(n)
a65907cc 9{
af3b84f4 10 data = Data$new()
8f84543c
BA
11 x = seq(0,9.5,0.1)
12 L = length(x) #96 1/4h
a65907cc
BA
13 s1 = cos(x)
14 s2 = sin(x)
15 s3 = c( s1[1:(L%/%2)] , s2[(L%/%2+1):L] )
8f84543c
BA
16 #sum((s1-s2)^2) == 96
17 #sum((s1-s3)^2) == 58
18 #sum((s2-s3)^2) == 38
19 s = list(s1, s2, s3)
a65907cc
BA
20 series = list()
21 for (i in seq_len(n))
1e20780e 22 {
af3b84f4
BA
23 serie = s[[I(i)]] + rnorm(L,sd=0.01)
24 level = mean(serie)
25 serie = serie - level
a65907cc 26 # 10 series with NAs for index 2
af3b84f4 27 if (I(i) == 2 && i >= 60 && i<= 90)
a65907cc 28 serie[sample(seq_len(L),1)] = NA
8f84543c
BA
29 time = as.POSIXct(i*15*60, origin="2007-01-01", tz="GMT")
30 exo = runif(4)
31 exo_hat = runif(4)
32 data$append(time, serie, level, exo, exo_hat)
6d97bfec 33 }
af3b84f4 34 data
6d97bfec
BA
35}
36
af3b84f4 37test_that("output is as expected on simulated series",
6d97bfec 38{
af3b84f4 39 data = getDataTest(150)
a65907cc 40
44a9990b 41 # index 143 : serie type 2
8f84543c
BA
42 pred = computeForecast(data, 143, "Neighbors", "Zero",
43 horizon=length(data$getSerie(1)), simtype="endo", h_window=1)
44 f = computeFilaments(data, pred, 1, limit=60, plot=FALSE)
98e958ca
BA
45
46 # Expected output: 50-3-10 series of type 2, then 23 series of type 3 (closest next)
47 expect_identical(length(f$neighb_indices), as.integer(60))
48 expect_identical(length(f$colors), as.integer(60))
49 expect_equal(f$index, 143)
50 expect_true(all(I(f$neighb_indices) >= 2))
51 for (i in 1:37)
6d97bfec 52 {
98e958ca 53 expect_equal(I(f$neighb_indices[i]), 2)
44a9990b 54 expect_match(f$colors[i], f$colors[1])
6d97bfec 55 }
98e958ca 56 for (i in 38:60)
6d97bfec 57 {
98e958ca
BA
58 expect_equal(I(f$neighb_indices[i]), 3)
59 expect_match(f$colors[i], f$colors[38])
6d97bfec 60 }
98e958ca
BA
61 expect_match(f$colors[1], "#1*")
62 expect_match(f$colors[38], "#E*")
6d97bfec 63
af3b84f4 64 # index 142 : serie type 1
8f84543c
BA
65 pred = computeForecast(data, 142, "Neighbors", "Zero",
66 horizon=length(data$getSerie(1)), simtype="endo", h_window=1)
67 f = computeFilaments(data, pred, 1, limit=50, plot=FALSE)
98e958ca
BA
68
69 # Expected output: 50-10-3 series of type 1, then 13 series of type 3 (closest next)
44a9990b 70 # NOTE: -10 because only past days with no-NAs tomorrow => exclude type 1 in [60,90[
98e958ca
BA
71 expect_identical(length(f$neighb_indices), as.integer(50))
72 expect_identical(length(f$colors), as.integer(50))
73 expect_equal(f$index, 142)
74 expect_true(all(I(f$neighb_indices) != 2))
75 for (i in 1:37)
6d97bfec 76 {
98e958ca 77 expect_equal(I(f$neighb_indices[i]), 1)
6d97bfec
BA
78 expect_match(f$colors[i], f$colors[1])
79 }
98e958ca 80 for (i in 38:50)
6d97bfec 81 {
98e958ca
BA
82 expect_equal(I(f$neighb_indices[i]), 3)
83 expect_match(f$colors[i], f$colors[38])
6d97bfec 84 }
98e958ca
BA
85 expect_match(f$colors[1], "#1*")
86 expect_match(f$colors[38], "#E*")
1e20780e 87})