예제 #1
0
파일: test_stamp.py 프로젝트: xhochy/stumpy
def naive_mass(Q, T, m, trivial_idx=None, excl_zone=0):
    D = np.linalg.norm(core.z_norm(core.rolling_window(T, m), 1) - core.z_norm(Q), axis=1)
    if trivial_idx is not None:
            start = max(0, trivial_idx - excl_zone)
            stop = min(T.shape[0]-Q.shape[0]+1, trivial_idx + excl_zone)
            D[start:stop] = np.inf
    I = np.argmin(D)
    P = D[I]
    return P, I
예제 #2
0
def naive_mass(Q, T, m, trivial_idx, excl_zone):
    D = np.linalg.norm(core.z_norm(core.rolling_window(T, m), 1) -
                       core.z_norm(Q),
                       axis=1)
    start = max(0, trivial_idx - excl_zone)
    stop = min(T.shape[0] - Q.shape[0] + 1, trivial_idx + excl_zone)
    D[start:stop] = np.inf

    return D
예제 #3
0
def test_mass(Q, T):
    Q = Q.copy()
    T = T.copy()
    m = Q.shape[0]
    ref = np.linalg.norm(core.z_norm(core.rolling_window(T, m), 1) -
                         core.z_norm(Q),
                         axis=1)
    comp = core.mass(Q, T)
    npt.assert_almost_equal(ref, comp)
예제 #4
0
def test_mass(Q, T):
    Q = Q.copy()
    T = T.copy()
    m = Q.shape[0]
    left = np.linalg.norm(core.z_norm(core.rolling_window(T, m), 1) -
                          core.z_norm(Q),
                          axis=1)
    right = core.mass(Q, T)
    npt.assert_almost_equal(left, right)
예제 #5
0
def test_calculate_distance_profile(Q, T):
    m = Q.shape[0]
    left = np.linalg.norm(core.z_norm(core.rolling_window(T, m), 1) -
                          core.z_norm(Q),
                          axis=1)
    QT = core.sliding_dot_product(Q, T)
    μ_Q, σ_Q = core.compute_mean_std(Q, m)
    M_T, Σ_T = core.compute_mean_std(T, m)
    right = core.calculate_distance_profile(m, QT, μ_Q, σ_Q, M_T, Σ_T)
    npt.assert_almost_equal(left, right)
예제 #6
0
def test_calculate_squared_distance_profile(Q, T):
    m = Q.shape[0]
    ref = (np.linalg.norm(core.z_norm(core.rolling_window(T, m), 1) -
                          core.z_norm(Q),
                          axis=1)**2)
    QT = core.sliding_dot_product(Q, T)
    μ_Q, σ_Q = core.compute_mean_std(Q, m)
    M_T, Σ_T = core.compute_mean_std(T, m)
    comp = core._calculate_squared_distance_profile(m, QT, μ_Q.item(0),
                                                    σ_Q.item(0), M_T, Σ_T)
    npt.assert_almost_equal(ref, comp)
예제 #7
0
def test_mass_inf(Q, T):
    T[1] = np.inf
    m = Q.shape[0]

    left = np.linalg.norm(core.z_norm(core.rolling_window(T, m), 1) -
                          core.z_norm(Q),
                          axis=1)
    left[np.isnan(left)] = np.inf

    right = core.mass(Q, T)
    npt.assert_almost_equal(left, right)
예제 #8
0
def test_mass_T_nan(Q, T):
    Q = Q.copy()
    T = T.copy()
    T[1] = np.nan
    m = Q.shape[0]

    ref = np.linalg.norm(core.z_norm(core.rolling_window(T, m), 1) -
                         core.z_norm(Q),
                         axis=1)
    ref[np.isnan(ref)] = np.inf

    comp = core.mass(Q, T)
    npt.assert_almost_equal(ref, comp)
예제 #9
0
def naive_right_mp(data, m):
    mp = stump(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)]
    mp[:, 0] = np.linalg.norm(core.z_norm(core.rolling_window(data, m), 1) -
                              core.z_norm(right_nn, 1),
                              axis=1)
    inf_indices = np.argwhere(mp[:, 3] < 0).flatten()
    mp[inf_indices, 0] = np.inf
    mp[inf_indices, 3] = inf_indices

    return mp
예제 #10
0
def test_gpu_calculate_squared_distance_profile(Q, T):
    m = Q.shape[0]
    left = np.linalg.norm(core.z_norm(core.rolling_window(T, m), 1) -
                          core.z_norm(Q),
                          axis=1)
    left = np.square(left)
    M_T, Σ_T = core.compute_mean_std(T, m)
    QT = core.sliding_dot_product(Q, T)
    μ_Q, σ_Q = core.compute_mean_std(Q, m)
    QT = cp.asarray(QT)
    μ_Q = cp.asarray(μ_Q)
    σ_Q = cp.asarray(σ_Q)
    M_T = cp.asarray(M_T)
    Σ_T = cp.asarray(Σ_T)
    right = _gpu_calculate_squared_distance_profile(m, QT, μ_Q[0], σ_Q[0], M_T,
                                                    Σ_T)
    right = cp.asnumpy(right)
    npt.assert_almost_equal(left, right)
