def test_scrump_nan_zero_mean_self_join(percentages): T = np.array([-1, 0, 1, np.inf, 1, 0, -1]) m = 3 zone = int(np.ceil(m / 4)) for percentage in percentages: seed = np.random.randint(100000) np.random.seed(seed) ref_mp = naive.scrump(T, m, T, percentage, zone, False, None) ref_P = ref_mp[:, 0] ref_I = ref_mp[:, 1] ref_left_I = ref_mp[:, 2] ref_right_I = ref_mp[:, 3] np.random.seed(seed) approx = scrump(T, m, percentage=percentage, pre_scrump=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_scrump_self_join_larger_window(T_A, T_B, m, percentages): if len(T_B) > m: zone = int(np.ceil(m / 4)) for percentage in percentages: seed = np.random.randint(100000) np.random.seed(seed) ref_mp = naive.scrump(T_B, m, T_B, percentage, zone, False, None) ref_P = ref_mp[:, 0] ref_I = ref_mp[:, 1] ref_left_I = ref_mp[:, 2] ref_right_I = ref_mp[:, 3] np.random.seed(seed) approx = scrump(T_B, m, ignore_trivial=True, percentage=percentage, pre_scrump=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_scrump_A_B_join_swap(T_A, T_B, percentages): m = 3 for percentage in percentages: seed = np.random.randint(100000) np.random.seed(seed) ref_mp = naive.scrump(T_B, m, T_A, percentage, None, False, None) ref_P = ref_mp[:, 0] # ref_I = ref_mp[:, 1] ref_left_I = ref_mp[:, 2] ref_right_I = ref_mp[:, 3] np.random.seed(seed) approx = scrump(T_B, m, T_A, ignore_trivial=False, percentage=percentage, pre_scrump=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_P, comp_P) npt.assert_almost_equal(ref_left_I, comp_left_I) npt.assert_almost_equal(ref_right_I, comp_right_I)
def test_stimp_max_m(T): threshold = 0.2 percentage = 0.01 min_m = 3 max_m = 5 n = T.shape[0] - min_m + 1 seed = np.random.randint(100000) np.random.seed(seed) pan = stimp( T, min_m=min_m, max_m=max_m, step=1, percentage=percentage, pre_scrump=True, # normalize=True, ) for i in range(n): pan.update() ref_PAN = np.full((pan.M_.shape[0], T.shape[0]), fill_value=np.inf) np.random.seed(seed) for idx, m in enumerate(pan.M_[:n]): zone = int(np.ceil(m / 4)) s = zone tmp_P, tmp_I = naive.prescrump(T, m, T, s=s, exclusion_zone=zone) ref_mp = naive.scrump(T, m, T, percentage, zone, True, s) for i in range(ref_mp.shape[0]): if tmp_P[i] < ref_mp[i, 0]: ref_mp[i, 0] = tmp_P[i] ref_mp[i, 1] = tmp_I[i] ref_PAN[pan._bfs_indices[idx], :ref_mp.shape[0]] = ref_mp[:, 0] # Compare raw pan cmp_PAN = pan._PAN naive.replace_inf(ref_PAN) naive.replace_inf(cmp_PAN) npt.assert_almost_equal(ref_PAN, cmp_PAN) # Compare transformed pan cmp_pan = pan.PAN_ ref_pan = naive.transform_pan(pan._PAN, pan._M, threshold, pan._bfs_indices, pan._n_processed) naive.replace_inf(ref_pan) naive.replace_inf(cmp_pan) npt.assert_almost_equal(ref_pan, cmp_pan)
def test_stimp_max_m(T): percentage = 0.01 min_m = 3 max_m = 5 n = T.shape[0] - min_m + 1 seed = np.random.randint(100000) np.random.seed(seed) pmp = stimp( T, min_m=min_m, max_m=max_m, step=1, percentage=percentage, pre_scrump=True, normalize=True, ) for i in range(n): pmp.update() ref_P = np.full((pmp.M_.shape[0], T.shape[0]), fill_value=np.inf) ref_I = np.ones((pmp.M_.shape[0], T.shape[0]), dtype=np.int64) * -1 np.random.seed(seed) for idx, m in enumerate(pmp.M_[:n]): zone = int(np.ceil(m / 4)) s = zone tmp_P, tmp_I = naive.prescrump(T, m, T, s=s, exclusion_zone=zone) ref_mp = naive.scrump(T, m, T, percentage, zone, True, s) for i in range(ref_mp.shape[0]): if tmp_P[i] < ref_mp[i, 0]: ref_mp[i, 0] = tmp_P[i] ref_mp[i, 1] = tmp_I[i] ref_P[pmp.bfs_indices_[idx], :ref_mp.shape[0]] = ref_mp[:, 0] ref_I[pmp.bfs_indices_[idx], :ref_mp.shape[0]] = ref_mp[:, 1] comp_P = pmp.P_ comp_I = pmp.I_ naive.replace_inf(ref_P) naive.replace_inf(ref_I) naive.replace_inf(comp_P) naive.replace_inf(comp_I) npt.assert_almost_equal(ref_P, comp_P) npt.assert_almost_equal(ref_I, comp_I)
def test_scrump_plus_plus_A_B_join(T_A, T_B, percentages): m = 3 zone = int(np.ceil(m / 4)) for s in range(1, zone + 1): for percentage in percentages: seed = np.random.randint(100000) np.random.seed(seed) ref_P, ref_I = naive.prescrump(T_A, m, T_B, s=s) ref_mp = naive.scrump(T_A, m, T_B, percentage, None, False, None) for i in range(ref_mp.shape[0]): if ref_P[i] < ref_mp[i, 0]: ref_mp[i, 0] = ref_P[i] ref_mp[i, 1] = ref_I[i] ref_P = ref_mp[:, 0] ref_I = ref_mp[:, 1] ref_left_I = ref_mp[:, 2] ref_right_I = ref_mp[:, 3] approx = scrump( T_A, m, T_B, ignore_trivial=False, percentage=percentage, pre_scrump=True, s=s, ) 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_scrump_nan_inf_self_join(T_A, T_B, substitute, substitution_locations, percentages): m = 3 T_B_sub = T_B.copy() for substitution_location in substitution_locations: T_B_sub[:] = T_B[:] T_B_sub[substitution_location] = substitute zone = int(np.ceil(m / 4)) for percentage in percentages: seed = np.random.randint(100000) np.random.seed(seed) ref_mp = naive.scrump(T_B_sub, m, T_B_sub, percentage, zone, False, None) ref_P = ref_mp[:, 0] ref_I = ref_mp[:, 1] ref_left_I = ref_mp[:, 2] ref_right_I = ref_mp[:, 3] np.random.seed(seed) approx = scrump(T_B_sub, m, percentage=percentage, pre_scrump=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_scrump_constant_subsequence_self_join(percentages): T = np.concatenate((np.zeros(20, dtype=np.float64), np.ones(5, dtype=np.float64))) m = 3 zone = int(np.ceil(m / 4)) for percentage in percentages: seed = np.random.randint(100000) np.random.seed(seed) ref_mp = naive.scrump(T, m, T, percentage, zone, False, None) ref_P = ref_mp[:, 0] ref_I = ref_mp[:, 1] ref_left_I = ref_mp[:, 2] ref_right_I = ref_mp[:, 3] np.random.seed(seed) approx = scrump(T, m, ignore_trivial=True, percentage=percentage, pre_scrump=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_scrump_identical_subsequence_self_join(percentages): identical = np.random.rand(8) T = np.random.rand(20) T[1:1 + identical.shape[0]] = identical T[11:11 + identical.shape[0]] = identical m = 3 zone = int(np.ceil(m / 4)) for percentage in percentages: seed = np.random.randint(100000) np.random.seed(seed) ref_mp = naive.scrump(T, m, T, percentage, zone, False, None) ref_P = ref_mp[:, 0] # ref_I = ref_mp[:, 1] # ref_left_I = ref_mp[:, 2] # ref_right_I = ref_mp[:, 3] np.random.seed(seed) approx = scrump(T, m, ignore_trivial=True, percentage=percentage, pre_scrump=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, decimal=config.STUMPY_TEST_PRECISION)