Skip to content

Commit 07ba1ee

Browse files
committed
Optimized rolling_window with isfinite calls
1 parent 2fb5d2d commit 07ba1ee

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

stumpy/core.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ def sliding_dot_product(Q, T):
271271

272272

273273
@njit(fastmath={"nsz", "arcp", "contract", "afn", "reassoc"})
274-
def _welford_nanvar(a, w, subseq_a_isfinite):
274+
def _welford_nanvar(a, w, a_subseq_isfinite):
275275
"""
276276
Compute the rolling variance for a 1-D array while ignoring NaNs using a modified
277277
version of Welford's algorithm but is much faster than using `np.nanstd` with stride
@@ -285,6 +285,10 @@ def _welford_nanvar(a, w, subseq_a_isfinite):
285285
w : ndarray
286286
The rolling window size
287287
288+
a_subseq_isfinite : ndarray
289+
A boolean array that describes whether each subequence of length `w` within `a`
290+
is finite.
291+
288292
Returns
289293
-------
290294
all_variances : ndarray
@@ -301,8 +305,8 @@ def _welford_nanvar(a, w, subseq_a_isfinite):
301305

302306
if (
303307
start_idx == 0
304-
or not subseq_a_isfinite[prev_start_idx]
305-
or not subseq_a_isfinite[start_idx]
308+
or not a_subseq_isfinite[prev_start_idx]
309+
or not a_subseq_isfinite[start_idx]
306310
):
307311
curr_mean = np.nanmean(a[start_idx:stop_idx])
308312
curr_var = np.nanvar(a[start_idx:stop_idx])
@@ -347,9 +351,9 @@ def welford_nanvar(a, w=None):
347351
if w is None:
348352
w = a.shape[0]
349353

350-
subseq_a_isfinite = np.all(np.isfinite(rolling_window(a, w)), axis=1)
354+
a_subseq_isfinite = np.all(rolling_window(np.isfinite(a), w), axis=1)
351355

352-
return _welford_nanvar(a, w, subseq_a_isfinite)
356+
return _welford_nanvar(a, w, a_subseq_isfinite)
353357

354358

355359
def welford_nanstd(a, w=None):
@@ -909,7 +913,7 @@ def mass_absolute(Q, T):
909913
if np.any(~np.isfinite(Q)):
910914
distance_profile[:] = np.inf
911915
else:
912-
T_subseq_isfinite = np.all(np.isfinite(rolling_window(T, m)), axis=1)
916+
T_subseq_isfinite = np.all(rolling_window(np.isfinite(T), m), axis=1)
913917
T[~np.isfinite(T)] = 0.0
914918
QT = sliding_dot_product(Q, T)
915919
Q_squared = np.sum(Q * Q)
@@ -1049,7 +1053,7 @@ def preprocess_non_normalized(T, m):
10491053
check_dtype(T)
10501054

10511055
T[np.isinf(T)] = np.nan
1052-
T_subseq_isfinite = np.all(np.isfinite(rolling_window(T, m)), axis=1)
1056+
T_subseq_isfinite = np.all(rolling_window(np.isfinite(T), m), axis=1)
10531057
T[np.isnan(T)] = 0
10541058

10551059
return T, T_subseq_isfinite

0 commit comments

Comments
 (0)