def test_aamp_two_constant_subsequences_A_B_join(): T_A = np.concatenate( (np.zeros(10, dtype=np.float64), np.ones(10, dtype=np.float64))) T_B = np.concatenate( (np.zeros(20, dtype=np.float64), np.ones(5, dtype=np.float64))) m = 3 ref_mp = naive.aamp(T_A, m, T_B=T_B) comp_mp = aamp(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 comp_mp = aamp(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.aamp(T_B, m, T_B=T_A) comp_mp = aamp(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 = aamp(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]) # ignore indices
def test_aamp_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.aamp(T_A, m, T_B=T_B) right = aamp(T_A, m, T_B, ignore_trivial=False) naive.replace_inf(left) naive.replace_inf(right) npt.assert_almost_equal(left[:, 0], right[:, 0], config.STUMPY_TEST_PRECISION) # ignore indices right = aamp(pd.Series(T_A), m, pd.Series(T_B), ignore_trivial=False) naive.replace_inf(right) npt.assert_almost_equal(left[:, 0], right[:, 0], config.STUMPY_TEST_PRECISION) # ignore indices # Swap inputs left = naive.aamp(T_B, m, T_B=T_A) right = aamp(T_B, m, T_A, ignore_trivial=False) naive.replace_inf(left) naive.replace_inf(right) npt.assert_almost_equal(left[:, 0], right[:, 0], config.STUMPY_TEST_PRECISION) # ignore indices
def test_gpu_aamp_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.aamp(T_A, m, T_B=T_B) comp_mp = gpu_aamp(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 # comp_mp = gpu_aamp(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], decimal=config.STUMPY_TEST_PRECISION # ) # ignore indices # Swap inputs ref_mp = naive.aamp(T_B, m, T_B=T_A) comp_mp = gpu_aamp(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
def test_aamp_nan_inf_A_B_join(T_A, T_B, substitute_A, substitute_B, substitution_locations): m = 3 T_A_sub = T_A.copy() T_B_sub = T_B.copy() for substitution_location_B in substitution_locations: for substitution_location_A in substitution_locations: T_A_sub[:] = T_A[:] T_B_sub[:] = T_B[:] T_A_sub[substitution_location_A] = substitute_A T_B_sub[substitution_location_B] = substitute_B ref_mp = naive.aamp(T_A_sub, m, T_B=T_B_sub) comp_mp = aamp(T_A_sub, m, T_B_sub, ignore_trivial=False) naive.replace_inf(ref_mp) naive.replace_inf(comp_mp) npt.assert_almost_equal(ref_mp, comp_mp) comp_mp = aamp(pd.Series(T_A_sub), m, pd.Series(T_B_sub), ignore_trivial=False) naive.replace_inf(comp_mp) npt.assert_almost_equal(ref_mp, comp_mp)
def test_aamp_motifs_two_motifs(): # Fix seed, because in some case motifs can be off by an index resulting in test # fails, which is caused since one of the motifs is not repeated perfectly in T. np.random.seed(1234) # The time series is random noise with two motifs for m=10: # * (almost) identical step functions at indices 10, 110 and 210 # * identical linear slopes at indices 70 and 170 T = np.random.normal(size=300) m = 20 T[10:30] = 1 T[12:28] = 2 # This is not part of the motif in the aamp case T[110:130] = 3 T[112:128] = 6 T[120] = 6.6 T[210:230] = 1 T[212:228] = 2 T[220] = 1.9 # naive.distance(naive.z_norm(T[10:30]), naive.z_norm(T[110:130])) = 0.47 # naive.distance(naive.z_norm(T[10:30]), naive.z_norm(T[210:230])) = 0.24 # naive.distance(naive.z_norm(T[110:130]), naive.z_norm(T[210:230])) = 0.72 # Hence T[10:30] is the motif representative for this motif T[70:90] = np.arange(m) * 0.1 T[170:190] = np.arange(m) * 0.1 # naive.distance(naive.z_norm(T[70:90]), naive.z_norm(T[170:190])) = 0.0 max_motifs = 2 mp = naive.aamp(T, m) # left_indices = [[70, 170], [10, 210]] left_profile_values = [ [0.0, 0.0], [ 0.0, naive.distance(T[10:30], T[210:230]), ], ] right_distance_values, right_indices = aamp_motifs( T, mp[:, 0], max_motifs=max_motifs, max_distance=0.5, cutoff=np.inf, ) # We ignore indices because of sorting ambiguities for equal distances. # As long as the distances are correct, the indices will be too. npt.assert_almost_equal(left_profile_values, right_distance_values, decimal=6) # Reset seed np.random.seed(None)
def test_gpu_aamp_A_B_join(T_A, T_B): m = 3 ref_mp = naive.aamp(T_B, m, T_B=T_A) comp_mp = gpu_aamp(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_scraamp_plus_plus_A_B_join_full_swap(T_A, T_B): m = 3 zone = int(np.ceil(m / 4)) ref_mp = naive.aamp(T_B, m, T_B=T_A) ref_P = ref_mp[:, 0] ref_I = ref_mp[:, 1] ref_left_I = ref_mp[:, 2] ref_right_I = ref_mp[:, 3] approx = scraamp( T_B, m, T_B=T_A, ignore_trivial=False, percentage=1.0, pre_scraamp=True, s=zone ) approx.update() comp_P = approx.P_ comp_I = approx.I_ comp_left_I = approx.left_I_ comp_right_I = approx.right_I_ naive.replace_inf(ref_P) naive.replace_inf(comp_P) npt.assert_almost_equal(ref_P, comp_P) npt.assert_almost_equal(ref_I, comp_I) npt.assert_almost_equal(ref_left_I, comp_left_I) npt.assert_almost_equal(ref_right_I, comp_right_I)
def test_aamp_motifs_one_motif(): # The top motif for m=3 is a [0 1 0] at indices 0 and 5, while the occurrence # at index 9 is not a motif in the aamp case. T = np.array( [0.0, 1.0, 0.0, -1.0, -1.0, 0.0, 1.0, 0.0, -0.5, 2.0, 3.0, 2.0]) m = 3 max_motifs = 1 left_indices = [[0, 5]] left_profile_values = [[0.0, 0.0]] for p in [1.0, 2.0, 3.0]: mp = naive.aamp(T, m, p=p) right_distance_values, right_indices = aamp_motifs( T, mp[:, 0], max_motifs=max_motifs, max_distance=0.001, cutoff=np.inf, p=p, ) npt.assert_array_equal(left_indices, right_indices) npt.assert_almost_equal(left_profile_values, right_distance_values, decimal=4)
def test_scraamp_self_join_full_larger_window(T_A, T_B, m): if len(T_B) > m: zone = int(np.ceil(m / 4)) ref_mp = naive.aamp(T_B, m, exclusion_zone=zone) ref_P = ref_mp[:, 0] ref_I = ref_mp[:, 1] ref_left_I = ref_mp[:, 2] ref_right_I = ref_mp[:, 3] approx = scraamp(T_B, m, ignore_trivial=True, percentage=1.0, pre_scraamp=False) approx.update() comp_P = approx.P_ comp_I = approx.I_ comp_left_I = approx.left_I_ comp_right_I = approx.right_I_ naive.replace_inf(ref_P) naive.replace_inf(comp_P) npt.assert_almost_equal(ref_P, comp_P) npt.assert_almost_equal(ref_I, comp_I) npt.assert_almost_equal(ref_left_I, comp_left_I) npt.assert_almost_equal(ref_right_I, comp_right_I)
def test_aampi_self_join(): m = 3 zone = int(np.ceil(m / 4)) seed = np.random.randint(100000) np.random.seed(seed) n = 30 T = np.random.rand(n) stream = aampi(T, m, egress=False) for i in range(34): t = np.random.rand() stream.update(t) right_P = stream.P_ right_I = stream.I_ right_left_P = stream.left_P_ right_left_I = stream.left_I_ left = naive.aamp(stream.T_, m) left_P = left[:, 0] left_I = left[:, 1] left_left_P = np.full(left_P.shape, np.inf) left_left_I = left[:, 2] for i, j in enumerate(left_left_I): if j >= 0: D = core.mass_absolute(stream.T_[i:i + m], stream.T_[j:j + m]) left_left_P[i] = D[0] naive.replace_inf(left_P) naive.replace_inf(left_left_P) naive.replace_inf(right_P) naive.replace_inf(right_left_P) npt.assert_almost_equal(left_P, right_P) npt.assert_almost_equal(left_I, right_I) npt.assert_almost_equal(left_left_P, right_left_P) npt.assert_almost_equal(left_left_I, right_left_I) np.random.seed(seed) n = 30 T = np.random.rand(n) T = pd.Series(T) stream = aampi(T, m, egress=False) for i in range(34): t = np.random.rand() stream.update(t) right_P = stream.P_ right_I = stream.I_ right_left_P = stream.left_P_ right_left_I = stream.left_I_ naive.replace_inf(right_P) naive.replace_inf(right_left_P) npt.assert_almost_equal(left_P, right_P) npt.assert_almost_equal(left_I, right_I) npt.assert_almost_equal(left_left_P, right_left_P) npt.assert_almost_equal(left_left_I, right_left_I)
def test_aampi_self_join(): m = 3 seed = np.random.randint(100000) np.random.seed(seed) n = 30 T = np.random.rand(n) stream = aampi(T, m, egress=False) for i in range(34): t = np.random.rand() stream.update(t) comp_P = stream.P_ comp_I = stream.I_ comp_left_P = stream.left_P_ comp_left_I = stream.left_I_ ref_mp = naive.aamp(stream.T_, m) ref_P = ref_mp[:, 0] ref_I = ref_mp[:, 1] ref_left_P = np.full(ref_P.shape, np.inf) ref_left_I = ref_mp[:, 2] for i, j in enumerate(ref_left_I): if j >= 0: D = core.mass_absolute(stream.T_[i:i + m], stream.T_[j:j + m]) ref_left_P[i] = D[0] naive.replace_inf(ref_P) naive.replace_inf(ref_left_P) naive.replace_inf(comp_P) naive.replace_inf(comp_left_P) npt.assert_almost_equal(ref_P, comp_P) npt.assert_almost_equal(ref_I, comp_I) npt.assert_almost_equal(ref_left_P, comp_left_P) npt.assert_almost_equal(ref_left_I, comp_left_I) np.random.seed(seed) n = 30 T = np.random.rand(n) T = pd.Series(T) stream = aampi(T, m, egress=False) for i in range(34): t = np.random.rand() stream.update(t) comp_P = stream.P_ comp_I = stream.I_ comp_left_P = stream.left_P_ comp_left_I = stream.left_I_ naive.replace_inf(comp_P) naive.replace_inf(comp_left_P) npt.assert_almost_equal(ref_P, comp_P) npt.assert_almost_equal(ref_I, comp_I) npt.assert_almost_equal(ref_left_P, comp_left_P) npt.assert_almost_equal(ref_left_I, comp_left_I)
def test_gpu_aamp_A_B_join(T_A, T_B): m = 3 for p in [1.0, 2.0, 3.0]: ref_mp = naive.aamp(T_B, m, T_B=T_A, p=p) comp_mp = gpu_aamp(T_B, m, T_A, ignore_trivial=False, p=p) naive.replace_inf(ref_mp) naive.replace_inf(comp_mp) npt.assert_almost_equal(ref_mp, comp_mp)
def test_aamped_A_B_join(T_A, T_B, dask_cluster): with Client(dask_cluster) as dask_client: m = 3 left = naive.aamp(T_A, m, T_B=T_B) right = aamped(dask_client, T_A, m, T_B, ignore_trivial=False) naive.replace_inf(left) naive.replace_inf(right) npt.assert_almost_equal(left, right)
def test_aamped_self_join_df(T_A, T_B, dask_cluster): with Client(dask_cluster) as dask_client: m = 3 left = naive.aamp(T_B, m) right = aamped(dask_client, pd.Series(T_B), m) naive.replace_inf(left) naive.replace_inf(right) npt.assert_almost_equal(left, right)
def test_aamped_A_B_join(T_A, T_B, dask_cluster): with Client(dask_cluster) as dask_client: m = 3 ref_mp = naive.aamp(T_A, m, T_B=T_B) comp_mp = aamped(dask_client, T_A, m, T_B, ignore_trivial=False) naive.replace_inf(ref_mp) naive.replace_inf(comp_mp) npt.assert_almost_equal(ref_mp, comp_mp)
def test_aamped_self_join_df(T_A, T_B, dask_cluster): with Client(dask_cluster) as dask_client: m = 3 ref_mp = naive.aamp(T_B, m) comp_mp = aamped(dask_client, pd.Series(T_B), m) naive.replace_inf(ref_mp) naive.replace_inf(comp_mp) npt.assert_almost_equal(ref_mp, comp_mp)
def test_gpu_aamp_self_join_larger_window(T_A, T_B, m): if len(T_B) > m: zone = int(np.ceil(m / 4)) left = naive.aamp(T_B, m, exclusion_zone=zone) right = gpu_aamp(T_B, m, ignore_trivial=True) naive.replace_inf(left) naive.replace_inf(right) npt.assert_almost_equal(left, right)
def test_aamped_self_join(T_A, T_B, dask_cluster): with Client(dask_cluster) as dask_client: m = 3 for p in [1.0, 2.0, 3.0]: ref_mp = naive.aamp(T_B, m, p=p) comp_mp = aamped(dask_client, T_B, m, p=p) naive.replace_inf(ref_mp) naive.replace_inf(comp_mp) npt.assert_almost_equal(ref_mp, comp_mp)
def test_aamped_self_join_larger_window(T_A, T_B, m, dask_cluster): with Client(dask_cluster) as dask_client: if len(T_B) > m: left = naive.aamp(T_B, m) right = aamped(dask_client, T_B, m) naive.replace_inf(left) naive.replace_inf(right) npt.assert_almost_equal(left, right)
def test_gpu_aamp_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.aamp(T_A, m, exclusion_zone=zone) comp_mp = gpu_aamp(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 test_aamped_self_join_larger_window(T_A, T_B, m, dask_cluster): with Client(dask_cluster) as dask_client: if len(T_B) > m: ref_mp = naive.aamp(T_B, m) comp_mp = aamped(dask_client, T_B, m) naive.replace_inf(ref_mp) naive.replace_inf(comp_mp) npt.assert_almost_equal(ref_mp, comp_mp)
def test_gpu_aamp_self_join_larger_window(T_A, T_B, m): if len(T_B) > m: zone = int(np.ceil(m / 4)) ref_mp = naive.aamp(T_B, m, exclusion_zone=zone) comp_mp = gpu_aamp(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_aamped_one_constant_subsequence_self_join_df(dask_cluster): with Client(dask_cluster) as dask_client: T_A = np.concatenate( (np.zeros(20, dtype=np.float64), np.ones(5, dtype=np.float64))) m = 3 ref_mp = naive.aamp(T_A, m) comp_mp = aamped(dask_client, pd.Series(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 test_aampdist_vect_k(T_A, T_B, k): m = 3 n_A = T_A.shape[0] n_B = T_B.shape[0] j = n_A - m + 1 # `k` is reserved for `P_ABBA` selection P_ABBA = np.empty(2 * j, dtype=np.float64) ref_mpdist_vect = np.empty(n_B - n_A + 1) k = min(int(k), P_ABBA.shape[0] - 1) for i in range(n_B - n_A + 1): P_ABBA[:j] = naive.aamp(T_A, m, T_B[i : i + n_A])[:, 0] P_ABBA[j:] = naive.aamp(T_B[i : i + n_A], m, T_A)[:, 0] P_ABBA.sort() ref_mpdist_vect[i] = P_ABBA[min(k, P_ABBA.shape[0] - 1)] comp_mpdist_vect = _aampdist_vect(T_A, T_B, m, k=k) npt.assert_almost_equal(ref_mpdist_vect, comp_mpdist_vect)
def test_aamp_nan_zero_mean_self_join(): T = np.array([-1, 0, 1, np.inf, 1, 0, -1]) m = 3 ref_mp = naive.aamp(T, m) comp_mp = aamp(T, 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_aamp_nan_zero_mean_self_join(): T = np.array([-1, 0, 1, np.inf, 1, 0, -1]) m = 3 zone = int(np.ceil(m / 4)) ref_mp = naive.aamp(T, m, exclusion_zone=zone) comp_mp = gpu_aamp(T, m, ignore_trivial=True) naive.replace_inf(ref_mp) naive.replace_inf(comp_mp) npt.assert_almost_equal(ref_mp, comp_mp)
def test_aamp_self_join(T_A, T_B): m = 3 ref_mp = naive.aamp(T_B, m) comp_mp = aamp(T_B, m) naive.replace_inf(ref_mp) naive.replace_inf(comp_mp) npt.assert_almost_equal(ref_mp, comp_mp) comp_mp = aamp(pd.Series(T_B), m) naive.replace_inf(comp_mp) npt.assert_almost_equal(ref_mp, comp_mp)
def test_aamped_nan_zero_mean_self_join(dask_cluster): with Client(dask_cluster) as dask_client: T = np.array([-1, 0, 1, np.inf, 1, 0, -1]) m = 3 ref_mp = naive.aamp(T, m) comp_mp = aamped(dask_client, T, m, ignore_trivial=True) naive.replace_inf(ref_mp) naive.replace_inf(comp_mp) npt.assert_almost_equal(ref_mp, comp_mp)
def test_aamp_A_B_join(T_A, T_B): m = 3 ref_mp = naive.aamp(T_A, m, T_B=T_B) comp_mp = aamp(T_A, m, T_B, ignore_trivial=False) naive.replace_inf(ref_mp) naive.replace_inf(comp_mp) npt.assert_almost_equal(ref_mp, comp_mp) comp_mp = aamp(pd.Series(T_A), m, pd.Series(T_B), ignore_trivial=False) naive.replace_inf(comp_mp) npt.assert_almost_equal(ref_mp, comp_mp)
def test_aamped_one_constant_subsequence_A_B_join(dask_cluster): with Client(dask_cluster) as dask_client: 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.aamp(T_A, m, T_B=T_B) comp_mp = aamped(dask_client, 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