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
66 changes: 66 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,69 @@
# to fix eventual module import errors that can arise, for example when
# running tests from inside VS code.
# See https://stackoverflow.com/a/34520971
import json

import numpy as np
import pytest

pytest.RNG = np.random.default_rng()
# pytest.RNG.bit_generator.state = {
# Set RNG state here
# }

pytest.LAST_RNG_STATE = None
pytest.FIXED_RNG_STATE = {
# DO NOT CHANGE/ALTER!!
"bit_generator": "PCG64",
"state": {
"state": 195349167630453735115769518810051464980,
"inc": 247589055400886363559049235690497450585,
},
"has_uint32": 0,
"uinteger": 0,
}


def pytest_configure(config):
"""
Called after command line options have been parsed
and all plugins and initial conftest files been loaded.
"""
state = json.dumps(pytest.RNG.bit_generator.state, indent=4)
print(f"conftest.py: pytest.RNG.bit_generator.state = {state}")


def _get_rng_state():
"""
Get a copy of the current RNG state
"""
return pytest.RNG.bit_generator.state.copy()


def _set_rng_state(state):
"""
Store existing RNG state and set RNG state
"""
pytest.LAST_RNG_STATE = pytest.RNG.bit_generator.state.copy()
pytest.RNG.bit_generator.state = state


def _reset_rng_state():
"""
Restore the RNG state to the last recorded RNG state
"""
pytest.RNG.bit_generator.state = pytest.LAST_RNG_STATE.copy()
pytest.LAST_RNG_STATE = None


def _fix_rng_state():
"""
Set the RNG state to a fixed, hardcoded, safe state
"""
_set_rng_state(pytest.FIXED_RNG_STATE)


pytest.get_rng_state = _get_rng_state
pytest.set_rng_state = _set_rng_state
pytest.fix_rng_state = _fix_rng_state
pytest.unfix_rng_state = _reset_rng_state
2 changes: 1 addition & 1 deletion test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ set_ray_coveragerc()
show_coverage_report()
{
set_ray_coveragerc
coverage report --show-missing --fail-under=100 --skip-covered --omit=fastmath.py,docstring.py,versions.py $fcoveragerc
coverage report --show-missing --fail-under=100 --skip-covered --omit=fastmath.py,docstring.py,versions.py,conftest.py $fcoveragerc
check_errs $?
}

Expand Down
16 changes: 8 additions & 8 deletions tests/test_aamp_mmotifs.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,15 @@


def test_aamp_mmotifs_default_parameters():
motif_distances_ref = np.array(
[[0.0, 0.06315749, 0.25275899, 0.34087884, 0.3452315]]
)
motif_indices_ref = np.array([[19, 77, 63, 52, 71]])
motif_subspaces_ref = [np.array([2])]
motif_distances_ref = np.array([[0.0, 0.10660435, 0.27927693]])
motif_indices_ref = np.array([[19, 41, 34]])
motif_subspaces_ref = [np.array([1])]
motif_mdls_ref = [
np.array([411.60964047, 423.69925001, 449.11032383, 476.95855027, 506.62406252])
np.array([411.60964047, 433.21928095, 429.77443751, 457.74925683, 477.80793557])
]

np.random.seed(0)
T = np.random.rand(500).reshape(5, 100)
pytest.fix_rng_state()
T = pytest.RNG.random(size=(5, 100))

m = 5
excl_zone = int(np.ceil(m / config.STUMPY_EXCL_ZONE_DENOM))
Expand All @@ -78,6 +76,8 @@ def test_aamp_mmotifs_default_parameters():
npt.assert_array_almost_equal(motif_subspaces_ref, motif_subspaces_cmp)
npt.assert_array_almost_equal(motif_mdls_ref, motif_mdls_cmp)

pytest.unfix_rng_state()


@pytest.mark.parametrize("T", test_data)
def test_aamp_mmotifs_max_distance(T):
Expand Down
8 changes: 4 additions & 4 deletions tests/test_aamp_motifs.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
np.array([0.0, 1.0, 2.0]),
np.array([0.1, 1.0, 2.0, 3.0, -1.0, 0.1, 1.0, 2.0, -0.5]),
),
(np.random.uniform(-1000, 1000, [8]), np.random.uniform(-1000, 1000, [64])),
(pytest.RNG.uniform(-1000, 1000, [8]), pytest.RNG.uniform(-1000, 1000, [64])),
]


