Skip to content

Commit f50268e

Browse files
committed
Fixed #247 Refactored _compute_diagonal for speed
1 parent 1d98e48 commit f50268e

1 file changed

Lines changed: 43 additions & 9 deletions

File tree

stumpy/stump.py

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ def _compute_diagonal(
2222
μ_Q,
2323
Σ_T_inverse,
2424
σ_Q_inverse,
25-
M_T_m_1,
26-
μ_Q_m_1,
25+
cov_a,
26+
cov_b,
27+
cov_c,
28+
cov_d,
2729
T_A_subseq_isfinite,
2830
T_B_subseq_isfinite,
2931
T_A_subseq_isconstant,
@@ -66,8 +68,17 @@ def _compute_diagonal(
6668
Inverse standard deviation of the query sequence, `Q`, relative to the current
6769
sliding window
6870
69-
M_T_m_1 : ndarray
70-
Sliding mean of time series, `T`, using a window size of `m-1`
71+
cov_a : ndarray
72+
The first covariance term relating T_A[i + k + m - 1] and M_T_m_1[i + k]
73+
74+
cov_b : ndarray
75+
The second covariance term relating T_B[i + m - 1] and μ_Q_m_1[i]
76+
77+
cov_c : ndarray
78+
The third covariance term relating T_A[i + k - 1] and M_T_m_1[i + k]
79+
80+
cov_d : ndarray
81+
The fourth covariance term relating T_B[i - 1] and μ_Q_m_1[i]
7182
7283
μ_Q_m_1 : ndarray
7384
Mean of the query sequence, `Q`, relative to the current sliding window and
@@ -154,10 +165,14 @@ def _compute_diagonal(
154165
* m_inverse
155166
)
156167
else:
168+
# The next lines are equivalent and left for reference
169+
# cov = cov + constant * (
170+
# (T_A[i + k + m - 1] - M_T_m_1[i + k])
171+
# * (T_B[i + m - 1] - μ_Q_m_1[i])
172+
# - (T_A[i + k - 1] - M_T_m_1[i + k]) * (T_B[i - 1] - μ_Q_m_1[i])
173+
# )
157174
cov = cov + constant * (
158-
(T_A[i + k + m - 1] - M_T_m_1[i + k])
159-
* (T_B[i + m - 1] - μ_Q_m_1[i])
160-
- (T_A[i + k - 1] - M_T_m_1[i + k]) * (T_B[i - 1] - μ_Q_m_1[i])
175+
cov_a[i + k] * cov_b[i] - cov_c[i + k] * cov_d[i]
161176
)
162177

163178
if T_A_subseq_isfinite[i + k] and T_B_subseq_isfinite[i]:
@@ -334,6 +349,23 @@ def _stump(
334349
ndist_counts = core._count_diagonal_ndist(diags, m, n_A, n_B)
335350
diags_ranges = core._get_array_ranges(ndist_counts, n_threads)
336351

352+
cov_a = T_A[m - 1 :] - M_T_m_1[:-1]
353+
cov_b = T_B[m - 1 :] - μ_Q_m_1[:-1]
354+
# The next lines are equivalent and left for reference
355+
# cov_c = np.roll(T_A, 1)
356+
# cov_ = cov_c[:M_T_m_1.shape[0]] - M_T_m_1[:]
357+
cov_c = np.empty(M_T_m_1.shape[0])
358+
cov_c[1:] = T_A[: M_T_m_1.shape[0] - 1]
359+
cov_c[0] = T_A[-1]
360+
cov_c[:] = cov_c - M_T_m_1
361+
# The next lines are equivalent and left for reference
362+
# cov_d = np.roll(T_B, 1)
363+
# cov_d = cov_d[:μ_Q_m_1.shape[0]] - μ_Q_m_1[:]
364+
cov_d = np.empty(μ_Q_m_1.shape[0])
365+
cov_d[1:] = T_B[: μ_Q_m_1.shape[0] - 1]
366+
cov_d[0] = T_B[-1]
367+
cov_d[:] = cov_d - μ_Q_m_1
368+
337369
for thread_idx in prange(n_threads):
338370
# Compute and update cov, I within a single thread to avoiding race conditions
339371
_compute_diagonal(
@@ -344,8 +376,10 @@ def _stump(
344376
μ_Q,
345377
Σ_T_inverse,
346378
σ_Q_inverse,
347-
M_T_m_1,
348-
μ_Q_m_1,
379+
cov_a,
380+
cov_b,
381+
cov_c,
382+
cov_d,
349383
T_A_subseq_isfinite,
350384
T_B_subseq_isfinite,
351385
T_A_subseq_isconstant,

0 commit comments

Comments
 (0)