Пример #1
0
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
    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],
                            config.STUMPY_TEST_PRECISION)  # 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],
                            config.STUMPY_TEST_PRECISION)  # 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],
                            config.STUMPY_TEST_PRECISION)  # ignore indices
Пример #2
0
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)
Пример #3
0
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
Пример #4
0
def test_aamp_A_B_join(T_A, T_B):
    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, right)

    right = aamp(pd.Series(T_A), m, pd.Series(T_B), ignore_trivial=False)
    naive.replace_inf(right)
    npt.assert_almost_equal(left, right)
Пример #5
0
def test_aamp_self_join(T_A, T_B):
    m = 3
    left = naive.aamp(T_B, m)
    right = aamp(T_B, m)
    naive.replace_inf(left)
    naive.replace_inf(right)
    npt.assert_almost_equal(left, right)

    right = aamp(pd.Series(T_B), m)
    naive.replace_inf(right)
    npt.assert_almost_equal(left, right)
Пример #6
0
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)
Пример #7
0
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)
Пример #8
0
def test_aamp_self_join(T_A, T_B):
    m = 3
    for p in [1.0, 2.0, 3.0]:
        ref_mp = naive.aamp(T_B, m, p=p)
        comp_mp = aamp(T_B, m, p=p)
        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, p=p)
        naive.replace_inf(comp_mp)
        npt.assert_almost_equal(ref_mp, comp_mp)
Пример #9
0
def test_aamp_constant_subsequence_self_join():
    T_A = np.concatenate(
        (np.zeros(20, dtype=np.float64), np.ones(5, dtype=np.float64)))
    m = 3
    left = naive.aamp(T_A, m)
    right = aamp(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 = aamp(pd.Series(T_A), m, ignore_trivial=True)
    naive.replace_inf(right)
    npt.assert_almost_equal(left[:, 0], right[:, 0])  # ignore indices
Пример #10
0
def test_aamp_constant_subsequence_self_join():
    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 = 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

    comp_mp = aamp(pd.Series(T_A), m, ignore_trivial=True)
    naive.replace_inf(comp_mp)
    npt.assert_almost_equal(ref_mp[:, 0], comp_mp[:, 0])  # ignore indices
Пример #11
0
def test_scraamp_self_join_full(T_A, T_B):
    m = 3
    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)

    ref_mp = aamp(T_B, m, ignore_trivial=True)
    ref_P = ref_mp[:, 0]
    ref_I = ref_mp[:, 1]
    ref_left_I = ref_mp[:, 2]
    ref_right_I = ref_mp[:, 3]

    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_stump(T, m):
    if T.ndim > 1:
        T = T.copy()
        T = T[0]

    ref = stumpy.aamp(T, m)
    comp = stumpy.stump(T, m, normalize=False)
    npt.assert_almost_equal(ref, comp)
Пример #13
0
    def __init__(self, T, m, excl_zone=None, egress=True):
        """
        Initialize the `stumpi` object

        Parameters
        ----------
        T : ndarray
            The time series or sequence for which the unnormalized matrix profile and
            matrix profile indices will be returned

        m : int
            Window size

        excl_zone : int, default None
            The half width for the exclusion zone relative to the current
            sliding window

        egress : bool, default True
            If set to `True`, the oldest data point in the time series is removed and
            the time series length remains constant rather than forever increasing
        """
        self._T = T.copy()
        self._T = np.asarray(self._T)
        core.check_dtype(self._T)
        self._m = m
        self._n = self._T.shape[0]
        if excl_zone is not None:  # pragma: no cover
            self._excl_zone = excl_zone
        else:
            self._excl_zone = int(np.ceil(self._m / 4))
        self._egress = egress

        mp = stumpy.aamp(self._T, self._m)
        self._P = mp[:, 0]
        self._I = mp[:, 1]
        self._left_I = mp[:, 2]
        self._left_P = np.empty(self._P.shape)
        self._left_P[:] = np.inf

        self._T_isfinite = np.isfinite(self._T)
        self._T, self._T_subseq_isfinite = core.preprocess_non_normalized(
            self._T, self._m)
        self._T_squared = np.sum(core.rolling_window(self._T * self._T,
                                                     self._m),
                                 axis=1)

        # Retrieve the left matrix profile values
        for i, j in enumerate(self._left_I):
            if j >= 0:
                D = core.mass_absolute(self._T[i:i + self._m],
                                       self._T[j:j + self._m])
                self._left_P[i] = D[0]

        Q = self._T[-m:]
        self._QT = core.sliding_dot_product(Q, self._T)
        if self._egress:
            self._QT_new = np.empty(self._QT.shape[0])
            self._n_appended = 0
def test_motifs(T, m):
    if T.ndim > 1:
        T = T.copy()
        T = T[0]

    mp = stumpy.aamp(T, m)
    ref = stumpy.aamp_motifs(T, mp[:, 0])
    comp = stumpy.motifs(T, mp[:, 0], normalize=False)
    npt.assert_almost_equal(ref, comp)
Пример #15
0
def test_aamp_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
    ref_mp = naive.aamp(T_A, m)
    comp_mp = 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],
        decimal=config.STUMPY_TEST_PRECISION)  # ignore indices

    comp_mp = aamp(pd.Series(T_A), m, ignore_trivial=True)
    naive.replace_inf(comp_mp)
    npt.assert_almost_equal(
        ref_mp[:, 0], comp_mp[:, 0],
        decimal=config.STUMPY_TEST_PRECISION)  # ignore indices