Expand Down Expand Up @@ -71,12 +71,12 @@ def test_aamp_motifs_one_motif():
def test_aamp_motifs_two_motifs():
# Fix seed, because in some case motifs can be off by an index resulting in test
# fails, which is caused since one of the motifs is not repeated perfectly in T.
np.random.seed(1234)
pytest.fix_rng_state()

# The time series is random noise with two motifs for m=10:
# * (almost) identical step functions at indices 10, 110 and 210
# * identical linear slopes at indices 70 and 170
T = np.random.normal(size=300)
T = pytest.RNG.normal(size=300)
m = 20

T[10:30] = 1
Expand Down Expand Up @@ -125,7 +125,7 @@ def test_aamp_motifs_two_motifs():
npt.assert_almost_equal(left_profile_values, right_distance_values, decimal=6)

# Reset seed
np.random.seed(None)
pytest.unfix_rng_state()


def test_aamp_naive_match_exact():
Expand Down
99 changes: 51 additions & 48 deletions tests/test_aamp_ostinato.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,78 +20,81 @@ def dask_cluster():
cluster.close()


@pytest.mark.parametrize(
"seed", np.random.choice(np.arange(10000), size=25, replace=False)
)
def test_random_ostinato(seed):
m = 50
np.random.seed(seed)
Ts = [np.random.rand(n) for n in [64, 128, 256]]

ref_radius, ref_Ts_idx, ref_subseq_idx = naive.aamp_ostinato(Ts, m)
comp_radius, comp_Ts_idx, comp_subseq_idx = aamp_ostinato(Ts, m)

npt.assert_almost_equal(ref_radius, comp_radius)
npt.assert_almost_equal(ref_Ts_idx, comp_Ts_idx)
npt.assert_almost_equal(ref_subseq_idx, comp_subseq_idx)


@pytest.mark.parametrize("seed", [41, 88, 290, 292, 310, 328, 538, 556, 563, 570])
def test_deterministic_ostinato(seed):
m = 50
np.random.seed(seed)
Ts = [np.random.rand(n) for n in [64, 128, 256]]
def test_random_ostinato():
for _ in range(25):
m = 50
Ts = [pytest.RNG.random(n) for n in [64, 128, 256]]

for p in [1.0, 2.0, 3.0]:
ref_radius, ref_Ts_idx, ref_subseq_idx = naive.aamp_ostinato(Ts, m, p=p)
comp_radius, comp_Ts_idx, comp_subseq_idx = aamp_ostinato(Ts, m, p=p)
ref_radius, ref_Ts_idx, ref_subseq_idx = naive.aamp_ostinato(Ts, m)
comp_radius, comp_Ts_idx, comp_subseq_idx = aamp_ostinato(Ts, m)

npt.assert_almost_equal(ref_radius, comp_radius)
npt.assert_almost_equal(ref_Ts_idx, comp_Ts_idx)
npt.assert_almost_equal(ref_subseq_idx, comp_subseq_idx)


@pytest.mark.parametrize(
"seed", np.random.choice(np.arange(10000), size=25, replace=False)
)
def test_random_ostinatoed(seed, dask_cluster):
with Client(dask_cluster) as dask_client:
def test_deterministic_ostinato():
pytest.fix_rng_state()

for _ in range(10):
m = 50
np.random.seed(seed)
Ts = [np.random.rand(n) for n in [64, 128, 256]]
Ts = [pytest.RNG.random(n) for n in [64, 128, 256]]

ref_radius, ref_Ts_idx, ref_subseq_idx = naive.aamp_ostinato(Ts, m)
comp_radius, comp_Ts_idx, comp_subseq_idx = aamp_ostinatoed(dask_client, Ts, m)
for p in [1.0, 2.0, 3.0]:
ref_radius, ref_Ts_idx, ref_subseq_idx = naive.aamp_ostinato(Ts, m, p=p)
comp_radius, comp_Ts_idx, comp_subseq_idx = aamp_ostinato(Ts, m, p=p)

