Skip to content

Commit

Permalink
Replaced k = len(T) - m + 1 with l where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
seanlaw committed Nov 20, 2023
1 parent 58ccc69 commit 21840d7
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 57 deletions.
18 changes: 9 additions & 9 deletions stumpy/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,9 +608,9 @@ def _sliding_dot_product(Q, T):
Sliding dot product between `Q` and `T`.
"""
m = Q.shape[0]
k = T.shape[0] - m + 1
out = np.empty(k)
for i in range(k):
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
Expand Down Expand Up @@ -1252,22 +1252,22 @@ def _p_norm_distance_profile(Q, T, p=2.0):
p-normalized distance profile between `Q` and `T`
"""
m = Q.shape[0]
k = T.shape[0] - m + 1
p_norm_profile = np.empty(k, dtype=np.float64)
l = T.shape[0] - m + 1
p_norm_profile = np.empty(l, dtype=np.float64)

if p == 2.0:
Q_squared = np.sum(Q * Q)
T_squared = np.empty(k, dtype=np.float64)
T_squared = np.empty(l, dtype=np.float64)
T_squared[0] = np.sum(T[:m] * T[:m])
for i in range(1, k):
for i in range(1, l):
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)
for i in range(k):
for i in range(l):
p_norm_profile[i] = Q_squared + T_squared[i] - 2.0 * QT[i]
else:
for i in range(k):
for i in range(l):
p_norm_profile[i] = np.sum(np.power(np.abs(Q - T[i : i + m]), p))

