forked from stumpy-dev/stumpy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_core.py
More file actions
61 lines (54 loc) · 2.34 KB
/
test_core.py
File metadata and controls
61 lines (54 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import numpy as np
import numpy.testing as npt
from stumpy import core
import pytest
def naive_rolling_window_dot_product(Q, T):
window = len(Q)
result = np.zeros(len(T) - window + 1)
for i in range(len(result)):
result[i] = np.dot(T[i:i + window], Q)
return result
test_data = [
(np.array([-1,1,2], dtype=np.float64),np.array(range(5), dtype=np.float64)),
(np.array([9,8100,-60], dtype=np.float64), np.array([584,-11,23,79,1001], dtype=np.float64)),
(np.random.uniform(-1000, 1000, [8]), np.random.uniform(-1000, 1000, [64])),
]
@pytest.mark.parametrize("Q, T", test_data)
def test_sliding_dot_product(Q, T):
left = naive_rolling_window_dot_product(Q, T)
right = core.sliding_dot_product(Q, T)
npt.assert_almost_equal(left, right)
@pytest.mark.parametrize("Q, T", test_data)
def test_compute_mean_std(Q, T):
m = Q.shape[0]
left_μ_Q = np.sum(Q)/m
left_σ_Q = np.sqrt(np.sum(np.square(Q-left_μ_Q)/m))
left_M_T = np.mean(core.rolling_window(T, m), axis=1)
left_Σ_T = np.std(core.rolling_window(T, m), axis=1)
right_μ_Q, right_σ_Q = core.compute_mean_std(Q, m)
right_M_T, right_Σ_T = core.compute_mean_std(T, m)
npt.assert_almost_equal(left_μ_Q, right_μ_Q)
npt.assert_almost_equal(left_σ_Q, right_σ_Q)
npt.assert_almost_equal(left_M_T, right_M_T)
npt.assert_almost_equal(left_Σ_T, right_Σ_T)
@pytest.mark.parametrize("Q, T", test_data)
def test_calculate_distance_profile(Q, T):
m = Q.shape[0]
left = np.linalg.norm(core.z_norm(core.rolling_window(T, m), 1) - core.z_norm(Q), axis=1)
QT = core.sliding_dot_product(Q, T)
μ_Q, σ_Q = core.compute_mean_std(Q, m)
M_T, Σ_T = core.compute_mean_std(T, m)
right = core.calculate_distance_profile(m, QT, μ_Q, σ_Q, M_T, Σ_T)
npt.assert_almost_equal(left, right)
@pytest.mark.parametrize("Q, T", test_data)
def test_mueen_calculate_distance_profile(Q, T):
m = Q.shape[0]
left = np.linalg.norm(core.z_norm(core.rolling_window(T, m), 1) - core.z_norm(Q), axis=1)
right = core.mueen_calculate_distance_profile(Q,T)
npt.assert_almost_equal(left, right)
@pytest.mark.parametrize("Q, T", test_data)
def test_mass(Q, T):
m = Q.shape[0]
left = np.linalg.norm(core.z_norm(core.rolling_window(T, m), 1) - core.z_norm(Q), axis=1)
right = core.mass(Q, T)
npt.assert_almost_equal(left, right)