def test_get_QT_kernel(T_A, T_B): m = 3 M_T, Σ_T = core.compute_mean_std(T_B, m) μ_Q, σ_Q = core.compute_mean_std(T_A, m) QT, QT_first = _get_QT(0, T_B, T_A, m) device_T_A = cuda.to_device(T_B) device_T_B = cuda.to_device(T_A) device_M_T = cuda.to_device(M_T) device_Σ_T = cuda.to_device(Σ_T) device_QT_odd = cuda.to_device(QT) device_QT_even = cuda.to_device(QT) device_QT_first = cuda.to_device(QT_first) threads_per_block = THREADS_PER_BLOCK blocks_per_grid = math.ceil(QT_first.shape[0] / threads_per_block) for i in range(1, QT_first.shape[0]): left = core.sliding_dot_product(T_A[i:i + m], T_B) _get_QT_kernel[blocks_per_grid, threads_per_block](i, device_T_A, device_T_B, m, device_QT_even, device_QT_odd, device_QT_first) if i % 2 == 0: right = device_QT_even.copy_to_host() npt.assert_almost_equal(left, right) else: right = device_QT_odd.copy_to_host() npt.assert_almost_equal(left, right)
def test_calculate_distance_profile(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) QT = core.sliding_dot_product(Q, T) μ_Q, σ_Q = core.compute_mean_std(Q, m) M_T, Σ_T = core.compute_mean_std(T, m) right = core.calculate_distance_profile(m, QT, μ_Q, σ_Q, M_T, Σ_T) npt.assert_almost_equal(left, right)
def test_calculate_squared_distance_profile(Q, T): m = Q.shape[0] ref = (np.linalg.norm(core.z_norm(core.rolling_window(T, m), 1) - core.z_norm(Q), axis=1)**2) QT = core.sliding_dot_product(Q, T) μ_Q, σ_Q = core.compute_mean_std(Q, m) M_T, Σ_T = core.compute_mean_std(T, m) comp = core._calculate_squared_distance_profile(m, QT, μ_Q.item(0), σ_Q.item(0), M_T, Σ_T) npt.assert_almost_equal(ref, comp)
def test_compute_mean_std(Q, T): m = Q.shape[0] left_μ_Q, left_σ_Q = naive_compute_mean_std(Q, m) left_M_T, left_Σ_T = naive_compute_mean_std(T, m) right_μ_Q, right_σ_Q = core.compute_mean_std(Q, m) right_M_T, right_Σ_T = core.compute_mean_std(T, m) npt.assert_almost_equal(left_μ_Q, right_μ_Q) npt.assert_almost_equal(left_σ_Q, right_σ_Q) npt.assert_almost_equal(left_M_T, right_M_T) npt.assert_almost_equal(left_Σ_T, right_Σ_T)
def test_compute_mean_std(Q, T): m = Q.shape[0] left_μ_Q = np.sum(Q) / m left_σ_Q = np.sqrt(np.sum(np.square(Q - left_μ_Q) / m)) left_M_T = np.mean(core.rolling_window(T, m), axis=1) left_Σ_T = np.std(core.rolling_window(T, m), axis=1) right_μ_Q, right_σ_Q = core.compute_mean_std(Q, m) right_M_T, right_Σ_T = core.compute_mean_std(T, m) npt.assert_almost_equal(left_μ_Q, right_μ_Q) npt.assert_almost_equal(left_σ_Q, right_σ_Q) npt.assert_almost_equal(left_M_T, right_M_T) npt.assert_almost_equal(left_Σ_T, right_Σ_T)
def test_compute_mean_std(Q, T): m = Q.shape[0] ref_μ_Q, ref_σ_Q = naive_compute_mean_std(Q, m) ref_M_T, ref_Σ_T = naive_compute_mean_std(T, m) comp_μ_Q, comp_σ_Q = core.compute_mean_std(Q, m) comp_M_T, comp_Σ_T = core.compute_mean_std(T, m) npt.assert_almost_equal(ref_μ_Q, comp_μ_Q) npt.assert_almost_equal(ref_σ_Q, comp_σ_Q) npt.assert_almost_equal(ref_M_T, comp_M_T) npt.assert_almost_equal(ref_Σ_T, comp_Σ_T)
def test_compute_mean_std_chunked_many(Q, T): m = Q.shape[0] config.STUMPY_MEAN_STD_NUM_CHUNKS = 128 ref_μ_Q, ref_σ_Q = naive_compute_mean_std(Q, m) ref_M_T, ref_Σ_T = naive_compute_mean_std(T, m) comp_μ_Q, comp_σ_Q = core.compute_mean_std(Q, m) comp_M_T, comp_Σ_T = core.compute_mean_std(T, m) config.STUMPY_MEAN_STD_NUM_CHUNKS = 1 npt.assert_almost_equal(ref_μ_Q, comp_μ_Q) npt.assert_almost_equal(ref_σ_Q, comp_σ_Q) npt.assert_almost_equal(ref_M_T, comp_M_T) npt.assert_almost_equal(ref_Σ_T, comp_Σ_T)
def test_compute_mean_std_chunked_many(Q, T): m = Q.shape[0] config.STUMPY_MEAN_STD_NUM_CHUNKS = 128 left_μ_Q, left_σ_Q = naive_compute_mean_std(Q, m) left_M_T, left_Σ_T = naive_compute_mean_std(T, m) right_μ_Q, right_σ_Q = core.compute_mean_std(Q, m) right_M_T, right_Σ_T = core.compute_mean_std(T, m) config.STUMPY_MEAN_STD_NUM_CHUNKS = 1 npt.assert_almost_equal(left_μ_Q, right_μ_Q) npt.assert_almost_equal(left_σ_Q, right_σ_Q) npt.assert_almost_equal(left_M_T, right_M_T) npt.assert_almost_equal(left_Σ_T, right_Σ_T)
def test_compute_mean_std_multidimensional(Q, T): m = Q.shape[0] Q = np.array([Q, np.random.uniform(-1000, 1000, [Q.shape[0]])]) T = np.array([T, T, np.random.uniform(-1000, 1000, [T.shape[0]])]) left_μ_Q, left_σ_Q = naive_compute_mean_std_multidimensional(Q, m) left_M_T, left_Σ_T = naive_compute_mean_std_multidimensional(T, m) right_μ_Q, right_σ_Q = core.compute_mean_std(Q, m) right_M_T, right_Σ_T = core.compute_mean_std(T, m) npt.assert_almost_equal(left_μ_Q, right_μ_Q) npt.assert_almost_equal(left_σ_Q, right_σ_Q) npt.assert_almost_equal(left_M_T, right_M_T) npt.assert_almost_equal(left_Σ_T, right_Σ_T)
def test_compute_mean_std_multidimensional_chunked_many(Q, T): m = Q.shape[0] Q = np.array([Q, np.random.uniform(-1000, 1000, [Q.shape[0]])]) T = np.array([T, T, np.random.uniform(-1000, 1000, [T.shape[0]])]) config.STUMPY_MEAN_STD_NUM_CHUNKS = 128 left_μ_Q, left_σ_Q = naive_compute_mean_std_multidimensional(Q, m) left_M_T, left_Σ_T = naive_compute_mean_std_multidimensional(T, m) right_μ_Q, right_σ_Q = core.compute_mean_std(Q, m) right_M_T, right_Σ_T = core.compute_mean_std(T, m) config.STUMPY_MEAN_STD_NUM_CHUNKS = 1 npt.assert_almost_equal(left_μ_Q, right_μ_Q) npt.assert_almost_equal(left_σ_Q, right_σ_Q) npt.assert_almost_equal(left_M_T, right_M_T) npt.assert_almost_equal(left_Σ_T, right_Σ_T)
def test_multi_distance_profile(T, m): for query_idx in range(T.shape[0] - m + 1): ref_D = naive.multi_distance_profile(query_idx, T, m) M_T, Σ_T = core.compute_mean_std(T, m) comp_D = multi_distance_profile(query_idx, T, m) npt.assert_almost_equal(ref_D, comp_D)
def test_gpu_calculate_squared_distance_profile(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) left = np.square(left) M_T, Σ_T = core.compute_mean_std(T, m) QT = core.sliding_dot_product(Q, T) μ_Q, σ_Q = core.compute_mean_std(Q, m) QT = cp.asarray(QT) μ_Q = cp.asarray(μ_Q) σ_Q = cp.asarray(σ_Q) M_T = cp.asarray(M_T) Σ_T = cp.asarray(Σ_T) right = _gpu_calculate_squared_distance_profile(m, QT, μ_Q[0], σ_Q[0], M_T, Σ_T) right = cp.asnumpy(right) npt.assert_almost_equal(left, right)
def test_multi_mass(T, m): trivial_idx = 2 Q = T[:, trivial_idx:trivial_idx + m] left = utils.naive_multi_mass(Q, T, m) M_T, Σ_T = core.compute_mean_std(T, m) right = _multi_mass(Q, T, m, M_T, Σ_T) npt.assert_almost_equal(left, right)
def test_multi_mass(T, m): trivial_idx = 2 Q = T[:, trivial_idx : trivial_idx + m] ref = naive.multi_mass(Q, T, m) M_T, Σ_T = core.compute_mean_std(T, m) comp = _multi_mass(Q, T, m, M_T, Σ_T, M_T[:, trivial_idx], Σ_T[:, trivial_idx]) npt.assert_almost_equal(ref, comp, decimal=config.STUMPY_TEST_PRECISION)
def test_multi_mass(T, m): trivial_idx = 2 Q = T[:, trivial_idx:trivial_idx + m] left = naive.multi_mass(Q, T, m) M_T, Σ_T = core.compute_mean_std(T, m) right = _multi_mass(Q, T, m, M_T, Σ_T, M_T[:, trivial_idx], Σ_T[:, trivial_idx]) npt.assert_almost_equal(left, right, decimal=naive.PRECISION)
def test_calculate_squared_distance_kernel(T_A, T_B): m = 3 for i in range(T_A.shape[0] - m + 1): Q = T_A[i:i + m] left = np.linalg.norm(core.z_norm(core.rolling_window(T_B, m), 1) - core.z_norm(Q), axis=1) left = np.square(left) M_T, Σ_T = core.compute_mean_std(T_B, m) QT = core.sliding_dot_product(Q, T_B) μ_Q, σ_Q = core.compute_mean_std(T_A, m) device_M_T = cuda.to_device(M_T) device_Σ_T = cuda.to_device(Σ_T) device_QT_even = cuda.to_device(QT) device_QT_odd = cuda.to_device(QT) device_QT_first = cuda.to_device(QT) device_μ_Q = cuda.to_device(μ_Q) device_σ_Q = cuda.to_device(σ_Q) device_D = cuda.device_array(QT.shape, dtype=np.float64) device_denom = cuda.device_array(QT.shape, dtype=np.float64) threads_per_block = THREADS_PER_BLOCK blocks_per_grid = math.ceil(QT.shape[0] / threads_per_block) _calculate_squared_distance_kernel[blocks_per_grid, threads_per_block]( i, m, device_M_T, device_Σ_T, device_QT_even, device_QT_odd, device_μ_Q, device_σ_Q, device_D, device_denom, ) right = device_D.copy_to_host() npt.assert_almost_equal(left, right)
def test_get_first_mstump_profile(T, m): excl_zone = int(np.ceil(m / 4)) start = 0 left_P, left_I = utils.naive_mstump(T, m, excl_zone) left_P = left_P[start, :] left_I = left_I[start, :] M_T, Σ_T = core.compute_mean_std(T, m) right_P, right_I = _get_first_mstump_profile(start, T, T, m, excl_zone, M_T, Σ_T) npt.assert_almost_equal(left_P, right_P) npt.assert_equal(left_I, right_I)
def test_query_mstump_profile(T, m): excl_zone = int(np.ceil(m / 4)) for query_idx in range(T.shape[0] - m + 1): ref_P, ref_I = naive.mstump(T, m, excl_zone) ref_P = ref_P[:, query_idx] ref_I = ref_I[:, query_idx] M_T, Σ_T = core.compute_mean_std(T, m) comp_P, comp_I = _query_mstump_profile(query_idx, T, T, m, excl_zone, M_T, Σ_T, M_T, Σ_T) npt.assert_almost_equal(ref_P, comp_P) npt.assert_equal(ref_I, comp_I)
def test_prescrump(T): m = 3 zone = int(np.ceil(m / 4)) left = np.array( [ utils.naive_mass(Q, T, m, i, zone, True) for i, Q in enumerate(core.rolling_window(T, m)) ], dtype=object, ) μ, σ = core.compute_mean_std(T, m) # Note that the below code only works for `s=1` right = prescrump(T, m, μ, σ, s=1)
def test_get_first_mstump_profile(T, m): excl_zone = int(np.ceil(m / 4)) start = 0 ref_P, ref_I = naive.mstump(T, m, excl_zone) ref_P = ref_P[start, :] ref_I = ref_I[start, :] M_T, Σ_T = core.compute_mean_std(T, m) comp_P, comp_I = _get_first_mstump_profile(start, T, T, m, excl_zone, M_T, Σ_T, M_T, Σ_T) npt.assert_almost_equal(ref_P, comp_P) npt.assert_equal(ref_I, comp_I)
def test_multi_mass_seeded(): np.random.seed(5) T = np.random.uniform(-1000, 1000, [3, 10]).astype(np.float64) m = 5 trivial_idx = 2 Q = T[:, trivial_idx : trivial_idx + m] ref = naive.multi_mass(Q, T, m) M_T, Σ_T = core.compute_mean_std(T, m) comp = _multi_mass(Q, T, m, M_T, Σ_T, M_T[:, trivial_idx], Σ_T[:, trivial_idx]) npt.assert_almost_equal(ref, comp, decimal=config.STUMPY_TEST_PRECISION)
def test_multi_mass_seeded(): np.random.seed(5) T = np.random.uniform(-1000, 1000, [3, 10]).astype(np.float64) m = 5 trivial_idx = 2 Q = T[:, trivial_idx:trivial_idx + m] left = naive.multi_mass(Q, T, m) M_T, Σ_T = core.compute_mean_std(T, m) right = _multi_mass(Q, T, m, M_T, Σ_T, M_T[:, trivial_idx], Σ_T[:, trivial_idx]) npt.assert_almost_equal(left, right, decimal=naive.PRECISION)
def test_stamp_mass_PI(T_A, T_B): m = 3 trivial_idx = 2 zone = int(np.ceil(m / 2)) Q = T_B[trivial_idx:trivial_idx + m] M_T, Σ_T = core.compute_mean_std(T_B, m) left_P, left_I, left_left_I, left_right_I = naive.mass( Q, T_B, m, trivial_idx=trivial_idx, excl_zone=zone, ignore_trivial=True) right_P, right_I = stamp._mass_PI(Q, T_B, M_T, Σ_T, trivial_idx=trivial_idx, excl_zone=zone) npt.assert_almost_equal(left_P, right_P) npt.assert_almost_equal(left_I, right_I) right_left_P, right_left_I = stamp._mass_PI(Q, T_B, M_T, Σ_T, trivial_idx=trivial_idx, excl_zone=zone, left=True) npt.assert_almost_equal(left_left_I, right_left_I) right_right_P, right_right_I = stamp._mass_PI(Q, T_B, M_T, Σ_T, trivial_idx=trivial_idx, excl_zone=zone, right=True) npt.assert_almost_equal(left_right_I, right_right_I)
def update(self, t): """ Append a single new data point, `t`, to the existing time series `T` and update the matrix profile and matrix profile indices. Parameters ---------- t : float A single new data point to be appended to `T` Notes ----- `DOI: 10.1007/s10618-017-0519-9 \ <https://www.cs.ucr.edu/~eamonn/MP_journal.pdf>`__ See Table V Note that line 11 is missing an important `sqrt` operation! """ n = self._T.shape[0] l = n - self._m + 1 T_new = np.append(self._T, t) QT_new = np.empty(self._QT.shape[0] + 1) S = T_new[l:] t_drop = T_new[l - 1] if np.isinf(t) or np.isnan(t): self._illegal = np.append(self._illegal, True) t = 0 T_new[-1] = 0 S[-1] = 0 else: self._illegal = np.append(self._illegal, False) if np.any(self._illegal[-self._m:]): μ_Q = np.inf σ_Q = np.nan else: μ_Q, σ_Q = core.compute_mean_std(S, self._m) μ_Q = μ_Q[0] σ_Q = σ_Q[0] M_T_new = np.append(self._M_T, μ_Q) Σ_T_new = np.append(self._Σ_T, σ_Q) for j in range(l, 0, -1): QT_new[j] = (self._QT[j - 1] - T_new[j - 1] * t_drop + T_new[j + self._m - 1] * t) QT_new[0] = 0 for j in range(self._m): QT_new[0] = QT_new[0] + T_new[j] * S[j] D = core.calculate_distance_profile(self._m, QT_new, μ_Q, σ_Q, M_T_new, Σ_T_new) if np.any(self._illegal[-self._m:]): D[:] = np.inf core.apply_exclusion_zone(D, D.shape[0] - 1, self._excl_zone) for j in range(l): if D[j] < self._P[j]: self._I[j] = l self._P[j] = D[j] I_last = np.argmin(D) if np.isinf(D[I_last]): I_new = np.append(self._I, -1) P_new = np.append(self._P, np.inf) else: I_new = np.append(self._I, I_last) P_new = np.append(self._P, D[I_last]) left_I_new = np.append(self._left_I, I_last) left_P_new = np.append(self._left_P, D[I_last]) self._T = T_new self._P = P_new self._I = I_new self._left_I = left_I_new self._left_P = left_P_new self._QT = QT_new self._M_T = M_T_new self._Σ_T = Σ_T_new