def naive_across_series_nearest_neighbors(q, Ts): """ For multiple time series find, per individual time series, the subsequences closest to a query. Parameters ---------- q : ndarray Query array or subsequence Ts : list List of time series for which to the nearest neighbors to `q` Returns ------- subseq_idx_nn : ndarray Indices to subsequences in `Ts` that are closest to `q` d : ndarray Distances to subsequences in `Ts` that are closest to `q` """ k = len(Ts) d = np.zeros(k, dtype=float) subseq_idx_nn = np.zeros(k, dtype=int) for i in range(k): dp = naive.distance_profile(q, Ts[i], len(q)) subseq_idx_nn[i] = np.argmin(dp) d[i] = dp[subseq_idx_nn[i]] return subseq_idx_nn, d
def test_floss(): data = np.random.uniform(-1000, 1000, [64]) m = 5 n = 30 old_data = data[:n] mp = naive_right_mp(old_data, m) comp_mp = stump(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) 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.distance_profile(ref_T[-m:], ref_T, m) D[zone_start:] = 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)
def naive_match(Q, T, excl_zone, max_distance): m = Q.shape[0] D = naive.distance_profile(Q, T, m) matches = [] for i in range(D.size): dist = D[i] if dist <= max_distance: matches.append(i) # Removes indices that are inside the exclusion zone of some occurrence with # a smaller distance to the query matches.sort(key=lambda x: D[x]) result = [] while len(matches) > 0: o = matches[0] result.append([D[o], o]) matches = [ x for x in matches if x < o - excl_zone or x > o + excl_zone ] return np.array(result, dtype=object)