diff --git a/stumpy/aampdist_snippets.py b/stumpy/aampdist_snippets.py index 345a4bfa1..17d8e7364 100644 --- a/stumpy/aampdist_snippets.py +++ b/stumpy/aampdist_snippets.py @@ -86,13 +86,14 @@ def _get_all_aampdist_profiles( ) right_pad = 0 + n_contiguous_windows = int(T.shape[0] // m) if T.shape[0] % m != 0: right_pad = int(m * np.ceil(T.shape[0] / m) - T.shape[0]) pad_width = (0, right_pad) T = np.pad(T, pad_width, mode="constant", constant_values=np.nan) n_padded = T.shape[0] - D = np.empty(((n_padded // m) - 1, n_padded - m + 1), dtype=np.float64) + D = np.empty((n_contiguous_windows, n_padded - m + 1), dtype=np.float64) if s is not None: s = min(int(s), m) @@ -101,7 +102,7 @@ def _get_all_aampdist_profiles( s = min(math.ceil(percentage * m), m) # Iterate over non-overlapping subsequences, see Definition 3 - for i in range((n_padded // m) - 1): + for i in range(n_contiguous_windows): start = i * m stop = (i + 1) * m S_i = T[start:stop] @@ -220,17 +221,13 @@ def aampdist_snippets( p=p, ) - pad_width = (0, int(m * np.ceil(T.shape[0] / m) - T.shape[0])) - T_padded = np.pad(T, pad_width, mode="constant", constant_values=np.nan) - n_padded = T_padded.shape[0] - snippets = np.empty((k, m)) snippets_indices = np.empty(k, dtype=np.int64) snippets_profiles = np.empty((k, D.shape[-1]), dtype=np.float64) snippets_fractions = np.empty(k, dtype=np.float64) snippets_areas = np.empty(k, dtype=np.float64) Q = np.full(D.shape[-1], np.inf, dtype=np.float64) - indices = np.arange(0, n_padded - m, m, dtype=np.int64) + indices = np.arange(D.shape[0], dtype=np.int64) * m snippets_regimes_list = [] for i in range(k): diff --git a/tests/naive.py b/tests/naive.py index 2687a46af..41d09ee7e 100644 --- a/tests/naive.py +++ b/tests/naive.py @@ -1676,13 +1676,14 @@ def get_all_aampdist_profiles( p=2.0, ): right_pad = 0 + n_contiguous_windows = int(T.shape[0] // m) if T.shape[0] % m != 0: right_pad = int(m * np.ceil(T.shape[0] / m) - T.shape[0]) pad_width = (0, right_pad) T = np.pad(T, pad_width, mode="constant", constant_values=np.nan) n_padded = T.shape[0] - D = np.empty(((n_padded // m) - 1, n_padded - m + 1)) + D = np.empty((n_contiguous_windows, n_padded - m + 1)) if s is not None: s = min(int(s), m) @@ -1692,7 +1693,7 @@ def get_all_aampdist_profiles( s = min(math.ceil(percentage * m), m) # Iterate over non-overlapping subsequences, see Definition 3 - for i in range((n_padded // m) - 1): + for i in range(n_contiguous_windows): start = i * m stop = (i + 1) * m S_i = T[start:stop] @@ -1727,17 +1728,13 @@ def aampdist_snippets( T, m, percentage, s, mpdist_percentage, mpdist_k, p=p ) - pad_width = (0, int(m * np.ceil(T.shape[0] / m) - T.shape[0])) - T_padded = np.pad(T, pad_width, mode="constant", constant_values=np.nan) - n_padded = T_padded.shape[0] - snippets = np.empty((k, m)) snippets_indices = np.empty(k, dtype=np.int64) snippets_profiles = np.empty((k, D.shape[-1])) snippets_fractions = np.empty(k) snippets_areas = np.empty(k) Q = np.inf - indices = np.arange(0, n_padded - m, m) + indices = np.arange(0, D.shape[0] * m, m) snippets_regimes_list = [] for snippet_idx in range(k): diff --git a/tests/test_aampdist_snippets.py b/tests/test_aampdist_snippets.py index 7b8dd7790..bdd5af38e 100644 --- a/tests/test_aampdist_snippets.py +++ b/tests/test_aampdist_snippets.py @@ -89,6 +89,21 @@ def test_aampdist_snippets(T, m, k): npt.assert_allclose(cmp_regimes, ref_regimes, atol=1.5e-07) +def test_aampdist_snippets_len_T_multiple_of_m(): + # Regression test: when `len(T) % m == 0`, the final non-overlapping + # window must be included rather than dropped + T = np.array([1.0, 2.0, 3.0, 4.0, 100.0, 200.0, 300.0, 400.0]) + + D = _get_all_aampdist_profiles(T, m=4) + npt.assert_equal(D.shape[0], 2) + + _, cmp_indices, *_ = aampdist_snippets(T, m=4, k=2) + # Both non-overlapping windows (starting at 0 and 4) must be selected; + # their ranking order is not asserted since it depends on the + # (non-normalized) AAMP distances + npt.assert_array_equal(np.sort(cmp_indices), np.array([0, 4])) + + @pytest.mark.parametrize("T", test_data) @pytest.mark.parametrize("m", m) @pytest.mark.parametrize("k", k)