-
Notifications
You must be signed in to change notification settings - Fork 348
Add new sdp functions #1128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
NimaSarajpoor
merged 11 commits into
stumpy-dev:main
from
NimaSarajpoor:add-sdp-methods
Feb 13, 2026
+200
−30
Merged
Add new sdp functions #1128
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5a7f315
improve njit_sdp
NimaSarajpoor dd98c83
simplify function
NimaSarajpoor b581b38
add new sdp func and its test
NimaSarajpoor e9c0e24
add pocketfft sdp and test
NimaSarajpoor 6c14ab5
fixed missing imports
NimaSarajpoor 7c1e52c
fixed coverage
NimaSarajpoor 9869285
refactored test functions
NimaSarajpoor 05b073c
minor change
NimaSarajpoor 8b6191b
enhanced test cases
NimaSarajpoor 275a087
add minor comment
NimaSarajpoor 55589d3
Minor change
NimaSarajpoor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,36 +1,154 @@ | ||
| import inspect | ||
| import warnings | ||
| from operator import eq, lt | ||
|
|
||
| import naive | ||
| import numpy as np | ||
| import pytest | ||
| from numpy import testing as npt | ||
|
|
||
| from stumpy import sdp | ||
|
|
||
| test_data = [ | ||
| (np.array([-1, 1, 2], dtype=np.float64), np.array(range(5), dtype=np.float64)), | ||
| # README | ||
| # Real FFT algorithm performs more efficiently when the length | ||
| # of the input array `arr` is composed of small prime factors. | ||
| # The next_fast_len(arr, real=True) function from Scipy returns | ||
| # the same length if len(arr) is composed of a subset of | ||
| # prime numbers 2, 3, 5. Therefore, these radices are | ||
| # considered as the most efficient for the real FFT algorithm. | ||
|
|
||
| # To ensure that the tests cover different cases, the following cases | ||
| # are considered: | ||
| # 1. len(T) is even, and len(T) == next_fast_len(len(T), real=True) | ||
| # 2. len(T) is odd, and len(T) == next_fast_len(len(T), real=True) | ||
| # 3. len(T) is even, and len(T) < next_fast_len(len(T), real=True) | ||
| # 4. len(T) is odd, and len(T) < next_fast_len(len(T), real=True) | ||
| # And 5. a special case of 1, where len(T) is power of 2. | ||
|
|
||
| # Therefore: | ||
| # 1. len(T) is composed of 2 and a subset of {3, 5} | ||
| # 2. len(T) is composed of a subset of {3, 5} | ||
| # 3. len(T) is composed of a subset of {7, 11, 13, ...} and 2 | ||
| # 4. len(T) is composed of a subset of {7, 11, 13, ...} | ||
| # 5. len(T) is power of 2 | ||
|
|
||
| # In some cases, the prime factors are raised to a power of | ||
| # certain degree to increase the length of array to be around | ||
| # 1000-2000. This allows us to test sliding_dot_product for | ||
| # wider range of query lengths. | ||
|
|
||
| # test cases 1-4 | ||
| test_inputs = [ | ||
| # Input format: | ||
| # ( | ||
| # len(T), | ||
| # remainder, # from `len(T) % 2` | ||
| # comparator, # for len(T) comparator next_fast_len(len(T), real=True) | ||
| # ) | ||
| ( | ||
| 2 * (3**2) * (5**3), | ||
| 0, | ||
| eq, | ||
| ), # = 2250, Even `len(T)`, and `len(T) == next_fast_len(len(T), real=True)` | ||
| ( | ||
| (3**2) * (5**3), | ||
| 1, | ||
| eq, | ||
| ), # = 1125, Odd `len(T)`, and `len(T) == next_fast_len(len(T), real=True)`. | ||
| ( | ||
| 2 * 7 * 11 * 13, | ||
| 0, | ||
| lt, | ||
| ), # = 2002, Even `len(T)`, and `len(T) < next_fast_len(len(T), real=True)` | ||
| ( | ||
| 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])), | ||
| 7 * 11 * 13, | ||
| 1, | ||
| lt, | ||
| ), # = 1001, Odd `len(T)`, and `len(T) < next_fast_len(len(T), real=True)` | ||
| ] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("Q, T", test_data) | ||
| def test_njit_sliding_dot_product(Q, T): | ||
| ref_mp = naive.rolling_window_dot_product(Q, T) | ||
| comp_mp = sdp._njit_sliding_dot_product(Q, T) | ||
| npt.assert_almost_equal(ref_mp, comp_mp) | ||
| def get_sdp_function_names(): | ||
| out = [] | ||
| for func_name, func in inspect.getmembers(sdp, inspect.isfunction): | ||
| if func_name.endswith("sliding_dot_product"): | ||
| out.append(func_name) | ||
|
|
||
| return out | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("n_T, remainder, comparator", test_inputs) | ||
| def test_sdp(n_T, remainder, comparator): | ||
| # test_sdp for cases 1-4 | ||
|
|
||
| n_Q_prime = [ | ||
| 2, | ||
| 3, | ||
| 5, | ||
| 7, | ||
| 11, | ||
| 13, | ||
| 17, | ||
| 19, | ||
| 23, | ||
| 29, | ||
| 31, | ||
| 37, | ||
| 41, | ||
| 43, | ||
| 47, | ||
| 53, | ||
| 59, | ||
| 61, | ||
| 67, | ||
| 71, | ||
| 73, | ||
| 79, | ||
| 83, | ||
| 89, | ||
| 97, | ||
| ] | ||
| n_Q_power2 = [2, 4, 8, 16, 32, 64] | ||
| n_Q_values = n_Q_prime + n_Q_power2 + [n_T] | ||
| n_Q_values = sorted(n_Q for n_Q in set(n_Q_values) if n_Q <= n_T) | ||
|
|
||
| for n_Q in n_Q_values: | ||
| Q = np.random.rand(n_Q) | ||
| T = np.random.rand(n_T) | ||
| ref = naive.rolling_window_dot_product(Q, T) | ||
| for func_name in get_sdp_function_names(): | ||
| func = getattr(sdp, func_name) | ||
| try: | ||
| comp = func(Q, T) | ||
| npt.assert_allclose(comp, ref) | ||
| except Exception as e: # pragma: no cover | ||
| msg = f"Error in {func_name}, with n_Q={len(Q)} and n_T={len(T)}" | ||
| warnings.warn(msg) | ||
| raise e | ||
|
|
||
|
|
||
| def test_sdp_power2(): | ||
| # test for case 5. len(T) is power of 2 | ||
| pmin = 3 | ||
| pmax = 13 | ||
|
|
||
| for func_name in get_sdp_function_names(): | ||
| func = getattr(sdp, func_name) | ||
| try: | ||
| for q in range(pmin, pmax + 1): | ||
| n_Q = 2**q | ||
| for p in range(q, pmax + 1): | ||
| n_T = 2**p | ||
| Q = np.random.rand(n_Q) | ||
| T = np.random.rand(n_T) | ||
|
|
||
| @pytest.mark.parametrize("Q, T", test_data) | ||
| def test_convolve_sliding_dot_product(Q, T): | ||
| ref_mp = naive.rolling_window_dot_product(Q, T) | ||
| comp_mp = sdp._convolve_sliding_dot_product(Q, T) | ||
| npt.assert_almost_equal(ref_mp, comp_mp) | ||
| ref = naive.rolling_window_dot_product(Q, T) | ||
| comp = func(Q, T) | ||
| npt.assert_allclose(comp, ref) | ||
|
|
||
| except Exception as e: # pragma: no cover | ||
| msg = f"Error in {func_name}, with q={q} and p={p}" | ||
| warnings.warn(msg) | ||
| raise e | ||
|
|
||
| @pytest.mark.parametrize("Q, T", test_data) | ||
| def test_sliding_dot_product(Q, T): | ||
| ref_mp = naive.rolling_window_dot_product(Q, T) | ||
| comp_mp = sdp._sliding_dot_product(Q, T) | ||
| npt.assert_almost_equal(ref_mp, comp_mp) | ||
| return | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.