From 0a7a3ebf533843b0332ffbf9e0d212537356e15b Mon Sep 17 00:00:00 2001 From: nimasarajpoor Date: Tue, 1 Aug 2023 01:17:15 -0400 Subject: [PATCH 01/23] add test function to test_precision, expecting assertion failure --- tests/test_precision.py | 48 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/tests/test_precision.py b/tests/test_precision.py index c819c7e22..525db95fd 100644 --- a/tests/test_precision.py +++ b/tests/test_precision.py @@ -1,3 +1,5 @@ +import functools + import naive import numpy as np import numpy.testing as npt @@ -55,3 +57,49 @@ def test_distace_profile(): ) npt.assert_almost_equal(D_ref, D_comp) + + +def test_snippets(): + m = 10 + k = 3 + s = 3 + seed = 332 + np.random.seed(seed) + T = np.random.uniform(-1000.0, 1000.0, [64]) + + isconstant_custom_func = functools.partial( + naive.isconstant_func_stddev_threshold, quantile_threshold=0.05 + ) + ( + ref_snippets, + ref_indices, + ref_profiles, + ref_fractions, + ref_areas, + ref_regimes, + ) = naive.mpdist_snippets( + T, m, k, s=s, mpdist_T_subseq_isconstant=isconstant_custom_func + ) + ( + cmp_snippets, + cmp_indices, + cmp_profiles, + cmp_fractions, + cmp_areas, + cmp_regimes, + ) = stumpy.snippets(T, m, k, s=s, mpdist_T_subseq_isconstant=isconstant_custom_func) + + npt.assert_almost_equal( + ref_snippets, cmp_snippets, decimal=config.STUMPY_TEST_PRECISION + ) + npt.assert_almost_equal( + ref_indices, cmp_indices, decimal=config.STUMPY_TEST_PRECISION + ) + npt.assert_almost_equal( + ref_profiles, cmp_profiles, decimal=config.STUMPY_TEST_PRECISION + ) + npt.assert_almost_equal( + ref_fractions, cmp_fractions, decimal=config.STUMPY_TEST_PRECISION + ) + npt.assert_almost_equal(ref_areas, cmp_areas, decimal=config.STUMPY_TEST_PRECISION) + npt.assert_almost_equal(ref_regimes, cmp_regimes) From 169412bf4591c6591ef78762320a7f044da9647e Mon Sep 17 00:00:00 2001 From: nimasarajpoor Date: Tue, 1 Aug 2023 01:38:21 -0400 Subject: [PATCH 02/23] add post processing to have symmetry property of distance without loss of precision --- stumpy/snippets.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/stumpy/snippets.py b/stumpy/snippets.py index 3613e297c..22ccfc00f 100644 --- a/stumpy/snippets.py +++ b/stumpy/snippets.py @@ -136,6 +136,10 @@ def _get_all_profiles( query_idx=start, ) + # post-processing `D` to avoid loss of precision + for j in range(0, i * m, m): + D[i, j] = D[j // m, i * m] + stop_idx = n_padded - m + 1 - right_pad D = D[:, :stop_idx] From 24929d8a4f4aecd4b71800c4066865d9a1871cd4 Mon Sep 17 00:00:00 2001 From: nimasarajpoor Date: Thu, 3 Aug 2023 07:02:48 -0400 Subject: [PATCH 03/23] retrieve code, expecting error --- stumpy/snippets.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/stumpy/snippets.py b/stumpy/snippets.py index 22ccfc00f..3613e297c 100644 --- a/stumpy/snippets.py +++ b/stumpy/snippets.py @@ -136,10 +136,6 @@ def _get_all_profiles( query_idx=start, ) - # post-processing `D` to avoid loss of precision - for j in range(0, i * m, m): - D[i, j] = D[j // m, i * m] - stop_idx = n_padded - m + 1 - right_pad D = D[:, :stop_idx] From df6a9b115616b5a786a3b4d87acca938c1539c31 Mon Sep 17 00:00:00 2001 From: nimasarajpoor Date: Thu, 3 Aug 2023 07:07:31 -0400 Subject: [PATCH 04/23] reduce arithmetic operation to reduce loss of precision --- stumpy/core.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/stumpy/core.py b/stumpy/core.py index 57d05f989..5099b6107 100644 --- a/stumpy/core.py +++ b/stumpy/core.py @@ -1097,13 +1097,13 @@ def _calculate_squared_distance( elif Q_subseq_isconstant or T_subseq_isconstant: D_squared = m else: - denom = m * σ_Q * Σ_T + denom = σ_Q * Σ_T denom = max(denom, config.STUMPY_DENOM_THRESHOLD) - ρ = (QT - m * μ_Q * M_T) / denom - ρ = min(ρ, 1.0) + mρ = (QT - m * μ_Q * M_T) / denom + mρ = min(mρ, m) - D_squared = np.abs(2 * m * (1.0 - ρ)) + D_squared = np.abs(2 * (m - mρ)) return D_squared From 731fdf2bea16384706991d84ee016d55ba7e5e19 Mon Sep 17 00:00:00 2001 From: nimasarajpoor Date: Thu, 3 Aug 2023 23:05:23 -0400 Subject: [PATCH 05/23] revise equation to mitigate loss of precision --- stumpy/core.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/stumpy/core.py b/stumpy/core.py index 5099b6107..e3a7ce506 100644 --- a/stumpy/core.py +++ b/stumpy/core.py @@ -1100,10 +1100,10 @@ def _calculate_squared_distance( denom = σ_Q * Σ_T denom = max(denom, config.STUMPY_DENOM_THRESHOLD) - mρ = (QT - m * μ_Q * M_T) / denom - mρ = min(mρ, m) + ρ = (QT / m - μ_Q * M_T) / denom + ρ = min(ρ, 1.0) - D_squared = np.abs(2 * (m - mρ)) + D_squared = np.abs(2 * m * (1.0 - ρ)) return D_squared From 97c31317eba887efc77c08a93a65e4f5596ea9f0 Mon Sep 17 00:00:00 2001 From: nimasarajpoor Date: Fri, 4 Aug 2023 12:35:05 -0400 Subject: [PATCH 06/23] add test function to check precision for _calculate_squared_distance --- tests/test_precision.py | 46 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/tests/test_precision.py b/tests/test_precision.py index 525db95fd..b1236ff42 100644 --- a/tests/test_precision.py +++ b/tests/test_precision.py @@ -103,3 +103,49 @@ def test_snippets(): ) npt.assert_almost_equal(ref_areas, cmp_areas, decimal=config.STUMPY_TEST_PRECISION) npt.assert_almost_equal(ref_regimes, cmp_regimes) + + +def test_calculate_squared_distance(): + # This test function raises an error if the distance between a subsequence + # and another does not satisfy the symmetry property + seed = 332 + np.random.seed(seed) + T = np.random.uniform(-1000.0, 1000.0, [64]) + m = 3 + + T_subseq_isconstant = core.rolling_isconstant(T, m) + M_T, Σ_T = core.compute_mean_std(T, m) + + i, j = 2, 10 + # testing the distance between query `Q_i = T[i : i + m]`, and the + # subsequence T[j : j + m] should be the same as the distance between + # the query `Q_j = T[j : j + m]` and the subsequence T[i : i + m] + + QT_i = core.sliding_dot_product(T[i : i + m], T) + dist_ij = core._calculate_squared_distance( + m, + QT_i[j], + M_T[i], + Σ_T[i], + M_T[j], + Σ_T[j], + T_subseq_isconstant[i], + T_subseq_isconstant[j], + ) + + QT_j = core.sliding_dot_product(T[j : j + m], T) + dist_ji = core._calculate_squared_distance( + m, + QT_j[i], + M_T[j], + Σ_T[j], + M_T[i], + Σ_T[i], + T_subseq_isconstant[j], + T_subseq_isconstant[i], + ) + + comp = dist_ij - dist_ji + ref = 0.0 + + npt.assert_almost_equal(ref, comp) From 8524458009a62963c70192185c00f55e1db2ced6 Mon Sep 17 00:00:00 2001 From: nimasarajpoor Date: Fri, 4 Aug 2023 17:17:07 -0400 Subject: [PATCH 07/23] minor change --- tests/test_precision.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_precision.py b/tests/test_precision.py index b1236ff42..d4473978a 100644 --- a/tests/test_precision.py +++ b/tests/test_precision.py @@ -107,7 +107,7 @@ def test_snippets(): def test_calculate_squared_distance(): # This test function raises an error if the distance between a subsequence - # and another does not satisfy the symmetry property + # and another does not satisfy the symmetry property. seed = 332 np.random.seed(seed) T = np.random.uniform(-1000.0, 1000.0, [64]) From e2ad6003a769d133e83e9e2c2f85e47daf1d554c Mon Sep 17 00:00:00 2001 From: nimasarajpoor Date: Fri, 4 Aug 2023 17:52:05 -0400 Subject: [PATCH 08/23] change order of test function in test_precision --- tests/test_precision.py | 92 ++++++++++++++++++++--------------------- 1 file changed, 46 insertions(+), 46 deletions(-) diff --git a/tests/test_precision.py b/tests/test_precision.py index d4473978a..eae34252d 100644 --- a/tests/test_precision.py +++ b/tests/test_precision.py @@ -59,52 +59,6 @@ def test_distace_profile(): npt.assert_almost_equal(D_ref, D_comp) -def test_snippets(): - m = 10 - k = 3 - s = 3 - seed = 332 - np.random.seed(seed) - T = np.random.uniform(-1000.0, 1000.0, [64]) - - isconstant_custom_func = functools.partial( - naive.isconstant_func_stddev_threshold, quantile_threshold=0.05 - ) - ( - ref_snippets, - ref_indices, - ref_profiles, - ref_fractions, - ref_areas, - ref_regimes, - ) = naive.mpdist_snippets( - T, m, k, s=s, mpdist_T_subseq_isconstant=isconstant_custom_func - ) - ( - cmp_snippets, - cmp_indices, - cmp_profiles, - cmp_fractions, - cmp_areas, - cmp_regimes, - ) = stumpy.snippets(T, m, k, s=s, mpdist_T_subseq_isconstant=isconstant_custom_func) - - npt.assert_almost_equal( - ref_snippets, cmp_snippets, decimal=config.STUMPY_TEST_PRECISION - ) - npt.assert_almost_equal( - ref_indices, cmp_indices, decimal=config.STUMPY_TEST_PRECISION - ) - npt.assert_almost_equal( - ref_profiles, cmp_profiles, decimal=config.STUMPY_TEST_PRECISION - ) - npt.assert_almost_equal( - ref_fractions, cmp_fractions, decimal=config.STUMPY_TEST_PRECISION - ) - npt.assert_almost_equal(ref_areas, cmp_areas, decimal=config.STUMPY_TEST_PRECISION) - npt.assert_almost_equal(ref_regimes, cmp_regimes) - - def test_calculate_squared_distance(): # This test function raises an error if the distance between a subsequence # and another does not satisfy the symmetry property. @@ -149,3 +103,49 @@ def test_calculate_squared_distance(): ref = 0.0 npt.assert_almost_equal(ref, comp) + + +def test_snippets(): + m = 10 + k = 3 + s = 3 + seed = 332 + np.random.seed(seed) + T = np.random.uniform(-1000.0, 1000.0, [64]) + + isconstant_custom_func = functools.partial( + naive.isconstant_func_stddev_threshold, quantile_threshold=0.05 + ) + ( + ref_snippets, + ref_indices, + ref_profiles, + ref_fractions, + ref_areas, + ref_regimes, + ) = naive.mpdist_snippets( + T, m, k, s=s, mpdist_T_subseq_isconstant=isconstant_custom_func + ) + ( + cmp_snippets, + cmp_indices, + cmp_profiles, + cmp_fractions, + cmp_areas, + cmp_regimes, + ) = stumpy.snippets(T, m, k, s=s, mpdist_T_subseq_isconstant=isconstant_custom_func) + + npt.assert_almost_equal( + ref_snippets, cmp_snippets, decimal=config.STUMPY_TEST_PRECISION + ) + npt.assert_almost_equal( + ref_indices, cmp_indices, decimal=config.STUMPY_TEST_PRECISION + ) + npt.assert_almost_equal( + ref_profiles, cmp_profiles, decimal=config.STUMPY_TEST_PRECISION + ) + npt.assert_almost_equal( + ref_fractions, cmp_fractions, decimal=config.STUMPY_TEST_PRECISION + ) + npt.assert_almost_equal(ref_areas, cmp_areas, decimal=config.STUMPY_TEST_PRECISION) + npt.assert_almost_equal(ref_regimes, cmp_regimes) From 0849c6d89fcef8d9751234d4364ed4a7bb9439eb Mon Sep 17 00:00:00 2001 From: nimasarajpoor Date: Fri, 4 Aug 2023 17:58:13 -0400 Subject: [PATCH 09/23] add parentheses to make sure the ouput remains the same when values are swapped --- stumpy/core.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stumpy/core.py b/stumpy/core.py index e3a7ce506..51498ec1d 100644 --- a/stumpy/core.py +++ b/stumpy/core.py @@ -1097,10 +1097,10 @@ def _calculate_squared_distance( elif Q_subseq_isconstant or T_subseq_isconstant: D_squared = m else: - denom = σ_Q * Σ_T + denom = m * (σ_Q * Σ_T) denom = max(denom, config.STUMPY_DENOM_THRESHOLD) - ρ = (QT / m - μ_Q * M_T) / denom + ρ = (QT - m * (μ_Q * M_T)) / denom ρ = min(ρ, 1.0) D_squared = np.abs(2 * m * (1.0 - ρ)) From d276b8b23f9304286b4c22d14cc82c1a2e943dbf Mon Sep 17 00:00:00 2001 From: nimasarajpoor Date: Sat, 5 Aug 2023 07:37:24 -0400 Subject: [PATCH 10/23] avoid fastmath reassoc flag to increase floating point precision --- stumpy/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stumpy/core.py b/stumpy/core.py index 51498ec1d..67fbad4b6 100644 --- a/stumpy/core.py +++ b/stumpy/core.py @@ -1042,7 +1042,7 @@ def compute_mean_std(T, m): @njit( # "f8(i8, f8, f8, f8, f8, f8)", - fastmath=True + fastmath={"nnan", "nsz", "arcp", "contract", "afn"} ) def _calculate_squared_distance( m, QT, μ_Q, σ_Q, M_T, Σ_T, Q_subseq_isconstant, T_subseq_isconstant From eeb860895e35e21246f15be078af7f9a1a1ac523 Mon Sep 17 00:00:00 2001 From: nimasarajpoor Date: Sat, 5 Aug 2023 09:24:48 -0400 Subject: [PATCH 11/23] enhance test function --- tests/test_precision.py | 65 ++++++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 33 deletions(-) diff --git a/tests/test_precision.py b/tests/test_precision.py index eae34252d..80847599f 100644 --- a/tests/test_precision.py +++ b/tests/test_precision.py @@ -70,39 +70,38 @@ def test_calculate_squared_distance(): T_subseq_isconstant = core.rolling_isconstant(T, m) M_T, Σ_T = core.compute_mean_std(T, m) - i, j = 2, 10 - # testing the distance between query `Q_i = T[i : i + m]`, and the - # subsequence T[j : j + m] should be the same as the distance between - # the query `Q_j = T[j : j + m]` and the subsequence T[i : i + m] - - QT_i = core.sliding_dot_product(T[i : i + m], T) - dist_ij = core._calculate_squared_distance( - m, - QT_i[j], - M_T[i], - Σ_T[i], - M_T[j], - Σ_T[j], - T_subseq_isconstant[i], - T_subseq_isconstant[j], - ) - - QT_j = core.sliding_dot_product(T[j : j + m], T) - dist_ji = core._calculate_squared_distance( - m, - QT_j[i], - M_T[j], - Σ_T[j], - M_T[i], - Σ_T[i], - T_subseq_isconstant[j], - T_subseq_isconstant[i], - ) - - comp = dist_ij - dist_ji - ref = 0.0 - - npt.assert_almost_equal(ref, comp) + n = len(T) + k = n - m + 1 + for i in range(k): + for j in range(k): + QT_i = core.sliding_dot_product(T[i : i + m], T) + dist_ij = core._calculate_squared_distance( + m, + QT_i[j], + M_T[i], + Σ_T[i], + M_T[j], + Σ_T[j], + T_subseq_isconstant[i], + T_subseq_isconstant[j], + ) + + QT_j = core.sliding_dot_product(T[j : j + m], T) + dist_ji = core._calculate_squared_distance( + m, + QT_j[i], + M_T[j], + Σ_T[j], + M_T[i], + Σ_T[i], + T_subseq_isconstant[j], + T_subseq_isconstant[i], + ) + + comp = dist_ij - dist_ji + ref = 0.0 + + npt.assert_almost_equal(ref, comp) def test_snippets(): From eb9b148aeae64eb81c461fc3fb5ed656304a9fed Mon Sep 17 00:00:00 2001 From: nimasarajpoor Date: Sat, 5 Aug 2023 09:25:37 -0400 Subject: [PATCH 12/23] add fatmath flag reassoc and remove ninf --- stumpy/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stumpy/core.py b/stumpy/core.py index 67fbad4b6..4b6a9e74d 100644 --- a/stumpy/core.py +++ b/stumpy/core.py @@ -1042,7 +1042,7 @@ def compute_mean_std(T, m): @njit( # "f8(i8, f8, f8, f8, f8, f8)", - fastmath={"nnan", "nsz", "arcp", "contract", "afn"} + fastmath={"nnan", "nsz", "arcp", "contract", "afn", "reassoc"} ) def _calculate_squared_distance( m, QT, μ_Q, σ_Q, M_T, Σ_T, Q_subseq_isconstant, T_subseq_isconstant From d6043b15d049b8737996de04e6ea84acaec04584 Mon Sep 17 00:00:00 2001 From: nimasarajpoor Date: Sat, 5 Aug 2023 09:36:43 -0400 Subject: [PATCH 13/23] minor change in test function --- tests/test_precision.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_precision.py b/tests/test_precision.py index 80847599f..4c44a93d3 100644 --- a/tests/test_precision.py +++ b/tests/test_precision.py @@ -74,7 +74,7 @@ def test_calculate_squared_distance(): k = n - m + 1 for i in range(k): for j in range(k): - QT_i = core.sliding_dot_product(T[i : i + m], T) + QT_i = core._sliding_dot_product(T[i : i + m], T) dist_ij = core._calculate_squared_distance( m, QT_i[j], @@ -86,7 +86,7 @@ def test_calculate_squared_distance(): T_subseq_isconstant[j], ) - QT_j = core.sliding_dot_product(T[j : j + m], T) + QT_j = core._sliding_dot_product(T[j : j + m], T) dist_ji = core._calculate_squared_distance( m, QT_j[i], From 549c8a9f005db30f396513deaaf5197783d8e45b Mon Sep 17 00:00:00 2001 From: nimasarajpoor Date: Sat, 5 Aug 2023 10:04:25 -0400 Subject: [PATCH 14/23] increase decimal coverage in assertion --- tests/test_precision.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_precision.py b/tests/test_precision.py index 4c44a93d3..e41db9585 100644 --- a/tests/test_precision.py +++ b/tests/test_precision.py @@ -101,7 +101,7 @@ def test_calculate_squared_distance(): comp = dist_ij - dist_ji ref = 0.0 - npt.assert_almost_equal(ref, comp) + npt.assert_almost_equal(ref, comp, decimal=15) def test_snippets(): From d4736022958589327fed1f2e2597d47077770fc7 Mon Sep 17 00:00:00 2001 From: nimasarajpoor Date: Sat, 5 Aug 2023 11:25:04 -0400 Subject: [PATCH 15/23] remove nnan from fastmath to be consistent with other func in core.py --- stumpy/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stumpy/core.py b/stumpy/core.py index 4b6a9e74d..4ee8341c4 100644 --- a/stumpy/core.py +++ b/stumpy/core.py @@ -1042,7 +1042,7 @@ def compute_mean_std(T, m): @njit( # "f8(i8, f8, f8, f8, f8, f8)", - fastmath={"nnan", "nsz", "arcp", "contract", "afn", "reassoc"} + fastmath={"nsz", "arcp", "contract", "afn", "reassoc"} ) def _calculate_squared_distance( m, QT, μ_Q, σ_Q, M_T, Σ_T, Q_subseq_isconstant, T_subseq_isconstant From ab044f241e49494190640fe954261b1b4e66d8b1 Mon Sep 17 00:00:00 2001 From: nimasarajpoor Date: Sat, 5 Aug 2023 12:53:57 -0400 Subject: [PATCH 16/23] use assert to make sure two values are identical --- tests/test_precision.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_precision.py b/tests/test_precision.py index e41db9585..6a7dfc826 100644 --- a/tests/test_precision.py +++ b/tests/test_precision.py @@ -101,7 +101,8 @@ def test_calculate_squared_distance(): comp = dist_ij - dist_ji ref = 0.0 - npt.assert_almost_equal(ref, comp, decimal=15) + assert ref == comp + # npt.assert_almost_equal(ref, comp, decimal=15) def test_snippets(): From 1365cb38b4c2527965a72b05df02e6217ddebeaf Mon Sep 17 00:00:00 2001 From: nimasarajpoor Date: Sat, 5 Aug 2023 12:57:35 -0400 Subject: [PATCH 17/23] re-order arithmetic operation to increase safety and stability of the code regarding precision --- stumpy/core.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stumpy/core.py b/stumpy/core.py index 4ee8341c4..320885999 100644 --- a/stumpy/core.py +++ b/stumpy/core.py @@ -1097,10 +1097,10 @@ def _calculate_squared_distance( elif Q_subseq_isconstant or T_subseq_isconstant: D_squared = m else: - denom = m * (σ_Q * Σ_T) + denom = (σ_Q * Σ_T) * m denom = max(denom, config.STUMPY_DENOM_THRESHOLD) - ρ = (QT - m * (μ_Q * M_T)) / denom + ρ = (QT - (μ_Q * M_T) * m) / denom ρ = min(ρ, 1.0) D_squared = np.abs(2 * m * (1.0 - ρ)) From 89b8afb266331646e1daf3bfea2467b191b15959 Mon Sep 17 00:00:00 2001 From: nimasarajpoor Date: Sat, 5 Aug 2023 18:38:02 -0400 Subject: [PATCH 18/23] add test function for distance symmetry propery in gpu --- tests/test_precision.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tests/test_precision.py b/tests/test_precision.py index 6a7dfc826..7c8bd85bc 100644 --- a/tests/test_precision.py +++ b/tests/test_precision.py @@ -149,3 +149,37 @@ def test_snippets(): ) npt.assert_almost_equal(ref_areas, cmp_areas, decimal=config.STUMPY_TEST_PRECISION) npt.assert_almost_equal(ref_regimes, cmp_regimes) + + +def test_distance_symmetry_property_in_gpu(): + # This test function raises an error if the distance between a subsequence + # and another does not satisfy the symmetry property. + + seed = 332 + np.random.seed(seed) + T = np.random.uniform(-1000.0, 1000.0, [64]) + m = 3 + + i, j = 2, 10 + # M_T, Σ_T = core.compute_mean_std(T, m) + # Σ_T[i] is `650.912209452633` + # Σ_T[j] is `722.0717285148525` + + # This test raises an error if arithmetic operation in ... + # ... `gpu_stump._compute_and_update_PI_kernel` does not + # generates the same result if values of variable for mean and std + # are swapped. + + T_A = T[i : i + m] + T_B = T[j : j + m] + + mp_AB = stumpy.gpu_stump(T_A, m, T_B) + mp_BA = stumpy.gpu_stump(T_B, m, T_A) + + d_ij = mp_AB[0, 0] + d_ji = mp_BA[0, 0] + + comp = d_ij - d_ji + ref = 0.0 + + npt.assert_almost_equal(comp, ref, decimal=15) From 22059165d9fe302529295b0dbcac0ee8e8a03265 Mon Sep 17 00:00:00 2001 From: nimasarajpoor Date: Sat, 5 Aug 2023 18:47:00 -0400 Subject: [PATCH 19/23] add parentheses and change order of multiplication --- stumpy/gpu_stump.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stumpy/gpu_stump.py b/stumpy/gpu_stump.py index d66283d7f..7f103b7ee 100644 --- a/stumpy/gpu_stump.py +++ b/stumpy/gpu_stump.py @@ -178,9 +178,9 @@ def _compute_and_update_PI_kernel( elif Q_subseq_isconstant[i] or T_subseq_isconstant[j]: p_norm = m else: - denom = m * σ_Q[i] * Σ_T[j] + denom = (σ_Q[i] * Σ_T[j]) * m denom = max(denom, config.STUMPY_DENOM_THRESHOLD) - ρ = (QT_out[i] - m * μ_Q[i] * M_T[j]) / denom + ρ = (QT_out[i] - (μ_Q[i] * M_T[j]) * m) / denom ρ = min(ρ, 1.0) p_norm = 2 * m * (1.0 - ρ) From 47a5094877b9a617d02ec820e03ce60d071a5d98 Mon Sep 17 00:00:00 2001 From: nimasarajpoor Date: Sat, 5 Aug 2023 19:08:23 -0400 Subject: [PATCH 20/23] skip testing gpu-based test func if cude is not available --- tests/test_precision.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/tests/test_precision.py b/tests/test_precision.py index 7c8bd85bc..a3dec0b93 100644 --- a/tests/test_precision.py +++ b/tests/test_precision.py @@ -1,12 +1,22 @@ import functools +from unittest.mock import patch import naive import numpy as np import numpy.testing as npt +import pytest +from numba import cuda import stumpy from stumpy import config, core +try: + from numba.errors import NumbaPerformanceWarning +except ModuleNotFoundError: + from numba.core.errors import NumbaPerformanceWarning + +TEST_THREADS_PER_BLOCK = 10 + def test_mpdist_snippets_s(): # This test function raises an error if the distance between @@ -151,10 +161,14 @@ def test_snippets(): npt.assert_almost_equal(ref_regimes, cmp_regimes) +@pytest.mark.filterwarnings("ignore", category=NumbaPerformanceWarning) +@patch("stumpy.config.STUMPY_THREADS_PER_BLOCK", TEST_THREADS_PER_BLOCK) def test_distance_symmetry_property_in_gpu(): + if not cuda.is_available(): # pragma: no cover + pytest.skip("Skipping Tests No GPUs Available") + # This test function raises an error if the distance between a subsequence # and another does not satisfy the symmetry property. - seed = 332 np.random.seed(seed) T = np.random.uniform(-1000.0, 1000.0, [64]) From 56cbf9d32caf9dfc6f66edad4121ad2edb7b6d3b Mon Sep 17 00:00:00 2001 From: nimasarajpoor Date: Sat, 5 Aug 2023 20:01:36 -0400 Subject: [PATCH 21/23] perform assersion with numpy testing instead of assert to be more explicit --- tests/test_precision.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/test_precision.py b/tests/test_precision.py index a3dec0b93..3e2448a59 100644 --- a/tests/test_precision.py +++ b/tests/test_precision.py @@ -111,8 +111,7 @@ def test_calculate_squared_distance(): comp = dist_ij - dist_ji ref = 0.0 - assert ref == comp - # npt.assert_almost_equal(ref, comp, decimal=15) + npt.assert_almost_equal(ref, comp, decimal=15) def test_snippets(): From 9736fee70d1bd0b5ae4c2e3dd792116f3ac5b078 Mon Sep 17 00:00:00 2001 From: nimasarajpoor Date: Thu, 10 Aug 2023 15:18:33 -0400 Subject: [PATCH 22/23] reduce precision to pass tests for minimum version --- tests/test_precision.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_precision.py b/tests/test_precision.py index 3e2448a59..0f53f7b90 100644 --- a/tests/test_precision.py +++ b/tests/test_precision.py @@ -111,7 +111,7 @@ def test_calculate_squared_distance(): comp = dist_ij - dist_ji ref = 0.0 - npt.assert_almost_equal(ref, comp, decimal=15) + npt.assert_almost_equal(ref, comp, decimal=14) def test_snippets(): From 5dcc5c49d847e1f37169ac823cf564525b4b84a8 Mon Sep 17 00:00:00 2001 From: nimasarajpoor Date: Fri, 11 Aug 2023 01:12:14 -0400 Subject: [PATCH 23/23] add comment to test function to explain its purpose --- tests/test_precision.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/test_precision.py b/tests/test_precision.py index 0f53f7b90..c87985092 100644 --- a/tests/test_precision.py +++ b/tests/test_precision.py @@ -115,6 +115,8 @@ def test_calculate_squared_distance(): def test_snippets(): + # This test function raises an error if there is a considerable loss of precision + # that violates the symmetry property of a distance measure. m = 10 k = 3 s = 3 @@ -167,7 +169,7 @@ def test_distance_symmetry_property_in_gpu(): pytest.skip("Skipping Tests No GPUs Available") # This test function raises an error if the distance between a subsequence - # and another does not satisfy the symmetry property. + # and another one does not satisfy the symmetry property. seed = 332 np.random.seed(seed) T = np.random.uniform(-1000.0, 1000.0, [64])