From 903fdf77bddec26d3de6db2f88292e3605b7a482 Mon Sep 17 00:00:00 2001 From: Aayush Shah Date: Sun, 9 Jul 2023 23:45:31 -0700 Subject: [PATCH] TST: Add tests to increase coverage for ksample.py and npc.py files. test_ksample.py: o Added call to k_sample() with stat as a function, and keep_dist as True. o Verified that it's return has 3 elements, and 3rd element has 1000 entries. o Verified that the value is same as previous/existing result o Added call to bivariate_k_sample() with stat as a function, and keep_dist as True. test_npc.py: o Added variations to test_sim_npc() function. o Also modified the whitespacing for parameters. o Also removed1 unnecessary white space in parameters. --- permute/tests/test_ksample.py | 8 ++++++++ permute/tests/test_npc.py | 30 +++++++++++++++++++++--------- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/permute/tests/test_ksample.py b/permute/tests/test_ksample.py index e135d383..e86d0b57 100644 --- a/permute/tests/test_ksample.py +++ b/permute/tests/test_ksample.py @@ -15,6 +15,10 @@ def test_worms_ksample(): res = k_sample(worms.x, worms.y, stat='one-way anova', reps=1000, seed=1234) np.testing.assert_array_less(0.006, res[0]) np.testing.assert_array_less(res[0], 0.02) + res2 = k_sample(worms.x, worms.y, stat=one_way_anova, reps=1000, keep_dist=True, seed=1234) + assert len(res2[2]) == 1000 + np.testing.assert_array_equal([res[0], res[1]], [res2[0], res2[1]]) + np.testing.assert_array_less([8, 2], [res2[2][0], res2[2][1]]) def test_one_way_anova(): @@ -52,3 +56,7 @@ def test_testosterone_ksample(): assert len(x) == 55 res = bivariate_k_sample(x, group1, group2, reps=5000, seed=5) np.testing.assert_array_less(res[0], 0.0002) + res2 = bivariate_k_sample(x, group1, group2, reps=5000, stat=two_way_anova, keep_dist=True, seed=5) + assert len(res2[2]) == 5000 + np.testing.assert_array_equal([res[0], res[1]], [res2[0], res2[1]]) + np.testing.assert_array_less([0.002, 0.0001], [res2[2][0], res2[2][1]]) diff --git a/permute/tests/test_npc.py b/permute/tests/test_npc.py index b245ef4d..5d4856f3 100644 --- a/permute/tests/test_npc.py +++ b/permute/tests/test_npc.py @@ -118,7 +118,7 @@ def test_sim_npc(): # test Y always greater than X so p-value should be 1 responses = np.array([[0, 1], [0, 1], [1, 2], [1, 2]]) group = np.array([1, 1, 2, 2]) - my_randomizer = Experiment.Randomizer(randomize = randomize_group, seed = prng) + my_randomizer = Experiment.Randomizer(randomize=randomize_group, seed=prng) data = Experiment(group, responses) # create median test statistic to apply to every column @@ -135,24 +135,36 @@ def med_diff(data, resp_index): test_array = Experiment.make_test_array(med_diff, [0, 1]) res = sim_npc(data, test_array, combine="fisher", seed=None, reps=int(1000)) np.testing.assert_almost_equal(res[0], 1) - + res = sim_npc(data, test_array, combine="liptak", seed=1234, reps=int(1000)) + np.testing.assert_almost_equal(res[0], 1) + res = sim_npc(data, test_array, combine=tippett, in_place=True, seed=None, reps=int(1000)) + np.testing.assert_almost_equal(res[0], 1) + # test X = Y so p-value should be 1 responses = np.array([[0, 1], [0, 1], [0, 1], [0, 1]]) group = np.array([1, 1, 2, 2]) - data = Experiment(group, responses, randomizer = my_randomizer) - res = sim_npc(data, test = Experiment.make_test_array(Experiment.TestFunc.mean_diff, [0, 1]), + data = Experiment(group, responses, randomizer=my_randomizer) + res = sim_npc(data, test=Experiment.make_test_array(Experiment.TestFunc.mean_diff, [0, 1]), combine="fisher", seed=None, reps=int(1000)) np.testing.assert_almost_equal(res[0], 1) # test stat for cat_1 is smaller if X all 0s which about 0.015 chance so pvalue should be about 0.985 responses = np.array([[0, 1], [1, 1], [0, 1], [0, 1], [1, 1], [1, 1], [1, 1], [0, 1]]) group = np.array([1, 1, 1, 1, 2, 2, 2, 2]) - data = Experiment(group, responses, randomizer = my_randomizer) - res = sim_npc(data, test = Experiment.make_test_array(Experiment.TestFunc.mean_diff, [0, 1]), + data = Experiment(group, responses, randomizer=my_randomizer) + res = sim_npc(data, test=Experiment.make_test_array(Experiment.TestFunc.mean_diff, [0, 1]), combine="fisher", seed=None, reps=int(1000)) - np.testing.assert_almost_equal(res[0], 0.985, decimal = 2) - - + np.testing.assert_almost_equal(res[0], 0.985, decimal=2) + + # test stat for cat_1 is smaller if X all 0s which about 0.015 chance so pvalue should be about 0.0209 + responses = np.array([[0, 1], [1, 1], [0, 1], [0, 1], [1, 1], [1, 1], [1, 1], [0, 1]]) + group = np.array([1, 1, 1, 1, 2, 2, 2, 2]) + data = Experiment(group, responses, randomizer=my_randomizer) + res = sim_npc(data, test=Experiment.make_test_array(Experiment.TestFunc.one_way_anova, [0, 1]), + combine="fisher", seed=None, reps=int(1000)) + np.testing.assert_almost_equal(res[0], 0.0209, decimal=2) + + def test_fwer_minp(): prng = RandomState(55) pvalues = np.linspace(0.05, 0.9, num=5)