|
| 1 | +import inspect |
| 2 | +import warnings |
| 3 | +from operator import eq, lt |
| 4 | + |
1 | 5 | import naive |
2 | 6 | import numpy as np |
3 | 7 | import pytest |
4 | 8 | from numpy import testing as npt |
5 | 9 |
|
6 | 10 | from stumpy import sdp |
7 | 11 |
|
8 | | -test_data = [ |
9 | | - (np.array([-1, 1, 2], dtype=np.float64), np.array(range(5), dtype=np.float64)), |
| 12 | +# README |
| 13 | +# Real FFT algorithm performs more efficiently when the length |
| 14 | +# of the input array `arr` is composed of small prime factors. |
| 15 | +# The next_fast_len(arr, real=True) function from Scipy returns |
| 16 | +# the same length if len(arr) is composed of a subset of |
| 17 | +# prime numbers 2, 3, 5. Therefore, these radices are |
| 18 | +# considered as the most efficient for the real FFT algorithm. |
| 19 | + |
| 20 | +# To ensure that the tests cover different cases, the following cases |
| 21 | +# are considered: |
| 22 | +# 1. len(T) is even, and len(T) == next_fast_len(len(T), real=True) |
| 23 | +# 2. len(T) is odd, and len(T) == next_fast_len(len(T), real=True) |
| 24 | +# 3. len(T) is even, and len(T) < next_fast_len(len(T), real=True) |
| 25 | +# 4. len(T) is odd, and len(T) < next_fast_len(len(T), real=True) |
| 26 | +# And 5. a special case of 1, where len(T) is power of 2. |
| 27 | + |
| 28 | +# Therefore: |
| 29 | +# 1. len(T) is composed of 2 and a subset of {3, 5} |
| 30 | +# 2. len(T) is composed of a subset of {3, 5} |
| 31 | +# 3. len(T) is composed of a subset of {7, 11, 13, ...} and 2 |
| 32 | +# 4. len(T) is composed of a subset of {7, 11, 13, ...} |
| 33 | +# 5. len(T) is power of 2 |
| 34 | + |
| 35 | +# In some cases, the prime factors are raised to a power of |
| 36 | +# certain degree to increase the length of array to be around |
| 37 | +# 1000-2000. This allows us to test sliding_dot_product for |
| 38 | +# wider range of query lengths. |
| 39 | + |
| 40 | +# test cases 1-4 |
| 41 | +test_inputs = [ |
| 42 | + # Input format: |
| 43 | + # ( |
| 44 | + # len(T), |
| 45 | + # remainder, # from `len(T) % 2` |
| 46 | + # comparator, # for len(T) comparator next_fast_len(len(T), real=True) |
| 47 | + # ) |
| 48 | + ( |
| 49 | + 2 * (3**2) * (5**3), |
| 50 | + 0, |
| 51 | + eq, |
| 52 | + ), # = 2250, Even `len(T)`, and `len(T) == next_fast_len(len(T), real=True)` |
| 53 | + ( |
| 54 | + (3**2) * (5**3), |
| 55 | + 1, |
| 56 | + eq, |
| 57 | + ), # = 1125, Odd `len(T)`, and `len(T) == next_fast_len(len(T), real=True)`. |
| 58 | + ( |
| 59 | + 2 * 7 * 11 * 13, |
| 60 | + 0, |
| 61 | + lt, |
| 62 | + ), # = 2002, Even `len(T)`, and `len(T) < next_fast_len(len(T), real=True)` |
10 | 63 | ( |
11 | | - np.array([9, 8100, -60], dtype=np.float64), |
12 | | - np.array([584, -11, 23, 79, 1001], dtype=np.float64), |
13 | | - ), |
14 | | - (np.random.uniform(-1000, 1000, [8]), np.random.uniform(-1000, 1000, [64])), |
| 64 | + 7 * 11 * 13, |
| 65 | + 1, |
| 66 | + lt, |
| 67 | + ), # = 1001, Odd `len(T)`, and `len(T) < next_fast_len(len(T), real=True)` |
15 | 68 | ] |
16 | 69 |
|
17 | 70 |
|
18 | | -@pytest.mark.parametrize("Q, T", test_data) |
19 | | -def test_njit_sliding_dot_product(Q, T): |
20 | | - ref_mp = naive.rolling_window_dot_product(Q, T) |
21 | | - comp_mp = sdp._njit_sliding_dot_product(Q, T) |
22 | | - npt.assert_almost_equal(ref_mp, comp_mp) |
| 71 | +def get_sdp_function_names(): |
| 72 | + out = [] |
| 73 | + for func_name, func in inspect.getmembers(sdp, inspect.isfunction): |
| 74 | + if func_name.endswith("sliding_dot_product"): |
| 75 | + out.append(func_name) |
| 76 | + |
| 77 | + return out |
| 78 | + |
| 79 | + |
| 80 | +@pytest.mark.parametrize("n_T, remainder, comparator", test_inputs) |
| 81 | +def test_sdp(n_T, remainder, comparator): |
| 82 | + # test_sdp for cases 1-4 |
| 83 | + |
| 84 | + n_Q_prime = [ |
| 85 | + 2, |
| 86 | + 3, |
| 87 | + 5, |
| 88 | + 7, |
| 89 | + 11, |
| 90 | + 13, |
| 91 | + 17, |
| 92 | + 19, |
| 93 | + 23, |
| 94 | + 29, |
| 95 | + 31, |
| 96 | + 37, |
| 97 | + 41, |
| 98 | + 43, |
| 99 | + 47, |
| 100 | + 53, |
| 101 | + 59, |
| 102 | + 61, |
| 103 | + 67, |
| 104 | + 71, |
| 105 | + 73, |
| 106 | + 79, |
| 107 | + 83, |
| 108 | + 89, |
| 109 | + 97, |
| 110 | + ] |
| 111 | + n_Q_power2 = [2, 4, 8, 16, 32, 64] |
| 112 | + n_Q_values = n_Q_prime + n_Q_power2 + [n_T] |
| 113 | + n_Q_values = sorted(n_Q for n_Q in set(n_Q_values) if n_Q <= n_T) |
| 114 | + |
| 115 | + for n_Q in n_Q_values: |
| 116 | + Q = np.random.rand(n_Q) |
| 117 | + T = np.random.rand(n_T) |
| 118 | + ref = naive.rolling_window_dot_product(Q, T) |
| 119 | + for func_name in get_sdp_function_names(): |
| 120 | + func = getattr(sdp, func_name) |
| 121 | + try: |
| 122 | + comp = func(Q, T) |
| 123 | + npt.assert_allclose(comp, ref) |
| 124 | + except Exception as e: # pragma: no cover |
| 125 | + msg = f"Error in {func_name}, with n_Q={n_Q} and n_T={n_T}" |
| 126 | + warnings.warn(msg) |
| 127 | + raise e |
| 128 | + |
| 129 | + |
| 130 | +def test_sdp_power2(): |
| 131 | + # test for case 5. len(T) is power of 2 |
| 132 | + pmin = 3 |
| 133 | + pmax = 13 |
23 | 134 |
|
| 135 | + for func_name in get_sdp_function_names(): |
| 136 | + func = getattr(sdp, func_name) |
| 137 | + try: |
| 138 | + for q in range(pmin, pmax + 1): |
| 139 | + n_Q = 2**q |
| 140 | + for p in range(q, pmax + 1): |
| 141 | + n_T = 2**p |
| 142 | + Q = np.random.rand(n_Q) |
| 143 | + T = np.random.rand(n_T) |
24 | 144 |
|
25 | | -@pytest.mark.parametrize("Q, T", test_data) |
26 | | -def test_convolve_sliding_dot_product(Q, T): |
27 | | - ref_mp = naive.rolling_window_dot_product(Q, T) |
28 | | - comp_mp = sdp._convolve_sliding_dot_product(Q, T) |
29 | | - npt.assert_almost_equal(ref_mp, comp_mp) |
| 145 | + ref = naive.rolling_window_dot_product(Q, T) |
| 146 | + comp = func(Q, T) |
| 147 | + npt.assert_allclose(comp, ref) |
30 | 148 |
|
| 149 | + except Exception as e: # pragma: no cover |
| 150 | + msg = f"Error in {func_name}, with q={q} and p={p}" |
| 151 | + warnings.warn(msg) |
| 152 | + raise e |
31 | 153 |
|
32 | | -@pytest.mark.parametrize("Q, T", test_data) |
33 | | -def test_sliding_dot_product(Q, T): |
34 | | - ref_mp = naive.rolling_window_dot_product(Q, T) |
35 | | - comp_mp = sdp._sliding_dot_product(Q, T) |
36 | | - npt.assert_almost_equal(ref_mp, comp_mp) |
| 154 | + return |
0 commit comments