-
Notifications
You must be signed in to change notification settings - Fork 344
Fixed #1112 Migrate to New NumPy Random Number Generator (RNG) API #1131
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
base: main
Are you sure you want to change the base?
Changes from all commits
8fe16ec
da87749
9efa0f5
ade0375
87f06a6
acf1394
2d799e8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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])), | ||||||
| ] | ||||||
|
|
||||||
|
|
||||||
|
|
@@ -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: | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
According to Line 80: |
||||||
| # * (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) | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be consistent with other test functions, I was wondering if we should go with |
||||||
| m = 20 | ||||||
|
|
||||||
| T[10:30] = 1 | ||||||
|
|
@@ -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(): | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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() | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to fix state here? I can see we do not fix state for |
||||||
|
|
||||||
| 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]) | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you missed removing this one. |
||||||
| def test_deterministic_ostinatoed(seed, dask_cluster): | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| pytest.fix_rng_state() | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to fix state here? I can see we do not fix state for |
||||||
|
|
||||||
| 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 | ||||||
|
|
||||||
|
|
@@ -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] | ||||||
|
|
||||||
|
|
@@ -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 | ||||||
|
|
||||||
|
|
@@ -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] | ||||||
|
|
||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I cannot figure out how these numbers are obtained. Since tests are passing, I guess all is good here. I guess these numbers are revised to match the actual output. So... an off-topic question: are we cheating here by using ref values that are based on the calculation of outputs obtained from performant function ?