Пример #16
0
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)
Пример #17
0
def test_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))
    left = naive.aamp(T, m)
    right = aamp(T, m, ignore_trivial=True)

    naive.replace_inf(left)
    naive.replace_inf(right)
    npt.assert_almost_equal(left, right)
Пример #18
0
def test_aamp_nan_inf_self_join(T_A, T_B, substitute_B,
                                substitution_locations):
    m = 3

    T_B_sub = T_B.copy()

    for substitution_location_B in substitution_locations:
        T_B_sub[:] = T_B[:]
        T_B_sub[substitution_location_B] = substitute_B

        zone = int(np.ceil(m / 4))
        ref_mp = naive.aamp(T_B_sub, m)
        comp_mp = aamp(T_B_sub, m, ignore_trivial=True)
        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_sub), m, ignore_trivial=True)
        naive.replace_inf(comp_mp)
        npt.assert_almost_equal(ref_mp, comp_mp)
Пример #19
0
def test_aamp_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
    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])  # 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])  # 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])  # ignore indices
Пример #20
0
def test_scraamp_A_B_join_full(T_A, T_B):

    m = 3

    ref_mp = naive.aamp(T_A, m, T_B=T_B)
    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_A,
                     m,
                     T_B,
                     ignore_trivial=False,
                     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)

    ref_mp = aamp(T_A, m, T_B=T_B, ignore_trivial=False)
    ref_P = ref_mp[:, 0]
    ref_I = ref_mp[:, 1]
    ref_left_I = ref_mp[:, 2]
    ref_right_I = ref_mp[:, 3]

    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)
Пример #21
0
def naive_right_mp(data, m, normalize=True):
    if normalize:
        mp = stump(data, m)
    else:
        mp = aamp(data, m)
    k = mp.shape[0]
    right_nn = np.zeros((k, m))
    right_indices = [np.arange(IR, IR + m) for IR in mp[:, 3].tolist()]
    right_nn[:] = data[np.array(right_indices)]
    if normalize:
        mp[:, 0] = np.linalg.norm(
            core.z_norm(core.rolling_window(data, m), 1) -
            core.z_norm(right_nn, 1),
            axis=1,
        )
    else:
        mp[:, 0] = np.linalg.norm(core.rolling_window(data, m) - right_nn,
                                  axis=1)
    inf_indices = np.argwhere(mp[:, 3] < 0).flatten()
    mp[inf_indices, 0] = np.inf
    mp[inf_indices, 3] = inf_indices

    return mp
Пример #22
0
def test_aamp_floss_inf_nan(substitute, substitution_locations):
    T = np.random.uniform(-1000, 1000, [64])
    m = 5
    n = 30
    data = T.copy()
    for substitution_location in substitution_locations:
        data[:] = T[:]
        data[substitution_location] = substitute
        old_data = data[:n]

        mp = naive_right_mp(old_data, m, normalize=False)
        comp_mp = aamp(old_data, m)
        k = mp.shape[0]

        rolling_Ts = core.rolling_window(data[1:], n)
        L = 5
        excl_factor = 1
        custom_iac = _iac(k, bidirectional=False)
        stream = floss(comp_mp,
                       old_data,
                       m,
                       L,
                       excl_factor,
                       custom_iac=custom_iac,
                       normalize=False)
        last_idx = n - m + 1
        excl_zone = int(np.ceil(m / 4))
        zone_start = max(0, k - excl_zone)
        for i, ref_T in enumerate(rolling_Ts):
            mp[:, 1] = -1
            mp[:, 2] = -1
            mp[:] = np.roll(mp, -1, axis=0)
            mp[-1, 0] = np.inf
            mp[-1, 3] = last_idx + i

            D = naive.aamp_distance_profile(ref_T[-m:], ref_T, m)
            D[zone_start:] = np.inf

            ref_T_isfinite = np.isfinite(ref_T)
            ref_T_subseq_isfinite = np.all(core.rolling_window(
                ref_T_isfinite, m),
                                           axis=1)

            D[~ref_T_subseq_isfinite] = np.inf
            update_idx = np.argwhere(D < mp[:, 0]).flatten()
            mp[update_idx, 0] = D[update_idx]
            mp[update_idx, 3] = last_idx + i

            ref_cac_1d = _cac(
                mp[:, 3] - i - 1,
                L,
                bidirectional=False,
                excl_factor=excl_factor,
                custom_iac=custom_iac,
            )

            ref_mp = mp.copy()
            ref_P = ref_mp[:, 0]
            ref_I = ref_mp[:, 3]

            stream.update(ref_T[-1])
            comp_cac_1d = stream.cac_1d_
            comp_P = stream.P_
            comp_I = stream.I_
            comp_T = stream.T_

            naive.replace_inf(ref_P)
            naive.replace_inf(comp_P)

            npt.assert_almost_equal(ref_cac_1d, comp_cac_1d)
            npt.assert_almost_equal(ref_P, comp_P)
            npt.assert_almost_equal(ref_I, comp_I)
            npt.assert_almost_equal(ref_T, comp_T)
Пример #23
0
def test_aamp_int_input():
    with pytest.raises(TypeError):
        aamp(np.arange(10), 5)