From f559bf95e4d0387a6e18301f2735888beaa1c343 Mon Sep 17 00:00:00 2001 From: Karthik Date: Mon, 16 Nov 2026 20:21:02 -0500 Subject: [PATCH] Migrate Ostinato test comparisons to assert_allclose --- tests/test_ostinato.py | 117 +++++++++++++++++++++-------------------- 1 file changed, 61 insertions(+), 56 deletions(-) diff --git a/tests/test_ostinato.py b/tests/test_ostinato.py index b269dad8b..be9a1a771 100644 --- a/tests/test_ostinato.py +++ b/tests/test_ostinato.py @@ -33,11 +33,11 @@ def test_random_ostinato(runs): Ts = [rng.RNG.rand(n) for n in [64, 128, 256]] ref_radius, ref_Ts_idx, ref_subseq_idx = naive.ostinato(Ts, m) - comp_radius, comp_Ts_idx, comp_subseq_idx = ostinato(Ts, m) + cmp_radius, cmp_Ts_idx, cmp_subseq_idx = 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) + npt.assert_allclose(cmp_radius, ref_radius, atol=1.5e-07) + npt.assert_allclose(cmp_Ts_idx, ref_Ts_idx, atol=1.5e-07) + npt.assert_allclose(cmp_subseq_idx, ref_subseq_idx, atol=1.5e-07) @pytest.mark.parametrize("seed", [79, 109, 112, 133, 151, 161, 251, 275, 309, 355]) @@ -48,11 +48,11 @@ def test_deterministic_ostinato(seed): Ts = [rng.RNG.rand(n) for n in [64, 128, 256]] ref_radius, ref_Ts_idx, ref_subseq_idx = naive.ostinato(Ts, m) - comp_radius, comp_Ts_idx, comp_subseq_idx = ostinato(Ts, m) + cmp_radius, cmp_Ts_idx, cmp_subseq_idx = 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) + npt.assert_allclose(cmp_radius, ref_radius, atol=1.5e-07) + npt.assert_allclose(cmp_Ts_idx, ref_Ts_idx, atol=1.5e-07) + npt.assert_allclose(cmp_subseq_idx, ref_subseq_idx, atol=1.5e-07) @pytest.mark.parametrize("runs", range(25)) @@ -62,11 +62,11 @@ def test_random_ostinatoed(runs, dask_cluster): Ts = [rng.RNG.rand(n) for n in [64, 128, 256]] ref_radius, ref_Ts_idx, ref_subseq_idx = naive.ostinato(Ts, m) - comp_radius, comp_Ts_idx, comp_subseq_idx = ostinatoed(dask_client, Ts, m) + cmp_radius, cmp_Ts_idx, cmp_subseq_idx = ostinatoed(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) + npt.assert_allclose(cmp_radius, ref_radius, atol=1.5e-07) + npt.assert_allclose(cmp_Ts_idx, ref_Ts_idx, atol=1.5e-07) + npt.assert_allclose(cmp_subseq_idx, ref_subseq_idx, atol=1.5e-07) @pytest.mark.parametrize("seed", [79, 109, 112, 133, 151, 161, 251, 275, 309, 355]) @@ -78,11 +78,11 @@ def test_deterministic_ostinatoed(seed, dask_cluster): Ts = [rng.RNG.rand(n) for n in [64, 128, 256]] ref_radius, ref_Ts_idx, ref_subseq_idx = naive.ostinato(Ts, m) - comp_radius, comp_Ts_idx, comp_subseq_idx = ostinatoed(dask_client, Ts, m) + cmp_radius, cmp_Ts_idx, cmp_subseq_idx = ostinatoed(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) + npt.assert_allclose(cmp_radius, ref_radius, atol=1.5e-07) + npt.assert_allclose(cmp_Ts_idx, ref_Ts_idx, atol=1.5e-07) + npt.assert_allclose(cmp_subseq_idx, ref_subseq_idx, atol=1.5e-07) @pytest.mark.parametrize("runs", range(25)) @@ -98,13 +98,13 @@ def test_random_ostinato_with_isconstant(runs): ref_radius, ref_Ts_idx, ref_subseq_idx = naive.ostinato( Ts, m, Ts_subseq_isconstant=Ts_subseq_isconstant ) - comp_radius, comp_Ts_idx, comp_subseq_idx = ostinato( + cmp_radius, cmp_Ts_idx, cmp_subseq_idx = ostinato( Ts, m, Ts_subseq_isconstant=Ts_subseq_isconstant ) - 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_allclose(cmp_radius, ref_radius, atol=1.5e-07) + npt.assert_allclose(cmp_Ts_idx, ref_Ts_idx, atol=1.5e-07) + npt.assert_allclose(cmp_subseq_idx, ref_subseq_idx, atol=1.5e-07) @pytest.mark.parametrize("seed", [79, 109, 112, 133, 151, 161, 251, 275, 309, 355]) @@ -131,13 +131,13 @@ def test_deterministic_ostinatoed_with_isconstant(seed, dask_cluster): ref_radius, ref_Ts_idx, ref_subseq_idx = naive.ostinato( Ts, m, Ts_subseq_isconstant=Ts_subseq_isconstant ) - comp_radius, comp_Ts_idx, comp_subseq_idx = ostinatoed( + cmp_radius, cmp_Ts_idx, cmp_subseq_idx = ostinatoed( dask_client, Ts, m, Ts_subseq_isconstant=Ts_subseq_isconstant ) - 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_allclose(cmp_radius, ref_radius, atol=1.5e-07) + npt.assert_allclose(cmp_Ts_idx, ref_Ts_idx, atol=1.5e-07) + npt.assert_allclose(cmp_subseq_idx, ref_subseq_idx, atol=1.5e-07) def test_input_not_overwritten_ostinato(): @@ -153,39 +153,43 @@ def test_input_not_overwritten_ostinato(): Ts_input = [T.copy() for T in Ts] ostinato(Ts_input, m) for i in range(len(Ts)): - T_ref = Ts[i] - T_comp = Ts_input[i] - npt.assert_almost_equal(T_ref[np.isfinite(T_ref)], T_comp[np.isfinite(T_comp)]) + ref_T = Ts[i] + cmp_T = Ts_input[i] + npt.assert_allclose( + cmp_T[np.isfinite(cmp_T)], ref_T[np.isfinite(ref_T)], atol=1.5e-07 + ) def test_extract_several_consensus_ostinato(): # This test is to further ensure that the function `ostinato` # does not tamper with the original data. Ts = [rng.RNG.rand(n) for n in [256, 512, 1024]] - Ts_ref = [T.copy() for T in Ts] - Ts_comp = [T.copy() for T in Ts] + ref_Ts = [T.copy() for T in Ts] + cmp_Ts = [T.copy() for T in Ts] m = 20 k = 5 # Get the first `k` consensus motifs for _ in range(k): - # Find consensus motif and its NN in each time series in Ts_comp - # Remove them from Ts_comp as well as Ts_ref, and assert that the + # Find consensus motif and its NN in each time series in cmp_Ts + # Remove them from cmp_Ts as well as ref_Ts, and assert that the # two time series are the same - radius, Ts_idx, subseq_idx = ostinato(Ts_comp, m) - consensus_motif = Ts_comp[Ts_idx][subseq_idx : subseq_idx + m].copy() - for i in range(len(Ts_comp)): + radius, Ts_idx, subseq_idx = ostinato(cmp_Ts, m) + consensus_motif = cmp_Ts[Ts_idx][subseq_idx : subseq_idx + m].copy() + for i in range(len(cmp_Ts)): if i == Ts_idx: query_idx = subseq_idx else: query_idx = None - idx = np.argmin(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 + idx = np.argmin(core.mass(consensus_motif, cmp_Ts[i], query_idx=query_idx)) + cmp_Ts[i][idx : idx + m] = np.nan + ref_Ts[i][idx : idx + m] = np.nan - npt.assert_almost_equal( - Ts_ref[i][np.isfinite(Ts_ref[i])], Ts_comp[i][np.isfinite(Ts_comp[i])] + npt.assert_allclose( + cmp_Ts[i][np.isfinite(cmp_Ts[i])], + ref_Ts[i][np.isfinite(ref_Ts[i])], + atol=1.5e-07, ) @@ -203,10 +207,10 @@ def test_input_not_overwritten_ostinatoed(dask_cluster): Ts_input = [T.copy() for T in Ts] ostinatoed(dask_client, Ts_input, m) for i in range(len(Ts)): - T_ref = Ts[i] - T_comp = Ts_input[i] - npt.assert_almost_equal( - T_ref[np.isfinite(T_ref)], T_comp[np.isfinite(T_comp)] + ref_T = Ts[i] + cmp_T = Ts_input[i] + npt.assert_allclose( + cmp_T[np.isfinite(cmp_T)], ref_T[np.isfinite(ref_T)], atol=1.5e-07 ) @@ -214,32 +218,33 @@ 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 = [rng.RNG.rand(n) for n in [256, 512, 1024]] - Ts_ref = [T.copy() for T in Ts] - Ts_comp = [T.copy() for T in Ts] + ref_Ts = [T.copy() for T in Ts] + cmp_Ts = [T.copy() for T in Ts] m = 20 with Client(dask_cluster) as dask_client: k = 5 # Get the first `k` consensus motifs for _ in range(k): - # Find consensus motif and its NN in each time series in Ts_comp - # Remove them from Ts_comp as well as Ts_ref, and assert that the + # Find consensus motif and its NN in each time series in cmp_Ts + # Remove them from cmp_Ts as well as ref_Ts, and assert that the # two time series are the same - radius, Ts_idx, subseq_idx = ostinatoed(dask_client, Ts_comp, m) - consensus_motif = Ts_comp[Ts_idx][subseq_idx : subseq_idx + m].copy() - for i in range(len(Ts_comp)): + radius, Ts_idx, subseq_idx = ostinatoed(dask_client, cmp_Ts, m) + consensus_motif = cmp_Ts[Ts_idx][subseq_idx : subseq_idx + m].copy() + for i in range(len(cmp_Ts)): if i == Ts_idx: query_idx = subseq_idx else: query_idx = None idx = np.argmin( - core.mass(consensus_motif, Ts_comp[i], query_idx=query_idx) + core.mass(consensus_motif, cmp_Ts[i], query_idx=query_idx) ) - Ts_comp[i][idx : idx + m] = np.nan - Ts_ref[i][idx : idx + m] = np.nan + cmp_Ts[i][idx : idx + m] = np.nan + ref_Ts[i][idx : idx + m] = np.nan - npt.assert_almost_equal( - Ts_ref[i][np.isfinite(Ts_ref[i])], - Ts_comp[i][np.isfinite(Ts_comp[i])], + npt.assert_allclose( + cmp_Ts[i][np.isfinite(cmp_Ts[i])], + ref_Ts[i][np.isfinite(ref_Ts[i])], + atol=1.5e-07, )