return p_norm_profile
Expand Down
24 changes: 12 additions & 12 deletions stumpy/maamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ def _multi_mass_absolute(Q, T, m, Q_subseq_isfinite, T_subseq_isfinite, p=2.0):
profile
"""
d, n = T.shape
k = n - m + 1
l = n - m + 1

D = np.empty((d, k), dtype=np.float64)
D = np.empty((d, l), dtype=np.float64)

for i in range(d):
if np.any(~Q_subseq_isfinite[i]):
Expand Down Expand Up @@ -364,7 +364,7 @@ def _maamp_multi_distance_profile(
`query_idx`
"""
d, n = T_A.shape
k = n - m + 1
l = n - m + 1
start_row_idx = 0
D = _multi_mass_absolute(
T_B[:, query_idx : query_idx + m],
Expand All @@ -384,7 +384,7 @@ def _maamp_multi_distance_profile(
else:
D[start_row_idx:].sort(axis=0, kind="mergesort")

D_prime = np.zeros(k, dtype=np.float64)
D_prime = np.zeros(l, dtype=np.float64)
for i in range(d):
D_prime[:] = D_prime + D[i]
D[i, :] = D_prime / (i + 1)
Expand Down Expand Up @@ -578,10 +578,10 @@ def _get_multi_p_norm(start, T, m, p=2.0):
Multi-dimensional p-norm for the first window
"""
d = T.shape[0]
k = T.shape[1] - m + 1
l = T.shape[1] - m + 1

p_norm = np.empty((d, k), dtype=np.float64)
p_norm_first = np.empty((d, k), dtype=np.float64)
p_norm = np.empty((d, l), dtype=np.float64)
p_norm_first = np.empty((d, l), dtype=np.float64)
for i in range(d):
p_norm[i] = np.power(core.mass_absolute(T[i, start : start + m], T[i], p=p), p)
p_norm_first[i] = np.power(core.mass_absolute(T[i, :m], T[i], p=p), p)
Expand Down Expand Up @@ -940,16 +940,16 @@ def maamp(T, m, include=None, discords=False, p=2.0):
include = core._preprocess_include(include)

d, n = T_B.shape
k = n - m + 1
l = n - m + 1
excl_zone = int(
np.ceil(m / config.STUMPY_EXCL_ZONE_DENOM)
) # See Definition 3 and Figure 3

P = np.empty((d, k), dtype=np.float64)
I = np.empty((d, k), dtype=np.int64)
P = np.empty((d, l), dtype=np.float64)
I = np.empty((d, l), dtype=np.int64)

start = 0
stop = k
stop = l

P[:, start], I[:, start] = _get_first_maamp_profile(
start,
Expand All @@ -975,7 +975,7 @@ def maamp(T, m, include=None, discords=False, p=2.0):
p,
p_norm,
p_norm_first,
k,
l,
start + 1,
include,
discords,
Expand Down
22 changes: 11 additions & 11 deletions stumpy/maamped.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,16 @@ def _dask_maamped(
corresponds to each matrix profile index for a given dimension.
"""
d, n = T_B.shape
k = n - m + 1
P = np.empty((d, k), dtype=np.float64)
I = np.empty((d, k), dtype=np.int64)
l = n - m + 1
P = np.empty((d, l), dtype=np.float64)
I = np.empty((d, l), dtype=np.int64)

hosts = list(dask_client.ncores().keys())
nworkers = len(hosts)

step = 1 + k // nworkers
step = 1 + l // nworkers

for i, start in enumerate(range(0, k, step)):
for i, start in enumerate(range(0, l, step)):
P[:, start], I[:, start] = _get_first_maamp_profile(
start,
T_A,
Expand All @@ -123,7 +123,7 @@ def _dask_maamped(
p_norm_futures = []
p_norm_first_futures = []

for i, start in enumerate(range(0, k, step)):
for i, start in enumerate(range(0, l, step)):
p_norm, p_norm_first = _get_multi_p_norm(start, T_A, m)

p_norm_future = dask_client.scatter(p_norm, workers=[hosts[i]], hash=False)
Expand All @@ -135,8 +135,8 @@ def _dask_maamped(
p_norm_first_futures.append(p_norm_first_future)

futures = []
for i, start in enumerate(range(0, k, step)):
stop = min(k, start + step)
for i, start in enumerate(range(0, l, step)):
stop = min(l, start + step)

futures.append(
dask_client.submit(
Expand All @@ -150,16 +150,16 @@ def _dask_maamped(
p,
p_norm_futures[i],
p_norm_first_futures[i],
k,
l,
start + 1,
include,
discords,
)
)

results = dask_client.gather(futures)
for i, start in enumerate(range(0, k, step)):
stop = min(k, start + step)
for i, start in enumerate(range(0, l, step)):
stop = min(l, start + step)
P[:, start + 1 : stop], I[:, start + 1 : stop] = results[i]

return P, I
Expand Down
24 changes: 12 additions & 12 deletions stumpy/mstump.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ def _multi_mass(
Multi-dimensional distance profile
"""
d, n = T.shape
k = n - m + 1
l = n - m + 1

D = np.empty((d, k), dtype=np.float64)
D = np.empty((d, l), dtype=np.float64)

for i in range(d):
if np.isinf(μ_Q[i]):
Expand Down Expand Up @@ -508,7 +508,7 @@ def _multi_distance_profile(
`query_idx`
"""
d, n = T_A.shape
k = n - m + 1
l = n - m + 1
start_row_idx = 0

D = _multi_mass(
Expand All @@ -533,7 +533,7 @@ def _multi_distance_profile(
else:
D[start_row_idx:].sort(axis=0, kind="mergesort")

D_prime = np.zeros(k, dtype=np.float64)
D_prime = np.zeros(l, dtype=np.float64)
for i in range(d):
D_prime[:] = D_prime + D[i]
D[i, :] = D_prime / (i + 1)
Expand Down Expand Up @@ -788,10 +788,10 @@ def _get_multi_QT(start, T, m):
Multi-dimensional QT for the first window
"""
d = T.shape[0]
k = T.shape[1] - m + 1
l = T.shape[1] - m + 1

QT = np.empty((d, k), dtype=np.float64)
QT_first = np.empty((d, k), dtype=np.float64)
QT = np.empty((d, l), dtype=np.float64)
QT_first = np.empty((d, l), dtype=np.float64)

for i in range(d):
QT[i] = core.sliding_dot_product(T[i, start : start + m], T[i])
Expand Down Expand Up @@ -1225,16 +1225,16 @@ def mstump(
include = core._preprocess_include(include)

d, n = T_B.shape
k = n - m + 1
l = n - m + 1
excl_zone = int(
np.ceil(m / config.STUMPY_EXCL_ZONE_DENOM)
) # See Definition 3 and Figure 3

P = np.empty((d, k), dtype=np.float64)
I = np.empty((d, k), dtype=np.int64)
P = np.empty((d, l), dtype=np.float64)
I = np.empty((d, l), dtype=np.int64)

start = 0
stop = k
stop = l

P[:, start], I[:, start] = _get_first_mstump_profile(
start,
Expand Down Expand Up @@ -1267,7 +1267,7 @@ def mstump(
σ_Q,
T_subseq_isconstant,
Q_subseq_isconstant,
k,
l,
start + 1,
include,
discords,
Expand Down
22 changes: 11 additions & 11 deletions stumpy/mstumped.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,16 @@ def _dask_mstumped(
corresponds to each matrix profile index for a given dimension.
"""
d, n = T_B.shape
k = n - m + 1
P = np.empty((d, k), dtype=np.float64)
I = np.empty((d, k), dtype=np.int64)
l = n - m + 1
P = np.empty((d, l), dtype=np.float64)
I = np.empty((d, l), dtype=np.int64)

hosts = list(dask_client.ncores().keys())
nworkers = len(hosts)

step = 1 + k // nworkers
step = 1 + l // nworkers

for i, start in enumerate(range(0, k, step)):
for i, start in enumerate(range(0, l, step)):
P[:, start], I[:, start] = _get_first_mstump_profile(
start,
T_A,
Expand Down Expand Up @@ -142,7 +142,7 @@ def _dask_mstumped(
QT_futures = []
QT_first_futures = []

for i, start in enumerate(range(0, k, step)):
for i, start in enumerate(range(0, l, step)):
QT, QT_first = _get_multi_QT(start, T_A, m)

QT_future = dask_client.scatter(QT, workers=[hosts[i]], hash=False)
Expand All @@ -152,8 +152,8 @@ def _dask_mstumped(
QT_first_futures.append(QT_first_future)

futures = []
for i, start in enumerate(range(0, k, step)):
stop = min(k, start + step)
for i, start in enumerate(range(0, l, step)):
stop = min(l, start + step)

futures.append(
dask_client.submit(
Expand All @@ -170,16 +170,16 @@ def _dask_mstumped(
σ_Q_future,
T_subseq_isconstant_future,
Q_subseq_isconstant_future,
k,
l,
start + 1,
include,
discords,
)
)

results = dask_client.gather(futures)
for i, start in enumerate(range(0, k, step)):
stop = min(k, start + step)
for i, start in enumerate(range(0, l, step)):
stop = min(l, start + step)
P[:, start + 1 : stop], I[:, start + 1 : stop] = results[i]

return P, I
Expand Down
4 changes: 2 additions & 2 deletions stumpy/stomp.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ def _stomp(T_A, m, T_B=None, ignore_trivial=True):
QT = core.sliding_dot_product(T_A[:m], T_B)
QT_first = core.sliding_dot_product(T_B[:m], T_A)

k = T_B.shape[0] - m + 1
w = T_B.shape[0] - m + 1
for i in range(1, l):
QT[1:] = (
QT[: k - 1] - T_A[i - 1] * T_B[: k - 1] + T_A[i - 1 + m] * T_B[-(k - 1) :]
QT[: w - 1] - T_A[i - 1] * T_B[: w - 1] + T_A[i - 1 + m] * T_B[-(w - 1) :]
)
QT[0] = QT_first[i]

Expand Down

0 comments on commit 21840d7

Please sign in to comment.