npt.assert_almost_equal(ref_radius, comp_radius)
npt.assert_almost_equal(ref_Ts_idx, comp_Ts_idx)
npt.assert_almost_equal(ref_subseq_idx, comp_subseq_idx)
npt.assert_almost_equal(ref_radius, comp_radius)
npt.assert_almost_equal(ref_Ts_idx, comp_Ts_idx)
npt.assert_almost_equal(ref_subseq_idx, comp_subseq_idx)

pytest.unfix_rng_state()

@pytest.mark.parametrize("seed", [41, 88, 290, 292, 310, 328, 538, 556, 563, 570])
def test_deterministic_ostinatoed(seed, dask_cluster):

def test_random_ostinatoed(dask_cluster):
with Client(dask_cluster) as dask_client:
m = 50
np.random.seed(seed)
Ts = [np.random.rand(n) for n in [64, 128, 256]]
for _ in range(25):
m = 50
Ts = [pytest.RNG.random(n) for n in [64, 128, 256]]

for p in [1.0, 2.0, 3.0]:
ref_radius, ref_Ts_idx, ref_subseq_idx = naive.aamp_ostinato(Ts, m, p=p)
ref_radius, ref_Ts_idx, ref_subseq_idx = naive.aamp_ostinato(Ts, m)
comp_radius, comp_Ts_idx, comp_subseq_idx = aamp_ostinatoed(
dask_client, Ts, m, p=p
dask_client, Ts, m
)

npt.assert_almost_equal(ref_radius, comp_radius)
npt.assert_almost_equal(ref_Ts_idx, comp_Ts_idx)
npt.assert_almost_equal(ref_subseq_idx, comp_subseq_idx)


@pytest.mark.parametrize("seed", [41, 88, 290, 292, 310, 328, 538, 556, 563, 570])
def test_deterministic_ostinatoed(seed, dask_cluster):
pytest.fix_rng_state()

with Client(dask_cluster) as dask_client:
for _ in range(10):
m = 50
Ts = [pytest.RNG.random(n) for n in [64, 128, 256]]

for p in [1.0, 2.0, 3.0]:
ref_radius, ref_Ts_idx, ref_subseq_idx = naive.aamp_ostinato(Ts, m, p=p)
comp_radius, comp_Ts_idx, comp_subseq_idx = aamp_ostinatoed(
dask_client, Ts, m, p=p
)

npt.assert_almost_equal(ref_radius, comp_radius)
npt.assert_almost_equal(ref_Ts_idx, comp_Ts_idx)
npt.assert_almost_equal(ref_subseq_idx, comp_subseq_idx)

pytest.unfix_rng_state()


def test_input_not_overwritten_ostinato():
# aamp_ostinato preprocesses its input, a list of time series,
# by replacing nan value with 0 in each time series.
# This test ensures that the original input is not overwritten
m = 50
Ts = [np.random.rand(n) for n in [64, 128, 256]]
Ts = [pytest.RNG.random(n) for n in [64, 128, 256]]
for T in Ts:
T[0] = np.nan

Expand All @@ -107,7 +110,7 @@ def test_input_not_overwritten_ostinato():
def test_extract_several_consensus_ostinato():
# This test is to further ensure that the function `aamp_ostinato`
# does not tamper with the original data.
Ts = [np.random.rand(n) for n in [256, 512, 1024]]
Ts = [pytest.RNG.random(n) for n in [256, 512, 1024]]
Ts_ref = [T.copy() for T in Ts]
Ts_comp = [T.copy() for T in Ts]

Expand Down Expand Up @@ -145,7 +148,7 @@ def test_input_not_overwritten_ostinatoed(dask_cluster):
# This test ensures that the original input is not overwritten
with Client(dask_cluster) as dask_client:
m = 50
Ts = [np.random.rand(n) for n in [64, 128, 256]]
Ts = [pytest.RNG.random(n) for n in [64, 128, 256]]
for T in Ts:
T[0] = np.nan

Expand All @@ -163,7 +166,7 @@ def test_input_not_overwritten_ostinatoed(dask_cluster):
def test_extract_several_consensus_ostinatoed(dask_cluster):
# This test is to further ensure that the function `ostinatoed`
# does not tamper with the original data.
Ts = [np.random.rand(n) for n in [256, 512, 1024]]
Ts = [pytest.RNG.random(n) for n in [256, 512, 1024]]
Ts_ref = [T.copy() for T in Ts]
Ts_comp = [T.copy() for T in Ts]

Expand Down
Loading