예제 #11
0
def naive_mass(Q, T, m, trivial_idx=None, excl_zone=0, ignore_trivial=False):
    D = np.linalg.norm(core.z_norm(core.rolling_window(T, m), 1) -
                       core.z_norm(Q),
                       axis=1)

    if ignore_trivial:
        start = max(0, trivial_idx - excl_zone)
        stop = min(T.shape[0] - Q.shape[0] + 1, trivial_idx + excl_zone)
        D[start:stop] = np.inf
    I = np.argmin(D)
    P = D[I]

    if P == np.inf:
        I = -1

    # Get left and right matrix profiles for self-joins
    if ignore_trivial and trivial_idx > 0:
        PL = np.inf
        IL = -1
        for i in range(trivial_idx):
            if D[i] < PL:
                IL = i
                PL = D[i]
        if start <= IL < stop:
            IL = -1
    else:
        IL = -1

    if ignore_trivial and trivial_idx + 1 < D.shape[0]:
        PR = np.inf
        IR = -1
        for i in range(trivial_idx + 1, D.shape[0]):
            if D[i] < PR:
                IR = i
                PR = D[i]
        if start <= IR < stop:
            IR = -1
    else:
        IR = -1

    return P, I, IL, IR
예제 #12
0
def test_calculate_squared_distance_kernel(T_A, T_B):
    m = 3
    for i in range(T_A.shape[0] - m + 1):
        Q = T_A[i:i + m]
        left = np.linalg.norm(core.z_norm(core.rolling_window(T_B, m), 1) -
                              core.z_norm(Q),
                              axis=1)
        left = np.square(left)
        M_T, Σ_T = core.compute_mean_std(T_B, m)
        QT = core.sliding_dot_product(Q, T_B)
        μ_Q, σ_Q = core.compute_mean_std(T_A, m)

        device_M_T = cuda.to_device(M_T)
        device_Σ_T = cuda.to_device(Σ_T)
        device_QT_even = cuda.to_device(QT)
        device_QT_odd = cuda.to_device(QT)
        device_QT_first = cuda.to_device(QT)
        device_μ_Q = cuda.to_device(μ_Q)
        device_σ_Q = cuda.to_device(σ_Q)
        device_D = cuda.device_array(QT.shape, dtype=np.float64)
        device_denom = cuda.device_array(QT.shape, dtype=np.float64)

        threads_per_block = THREADS_PER_BLOCK
        blocks_per_grid = math.ceil(QT.shape[0] / threads_per_block)

        _calculate_squared_distance_kernel[blocks_per_grid, threads_per_block](
            i,
            m,
            device_M_T,
            device_Σ_T,
            device_QT_even,
            device_QT_odd,
            device_μ_Q,
            device_σ_Q,
            device_D,
            device_denom,
        )

        right = device_D.copy_to_host()
        npt.assert_almost_equal(left, right)
예제 #13
0
def subspace(T, m, subseq_idx, nn_idx, k, include=None, discords=False):
    n_bit = 8
    bins = norm.ppf(np.arange(1, (2**n_bit)) / (2**n_bit))

    subseqs = core.z_norm(T[:, subseq_idx:subseq_idx + m], axis=1)
    neighbors = core.z_norm(T[:, nn_idx:nn_idx + m], axis=1)

    disc_subseqs = np.searchsorted(bins, subseqs)
    disc_neighbors = np.searchsorted(bins, neighbors)

    D = distance(
        disc_subseqs,
        disc_neighbors,
        axis=1,
    )

    if discords:
        sorted_idx = D[::-1].argsort(axis=0, kind="mergesort")
    else:
        sorted_idx = D.argsort(axis=0, kind="mergesort")

    # `include` processing can occur since we are dealing with indices, not distances
    if include is not None:
        include_idx = []
        for i in range(include.shape[0]):
            include_idx.append(np.isin(sorted_idx, include[i]).nonzero()[0])
        include_idx = np.array(include_idx).flatten()
        include_idx.sort()
        exclude_idx = np.ones(T.shape[0], dtype=bool)
        exclude_idx[include_idx] = False
        exclude_idx = exclude_idx.nonzero()[0]
        sorted_idx[:include_idx.
                   shape[0]], sorted_idx[include_idx.shape[0]:] = (
                       sorted_idx[include_idx],
                       sorted_idx[exclude_idx],
                   )

    S = sorted_idx[:k + 1]

    return S
예제 #14
0
def naive_distance_profile(Q, T, m):
    D = np.linalg.norm(core.z_norm(core.rolling_window(T, m), 1) -
                       core.z_norm(Q),
                       axis=1)
    return D