From 94cbda2d4dcd96eb51e5f076a7c9dedd2b38fb91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20H=C3=A4hnke?= <11663234+DanBenHa@users.noreply.github.com> Date: Wed, 2 Dec 2020 19:38:35 +0100 Subject: [PATCH] Fixed #286 mass absolute nan (#288) * test mass_absolute for nan caused by float imprecision * Min-clip values at 0 in _mass_absolute Fixes #286 --- stumpy/core.py | 4 +++- tests/test_core.py | 60 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/stumpy/core.py b/stumpy/core.py index dba5e8781..96bb7d3ae 100644 --- a/stumpy/core.py +++ b/stumpy/core.py @@ -861,7 +861,9 @@ def _mass_absolute(Q_squared, T_squared, QT): `See Mueen's Absolute Algorithm for Similarity Search \ `__ """ - return np.sqrt(Q_squared + T_squared - 2 * QT) + D = Q_squared + T_squared - 2 * QT + D[D < 0] = 0.0 + return np.sqrt(D) def mass_absolute(Q, T): diff --git a/tests/test_core.py b/tests/test_core.py index 482ad79df..3361beba3 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -430,6 +430,66 @@ def test_mass_absolute_T_inf(Q, T): npt.assert_almost_equal(ref, comp) +def test_mass_absolute_sqrt_input_negative(): + Q = np.array( + [ + -13.09, + -14.1, + -15.08, + -16.31, + -17.13, + -17.5, + -18.07, + -18.07, + -17.48, + -16.24, + -14.88, + -13.56, + -12.65, + -11.93, + -11.48, + -11.06, + -10.83, + -10.67, + -10.59, + -10.81, + -10.92, + -11.15, + -11.37, + -11.53, + -11.19, + -11.08, + -10.48, + -10.14, + -9.92, + -9.99, + -10.11, + -9.92, + -9.7, + -9.47, + -9.06, + -9.01, + -8.79, + -8.67, + -8.33, + -8.0, + -8.26, + -8.0, + -7.54, + -7.32, + -7.13, + -7.24, + -7.43, + -7.93, + -8.8, + -9.71, + ] + ) + ref = 0 + comp = core.mass_absolute(Q, Q) + npt.assert_almost_equal(ref, comp) + + def test_apply_exclusion_zone(): T = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=float) ref = np.empty(T.shape)