-
Notifications
You must be signed in to change notification settings - Fork 341
Refactor/sdp structure cleanup #1126
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
Open
NimaSarajpoor
wants to merge
4
commits into
stumpy-dev:main
Choose a base branch
from
NimaSarajpoor:refactor/sdp-structure-cleanup
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| import numpy as np | ||
| from numba import njit | ||
| from scipy.signal import convolve | ||
|
|
||
| from . import config | ||
|
|
||
|
|
||
| @njit(fastmath=config.STUMPY_FASTMATH_TRUE) | ||
| def _njit_sliding_dot_product(Q, T): | ||
| """ | ||
| A Numba JIT-compiled implementation of the sliding window dot product. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| Q : numpy.ndarray | ||
| Query array or subsequence | ||
|
|
||
| T : numpy.ndarray | ||
| Time series or sequence | ||
|
|
||
| Returns | ||
| ------- | ||
| out : numpy.ndarray | ||
| Sliding dot product between `Q` and `T`. | ||
| """ | ||
| m = Q.shape[0] | ||
| l = T.shape[0] - m + 1 | ||
| out = np.empty(l) | ||
| for i in range(l): | ||
| out[i] = np.dot(Q, T[i : i + m]) | ||
|
|
||
| return out | ||
|
|
||
|
|
||
| def _convolve_sliding_dot_product(Q, T): | ||
| """ | ||
| Use (direct or FFT) convolution to calculate the sliding window dot product. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| Q : numpy.ndarray | ||
| Query array or subsequence | ||
|
|
||
| T : numpy.ndarray | ||
| Time series or sequence | ||
|
|
||
| Returns | ||
| ------- | ||
| output : numpy.ndarray | ||
| Sliding dot product between `Q` and `T`. | ||
| """ | ||
| n = T.shape[0] | ||
| m = Q.shape[0] | ||
| Qr = np.flipud(Q) # Reverse/flip Q | ||
| QT = convolve(Qr, T) | ||
|
|
||
| return QT.real[m - 1 : n] | ||
|
|
||
|
|
||
| def _sliding_dot_product(Q, T): | ||
| """ | ||
| Compute the sliding dot product between `Q` and `T` | ||
|
|
||
| Parameters | ||
| ---------- | ||
| Q : numpy.ndarray | ||
| Query array or subsequence | ||
|
|
||
| T : numpy.ndarray | ||
| Time series or sequence | ||
|
|
||
| Returns | ||
| ------- | ||
| out : numpy.ndarray | ||
| Sliding dot product between `Q` and `T`. | ||
| """ | ||
| return _convolve_sliding_dot_product(Q, T) |
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
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 |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| 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)), | ||
| ( | ||
| 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_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) | ||
|
|
||
|
|
||
| @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) | ||
|
|
||
|
|
||
| @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) |
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.