1
- # Collection of tests that show how Stationary boostrap works. This will be written again in pytest
2
1
import numpy as np
3
2
import pytest
4
- from StationaryBootstrap import StationaryBootstrap
3
+ from stationary_bootstrap import stationary_bootstrap
5
4
6
5
7
6
# Normal behaviour
8
7
def test_normal ():
9
8
data = np .array ([0.4 ,0.2 ,0.1 ,0.4 ,0.3 ,0.1 ,0.3 ,0.4 ,0.2 ,0.5 ,0.1 ,0.2 ]) # Original time-series
10
9
m = 4 # Average length of the block
11
10
sampleLength = 12 # Length of output sample
12
- ans = StationaryBootstrap (data , m , sampleLength )
13
- assert (isinstance (ans , np .ndarray ))
11
+ ans = stationary_bootstrap (data , m , sampleLength )
12
+ assert (isinstance (ans , np .ndarray ), "Output is not a numpy ndarray." )
14
13
15
14
16
15
# Is output same length as sampleLength
17
16
def test_correct_length ():
18
17
data = np .array ([0.4 ,0.2 ,0.1 ,0.4 ,0.3 ,0.1 ,0.3 ,0.4 ,0.2 ,0.5 ,0.1 ,0.2 ]) # Original time-series
19
18
m = 4 # Average length of the block
20
19
sampleLength = 12 # Length of output sample
21
- ans = StationaryBootstrap (data , m , sampleLength )
22
- assert (len (ans )== sampleLength )
20
+ ans = stationary_bootstrap (data , m , sampleLength )
21
+ assert (len (ans )== sampleLength , "Sample length does not match the specified sample length." )
23
22
23
+ # Is output same length as sampleLength
24
+ def test_correct_shape ():
25
+ data = np .array ([0.4 ,0.2 ,0.1 ,0.4 ,0.3 ,0.1 ,0.3 ,0.4 ,0.2 ,0.5 ,0.1 ,0.2 ]) # Original time-series
26
+ m = 4 # Average length of the block
27
+ sample_length = 12 # Length of output sample
28
+ ans = stationary_bootstrap (data , m , sample_length )
29
+ assert (ans .shape == (sample_length ,), "Output is not the specified shape." )
30
+
31
+ # Test if the output values are within the input data range
32
+ def test_bootstrap_validity_of_values ():
33
+ data = np .array ([10 , 20 , 30 , 40 ])
34
+ m = 1.5
35
+ sample_length = 15
36
+ result = stationary_bootstrap (data , m , sample_length )
37
+ assert np .all (np .isin (result , data )), "Output contains values not in the original data."
24
38
25
39
# One element sampled always
26
40
def test_one_element_always_sampled ():
27
41
data = np .array ([0.4 ])
28
42
sampleLength = 4
29
43
m = 4
30
- ans = StationaryBootstrap (data , m , sampleLength )
31
- assert (ans == np .array ([[0.4 ], [0.4 ], [0.4 ], [0.4 ]]))
32
-
44
+ ans = stationary_bootstrap (data , m , sampleLength )
45
+ assert (np .array_equal (ans , np .array ([[0.4 ], [0.4 ], [0.4 ], [0.4 ]])), "Single element should be repeated in the output." )
33
46
34
47
# Sample of length 1
35
48
def test_sample_of_length_one ():
36
49
data = np .array ([0.5 ])
37
50
m = 4
38
51
sampleLength = 1
39
- ans = StationaryBootstrap (data , m , sampleLength )
52
+ ans = stationary_bootstrap (data , m , sampleLength )
40
53
assert (ans == np .array ([0.5 ]))
41
54
42
- # Sampling empty data
43
- #data = np.array([])
44
- #sampleLength = 1
45
- #ans = StationaryBootstrap(data, m, sampleLength)
46
- #print(ans == np.array([0.5]))
47
-
48
- # Negative sample length parameter
49
- #data = np.array([0.5])
50
- #sampleLength = -1
51
- #ans = StationaryBootstrap(data, m, sampleLength)
52
- #print(ans == np.array([0.5]))
53
-
54
-
55
- # negative average length
56
- #def test_negative_average_length():
57
- # data = np.array([0.4,0.2,0.1,0.4,0.3,0.1,0.3,0.4,0.2,0.5,0.1,0.2]) # Original time-series
58
- # m = -4 # Average length of the block
59
- # sampleLength = 12 # Length of output sample
60
- # ans = StationaryBootstrap(data, m, sampleLength)
61
- # print(ans)
62
- #print("Fix this")
63
-
55
+ # Test if an error is raised for non-positive block length (m)
56
+ def test_invalid_block_length ():
57
+ data = np .array ([1 , 2 , 3 ])
58
+ m = 0 # Invalid block length
59
+ sample_length = 10
60
+ with pytest .raises (ValueError , match = "Block length 'm' must be positive" ):
61
+ stationary_bootstrap (data , m , sample_length )
62
+
63
+ # Test if an error is raised when data array is empty
64
+ def test_empty_data_array ():
65
+ data = np .array ([])
66
+ m = 2.0
67
+ sample_length = 5
68
+ with pytest .raises (ValueError , match = "Data array cannot be empty" ):
69
+ stationary_bootstrap (data , m , sample_length )
70
+
71
+ # Test if an error is raised for invalid sample length
72
+ def test_invalid_sample_length ():
73
+ data = np .array ([1 , 2 , 3 ])
74
+ m = 1.0
75
+ sample_length = - 5 # Invalid sample length
76
+ with pytest .raises (ValueError , match = "Sample length must be positive" ):
77
+ stationary_bootstrap (data , m , sample_length )
64
78
65
79
# Average length longer than sample
66
80
def test_average_length_longer_than_sample ():
67
81
data = np .array ([0.4 ,0.2 ,0.1 ,0.4 ,0.3 ,0.1 ,0.3 ,0.4 ,0.2 ,0.5 ,0.1 ,0.2 ]) # Original time-series
68
82
m = 20 # Average length of the block
69
83
sampleLength = 12 # Length of output sample
70
- ans = StationaryBootstrap (data , m , sampleLength )
84
+ ans = stationary_bootstrap (data , m , sampleLength )
71
85
assert (len (ans )== sampleLength )
72
86
73
-
74
87
# Data in columns
75
88
def test_data_passed_in_column ():
76
89
data = np .array ([[0.4 ],[0.2 ],[0.1 ],[0.4 ],[0.3 ],[0.1 ],[0.3 ],[0.4 ],[0.2 ],[0.5 ],[0.1 ],[0.2 ]]) # Original time-series
77
90
m = 4 # Average length of the block
78
91
sampleLength = 12 # Length of output sample
79
- ans = StationaryBootstrap (data , m , sampleLength )
92
+ ans = stationary_bootstrap (data , m , sampleLength )
80
93
data2 = np .array ([0.4 ,0.2 ,0.1 ,0.4 ,0.3 ,0.1 ,0.3 ,0.4 ,0.2 ,0.5 ,0.1 ,0.2 ])
81
- ans2 = StationaryBootstrap (data2 , m , sampleLength )
94
+ ans2 = stationary_bootstrap (data2 , m , sampleLength )
82
95
assert (ans .size == ans2 .size )
83
96
84
97
@@ -87,23 +100,24 @@ def test_negative_input_data():
87
100
data = np .array ([- 0.4 ,0.2 ,- 0.1 ,0.4 ,- 0.3 ,0.1 ,- 0.3 ,0.4 ,- 0.2 ,- 0.5 ,0.1 ,- 0.2 ]) # Original time-series
88
101
m = 4 # Average length of the block
89
102
sampleLength = 12 # Length of output sample
90
- ans = StationaryBootstrap (data , m , sampleLength )
103
+ ans = stationary_bootstrap (data , m , sampleLength )
91
104
assert (len (ans )== sampleLength )
92
105
93
106
94
107
# Data not in numpy array
95
- #data = [0.4,0.2,0.1,0.4,0.3,0.1,0.3,0.4,0.2,0.5,0.1,0.2] # Original time-series
96
- #m = 4 # Average length of the block
97
- #sampleLength = 12 # Length of output sample
98
- #ans = StationaryBootstrap(data, m, sampleLength)
99
- #print(ans)
108
+ def test_data_not_numpy ():
109
+ data = [0.4 ,0.2 ,0.1 ,0.4 ,0.3 ,0.1 ,0.3 ,0.4 ,0.2 ,0.5 ,0.1 ,0.2 ] # Original time-series
110
+ m = 4 # Average length of the block
111
+ sampleLength = 12 # Length of output sample
112
+ with pytest .raises (ValueError , match = "data needs to be as a numpy array" ):
113
+ stationary_bootstrap (data , m , sampleLength )
100
114
101
115
# Data contains strings
102
116
def test_string_number_input_data ():
103
117
data = np .array (["-0.4" ,"0.2" ,"-0.1" ,"0.4" ,"-0.3" ,"0.1" ,"0.3" ,"0.4" ,"0.2" ,"0.5" ,"0.1" ,"0.2" ]) # Original time-series
104
118
m = 4 # Average length of the block
105
119
sampleLength = 12 # Length of output sample
106
- ans = StationaryBootstrap (data , m , sampleLength )
120
+ ans = stationary_bootstrap (data , m , sampleLength )
107
121
assert (len (ans )== sampleLength )
108
122
109
123
0 commit comments