Skip to content
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

[WIP] Add Top-K Nearest Neighbors for Normalized Matrix Profile #592 #595

Merged
merged 453 commits into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
453 commits
Select commit Hold shift + click to select a range
4ec3c5a
Added test function to test TopK scrump in AB_join
NimaSarajpoor Jun 14, 2022
40132d4
Refactored
NimaSarajpoor Jun 16, 2022
b0132ca
Added definition of parameter k to docstring
NimaSarajpoor Jun 16, 2022
fdfdf07
Improved docstring
NimaSarajpoor Jun 16, 2022
7d9c76a
Removed trailing colon
NimaSarajpoor Jun 16, 2022
2674988
Cleaned code
NimaSarajpoor Jun 16, 2022
a1855a0
Avoided allocating new memory in inner for-loop
NimaSarajpoor Jun 16, 2022
5b561ff
Fixed typos
NimaSarajpoor Jun 16, 2022
3d02bf4
Improved comments
NimaSarajpoor Jun 17, 2022
551d223
Avoided allocating new memory in each iteration
NimaSarajpoor Jun 17, 2022
0de3a28
Same ndim in output regardless of value of k
NimaSarajpoor Jun 17, 2022
d1e95f6
Revised docstrings
NimaSarajpoor Jun 17, 2022
bfc4c8e
Enhanced function to perform shift left as well
NimaSarajpoor Jun 17, 2022
bbcb71f
Enhanced test function to test newly added functionality
NimaSarajpoor Jun 17, 2022
ec889b4
Fixed format
NimaSarajpoor Jun 17, 2022
f7ef962
Fixed format
NimaSarajpoor Jun 17, 2022
9288916
Removed/Renamed intermediate variables
NimaSarajpoor Jun 17, 2022
163a775
Renamed variable for the sake of consistency
NimaSarajpoor Jun 17, 2022
cf3748d
Avoided shape mismatch by reshaping ndarray
NimaSarajpoor Jun 17, 2022
467f4a3
Refactored
NimaSarajpoor Jun 17, 2022
d0f5956
Fixed comment
NimaSarajpoor Jun 17, 2022
80b8594
Refacored and Minor restructuring of lines
NimaSarajpoor Jun 17, 2022
33a96c6
Modified stimp after changing output shape in scrump
NimaSarajpoor Jun 17, 2022
41007f6
Add pragma no cover
NimaSarajpoor Jun 17, 2022
68efe20
Revised Docstrings
NimaSarajpoor Jun 17, 2022
7cbeae9
Fixed docstring
NimaSarajpoor Jun 17, 2022
2a38dbb
Revised docstring
NimaSarajpoor Jun 23, 2022
616332e
Removed unnecessary dangling else
NimaSarajpoor Jun 23, 2022
97a17ce
Removed unnecessary comment
NimaSarajpoor Jun 23, 2022
e55ee07
Revised structure of test function
NimaSarajpoor Jun 23, 2022
b177136
Replaced ravel with flatten to get copy of array
NimaSarajpoor Jun 23, 2022
fc7c210
Changed the type of input parameter and revised docstring
NimaSarajpoor Jun 23, 2022
7a4b46e
Update the value of parameter to match its type
NimaSarajpoor Jun 23, 2022
2622d13
Update the value of parameter to match its type
NimaSarajpoor Jun 23, 2022
fc3be79
Correct format
NimaSarajpoor Jun 23, 2022
6411b7a
Changed output structure of naive.scrump
NimaSarajpoor Jun 23, 2022
71d68c8
Correct format
NimaSarajpoor Jun 23, 2022
3e42343
Add test function for scrump_plus_plus for TopK
NimaSarajpoor Jun 23, 2022
e512a63
Add naive version to merge peason profiles
NimaSarajpoor Jun 24, 2022
382bda2
Add test function for merging pearson profiles
NimaSarajpoor Jun 24, 2022
b0a56f7
Corret format
NimaSarajpoor Jun 24, 2022
b5a4e15
Add performant function to merge pearson profiles
NimaSarajpoor Jun 24, 2022
d4d28fe
Optimize function
NimaSarajpoor Jun 24, 2022
99f2a57
Avoid creating new memory
NimaSarajpoor Jun 24, 2022
855c429
Improve docstring
NimaSarajpoor Jun 24, 2022
9e02bac
Refactored
NimaSarajpoor Jun 24, 2022
be44ab0
Avoid creating new memory in for-loop
NimaSarajpoor Jun 24, 2022
e0ad42a
Update test function
NimaSarajpoor Jun 24, 2022
33c2151
Revise function to make it parallelizable
NimaSarajpoor Jun 24, 2022
4a995d1
Full test and coverage in 1hr
NimaSarajpoor Jun 25, 2022
2c55c88
Revise docstrings
NimaSarajpoor Jun 25, 2022
07b83ab
Revise docstrings
NimaSarajpoor Jun 25, 2022
5be2cf6
Optimize function
NimaSarajpoor Jun 25, 2022
4896fe8
Optimize function
NimaSarajpoor Jun 25, 2022
d9a0a20
Rename variable to improve readability
NimaSarajpoor Jun 25, 2022
eabe0fb
Revise comments
NimaSarajpoor Jun 25, 2022
e298fd3
Improve comments and docstrings
NimaSarajpoor Jun 25, 2022
9afba6c
Correct naive implementation
NimaSarajpoor Jun 25, 2022
05945a0
Enhance naive function to support top matrix profile
NimaSarajpoor Jun 25, 2022
a72aeb7
Enhace performant function to support topk matrix profile
NimaSarajpoor Jun 25, 2022
c7ffdac
Update existing test functions
NimaSarajpoor Jun 25, 2022
34941c2
Correct format
NimaSarajpoor Jun 25, 2022
8cbe308
Fix shape of array
NimaSarajpoor Jun 25, 2022
2b5038d
Fix shape of array
NimaSarajpoor Jun 25, 2022
9cc800a
Add kind keyword for sorting
NimaSarajpoor Jun 25, 2022
5b0b3fe
Fix bugs
NimaSarajpoor Jun 25, 2022
0335294
Remove ineffective inner prange
NimaSarajpoor Jun 26, 2022
6c05e30
Temporarily added parameter k to avoid decorator failure
NimaSarajpoor Jun 26, 2022
23a54ba
Improve comments
NimaSarajpoor Jun 26, 2022
5af6ec0
Improve comments
NimaSarajpoor Jun 26, 2022
26cec6e
Improve docstring
NimaSarajpoor Jun 26, 2022
b177c84
Add KNN test function for stumpi
NimaSarajpoor Jun 26, 2022
c5d2345
Fix shape of output for KNN test
NimaSarajpoor Jun 26, 2022
cdc11c8
Full test and coverage 1 hr
NimaSarajpoor Jun 26, 2022
fa7fa4a
Avoid using searchsort when k is 1
NimaSarajpoor Jun 27, 2022
6a6757c
merge main and resolve conflict
NimaSarajpoor Jun 29, 2022
d41a2e9
Revise code according to top k matrix profile structure
NimaSarajpoor Jun 29, 2022
38f4c1d
Remove if condition
NimaSarajpoor Jun 30, 2022
e4fd875
Improve dosctrings
NimaSarajpoor Jul 1, 2022
0afb3ec
Avoid allocating new memory
NimaSarajpoor Jul 1, 2022
13da458
Avoid allocating new memory
NimaSarajpoor Jul 1, 2022
93b5708
Improve comments
NimaSarajpoor Jul 6, 2022
114c0cc
Remove numpy.where to avoid copying unchanged values
NimaSarajpoor Jul 6, 2022
719aefd
Remove unnecessary trailing colon
NimaSarajpoor Jul 6, 2022
528bf12
Replace negative np.inf with np.NINF
NimaSarajpoor Jul 6, 2022
ce58a59
delete a wrong file
NimaSarajpoor Jul 6, 2022
ba4986b
Avoid advance indexing by using chain slicing so it can be run by njit
NimaSarajpoor Jul 6, 2022
2f0f53c
Improve docstring
NimaSarajpoor Jul 6, 2022
6b49de8
Added gpu_searchsorted checks when GPUs unavailable
seanlaw Jul 6, 2022
0d1e482
Added error checks and pytest ignore warning
seanlaw Jul 6, 2022
9222ab6
Merge branch 'TopK_MatrixProfile' of https://github.com/NimaSarajpoor…
NimaSarajpoor Jul 6, 2022
f72ca7a
Improve docstrings
NimaSarajpoor Jul 7, 2022
af40906
minor changes in if-block and dosctring
NimaSarajpoor Jul 7, 2022
42ec617
Improve docstrings
NimaSarajpoor Jul 7, 2022
0d8f5de
Improve comments
NimaSarajpoor Jul 7, 2022
fe9c4db
minor changes
NimaSarajpoor Jul 7, 2022
9aba6d2
Correct format
NimaSarajpoor Jul 7, 2022
2565c91
Improve docstrings
NimaSarajpoor Jul 7, 2022
283c31e
Merge branch 'main' into TopK_MatrixProfile
NimaSarajpoor Jul 10, 2022
9012235
resolve conflicts and merge main
NimaSarajpoor Jul 10, 2022
0bd70aa
Merge branch 'main' into TopK_MatrixProfile
NimaSarajpoor Jul 10, 2022
a89e214
optimize functions
NimaSarajpoor Jul 10, 2022
9b845b1
Remove redundant import
NimaSarajpoor Jul 10, 2022
85f1226
minor change
NimaSarajpoor Jul 11, 2022
fb6ed07
Revise docstrings
NimaSarajpoor Jul 11, 2022
a2129c7
Merge branch 'main' into TopK_MatrixProfile
seanlaw Jul 11, 2022
9f7b6d8
Fixed black formatting after conflict resolution
seanlaw Jul 11, 2022
8da2920
Resolve conflicts and merge changes
NimaSarajpoor Jul 11, 2022
54d1d1f
Correct docstring
NimaSarajpoor Jul 11, 2022
6735167
Revise docstrings
NimaSarajpoor Jul 16, 2022
0112989
minor change
NimaSarajpoor Jul 16, 2022
ff322a0
Revise comments
NimaSarajpoor Jul 16, 2022
598fcf4
Avoid redundant allocation of memory
NimaSarajpoor Jul 16, 2022
54643e2
Revise docstrings and comments
NimaSarajpoor Jul 16, 2022
9433499
rename variables
NimaSarajpoor Jul 16, 2022
902d7ab
minor correction
NimaSarajpoor Jul 16, 2022
3c16d33
Fix indexing
NimaSarajpoor Jul 18, 2022
9bb8b16
Add new test function
NimaSarajpoor Jul 18, 2022
33c6112
Modify test function
NimaSarajpoor Jul 18, 2022
7993cc2
merge main and resolve conflicts
NimaSarajpoor Jul 18, 2022
30f4bcf
Avoid dumplicate in naive prescrump
NimaSarajpoor Jul 19, 2022
77e56f7
Add parameter assume_unique to handle duplicates
NimaSarajpoor Jul 19, 2022
7a93a7c
Add test function to test for duplicates in topk_merge
NimaSarajpoor Jul 19, 2022
fefcaa9
Add parameter assume_unique to performant merge_topk
NimaSarajpoor Jul 19, 2022
5e9c5fc
fix test function
NimaSarajpoor Jul 19, 2022
9685e44
Fix bug
NimaSarajpoor Jul 19, 2022
9dd452b
Revise prescrump to avoid duplicates
NimaSarajpoor Jul 19, 2022
3d68ae7
Avoid duplocates in scrump
NimaSarajpoor Jul 19, 2022
4c17119
Revise test function to consider new parameter
NimaSarajpoor Jul 19, 2022
2c662a9
Fix bug
NimaSarajpoor Jul 19, 2022
3a0f4da
Revise naive scrump to avoid duplicates
NimaSarajpoor Jul 19, 2022
d8728c9
Add comment
NimaSarajpoor Jul 19, 2022
561b428
minor optimization
NimaSarajpoor Jul 19, 2022
44b85a8
Correct style
NimaSarajpoor Jul 19, 2022
dbdc7c9
Correct style
NimaSarajpoor Jul 19, 2022
19129ab
increase threshold
NimaSarajpoor Jul 19, 2022
5d96bbd
Specifiy kind in sort
NimaSarajpoor Jul 19, 2022
d3a9b31
minor change
NimaSarajpoor Jul 19, 2022
970efc7
specify kind in sort
NimaSarajpoor Jul 20, 2022
cd7fe1a
minor changes
NimaSarajpoor Jul 20, 2022
6ca36d0
De-otpimize if condition
NimaSarajpoor Jul 20, 2022
b1baa76
merge so far solved conflicts
NimaSarajpoor Jul 20, 2022
5d930b2
Update scrump
NimaSarajpoor Jul 20, 2022
4b58765
minor changes
NimaSarajpoor Jul 20, 2022
aaa8ff7
add new test function
NimaSarajpoor Jul 20, 2022
6c8eddc
optimize if condition
NimaSarajpoor Jul 20, 2022
5bb6879
Give priority to PA in case of ties between IA and IB
NimaSarajpoor Jul 21, 2022
fc10e8a
Remove trailing colon
NimaSarajpoor Jul 21, 2022
ef1309b
update test function
NimaSarajpoor Jul 21, 2022
c2fe4d2
revise function to avoid adding new parameter
NimaSarajpoor Jul 21, 2022
99806a9
Update module scrump and improvee its readability
NimaSarajpoor Jul 21, 2022
b57c691
Fix syntax
NimaSarajpoor Jul 21, 2022
e499057
update test functions
NimaSarajpoor Jul 21, 2022
b811319
minor fix
NimaSarajpoor Jul 21, 2022
11ee8de
correct format
NimaSarajpoor Jul 21, 2022
7925119
Improve docstring
NimaSarajpoor Jul 23, 2022
c3b82dd
Avoid overlap while merging matrix profiles
NimaSarajpoor Jul 26, 2022
f073d6c
Add function to find overlapping values
NimaSarajpoor Jul 26, 2022
a514943
replace numpy function with our implementation
NimaSarajpoor Jul 26, 2022
edb62a2
Avoid unnecessary call of a function
NimaSarajpoor Jul 26, 2022
ec020e0
Revise docsting and comment
NimaSarajpoor Jul 26, 2022
7ab480e
Improve test function
NimaSarajpoor Jul 26, 2022
b2bc500
Remove comment
NimaSarajpoor Jul 26, 2022
c156530
Add test function to ensure duplicates are avoided
NimaSarajpoor Jul 26, 2022
5f1acae
Improve comments
NimaSarajpoor Jul 26, 2022
5f9c537
Enhance naive version to avoid duplicates while merging
NimaSarajpoor Jul 26, 2022
f37bc29
Add test function and revise naive version
NimaSarajpoor Jul 26, 2022
dc97a12
Improve code readability and comment
NimaSarajpoor Jul 26, 2022
fa340ba
Update top-k profile by getting insertion index
NimaSarajpoor Jul 28, 2022
d9a997d
Merge nested if statements into one
NimaSarajpoor Jul 28, 2022
a52564f
Remove blank lines
NimaSarajpoor Jul 28, 2022
526618c
Fix typo
NimaSarajpoor Jul 28, 2022
3607711
Improve comment
NimaSarajpoor Jul 28, 2022
1b19a45
Improve comments
NimaSarajpoor Jul 28, 2022
bba35e1
Improve docstring
NimaSarajpoor Jul 28, 2022
6d4d127
Remove unnecessary comments
NimaSarajpoor Jul 28, 2022
e531385
passing copy of variable as input
NimaSarajpoor Jul 28, 2022
8e28aeb
minor change in test functions
NimaSarajpoor Jul 28, 2022
be1d1e7
Correct style
NimaSarajpoor Jul 29, 2022
956fc31
Revise comment
NimaSarajpoor Jul 29, 2022
9b3daef
Remove comment
NimaSarajpoor Jul 29, 2022
6abd601
Revise comment
NimaSarajpoor Jul 29, 2022
680ed2a
Merge branch 'main' into TopK_MatrixProfile
NimaSarajpoor Jul 29, 2022
ff2c06c
Merge branch 'main' into TopK_MatrixProfile
seanlaw Aug 5, 2022
355c8e5
Fix format
NimaSarajpoor Aug 9, 2022
04685c7
Remove unnecessary newline
NimaSarajpoor Aug 9, 2022
ba7b6ca
Return 1D array for matrix profile when `k` is 1
NimaSarajpoor Aug 9, 2022
36a7fcb
Remove unnecessary flattening operatiton on array
NimaSarajpoor Aug 9, 2022
4f1b2dc
Fix comments
NimaSarajpoor Aug 9, 2022
aa52529
Make matrix profile and mp index 1D when k=1
NimaSarajpoor Aug 9, 2022
ab22972
Revise tests functions
NimaSarajpoor Aug 9, 2022
249d928
Improve Docstrings
NimaSarajpoor Aug 9, 2022
5e515c4
Make prescrump output 1D when k is one
NimaSarajpoor Aug 28, 2022
752a22c
minor change
NimaSarajpoor Aug 28, 2022
e1f49af
update test functions
NimaSarajpoor Aug 28, 2022
6e541ea
Modify merge_topk to support 1D input
NimaSarajpoor Aug 28, 2022
354d96f
minor change and fix conflict
NimaSarajpoor Aug 28, 2022
0bff1ae
Fix merge_topk
NimaSarajpoor Aug 28, 2022
39e5ea3
Fix shape of variables in test functions
NimaSarajpoor Aug 28, 2022
e9fd14c
Remove unnecessary flatten operation
NimaSarajpoor Aug 28, 2022
d385829
Update test function for case k=1
NimaSarajpoor Aug 28, 2022
90ab9e3
revise comment
NimaSarajpoor Aug 29, 2022
0b163eb
Avoid using return in the middle of code
NimaSarajpoor Aug 29, 2022
bf6df9b
Add new private function to get 2D ouput when k=1
NimaSarajpoor Aug 30, 2022
e6a05d6
Remove check for 1D in merge_topk
NimaSarajpoor Aug 30, 2022
fe905d2
Revise test functions
NimaSarajpoor Aug 30, 2022
8e8d48b
Revise docstring to provide description for 1D case
NimaSarajpoor Aug 30, 2022
3bebc47
Add overlap check in merge_topk with 1D input
NimaSarajpoor Aug 30, 2022
4fcf797
Add overlap check in 1D and revise docstring
NimaSarajpoor Aug 31, 2022
41097a7
Add separate test function for _merge_topk 1D case
NimaSarajpoor Aug 31, 2022
948d674
Add preprocessing function for prescrump
NimaSarajpoor Aug 31, 2022
391c97d
Update test function
NimaSarajpoor Aug 31, 2022
4d7cccf
fix missing argument
NimaSarajpoor Aug 31, 2022
e8814cf
Fix Docstring
NimaSarajpoor Aug 31, 2022
eb56346
Resolved Merge conflict
NimaSarajpoor Aug 31, 2022
3946915
Put back the missing decorator
NimaSarajpoor Aug 31, 2022
eff9ca4
Add preprocessing function in prescraamp
NimaSarajpoor Sep 1, 2022
666b93e
Revise naive function
NimaSarajpoor Sep 1, 2022
eee6d75
Fix value of imprecision in test functions
NimaSarajpoor Sep 1, 2022
27d229b
create overlaps randomly for test merge_topk in 1D case
NimaSarajpoor Sep 1, 2022
6449d4b
Merge main into this branch
NimaSarajpoor Sep 1, 2022
3b9d1de
Merge main and Resolve conflict
NimaSarajpoor Sep 1, 2022
03f19d8
Revise docstrings
NimaSarajpoor Sep 3, 2022
5907f8b
Merge branch 'main' into TopK_MatrixProfile
NimaSarajpoor Sep 14, 2022
34a5f2d
Merge branch 'TopK_MatrixProfile' of https://github.com/NimaSarajpoor…
NimaSarajpoor Sep 14, 2022
d35de3e
Fix docstrings
NimaSarajpoor Sep 14, 2022
dbf2524
merge main
NimaSarajpoor Oct 13, 2022
0c80852
minor changes
NimaSarajpoor Oct 13, 2022
2e3af6a
minor fix
NimaSarajpoor Oct 13, 2022
a646034
change variable name
NimaSarajpoor Oct 15, 2022
d6a0a3d
change variables names
NimaSarajpoor Oct 15, 2022
6ae95ec
convert attr to property attr to get 1D when k is 1
NimaSarajpoor Oct 15, 2022
73ebe40
avoid calling performant function in a naive function
NimaSarajpoor Oct 15, 2022
4719e2f
minor modification on z_norm functions
NimaSarajpoor Oct 15, 2022
308af69
merge from remote branch
NimaSarajpoor Oct 15, 2022
63b2828
fix function
NimaSarajpoor Oct 15, 2022
3787776
update local branch
NimaSarajpoor Oct 15, 2022
d1f3119
revise docstrings
NimaSarajpoor Oct 18, 2022
4a94c0e
change variable name
NimaSarajpoor Oct 18, 2022
34361f7
Relocate comment
NimaSarajpoor Oct 18, 2022
8d0258a
minor changes
NimaSarajpoor Oct 18, 2022
1b64959
Update branch
NimaSarajpoor Nov 6, 2022
428ef8c
pull latest changes and resolve conflict
NimaSarajpoor Nov 7, 2022
9ad63d3
update local branch
NimaSarajpoor Nov 8, 2022
abb4518
fix uint
NimaSarajpoor Nov 8, 2022
329889e
fixed uint
NimaSarajpoor Nov 8, 2022
6864f11
merge remote branch
NimaSarajpoor Nov 9, 2022
c0e9f74
fixed test function
NimaSarajpoor Nov 9, 2022
27c05c3
fixed calling function
NimaSarajpoor Nov 9, 2022
c45b8a4
Removed redundant return statement
NimaSarajpoor Nov 9, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 26 additions & 21 deletions stumpy/stump.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,14 @@ def _compute_diagonal(
iter_range = range(-g, min(n_A - m + 1, n_B - m + 1 - g))

for i in iter_range:
if i == 0 or (g < 0 and i == -g):
uint64_i = np.uint64(i)
uint64_j = np.uint64(i + g)

if uint64_i == 0 or uint64_j == 0:
cov = (
np.dot(
(T_B[i + g : i + g + m] - M_T[i + g]), (T_A[i : i + m] - μ_Q[i])
(T_B[uint64_j : uint64_j + uint64_m] - M_T[uint64_j]),
(T_A[uint64_i : uint64_i + uint64_m] - μ_Q[uint64_i]),
)
* m_inverse
)
Expand All @@ -200,52 +204,53 @@ def _compute_diagonal(
# - (T_B[i + k - 1] - M_T_m_1[i + k]) * (T_A[i - 1] - μ_Q_m_1[i])
# )
cov = cov + constant * (
cov_a[i + g] * cov_b[i] - cov_c[i + g] * cov_d[i]
cov_a[uint64_j] * cov_b[uint64_i]
- cov_c[uint64_j] * cov_d[uint64_i]
)

if T_B_subseq_isfinite[i + g] and T_A_subseq_isfinite[i]:
if T_B_subseq_isfinite[uint64_j] and T_A_subseq_isfinite[uint64_i]:
# Neither subsequence contains NaNs
if T_B_subseq_isconstant[i + g] or T_A_subseq_isconstant[i]:
if T_B_subseq_isconstant[uint64_j] or T_A_subseq_isconstant[uint64_i]:
pearson = 0.5
else:
pearson = cov * Σ_T_inverse[i + g] * σ_Q_inverse[i]
pearson = cov * Σ_T_inverse[uint64_j] * σ_Q_inverse[uint64_i]

if T_B_subseq_isconstant[i + g] and T_A_subseq_isconstant[i]:
if T_B_subseq_isconstant[uint64_j] and T_A_subseq_isconstant[uint64_i]:
pearson = 1.0

# `ρ[thread_idx, i, :]` is sorted ascendingly and MUST be updated
# when the newly-calculated `pearson` value becomes greater than the
# first (i.e. smallest) element in this array. Note that a higher
# pearson value corresponds to a lower distance.
if pearson > ρ[thread_idx, i, 0]:
NimaSarajpoor marked this conversation as resolved.
Show resolved Hide resolved
idx = np.searchsorted(ρ[thread_idx, i], pearson)
idx = np.searchsorted(ρ[thread_idx, uint64_i], pearson)
core._shift_insert_at_index(
ρ[thread_idx, i], idx, pearson, shift="left"
ρ[thread_idx, uint64_i], idx, pearson, shift="left"
)
core._shift_insert_at_index(
I[thread_idx, i], idx, i + g, shift="left"
I[thread_idx, uint64_i], idx, uint64_j, shift="left"
)

if ignore_trivial: # self-joins only
if pearson > ρ[thread_idx, i + g, 0]:
idx = np.searchsorted(ρ[thread_idx, i + g], pearson)
if pearson > ρ[thread_idx, uint64_j, 0]:
idx = np.searchsorted(ρ[thread_idx, uint64_j], pearson)
core._shift_insert_at_index(
ρ[thread_idx, i + g], idx, pearson, shift="left"
ρ[thread_idx, uint64_j], idx, pearson, shift="left"
)
core._shift_insert_at_index(
I[thread_idx, i + g], idx, i, shift="left"
I[thread_idx, uint64_j], idx, uint64_i, shift="left"
)

if i < i + g:
if uint64_i < uint64_j:
# left pearson correlation and left matrix profile index
if pearson > ρL[thread_idx, i + g]:
ρL[thread_idx, i + g] = pearson
IL[thread_idx, i + g] = i
if pearson > ρL[thread_idx, uint64_j]:
ρL[thread_idx, uint64_j] = pearson
IL[thread_idx, uint64_j] = uint64_i

# right pearson correlation and right matrix profile index
if pearson > ρR[thread_idx, i]:
ρR[thread_idx, i] = pearson
IR[thread_idx, i] = i + g
if pearson > ρR[thread_idx, uint64_i]:
ρR[thread_idx, uint64_i] = pearson
IR[thread_idx, uint64_i] = uint64_j

return
NimaSarajpoor marked this conversation as resolved.
Show resolved Hide resolved

Expand Down
66 changes: 37 additions & 29 deletions tests/naive.py
Original file line number Diff line number Diff line change
Expand Up @@ -1845,25 +1845,30 @@ def merge_topk_PI(PA, PB, IA, IB):
IA[i] = IB[i]
return

k = PA.shape[1]
for i in range(PA.shape[0]):
_, _, overlap_idx_B = np.intersect1d(IA[i], IB[i], return_indices=True)
PB[i, overlap_idx_B] = np.inf
IB[i, overlap_idx_B] = -1
else:
k = PA.shape[1]
for i in range(PA.shape[0]):
_, _, overlap_idx_B = np.intersect1d(IA[i], IB[i], return_indices=True)
PB[i, overlap_idx_B] = np.inf
IB[i, overlap_idx_B] = -1

profile = np.column_stack((PA, PB))
indices = np.column_stack((IA, IB))
IDX = np.argsort(profile, axis=1, kind="mergesort")
profile[:, :] = np.take_along_axis(profile, IDX, axis=1)
indices[:, :] = np.take_along_axis(indices, IDX, axis=1)
profile = np.column_stack((PA, PB))
indices = np.column_stack((IA, IB))
IDX = np.argsort(profile, axis=1, kind="mergesort")
profile[:, :] = np.take_along_axis(profile, IDX, axis=1)
indices[:, :] = np.take_along_axis(indices, IDX, axis=1)

PA[:, :] = profile[:, :k]
IA[:, :] = indices[:, :k]
PA[:, :] = profile[:, :k]
IA[:, :] = indices[:, :k]

return


def merge_topk_ρI(ρA, ρB, IA, IB):
NimaSarajpoor marked this conversation as resolved.
Show resolved Hide resolved
# this is to merge two pearson profiles `ρA` and `ρB`, where each is a 2D array
# and each row is sorted ascendingly. we want to keep top-k largest values in
# This function merges two pearson profiles `ρA` and `ρB`, and updates `ρA`
# and `IA` accordingly. When the inputs are 1D, `ρA[i]` is updated if
# `ρA[i] < ρB[i]` and IA[i] != IB[i]. When the inputs are 2D, each row in
# `ρA` and `ρB` is sorted ascendingly. we want to keep top-k largest values in
# merging row `ρA[i]` and `ρB[i]`.

# In case of ties between `ρA` and `ρB`, the priority is with `ρA`. In case
Expand All @@ -1879,31 +1884,34 @@ def merge_topk_ρI(ρA, ρB, IA, IB):

# For the same example:
# merging `ρB` and `ρA` ascendingly while choosing `ρB` over `ρA` in case of
# ties: [0_B, 0_A, 0'_A, 1_B, 1'_B, 1_A], and we just need to keep the second
# half of this array, and discard the first half.
# ties: [0_B, 0_A, 0'_A, 1_B, 1'_B, 1_A], and the second half of this array
# is the desribale outcome.
if ρA.ndim == 1:
NimaSarajpoor marked this conversation as resolved.
Show resolved Hide resolved
for i in range(ρA.shape[0]):
if ρB[i] > ρA[i] and IB[i] != IA[i]:
ρA[i] = ρB[i]
IA[i] = IB[i]
return

k = ρA.shape[1]
for i in range(ρA.shape[0]):
_, _, overlap_idx_B = np.intersect1d(IA[i], IB[i], return_indices=True)
ρB[i, overlap_idx_B] = np.NINF
IB[i, overlap_idx_B] = -1
else:
k = ρA.shape[1]
for i in range(ρA.shape[0]):
_, _, overlap_idx_B = np.intersect1d(IA[i], IB[i], return_indices=True)
ρB[i, overlap_idx_B] = np.NINF
IB[i, overlap_idx_B] = -1

profile = np.column_stack((ρB, ρA))
indices = np.column_stack((IB, IA))
profile = np.column_stack((ρB, ρA))
indices = np.column_stack((IB, IA))

idx = np.argsort(profile, axis=1, kind="mergesort")
profile[:, :] = np.take_along_axis(profile, idx, axis=1)
indices[:, :] = np.take_along_axis(indices, idx, axis=1)
idx = np.argsort(profile, axis=1, kind="mergesort")
profile[:, :] = np.take_along_axis(profile, idx, axis=1)
indices[:, :] = np.take_along_axis(indices, idx, axis=1)

# keep the last k elements (top-k largest values)
ρA[:, :] = profile[:, k:]
IA[:, :] = indices[:, k:]
# keep the last k elements (top-k largest values)
ρA[:, :] = profile[:, k:]
IA[:, :] = indices[:, k:]

return


def find_matches(D, excl_zone, max_distance, max_matches=None):
Expand Down