Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
57 changes: 5 additions & 52 deletions stumpy/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
from numba import cuda, njit, prange
from scipy import linalg
from scipy.ndimage import maximum_filter1d, minimum_filter1d
from scipy.signal import convolve
from scipy.spatial.distance import cdist

from . import config
from . import config, sdp

try:
from numba.cuda.cudadrv.driver import _raise_driver_not_found
Expand Down Expand Up @@ -649,36 +648,9 @@ def check_window_size(m, max_size=None, n=None):
warnings.warn(msg)


@njit(fastmath=config.STUMPY_FASTMATH_TRUE)
def _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 sliding_dot_product(Q, T):
"""
Use FFT convolution to calculate the sliding window dot product.
Calculate the sliding window dot product.

Parameters
----------
Expand All @@ -692,27 +664,8 @@ def sliding_dot_product(Q, T):
-------
output : numpy.ndarray
Sliding dot product between `Q` and `T`.

Notes
-----
Calculate the sliding dot product

`DOI: 10.1109/ICDM.2016.0179 \
<https://www.cs.ucr.edu/~eamonn/PID4481997_extend_Matrix%20Profile_I.pdf>`__

See Table I, Figure 4

Following the inverse FFT, Fig. 4 states that only cells [m-1:n]
contain valid dot products

Padding is done automatically in fftconvolve step
"""
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]
return sdp._sliding_dot_product(Q, T)
Copy link
Collaborator Author

@NimaSarajpoor NimaSarajpoor Feb 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@seanlaw
Regarding core.sliding_dot_prodiuct: Is this what you meant when you said in this comment (see notes on PR 1):

  1. Make core.sliding_dot_product = sdp._sliding_dot_product

?


I think we should handle sdp._convolve_sliding_dot_product (see PR 2 in this comment) in this PR. The function core.sliding_dot_product in the branch main can be copied to the sdp module, and renamed to _convolve_sliding_dot_product. sdp._sliding_dot_product should point to sdp._convolve_sliding_dot_product. This is not a serious issue if the plan is to handle PR 2 quickly right after PR 1.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that makes perfect sense. It's not actually new code or new SDP methods. It's simply a refactoring of existing code

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sdp._sliding_dot_product should point to sdp._convolve_sliding_dot_product

And then, in subsequent PRs, it should point to multiple functions using some branching logic, right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct 👍



@njit(
Expand Down Expand Up @@ -1327,7 +1280,7 @@ def _p_norm_distance_profile(Q, T, p=2.0):
T_squared[i] = (
T_squared[i - 1] - T[i - 1] * T[i - 1] + T[i + m - 1] * T[i + m - 1]
)
QT = _sliding_dot_product(Q, T)
QT = sdp._njit_sliding_dot_product(Q, T)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the name being sdp._njit_sliding_dot_product, it actually makes logical sense. This _p_norm_distance_profile is an NJIT decorated function and, therefore, it MUST use an NJIT capable SDP function! I like that this distinction is clear and obvious

While core.sliding_dot_product is NOT necessarily an NJIT function (it could be but doesn't have to be).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that this distinction is clear and obvious

While core.sliding_dot_product is NOT necessarily an NJIT function (it could be but doesn't have to be).

YES, and YES!!!

for i in range(l):
p_norm_profile[i] = Q_squared + T_squared[i] - 2.0 * QT[i]
else:
Expand Down Expand Up @@ -1900,7 +1853,7 @@ def _mass_distance_matrix(
if np.any(~np.isfinite(Q[i : i + m])): # pragma: no cover
distance_matrix[i, :] = np.inf
else:
QT = _sliding_dot_product(Q[i : i + m], T)
QT = sdp._njit_sliding_dot_product(Q[i : i + m], T)
distance_matrix[i, :] = _mass(
Q[i : i + m],
T,
Expand Down
4 changes: 2 additions & 2 deletions stumpy/scrump.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import numpy as np
from numba import njit, prange

from . import config, core
from . import config, core, sdp
from .scraamp import prescraamp, scraamp
from .stump import _stump

Expand Down Expand Up @@ -235,7 +235,7 @@ def _compute_PI(
QT = np.empty(w, dtype=np.float64)
for i in indices[start:stop]:
Q = T_A[i : i + m]
QT[:] = core._sliding_dot_product(Q, T_B)
QT[:] = sdp._njit_sliding_dot_product(Q, T_B)
squared_distance_profile[:] = core._calculate_squared_distance_profile(
m,
QT,
Expand Down
52 changes: 52 additions & 0 deletions stumpy/sdp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import numpy as np
from numba import njit

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 _sliding_dot_product(Q, T):
"""
A wrapper function for the 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`.
"""
return _njit_sliding_dot_product(Q, T)
7 changes: 0 additions & 7 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,6 @@ def test_check_window_size_excl_zone():
core.check_window_size(m, max_size=len(T), n=len(T))


@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 = core._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)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_precision.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import pytest
from numba import cuda

from stumpy import cache, config, core, fastmath
from stumpy import cache, config, core, fastmath, sdp

if cuda.is_available():
from stumpy.gpu_stump import gpu_stump
Expand Down Expand Up @@ -90,7 +90,7 @@ def test_calculate_squared_distance():
k = n - m + 1
for i in range(k):
for j in range(k):
QT_i = core._sliding_dot_product(T[i : i + m], T)
QT_i = sdp._njit_sliding_dot_product(T[i : i + m], T)
dist_ij = core._calculate_squared_distance(
m,
QT_i[j],
Expand All @@ -102,7 +102,7 @@ def test_calculate_squared_distance():
T_subseq_isconstant[j],
)

QT_j = core._sliding_dot_product(T[j : j + m], T)
QT_j = sdp._njit_sliding_dot_product(T[j : j + m], T)
dist_ji = core._calculate_squared_distance(
m,
QT_j[i],
Expand Down
37 changes: 37 additions & 0 deletions tests/test_sdp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import numpy as np
import pytest
from numpy import testing as npt

from stumpy import sdp


def naive_rolling_window_dot_product(Q, T):
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a similar function in tests/test_core.py. Refactor and put this function in tests/naive.py.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only reason why I didn't want to put it in naive.py was because it wasn't actually being used in any other file except for test_core.py. The only time I would move a function to naive.py is when it was used across multiple test files.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only time I would move a function to naive.py is when it was used across multiple test files.

Noted. Since I am now using it in tests/test_sdp.py too, it makes sense to just move it to naive.py. Is that correct?

window = len(Q)
result = np.zeros(len(T) - window + 1)
for i in range(len(result)):
result[i] = np.dot(T[i : i + window], Q)
return result


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_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)