Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_mass(Q, T):
m = Q.shape[0]
left = np.linalg.norm(
core.z_norm(core.rolling_window(T, m), 1) - core.z_norm(Q), axis=1
)
right = core.mass(Q, T)
npt.assert_almost_equal(left, right)
left : bool
Return the left matrix profile indices if `True`. If `right` is True
then this parameter is ignored.
right : bool
Return the right matrix profiles indices if `True`
Returns
-------
P : ndarray
Matrix profile
I : ndarray
Matrix profile indices
"""
D = core.mass(Q, T, M_T, Σ_T)
if trivial_idx is not None:
zone_start = max(0, trivial_idx - excl_zone)
zone_stop = min(T.shape[0] - Q.shape[0] + 1, trivial_idx + excl_zone)
D[zone_start:zone_stop] = np.inf
# Get left and right matrix profiles
IL = -1
PL = np.inf
if D[:trivial_idx].size:
IL = np.argmin(D[:trivial_idx])
PL = D[IL]
if zone_start <= IL < zone_stop:
IL = -1
IR = -1
PR = np.inf
# Note that the start of the exclusion zone is relative to
# the unchanging length of the matrix profile index
zone_start = max(0, k - excl_zone)
for i, (Q, T) in enumerate(zip(rolling_Qs, rolling_Ts)):
# Egress
# Remove the first element in the matrix profile index
# Shift mp up by one and replace the last row with new values
mp_out[:] = np.roll(mp_out, -1, axis=0)
mp_out[-1, 0] = np.inf
mp_out[-1, 3] = last_idx + i
# Ingress
M_T[:], Σ_T[:] = core.compute_mean_std(T, m)
D[:] = core.mass(Q, T, M_T, Σ_T)
D[zone_start:] = np.inf
# Update nearest neighbor for old data if any old subsequences
# are closer to the newly arrived subsequence
update_idx = np.argwhere(D < mp_out[:, 0]).flatten()
mp_out[update_idx, 0] = D[update_idx]
mp_out[update_idx, 3] = last_idx + i
if i % (skip + 1) == 0:
cac_out[:] = _cac(
mp_out[:, 3] - i - 1,
L,
bidirectional=False,
excl_factor=excl_factor,
custom_iac=custom_iac,
)
Multi-dimensional matrix profile
I : ndarray
Multi-dimensional matrix profile indices
"""
d = T.shape[0]
n = T.shape[1]
k = n - m + 1
P = np.full((d, k), np.inf, dtype="float64")
D = np.empty((d, k), dtype="float64")
I = np.ones((d, k), dtype="int64") * -1
for i in range(d):
D[i, :] = core.mass(Q[i], T[i], M_T[i], Σ_T[i])
zone_start = max(0, trivial_idx - excl_zone)
zone_stop = min(k, trivial_idx + excl_zone)
D[:, zone_start:zone_stop] = np.inf
# Column-wise sort
# row_idx = np.argsort(D, axis=0)
# D = D[row_idx, np.arange(row_idx.shape[1])]
D = np.sort(D, axis=0)
D_prime = np.zeros(k)
for i in range(d):
D_prime = D_prime + D[i]
D_prime_prime = D_prime / (i + 1)
# Element-wise Min
# col_idx = np.argmin([P[i, :], D_prime_prime], axis=0)