Skip to content
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

Fixed #1006 RuntimeWarning divide by zero encountered in divide #1012

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions stumpy/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2254,6 +2254,7 @@ def preprocess_diagonal(

M_T, Σ_T = compute_mean_std(T, m)
Σ_T[T_subseq_isconstant] = 1.0 # Avoid divide by zero in next inversion step
Σ_T[~T_subseq_isfinite] = 1.0
NimaSarajpoor marked this conversation as resolved.
Show resolved Hide resolved
Σ_T_inverse = 1.0 / Σ_T
M_T_m_1, _ = compute_mean_std(T, m - 1)

Expand Down
16 changes: 16 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import functools
import math
import os
import warnings
from unittest.mock import patch

import naive
Expand Down Expand Up @@ -880,9 +881,11 @@ def test_preprocess_non_normalized():
def test_preprocess_diagonal():
T = np.array([0, np.nan, 2, 3, 4, 5, 6, 7, np.inf, 9])
m = 3
T_subseq_isfinite = core.rolling_isfinite(T, m)

ref_T = np.array([0, 0, 2, 3, 4, 5, 6, 7, 0, 9], dtype=float)
ref_M, ref_Σ = naive.compute_mean_std(ref_T, m)
ref_Σ[~T_subseq_isfinite] = 1.0
ref_Σ_inverse = 1.0 / ref_Σ
ref_M_m_1, _ = naive.compute_mean_std(ref_T, m - 1)

Expand Down Expand Up @@ -1753,3 +1756,16 @@ def test_process_isconstant_2d():
T_subseq_isconstant_comp = core.process_isconstant(T, m, T_subseq_isconstant)

npt.assert_array_equal(T_subseq_isconstant_ref, T_subseq_isconstant_comp)


def test_preprocess_diagonal_std_inverse():
T = np.random.rand(64)
m = 3
T[:m] = np.nan

with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
NimaSarajpoor marked this conversation as resolved.
Show resolved Hide resolved
core.preprocess_diagonal(T, m)
for item in w:
if issubclass(item.category, RuntimeWarning):
assert "divide by zero encountered in divide" not in str(item.message)
Loading