-
Couldn't load subscription status.
- Fork 337
Fixed #1061 failure in snippet unit tests due to the instability of np.sum for array with many small floating point numbers
#1080
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
10f4651
Add rare-case tests for snippets
NimaSarajpoor ff3cd66
replace np.sum with math.fsum to fix loss of precision
NimaSarajpoor 99ef190
move precision-related test functions to test_precision and allow sec...
NimaSarajpoor f8bf7b6
swap assertions to get better visibility when unit testing fails
NimaSarajpoor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -115,15 +115,54 @@ def test_calculate_squared_distance(): | |
| npt.assert_almost_equal(ref, comp, decimal=14) | ||
|
|
||
|
|
||
| def test_snippets(): | ||
| @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 one 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "generate" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "same values AS" |
||
| # 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) | ||
|
|
||
|
|
||
| def test_snippets_rare_case_1(): | ||
| # This test function raises an error if there is a considerable loss of precision | ||
| # that violates the symmetry property of a distance measure. | ||
| seed = 332 | ||
| np.random.seed(seed) | ||
| T = np.random.uniform(-1000.0, 1000.0, 64) | ||
|
|
||
| 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 | ||
|
|
@@ -169,12 +208,91 @@ def test_snippets(): | |
| T, m, k, s=s, mpdist_T_subseq_isconstant=isconstant_custom_func | ||
| ) | ||
|
|
||
| npt.assert_almost_equal( | ||
| ref_indices, cmp_indices, decimal=config.STUMPY_TEST_PRECISION | ||
| ) | ||
| npt.assert_almost_equal( | ||
| ref_snippets, cmp_snippets, 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) | ||
|
|
||
| if not numba.config.DISABLE_JIT: # pragma: no cover | ||
| # Revert fastmath flag back to their default values | ||
| fastmath._reset("core", "_calculate_squared_distance") | ||
| cache._recompile() | ||
|
|
||
|
|
||
| def test_snippets_rare_case_2(): | ||
| # This test fails when the naive implementation of snippet, | ||
| # i.e. `naive.mpdist_snippets`, uses `np.sum` instead of | ||
| # math.fsum when calculating the sum of many small | ||
| # floating point numbers. For more details, see issue #1061 | ||
|
|
||
| seed = 1615 | ||
| np.random.seed(seed) | ||
| T = np.random.uniform(-1000.0, 1000.0, 64) | ||
|
|
||
| m = 10 | ||
| s = 3 | ||
| k = 3 | ||
|
|
||
| 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) | ||
|
|
||
| if ( | ||
| not np.allclose(ref_snippets, cmp_snippets) and not numba.config.DISABLE_JIT | ||
| ): # pragma: no cover | ||
| # Revise fastmath flags by removing reassoc (to improve precision), | ||
| # recompile njit functions, and re-compute snippets. | ||
| fastmath._set( | ||
| "core", "_calculate_squared_distance", {"nsz", "arcp", "contract", "afn"} | ||
| ) | ||
| cache._recompile() | ||
|
|
||
| ( | ||
| 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_indices, cmp_indices, decimal=config.STUMPY_TEST_PRECISION | ||
| ) | ||
| npt.assert_almost_equal( | ||
| ref_snippets, cmp_snippets, decimal=config.STUMPY_TEST_PRECISION | ||
| ) | ||
| npt.assert_almost_equal( | ||
| ref_profiles, cmp_profiles, decimal=config.STUMPY_TEST_PRECISION | ||
| ) | ||
|
|
@@ -190,39 +308,79 @@ def test_snippets(): | |
| cache._recompile() | ||
|
|
||
|
|
||
| @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") | ||
| def test_snippets_rare_case_3(): | ||
| # This test fails when the naive implementation of snippet, | ||
| # i.e. `naive.mpdist_snippets`, uses `np.sum` instead of | ||
| # math.fsum when calculating the sum of many small | ||
| # floating point numbers. For more details, see issue #1061 | ||
|
|
||
| # This test function raises an error if the distance between a subsequence | ||
| # and another one does not satisfy the symmetry property. | ||
| seed = 332 | ||
| seed = 2636 | ||
| 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` | ||
| T = np.random.uniform(-1000.0, 1000.0, 64) | ||
| m = 9 | ||
| s = 3 | ||
| k = 3 | ||
|
|
||
| # 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. | ||
| 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 | ||
| ) | ||
|
|
||
| T_A = T[i : i + m] | ||
| T_B = T[j : j + m] | ||
| ( | ||
| 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) | ||
|
|
||
| mp_AB = stumpy.gpu_stump(T_A, m, T_B) | ||
| mp_BA = stumpy.gpu_stump(T_B, m, T_A) | ||
| if ( | ||
| not np.allclose(ref_snippets, cmp_snippets) and not numba.config.DISABLE_JIT | ||
| ): # pragma: no cover | ||
| # Revise fastmath flags by removing reassoc (to improve precision), | ||
| # recompile njit functions, and re-compute snippets. | ||
| fastmath._set( | ||
| "core", "_calculate_squared_distance", {"nsz", "arcp", "contract", "afn"} | ||
| ) | ||
| cache._recompile() | ||
|
|
||
| d_ij = mp_AB[0, 0] | ||
| d_ji = mp_BA[0, 0] | ||
| ( | ||
| 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 | ||
| ) | ||
|
|
||
| comp = d_ij - d_ji | ||
| ref = 0.0 | ||
| npt.assert_almost_equal( | ||
| ref_indices, cmp_indices, decimal=config.STUMPY_TEST_PRECISION | ||
| ) | ||
| npt.assert_almost_equal( | ||
| ref_snippets, cmp_snippets, 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) | ||
|
|
||
| npt.assert_almost_equal(comp, ref, decimal=15) | ||
| if not numba.config.DISABLE_JIT: # pragma: no cover | ||
| # Revert fastmath flag back to their default values | ||
| fastmath._reset("core", "_calculate_squared_distance") | ||
| cache._recompile() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.