def test_gpu_stump_identical_subsequence_A_B_join(): identical = np.random.rand(8) T_A = np.random.rand(20) T_B = np.random.rand(20) T_A[1:1 + identical.shape[0]] = identical T_B[11:11 + identical.shape[0]] = identical m = 3 ref_mp = naive.stamp(T_B, m, T_B=T_A) comp_mp = gpu_stump(T_B, m, T_A, ignore_trivial=False) naive.replace_inf(ref_mp) naive.replace_inf(comp_mp) npt.assert_almost_equal( ref_mp[:, 0], comp_mp[:, 0], decimal=config.STUMPY_TEST_PRECISION) # ignore indices # comp_mp = gpu_stump(pd.Series(T_B), m, pd.Series(T_A), ignore_trivial=False) # naive.replace_inf(comp_mp) # npt.assert_almost_equal( # ref_mp[:, 0], comp_mp[:, 0], decimal=config.STUMPY_TEST_PRECISION # ) # ignore indices # Swap inputs ref_mp = naive.stamp(T_A, m, T_B=T_B) comp_mp = gpu_stump(T_A, m, T_B, ignore_trivial=False) naive.replace_inf(ref_mp) naive.replace_inf(comp_mp) npt.assert_almost_equal( ref_mp[:, 0], comp_mp[:, 0], decimal=config.STUMPY_TEST_PRECISION) # ignore indices
def test_two_constant_subsequences_A_B_join(): T_A = np.array([0, 0, 0, 0, 0, 1], dtype=np.float64) T_B = np.concatenate( (np.zeros(20, dtype=np.float64), np.ones(5, dtype=np.float64))) m = 3 left = np.array( [utils.naive_mass(Q, T_A, m) for Q in core.rolling_window(T_B, m)], dtype=object) right = gpu_stump(T_A, m, T_B, ignore_trivial=False) utils.replace_inf(left) utils.replace_inf(right) npt.assert_almost_equal(left[:, 0], right[:, 0]) # ignore indices right = gpu_stump(pd.Series(T_A), m, pd.Series(T_B), ignore_trivial=False) utils.replace_inf(right) npt.assert_almost_equal(left[:, 0], right[:, 0]) # ignore indices # Swap inputs left = np.array( [utils.naive_mass(Q, T_B, m) for Q in core.rolling_window(T_A, m)], dtype=object) right = gpu_stump(T_B, m, T_A, ignore_trivial=False) utils.replace_inf(left) utils.replace_inf(right) npt.assert_almost_equal(left[:, 0], right[:, 0]) # ignore indices right = gpu_stump(pd.Series(T_B), m, pd.Series(T_A), ignore_trivial=False) utils.replace_inf(right) npt.assert_almost_equal(left[:, 0], right[:, 0]) # ignore indices
def test_gpu_stump_self_join_larger_window(T_A, T_B): for m in [8, 16, 32]: if len(T_B) > m: zone = int(np.ceil(m / 4)) left = np.array( [ utils.naive_mass(Q, T_B, m, i, zone, True) for i, Q in enumerate(core.rolling_window(T_B, m)) ], dtype=object, ) right = gpu_stump(T_B, m, ignore_trivial=True, threads_per_block=THREADS_PER_BLOCK) utils.replace_inf(left) utils.replace_inf(right) npt.assert_almost_equal(left, right) right = gpu_stump( pd.Series(T_B), m, ignore_trivial=True, threads_per_block=THREADS_PER_BLOCK, ) utils.replace_inf(right) npt.assert_almost_equal(left, right)
def test_parallel_gpu_stump_self_join(T_A, T_B): device_ids = [device.id for device in cuda.list_devices()] if len(T_B) > 10: m = 3 zone = int(np.ceil(m / 4)) left = np.array( [ utils.naive_mass(Q, T_B, m, i, zone, True) for i, Q in enumerate(core.rolling_window(T_B, m)) ], dtype=object, ) right = gpu_stump( T_B, m, ignore_trivial=True, threads_per_block=THREADS_PER_BLOCK, device_id=device_ids, ) utils.replace_inf(left) utils.replace_inf(right) npt.assert_almost_equal(left, right) right = gpu_stump( pd.Series(T_B), m, ignore_trivial=True, threads_per_block=THREADS_PER_BLOCK, device_id=device_ids, ) utils.replace_inf(right) npt.assert_almost_equal(left, right)
def test_parallel_gpu_stump_A_B_join(T_A, T_B): device_ids = [device.id for device in cuda.list_devices()] if len(T_B) > 10: m = 3 left = np.array( [utils.naive_mass(Q, T_A, m) for Q in core.rolling_window(T_B, m)], dtype=object, ) right = gpu_stump( T_A, m, T_B, ignore_trivial=False, threads_per_block=THREADS_PER_BLOCK, device_id=device_ids, ) utils.replace_inf(left) utils.replace_inf(right) npt.assert_almost_equal(left, right) right = gpu_stump( pd.Series(T_A), m, pd.Series(T_B), ignore_trivial=False, threads_per_block=THREADS_PER_BLOCK, device_id=device_ids, ) utils.replace_inf(right) npt.assert_almost_equal(left, right)
def test_gpu_stump_nan_inf_A_B_join( T_A, T_B, substitute_A, substitute_B, substitution_locations ): m = 3 stop = 16 T_A_sub = T_A.copy() T_B_sub = T_B.copy()[:stop] for substitution_location_B in substitution_locations: for substitution_location_A in substitution_locations: T_A_sub[:] = T_A T_B_sub[:] = T_B[:stop] T_A_sub[substitution_location_A] = substitute_A T_B_sub[substitution_location_B] = substitute_B left = naive.stamp(T_A_sub, m, T_B=T_B_sub) right = gpu_stump(T_A_sub, m, T_B_sub, ignore_trivial=False) naive.replace_inf(left) naive.replace_inf(right) npt.assert_almost_equal(left, right) right = gpu_stump( pd.Series(T_A_sub), m, pd.Series(T_B_sub), ignore_trivial=False ) naive.replace_inf(right) npt.assert_almost_equal(left, right)
def test_gpu_stump_two_constant_subsequences_A_B_join(): T_A = np.array([0, 0, 0, 0, 0, 1], dtype=np.float64) T_B = np.concatenate( (np.zeros(20, dtype=np.float64), np.ones(5, dtype=np.float64))) m = 3 left = naive.stamp(T_A, m, T_B=T_B) right = gpu_stump(T_A, m, T_B, ignore_trivial=False) naive.replace_inf(left) naive.replace_inf(right) npt.assert_almost_equal(left[:, 0], right[:, 0]) # ignore indices right = gpu_stump(pd.Series(T_A), m, pd.Series(T_B), ignore_trivial=False) naive.replace_inf(right) npt.assert_almost_equal(left[:, 0], right[:, 0]) # ignore indices # Swap inputs left = naive.stamp(T_B, m, T_B=T_A) right = gpu_stump(T_B, m, T_A, ignore_trivial=False) naive.replace_inf(left) naive.replace_inf(right) npt.assert_almost_equal(left[:, 0], right[:, 0]) # ignore indices right = gpu_stump(pd.Series(T_B), m, pd.Series(T_A), ignore_trivial=False) naive.replace_inf(right) npt.assert_almost_equal(left[:, 0], right[:, 0]) # ignore indices
def test_parallel_gpu_stump_A_B_join(T_A, T_B): device_ids = [device.id for device in cuda.list_devices()] if len(T_B) > 10: m = 3 left = naive.stamp(T_A, m, T_B=T_B) right = gpu_stump( T_A, m, T_B, ignore_trivial=False, device_id=device_ids, ) naive.replace_inf(left) naive.replace_inf(right) npt.assert_almost_equal(left, right) right = gpu_stump( pd.Series(T_A), m, pd.Series(T_B), ignore_trivial=False, device_id=device_ids, ) naive.replace_inf(right) npt.assert_almost_equal(left, right)
def test_gpu_stump_identical_subsequence_A_B_join(): identical = np.random.rand(8) T_A = np.random.rand(20) T_B = np.random.rand(20) T_A[1 : 1 + identical.shape[0]] = identical T_B[11 : 11 + identical.shape[0]] = identical m = 3 left = naive.stamp(T_A, m, T_B=T_B) right = gpu_stump(T_A, m, T_B, ignore_trivial=False) naive.replace_inf(left) naive.replace_inf(right) npt.assert_almost_equal( left[:, 0], right[:, 0], decimal=config.STUMPY_TEST_PRECISION ) # ignore indices right = gpu_stump(pd.Series(T_A), m, pd.Series(T_B), ignore_trivial=False) naive.replace_inf(right) npt.assert_almost_equal( left[:, 0], right[:, 0], decimal=config.STUMPY_TEST_PRECISION ) # ignore indices # Swap inputs left = naive.stamp(T_B, m, T_B=T_A) right = gpu_stump(T_B, m, T_A, ignore_trivial=False) naive.replace_inf(left) naive.replace_inf(right) npt.assert_almost_equal( left[:, 0], right[:, 0], decimal=config.STUMPY_TEST_PRECISION ) # ignore indices right = gpu_stump(pd.Series(T_B), m, pd.Series(T_A), ignore_trivial=False) naive.replace_inf(right) npt.assert_almost_equal( left[:, 0], right[:, 0], decimal=config.STUMPY_TEST_PRECISION ) # ignore indices
def test_gpu_stump_A_B_join(T_A, T_B): m = 3 left = naive.stamp(T_A, m, T_B=T_B) right = gpu_stump(T_A, m, T_B, ignore_trivial=False) naive.replace_inf(left) naive.replace_inf(right) npt.assert_almost_equal(left, right) right = gpu_stump(pd.Series(T_A), m, pd.Series(T_B), ignore_trivial=False) naive.replace_inf(right) npt.assert_almost_equal(left, right)
def test_gpu_stump_self_join(T_A, T_B): m = 3 zone = int(np.ceil(m / 4)) ref_mp = naive.stamp(T_B, m, exclusion_zone=zone) comp_mp = gpu_stump(T_B, m, ignore_trivial=True) naive.replace_inf(ref_mp) naive.replace_inf(comp_mp) npt.assert_almost_equal(ref_mp, comp_mp) comp_mp = gpu_stump(pd.Series(T_B), m, ignore_trivial=True) naive.replace_inf(comp_mp) npt.assert_almost_equal(ref_mp, comp_mp)
def test_gpu_stump_self_join(T_A, T_B): m = 3 zone = int(np.ceil(m / 4)) left = naive.stamp(T_B, m, exclusion_zone=zone) right = gpu_stump(T_B, m, ignore_trivial=True) naive.replace_inf(left) naive.replace_inf(right) npt.assert_almost_equal(left, right) right = gpu_stump(pd.Series(T_B), m, ignore_trivial=True) naive.replace_inf(right) npt.assert_almost_equal(left, right)
def test_stump_A_B_join(T_A, T_B): m = 3 left = np.array( [naive_mass(Q, T_A, m) for Q in core.rolling_window(T_B, m)], dtype=object) right = gpu_stump(T_A, m, T_B, ignore_trivial=False) replace_inf(left) replace_inf(right) npt.assert_almost_equal(left, right) right = gpu_stump(pd.Series(T_A), m, pd.Series(T_B), ignore_trivial=False) replace_inf(right) npt.assert_almost_equal(left, right)
def test_gpu_stump_constant_subsequence_self_join(): T_A = np.concatenate((np.zeros(20, dtype=np.float64), np.ones(5, dtype=np.float64))) m = 3 zone = int(np.ceil(m / 4)) left = naive.stamp(T_A, m, exclusion_zone=zone) right = gpu_stump(T_A, m, ignore_trivial=True) naive.replace_inf(left) naive.replace_inf(right) npt.assert_almost_equal(left[:, 0], right[:, 0]) # ignore indices right = gpu_stump(pd.Series(T_A), m, ignore_trivial=True) naive.replace_inf(right) npt.assert_almost_equal(left[:, 0], right[:, 0]) # ignore indices
def test_gpu_stump_self_join_larger_window(T_A, T_B): for m in [8, 16, 32]: if len(T_B) > m: zone = int(np.ceil(m / 4)) left = naive.stamp(T_B, m, exclusion_zone=zone) right = gpu_stump(T_B, m, ignore_trivial=True) naive.replace_inf(left) naive.replace_inf(right) npt.assert_almost_equal(left, right) right = gpu_stump(pd.Series(T_B), m, ignore_trivial=True,) naive.replace_inf(right) npt.assert_almost_equal(left, right)
def test_parallel_gpu_stump_self_join(T_A, T_B): device_ids = [device.id for device in cuda.list_devices()] if len(T_B) > 10: m = 3 zone = int(np.ceil(m / 4)) left = naive.stamp(T_B, m, exclusion_zone=zone) right = gpu_stump(T_B, m, ignore_trivial=True, device_id=device_ids,) naive.replace_inf(left) naive.replace_inf(right) npt.assert_almost_equal(left, right) right = gpu_stump(pd.Series(T_B), m, ignore_trivial=True, device_id=device_ids,) naive.replace_inf(right) npt.assert_almost_equal(left, right)
def calculate_matrix_profile(column, seq_length): import stumpy try: # stumpy needs np float old_data = np.array(column, dtype=np.floating) except ValueError: raise Exception('Can\'t convert column to float') try: if cuda.is_available(): gpu_device_ids = [device.id for device in cuda.list_devices()] mp = stumpy.gpu_stump(old_data, m=seq_length, ignore_trivial=False, device_id=gpu_device_ids) else: mp = stumpy.stump(old_data, m=seq_length, ignore_trivial=False) except TypeError as e: print('Type issue in stumpy:') raise e except ValueError as e: print('Seq_length issue in stumpy') raise e if pd.isnull(mp).any(): raise Exception( 'Matrix profile for the column contains NaN values. Try to increase the dataset size' ) return mp
def test_gpu_stump_A_B_join(T_A, T_B): m = 3 ref_mp = naive.stamp(T_B, m, T_B=T_A) comp_mp = gpu_stump(T_B, m, T_A, ignore_trivial=False) naive.replace_inf(ref_mp) naive.replace_inf(comp_mp) npt.assert_almost_equal(ref_mp, comp_mp)
def test_gpu_stump_identical_subsequence_self_join(): identical = np.random.rand(8) T_A = np.random.rand(20) T_A[1:1 + identical.shape[0]] = identical T_A[11:11 + identical.shape[0]] = identical m = 3 zone = int(np.ceil(m / 4)) left = naive.stamp(T_A, m, exclusion_zone=zone) right = gpu_stump(T_A, m, ignore_trivial=True) naive.replace_inf(left) naive.replace_inf(right) npt.assert_almost_equal(left[:, 0], right[:, 0], decimal=naive.PRECISION) # ignore indices right = gpu_stump(pd.Series(T_A), m, ignore_trivial=True) naive.replace_inf(right) npt.assert_almost_equal(left[:, 0], right[:, 0], decimal=naive.PRECISION) # ignore indices
def test_stump_self_join(T_A, T_B): m = 3 zone = int(np.ceil(m / 4)) left = np.array( [ naive_mass(Q, T_B, m, i, zone, True) for i, Q in enumerate(core.rolling_window(T_B, m)) ], dtype=object, ) right = gpu_stump(T_B, m, ignore_trivial=True) replace_inf(left) replace_inf(right) npt.assert_almost_equal(left, right) right = gpu_stump(pd.Series(T_B), m, ignore_trivial=True) replace_inf(right) npt.assert_almost_equal(left, right)
def test_gpu_stump_self_join_larger_window(T_A, T_B, m): if len(T_B) > m: zone = int(np.ceil(m / 4)) ref_mp = naive.stamp(T_B, m, exclusion_zone=zone) comp_mp = gpu_stump(T_B, m, ignore_trivial=True) naive.replace_inf(ref_mp) naive.replace_inf(comp_mp) npt.assert_almost_equal(ref_mp, comp_mp)
def test_gpu_stump_constant_subsequence_self_join(): T_A = np.concatenate((np.zeros(20, dtype=np.float64), np.ones(5, dtype=np.float64))) m = 3 zone = int(np.ceil(m / 4)) ref_mp = naive.stamp(T_A, m, exclusion_zone=zone) comp_mp = gpu_stump(T_A, m, ignore_trivial=True) naive.replace_inf(ref_mp) naive.replace_inf(comp_mp) npt.assert_almost_equal(ref_mp[:, 0], comp_mp[:, 0]) # ignore indices
def _transform(self, X, y=None): n_ts, sz, d = X.shape if d > 1: raise NotImplementedError("We currently don't support using " "multi-dimensional matrix profiles " "from the stumpy library.") output_size = sz - self.subsequence_length + 1 X_transformed = np.empty((n_ts, output_size, 1)) if self.implementation == "stump": if not STUMPY_INSTALLED: raise ImportError(stumpy_msg) for i_ts in range(n_ts): result = stumpy.stump(T_A=X[i_ts, :, 0].ravel(), m=self.subsequence_length) X_transformed[i_ts, :, 0] = result[:, 0].astype(np.float) elif self.implementation == "gpu_stump": if not STUMPY_INSTALLED: raise ImportError(stumpy_msg) for i_ts in range(n_ts): result = stumpy.gpu_stump(T_A=X[i_ts, :, 0].ravel(), m=self.subsequence_length) X_transformed[i_ts, :, 0] = result[:, 0].astype(np.float) elif self.implementation == "numpy": scaler = TimeSeriesScalerMeanVariance() band_width = int(np.ceil(self.subsequence_length / 4)) for i_ts in range(n_ts): segments = _series_to_segments(X[i_ts], self.subsequence_length) if self.scale: segments = scaler.fit_transform(segments) n_segments = segments.shape[0] segments_2d = segments.reshape( (-1, self.subsequence_length * d)) dists = squareform(pdist(segments_2d, "euclidean")) band = (np.tri( n_segments, n_segments, band_width, dtype=np.bool ) & ~np.tri( n_segments, n_segments, -(band_width + 1), dtype=np.bool)) dists[band] = np.inf X_transformed[i_ts] = dists.min(axis=1, keepdims=True) else: available_implementations = ["numpy", "stump", "gpu_stump"] raise ValueError( 'This "{}" matrix profile implementation is not' ' recognized. Available implementations are {}.'.format( self.implementation, available_implementations)) return X_transformed
def test_gpu_stump_nan_inf_self_join(T_A, T_B, substitute_B, substitution_locations): m = 3 stop = 16 T_B_sub = T_B.copy()[:stop] for substitution_location_B in substitution_locations: T_B_sub[:] = T_B[:stop] T_B_sub[substitution_location_B] = substitute_B zone = int(np.ceil(m / 4)) left = naive.stamp(T_B_sub, m, exclusion_zone=zone) right = gpu_stump(T_B_sub, m, ignore_trivial=True) naive.replace_inf(left) naive.replace_inf(right) npt.assert_almost_equal(left, right) right = gpu_stump(pd.Series(T_B_sub), m, ignore_trivial=True) naive.replace_inf(right) npt.assert_almost_equal(left, right)
def test_gpu_stump(T, m): if not cuda.is_available(): # pragma: no cover pytest.skip("Skipping Tests No GPUs Available") if T.ndim > 1: T = T.copy() T = T[0] ref = stumpy.gpu_aamp(T, m) comp = stumpy.gpu_stump(T, m, normalize=False) npt.assert_almost_equal(ref, comp)
def test_constant_subsequence_self_join(): T_A = np.concatenate( (np.zeros(20, dtype=np.float64), np.ones(5, dtype=np.float64))) m = 3 zone = int(np.ceil(m / 4)) left = np.array( [ utils.naive_mass(Q, T_A, m, i, zone, True) for i, Q in enumerate(core.rolling_window(T_A, m)) ], dtype=object, ) right = gpu_stump(T_A, m, ignore_trivial=True) utils.replace_inf(left) utils.replace_inf(right) npt.assert_almost_equal(left[:, 0], right[:, 0]) # ignore indices right = gpu_stump(pd.Series(T_A), m, ignore_trivial=True) utils.replace_inf(right) npt.assert_almost_equal(left[:, 0], right[:, 0]) # ignore indices
def test_gpu_stump_one_constant_subsequence_A_B_join(): T_A = np.random.rand(20) T_B = np.concatenate((np.zeros(20, dtype=np.float64), np.ones(5, dtype=np.float64))) m = 3 ref_mp = naive.stamp(T_B, m, T_B=T_A) comp_mp = gpu_stump(T_B, m, T_A, ignore_trivial=False) naive.replace_inf(ref_mp) naive.replace_inf(comp_mp) npt.assert_almost_equal(ref_mp[:, 0], comp_mp[:, 0]) # ignore indices # comp_mp = gpu_stump(pd.Series(T_A), m, pd.Series(T_B), ignore_trivial=False) # naive.replace_inf(comp_mp) # npt.assert_almost_equal(ref_mp[:, 0], comp_mp[:, 0]) # ignore indices # Swap inputs ref_mp = naive.stamp(T_A, m, T_B=T_B) comp_mp = gpu_stump(T_A, m, T_B, ignore_trivial=False) naive.replace_inf(ref_mp) naive.replace_inf(comp_mp) npt.assert_almost_equal(ref_mp[:, 0], comp_mp[:, 0]) # ignore indices
def test_gpu_stump_nan_zero_mean_self_join(): T = np.array([-1, 0, 1, np.inf, 1, 0, -1]) m = 3 zone = int(np.ceil(m / 4)) left = naive.stamp(T, m, exclusion_zone=zone) right = gpu_stump(T, m, ignore_trivial=True) naive.replace_inf(left) naive.replace_inf(right) npt.assert_almost_equal(left, right)
def test_gpu_stump_A_B_join(T_A, T_B): m = 3 left = np.array( [utils.naive_mass(Q, T_A, m) for Q in core.rolling_window(T_B, m)], dtype=object) right = gpu_stump(T_A, m, T_B, ignore_trivial=False, threads_per_block=THREADS_PER_BLOCK) utils.replace_inf(left) utils.replace_inf(right) npt.assert_almost_equal(left, right) right = gpu_stump( pd.Series(T_A), m, pd.Series(T_B), ignore_trivial=False, threads_per_block=THREADS_PER_BLOCK, ) utils.replace_inf(right) npt.assert_almost_equal(left, right)
def test_gpu_stump_identical_subsequence_self_join(): identical = np.random.rand(8) T_A = np.random.rand(20) T_A[1:1 + identical.shape[0]] = identical T_A[11:11 + identical.shape[0]] = identical m = 3 zone = int(np.ceil(m / 4)) ref_mp = naive.stamp(T_A, m, exclusion_zone=zone) comp_mp = gpu_stump(T_A, m, ignore_trivial=True) naive.replace_inf(ref_mp) naive.replace_inf(comp_mp) npt.assert_almost_equal( ref_mp[:, 0], comp_mp[:, 0], decimal=config.STUMPY_TEST_PRECISION) # ignore indices