stumpy icon indicating copy to clipboard operation
stumpy copied to clipboard

RuntimeWarning: divide by zero encountered in divide

Open NimaSarajpoor opened this issue 1 year ago • 13 comments

Running the following script

import numpy as np
import stumpy
import time

def test_extract_several_consensus():
    Ts = [np.random.rand(n) for n in [64, 128]]
    Ts_ref = [T.copy() for T in Ts]
    Ts_comp = [T.copy() for T in Ts]

    m = 20

    k = 2  # Get the first `k` consensus motifs
    for _ in range(k):
        print(f'===== {_} =====')
        radius, Ts_idx, subseq_idx = stumpy.ostinato(Ts_comp, m)
    
        consensus_motif = Ts_comp[Ts_idx][subseq_idx : subseq_idx + m].copy()
        for i in range(len(Ts_comp)):
            if i == Ts_idx:
                query_idx = subseq_idx
            else:
                query_idx = None

            idx = np.argmin(stumpy.core.mass(consensus_motif, Ts_comp[i], query_idx=query_idx))

            Ts_comp[i][idx : idx + m] = np.nan
            Ts_ref[i][idx : idx + m] = np.nan

            np.testing.assert_almost_equal(
                Ts_ref[i][np.isfinite(Ts_ref[i])], Ts_comp[i][np.isfinite(Ts_comp[i])]
            )


if __name__ == "__main__":
    test_extract_several_consensus()

Gives:

===== 0 =====
===== 1 =====
/Users/nimasarajpoor/miniconda3/envs/py310/lib/python3.10/site-packages/stumpy/core.py:2257: 
RuntimeWarning: divide by zero encountered in divide

I took a look at the code:

https://github.com/TDAmeritrade/stumpy/blob/e7bb2b85ee8761683480ac093437e71989252d15/stumpy/core.py#L2248-L2260

and tried to swap lines 2252 and 2253, but that did not work.


Also: If I disable JIT, I get an extra warning, saying:

python3.10/site-packages/numpy/lib/nanfunctions.py:1879: RuntimeWarning: Degrees of freedom <= 0 for slice.

NimaSarajpoor avatar Jul 13 '24 13:07 NimaSarajpoor