Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions tests/naive.py
Original file line number Diff line number Diff line change
Expand Up @@ -1591,16 +1591,18 @@ def mpdist_snippets(
mpdist_percentage=0.05,
mpdist_k=None,
mpdist_T_subseq_isconstant=None,
D=None,
):
D = get_all_mpdist_profiles(
T,
m,
percentage,
s,
mpdist_percentage,
mpdist_k,
mpdist_T_subseq_isconstant=mpdist_T_subseq_isconstant,
)
if D is None: # pragma: no cover
D = get_all_mpdist_profiles(
T,
m,
percentage,
s,
mpdist_percentage,
mpdist_k,
mpdist_T_subseq_isconstant=mpdist_T_subseq_isconstant,
)

snippets = np.empty((k, m))
snippets_indices = np.empty(k, dtype=np.int64)
Expand Down Expand Up @@ -1718,8 +1720,12 @@ def aampdist_snippets(
mpdist_percentage=0.05,
mpdist_k=None,
p=2.0,
D=None,
):
D = get_all_aampdist_profiles(T, m, percentage, s, mpdist_percentage, mpdist_k, p=p)
if D is None: # pragma: no cover
D = get_all_aampdist_profiles(
T, m, percentage, s, mpdist_percentage, mpdist_k, p=p
)

pad_width = (0, int(m * np.ceil(T.shape[0] / m) - T.shape[0]))
T_padded = np.pad(T, pad_width, mode="constant", constant_values=np.nan)
Expand Down
84 changes: 70 additions & 14 deletions tests/test_aampdist_snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pytest

from stumpy import config, rng
from stumpy.aampdist_snippets import aampdist_snippets
from stumpy.aampdist_snippets import _get_all_aampdist_profiles, aampdist_snippets

test_data = [rng.RNG.uniform(-1000, 1000, size=64).astype(np.float64)]
s = [6, 7, 8]
Expand All @@ -13,21 +13,55 @@
k = [1, 2, 3]


@pytest.mark.parametrize("T", test_data)
@pytest.mark.parametrize("m", m)
def test_get_all_aampdist_profiles(T, m):
ref_profiles = naive.get_all_aampdist_profiles(T, m)
cmp_profiles = _get_all_aampdist_profiles(T, m)

npt.assert_almost_equal(
ref_profiles, cmp_profiles, decimal=config.STUMPY_TEST_PRECISION
)


@pytest.mark.parametrize("T", test_data)
@pytest.mark.parametrize("m", m)
@pytest.mark.parametrize("s", s)
def test_get_all_aampdist_profiles_s(T, m, s):
ref_profiles = naive.get_all_aampdist_profiles(T, m, s=s)
cmp_profiles = _get_all_aampdist_profiles(T, m, s=s)

npt.assert_almost_equal(
ref_profiles, cmp_profiles, decimal=config.STUMPY_TEST_PRECISION
)


@pytest.mark.parametrize("T", test_data)
@pytest.mark.parametrize("m", m)
@pytest.mark.parametrize("k", k)
def test_aampdist_snippets(T, m, k):
for p in [1.0, 2.0, 3.0]:
D = _get_all_aampdist_profiles(
T,
m,
p=p,
)
(
ref_snippets,
ref_aampdist_snippets,
ref_indices,
ref_profiles,
ref_fractions,
ref_areas,
ref_regimes,
) = naive.aampdist_snippets(T, m, k, p=p)
) = naive.aampdist_snippets(
T,
m,
k,
p=p,
D=D,
)
(
cmp_snippets,
cmp_aampdist_snippets,
cmp_indices,
cmp_profiles,
cmp_fractions,
Expand All @@ -36,7 +70,9 @@ def test_aampdist_snippets(T, m, k):
) = aampdist_snippets(T, m, k, p=p)

npt.assert_almost_equal(
ref_snippets, cmp_snippets, decimal=config.STUMPY_TEST_PRECISION
ref_aampdist_snippets,
cmp_aampdist_snippets,
decimal=config.STUMPY_TEST_PRECISION,
)
npt.assert_almost_equal(
ref_indices, cmp_indices, decimal=config.STUMPY_TEST_PRECISION
Expand All @@ -57,17 +93,22 @@ def test_aampdist_snippets(T, m, k):
@pytest.mark.parametrize("m", m)
@pytest.mark.parametrize("k", k)
@pytest.mark.parametrize("percentage", percentage)
def test_mpdist_snippets_percentage(T, m, k, percentage):
def test_aampdist_snippets_percentage(T, m, k, percentage):
D = _get_all_aampdist_profiles(
T,
m,
percentage=percentage,
)
(
ref_snippets,
ref_aampdist_snippets,
ref_indices,
ref_profiles,
ref_fractions,
ref_areas,
ref_regimes,
) = naive.aampdist_snippets(T, m, k, percentage=percentage)
) = naive.aampdist_snippets(T, m, k, percentage=percentage, D=D)
(
cmp_snippets,
cmp_aampdist_snippets,
cmp_indices,
cmp_profiles,
cmp_fractions,
Expand All @@ -76,7 +117,9 @@ def test_mpdist_snippets_percentage(T, m, k, percentage):
) = aampdist_snippets(T, m, k, percentage=percentage)

