Example #1
0
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)
Example #2
0
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_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)