From 161e43619f07ad1f766beca4b1681b7bdbb3977f Mon Sep 17 00:00:00 2001 From: j1c Date: Wed, 10 Jan 2024 23:11:07 -0500 Subject: [PATCH] More unit tests and black --- hyppo/conditional/tests/test_pcorr.py | 8 ++++++-- hyppo/conditional/tests/test_pdcorr.py | 11 +++++++++++ hyppo/tools/tests/test_common.py | 13 +++++++++++++ hyppo/tools/tests/test_conditional_indep_sim.py | 5 +++-- hyppo/tools/tests/test_power.py | 12 ++++++++++++ 5 files changed, 45 insertions(+), 4 deletions(-) diff --git a/hyppo/conditional/tests/test_pcorr.py b/hyppo/conditional/tests/test_pcorr.py index 758aee938..45a31b7c1 100644 --- a/hyppo/conditional/tests/test_pcorr.py +++ b/hyppo/conditional/tests/test_pcorr.py @@ -21,8 +21,12 @@ def test_linear_oned(self, n, obs_stat): @pytest.mark.parametrize("n", [100, 200]) def test_rep(self, n): x, y, z = indep_normal(n, 1) - stat1, pvalue1 = PartialCorr().test(x, y, z, random_state=2, auto=False, reps=1000) - stat2, pvalue2 = PartialCorr().test(x, y, z, random_state=2, auto=False, reps=1000) + stat1, pvalue1 = PartialCorr().test( + x, y, z, random_state=2, auto=False, reps=1000 + ) + stat2, pvalue2 = PartialCorr().test( + x, y, z, random_state=2, auto=False, reps=1000 + ) assert stat1 == stat2 assert pvalue1 == pvalue2 diff --git a/hyppo/conditional/tests/test_pdcorr.py b/hyppo/conditional/tests/test_pdcorr.py index 79e35dffa..f4b725a6a 100644 --- a/hyppo/conditional/tests/test_pdcorr.py +++ b/hyppo/conditional/tests/test_pdcorr.py @@ -27,6 +27,17 @@ def test_rep(self, n): assert stat1 == stat2 assert pvalue1 == pvalue2 + @pytest.mark.parametrize("n", [100]) + @pytest.mark.parametrize("obs_stat", [0.0143057]) + @pytest.mark.parametrize("obs_pvalue", [0.12]) + def test_indep_normal_corr(self, n, obs_stat, obs_pvalue): + np.random.seed(123456789) + x, y, z = indep_normal(n, 1) + stat1, pvalue1 = PartialDcorr(use_cov=False).test(x, y, z) + + assert_almost_equal(stat1, obs_stat, decimal=2) + assert_almost_equal(pvalue1, obs_pvalue, decimal=2) + class TestPDcorrTypeIError: def test_oned(self): diff --git a/hyppo/tools/tests/test_common.py b/hyppo/tools/tests/test_common.py index bab2618f9..bf5b127f6 100644 --- a/hyppo/tools/tests/test_common.py +++ b/hyppo/tools/tests/test_common.py @@ -144,6 +144,19 @@ def test_error_distkern(self): assert_raises(ValueError, _check_distmat, x, y) assert_raises(ValueError, _check_kernmat, x, y) + def test_error_threedist(self): + x = np.arange(10).reshape(-1, 1) + y = np.arange(10).reshape(1, -1) + z = np.arange(10).reshape(1, -1) + assert_raises(ValueError, _check_distmat, x, y, z) + + def test_error_nonzerodiag(self): + x = np.eye(10, dtype=int) ^ 1 # 0 on diag, 1 elsewhere + y = np.eye(10, dtype=int) ^ 1 # 0 on diag, 1 elsewhere + z = np.eye(10, dtype=int) + + assert_raises(ValueError, _check_distmat, x, y, z) + def test_error_multidistkern(self): # raises error if samples are low (< 3) x = np.arange(10).reshape(-1, 1) diff --git a/hyppo/tools/tests/test_conditional_indep_sim.py b/hyppo/tools/tests/test_conditional_indep_sim.py index e2ad5e561..b1ef888f7 100644 --- a/hyppo/tools/tests/test_conditional_indep_sim.py +++ b/hyppo/tools/tests/test_conditional_indep_sim.py @@ -61,7 +61,9 @@ class TestCondiIndepSimErrorWarn: def test_np_inctype(self): assert_raises(ValueError, condi_indep_sim, n="a", p=1, sim="independent_normal") - assert_raises(ValueError, condi_indep_sim, n=10, p=7.0, sim="independent_normal") + assert_raises( + ValueError, condi_indep_sim, n=10, p=7.0, sim="independent_normal" + ) def test_low_n(self): assert_raises(ValueError, condi_indep_sim, n=3, p=1, sim="independent_normal") @@ -71,4 +73,3 @@ def test_low_p(self): def test_wrong_sim(self): assert_raises(ValueError, condi_indep_sim, n=100, p=1, sim="abcd") - diff --git a/hyppo/tools/tests/test_power.py b/hyppo/tools/tests/test_power.py index 26c7b1070..4dc094670 100644 --- a/hyppo/tools/tests/test_power.py +++ b/hyppo/tools/tests/test_power.py @@ -42,6 +42,18 @@ def test_multi(self): ) assert_almost_equal(est_power, 0.05, decimal=1) + def test_condi_power_perm(self): + np.random.seed(123456789) + est_power = power( + "conditionaldcorr", + sim_type="condi", + sim="independent_normal", + n=50, + p=1, + auto=False, + ) + assert_almost_equal(est_power, 0.05, decimal=2) + class TestPowerErrorWarn: def test_power_nosim(self):