npt.assert_almost_equal(
ref_snippets, cmp_snippets, decimal=config.STUMPY_TEST_PRECISION
ref_aampdist_snippets,
cmp_aampdist_snippets,
decimal=config.STUMPY_TEST_PRECISION,
)
npt.assert_almost_equal(
ref_indices, cmp_indices, decimal=config.STUMPY_TEST_PRECISION
Expand All @@ -98,16 +141,27 @@ def test_mpdist_snippets_percentage(T, m, k, percentage):
@pytest.mark.parametrize("k", k)
@pytest.mark.parametrize("s", s)
def test_aampdist_snippets_s(T, m, k, s):
D = _get_all_aampdist_profiles(
T,
m,
s=s,
)
(
ref_snippets,
ref_aampdist_snippets,
ref_indices,
ref_profiles,
ref_fractions,
ref_areas,
ref_regimes,
) = naive.aampdist_snippets(T, m, k, s=s)
) = naive.aampdist_snippets(
T,
m,
k,
s=s,
D=D,
)
(
cmp_snippets,
cmp_aampdist_snippets,
cmp_indices,
cmp_profiles,
cmp_fractions,
Expand All @@ -116,7 +170,9 @@ def test_aampdist_snippets_s(T, m, k, s):
) = aampdist_snippets(T, m, k, s=s)

npt.assert_almost_equal(
ref_snippets, cmp_snippets, decimal=config.STUMPY_TEST_PRECISION
ref_aampdist_snippets,
cmp_aampdist_snippets,
decimal=config.STUMPY_TEST_PRECISION,
)
npt.assert_almost_equal(
ref_indices, cmp_indices, decimal=config.STUMPY_TEST_PRECISION
Expand Down
67 changes: 25 additions & 42 deletions tests/test_precision.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
from unittest.mock import patch

import naive
import numba
import numpy as np
import numpy.testing as npt
import pytest
from numba import cuda

from stumpy import cache, config, core, fastmath, rng, sdp
from stumpy import config, core, rng, sdp

if cuda.is_available():
from stumpy.gpu_stump import gpu_stump
else: # pragma: no cover
from stumpy.core import _gpu_stump_driver_not_found as gpu_stump # noqa: F401
from stumpy.snippets import snippets

from stumpy.snippets import _get_all_profiles, snippets

try:
from numba.errors import NumbaPerformanceWarning
Expand Down Expand Up @@ -118,18 +118,25 @@ def test_calculate_squared_distance():
npt.assert_almost_equal(ref, comp, decimal=14)


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
with rng.fix_seed(332):
T = rng.RNG.uniform(-1000.0, 1000.0, [64])
@pytest.mark.parametrize(
"seed, m, k, s",
[(2135137202, 10, 3, 3), (2636, 9, 3, 3), (332, 10, 3, 3), (1615, 10, 3, 3)],
)
def test_snippets(seed, m, k, s):
with rng.fix_seed(seed):
T = rng.RNG.uniform(-1000, 1000, [64]).astype(np.float64)

isconstant_custom_func = functools.partial(
naive.isconstant_func_stddev_threshold, quantile_threshold=0.05
)

D = _get_all_profiles(
T,
m,
s=s,
mpdist_T_subseq_isconstant=isconstant_custom_func,
)

(
ref_snippets,
ref_indices,
Expand All @@ -138,9 +145,13 @@ def test_snippets():
ref_areas,
ref_regimes,
) = naive.mpdist_snippets(
T, m, k, s=s, mpdist_T_subseq_isconstant=isconstant_custom_func
T,
m,
k,
s=s,
mpdist_T_subseq_isconstant=isconstant_custom_func,
D=D,
)

(
cmp_snippets,
cmp_indices,
Expand All @@ -150,29 +161,6 @@ def test_snippets():
cmp_regimes,
) = 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
Comment thread
NimaSarajpoor marked this conversation as resolved.
): # 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,
) = 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
)
Expand All @@ -190,11 +178,6 @@ def test_snippets():
)
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()
Comment thread
NimaSarajpoor marked this conversation as resolved.


@pytest.mark.filterwarnings("ignore", category=NumbaPerformanceWarning)
@patch("stumpy.config.STUMPY_THREADS_PER_BLOCK", TEST_THREADS_PER_BLOCK)
Expand All @@ -215,7 +198,7 @@ def test_distance_symmetry_property_in_gpu():

# 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
# generate the same result if values of variable for mean and std
# are swapped.

T_A = T[i : i + m]
Expand Down
Loading
Loading