-
Notifications
You must be signed in to change notification settings - Fork 2
Fixed #34 Customizing scipy's oaconvolve #35
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
21
commits into
main
Choose a base branch
from
oaconvolve
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
21 commits
Select commit
Hold shift + click to select a range
72f0fb2
modified oaconvolve
NimaSarajpoor be0035e
update code logic
NimaSarajpoor 907e2e2
minor clean ups
NimaSarajpoor 969f187
major changes to imporve readability
NimaSarajpoor b366e35
Add param blocksize
NimaSarajpoor a3d2e44
add temp test for challenger
NimaSarajpoor 22d233b
add func for computing block size
NimaSarajpoor d6eedfa
remove redundant code
NimaSarajpoor 4e23694
added clearer functions
NimaSarajpoor 01a0e7a
minor change
NimaSarajpoor dddb708
minor change to help with future refactoring
NimaSarajpoor 41db845
minor change
NimaSarajpoor 99e450b
Added reference for finding optimal block size
NimaSarajpoor e8fa331
fixed test
NimaSarajpoor f45f541
revise comment
NimaSarajpoor 39e936c
removed overlap-add explanation. Created PR#36 instead
NimaSarajpoor f6fed15
renaming private functions to reflect valid convolution
NimaSarajpoor ccbb651
Merge branch 'main' into oaconvolve
NimaSarajpoor 1033584
address comments
NimaSarajpoor d548d0d
add docstrings and comments
NimaSarajpoor c78d67b
improved docstrings and comments
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,194 @@ | ||
| import math | ||
| import numpy as np | ||
| from scipy.special import lambertw | ||
| from scipy.fft import next_fast_len | ||
| from scipy.fft._pocketfft.basic import r2c, c2r | ||
| from . import pocketfft_r2c_c2r_sdp | ||
|
|
||
|
|
||
| def setup(Q, T): | ||
| return | ||
| def _compute_block_size(m, n, conv_block_size=None): | ||
| """ | ||
| Return a block size for the overlap-add method. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| m : int | ||
| Length of the query array Q. | ||
|
|
||
| n : int | ||
| Length of the time series T. | ||
|
|
||
| conv_block_size : int, default None | ||
| Block size for the convolution. When `conv_block_size` is None, | ||
| it will be automatically set to an optimal value, internally | ||
| computed based on the lengths of Q and T. | ||
|
|
||
| Returns | ||
| ------- | ||
| conv_block_size : int | ||
| Block size for the convolution. Will be at least `m` and at most `n`. | ||
| """ | ||
| if conv_block_size is None: | ||
| if m >= n / 2: | ||
| conv_block_size = n | ||
| else: | ||
| # To minimize Eq. 3 in | ||
| # https://en.wikipedia.org/wiki/Overlap–add_method | ||
| overlap = m - 1 | ||
|
NimaSarajpoor marked this conversation as resolved.
|
||
| opt_size = -overlap * lambertw(-1 / (2 * math.e * overlap), k=-1).real | ||
| conv_block_size = next_fast_len(math.ceil(opt_size), real=True) | ||
|
|
||
| # Ensure that conv_block_size is at least m, so that | ||
| # it can cover at least one element of `T` in each block | ||
| conv_block_size = max(conv_block_size, m) | ||
|
NimaSarajpoor marked this conversation as resolved.
|
||
|
|
||
| return min(conv_block_size, n) | ||
|
|
||
|
|
||
| def _pocketfft_circular_convolve_block(Q, T, conv_block_size): | ||
| m = Q.shape[0] | ||
| n = T.shape[0] | ||
|
|
||
| # Each block in overlap-add method needs to be padded | ||
| # with `m-1` zeros. Therefore, the effective block size | ||
| # for T is `conv_block_size - (m-1)`. | ||
| T_block_size = conv_block_size - (m - 1) | ||
| n_blocks = math.ceil(n / T_block_size) | ||
| last_block_start = (n_blocks - 1) * T_block_size | ||
|
|
||
| # To compute the circular convolution between the zero-padded Q | ||
| # and each zero-padded block of T, the data can be loaded into | ||
| # a 2D array with `n_blocks + 1` rows, where the first `n_blocks` | ||
| # rows correspond to the blocks of T, and the last row is the | ||
| # zero-padded Q. | ||
| tmp = np.empty((n_blocks + 1, conv_block_size), dtype=np.float64) | ||
| tmp[: n_blocks - 1, :T_block_size] = T[:last_block_start].reshape( | ||
| n_blocks - 1, T_block_size | ||
| ) | ||
| tmp[: n_blocks - 1, T_block_size:] = 0.0 | ||
| tmp[n_blocks - 1, : n - last_block_start] = T[last_block_start:] | ||
| tmp[n_blocks - 1, n - last_block_start :] = 0.0 | ||
|
|
||
| tmp[n_blocks, :m] = Q | ||
| tmp[n_blocks, m:] = 0.0 | ||
|
|
||
| fft_2d = r2c(True, tmp, axis=-1) | ||
|
|
||
| return c2r(False, np.multiply(fft_2d[:-1], fft_2d[[-1]]), n=conv_block_size) | ||
|
|
||
|
|
||
| def _pocketfft_valid_oaconvolve(Q, T, conv_block_size): | ||
| """ | ||
| Compute the valid convolution between Q and T using the overlap-add method. | ||
| This method performs several circular convolutions between Q and blocks of T, | ||
| and then combines the results to obtain the valid convolution between Q and T | ||
|
|
||
| Parameters | ||
| ---------- | ||
| Q : numpy.ndarray | ||
| Query array or subsequence. | ||
|
|
||
| T : numpy.ndarray | ||
| Time series or sequence. | ||
|
|
||
| conv_block_size : int | ||
| Block size for the overlap-add method. | ||
| The value cannot be less than len(Q). | ||
|
|
||
| Returns | ||
| ------- | ||
| out : numpy.ndarray | ||
| The valid convolution between Q and T. | ||
|
|
||
| def sliding_dot_product(Q, T): | ||
| Notes | ||
| ----- | ||
| Each block of the convolution contains part of `T`, padded with `len(Q)-1` | ||
| zeros. Therefore, `conv_block_size` must be at least `len(Q)` so that it | ||
| can cover at least one element of `T` in each block. | ||
| """ | ||
| # performs several circular convolutions between | ||
| # zero-padded Q and zero-padded blocks of T | ||
| # and returns a 2D array of the results, | ||
| # where each row is associated with a block of T | ||
| QT_conv_blocks = _pocketfft_circular_convolve_block(Q, T, conv_block_size) | ||
|
|
||
| # The subsequences at the boundaries of the blocks | ||
| # are shared between adjacent blocks. | ||
| # The following logic is needed to reconstruct | ||
| # the valid convolution between Q and T | ||
| overlap = len(Q) - 1 | ||
| out = QT_conv_blocks[:, :-overlap] | ||
| out[1:, :overlap] += QT_conv_blocks[:-1, -overlap:] | ||
|
|
||
| return np.reshape(out, (-1,))[len(Q) - 1 : len(T)] | ||
|
|
||
|
|
||
| def _valid_convolve(Q, T, conv_block_size=None): | ||
| """ | ||
| Compute the valid convolution between Q and T | ||
|
|
||
| Parameters | ||
| ---------- | ||
| Q : numpy.ndarray | ||
| Query array or subsequence. | ||
|
|
||
| T : numpy.ndarray | ||
| Time series or sequence. | ||
|
|
||
| conv_block_size : int, default None | ||
| Block size for the overlap-add method. When `conv_block_size` | ||
| is None, it will automatically be set to an optimal value, | ||
| internally computed based on the lengths of Q and T. | ||
|
|
||
| Returns | ||
| ------- | ||
| out : numpy.ndarray | ||
| The valid convolution between Q and T. | ||
|
|
||
| Notes | ||
| ----- | ||
| The valid convolution between `Q` and `T` is computed by sliding Q[::-1] | ||
| over T and computing the dot product at each position when there is a | ||
| a full overlap between Q and a subsequence of T. | ||
| """ | ||
| m = len(Q) | ||
| l = T.shape[0] - m + 1 | ||
| out = np.empty(l) | ||
| for i in range(l): | ||
| out[i] = np.dot(Q, T[i : i + m]) | ||
| n = len(T) | ||
| conv_block_size = _compute_block_size(m, n, conv_block_size=conv_block_size) | ||
| if conv_block_size >= n: | ||
| out = pocketfft_r2c_c2r_sdp._pocketfft_valid_convolve(Q, T) | ||
| else: | ||
| out = _pocketfft_valid_oaconvolve(Q, T, conv_block_size) | ||
|
|
||
| return out | ||
|
|
||
|
|
||
| def setup(Q, T): | ||
| return | ||
|
|
||
|
|
||
| def sliding_dot_product(Q, T, conv_block_size=None): | ||
|
NimaSarajpoor marked this conversation as resolved.
|
||
| """ | ||
| Compute the sliding dot product between Q and T | ||
|
|
||
| Parameters | ||
| ---------- | ||
| Q : numpy.ndarray | ||
| Query array or subsequence. | ||
|
|
||
| T : numpy.ndarray | ||
| Time series or sequence. | ||
|
|
||
| conv_block_size : int, default None | ||
| Block size for the overlap-add method. When `conv_block_size` | ||
| is None, it will automatically be set to an optimal value, | ||
| internally computed based on the lengths of Q and T. | ||
|
|
||
| Returns | ||
| ------- | ||
| out : numpy.ndarray | ||
| The sliding dot product between Q and T. | ||
| """ | ||
| if len(Q) == len(T): | ||
| return np.dot(Q, T) | ||
|
NimaSarajpoor marked this conversation as resolved.
|
||
| else: | ||
| return _valid_convolve(Q[::-1], T, conv_block_size=conv_block_size) | ||
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 |
|---|---|---|
|
|
@@ -210,3 +210,18 @@ def test_pyfftw_sdp_max_n(): | |
| np.testing.assert_allclose(comp, ref) | ||
|
|
||
| return | ||
|
|
||
|
|
||
| def test_oaconvolve_sdp_blocksize(): | ||
| from sdp.challenger_sdp import sliding_dot_product | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line needs to be modified if, at a later time, we decide to move the proposal to a new file (module). |
||
|
|
||
| T = np.random.rand(2**10) | ||
| Q = np.random.rand(2**8) | ||
| conv_block_size = 2**9 | ||
|
|
||
| comp = sliding_dot_product(Q, T, conv_block_size=conv_block_size) | ||
| ref = naive_sliding_dot_product(Q, T) | ||
|
|
||
| np.testing.assert_allclose(comp, ref) | ||
|
|
||
| return | ||
Oops, something went wrong.
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.