From 66b45ce0737fa856a424584c349c5f14c1b96efb Mon Sep 17 00:00:00 2001 From: James Kent Date: Tue, 10 Sep 2024 19:22:46 -0500 Subject: [PATCH 1/4] have directional z-values --- pymare/estimators/combination.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/pymare/estimators/combination.py b/pymare/estimators/combination.py index 5fd4fc4..73d3c5e 100644 --- a/pymare/estimators/combination.py +++ b/pymare/estimators/combination.py @@ -45,11 +45,15 @@ def fit(self, z, *args, **kwargs): p1 = ose.p_value(z, *args, **kwargs) p2 = ose.p_value(-z, *args, **kwargs) p = np.minimum(1, 2 * np.minimum(p1, p2)) + z_calc = ss.norm.isf(p) + z_calc[p2 < p1] *= -1 else: if self.mode == "undirected": z = np.abs(z) p = self.p_value(z, *args, **kwargs) - self.params_ = {"p": p} + z_calc = ss.norm.isf(p) + + self.params_ = {"p": p, "z": z_calc} return self def summary(self): @@ -60,7 +64,9 @@ def summary(self): "This {} instance hasn't been fitted yet. Please " "call fit() before summary().".format(name) ) - return CombinationTestResults(self, self.dataset_, p=self.params_["p"]) + return CombinationTestResults( + self, self.dataset_, z=self.params_["z"], p=self.params_["p"] + ) class StoufferCombinationTest(CombinationTest): From 25306be17e53cfe7d205f55d198a576c5bb05655 Mon Sep 17 00:00:00 2001 From: James Kent Date: Tue, 10 Sep 2024 19:40:27 -0500 Subject: [PATCH 2/4] modify tests --- pymare/tests/test_combination_tests.py | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/pymare/tests/test_combination_tests.py b/pymare/tests/test_combination_tests.py index ac6e956..b18fab4 100644 --- a/pymare/tests/test_combination_tests.py +++ b/pymare/tests/test_combination_tests.py @@ -16,13 +16,13 @@ (StoufferCombinationTest, _z1, "concordant", [4.55204117]), (StoufferCombinationTest, _z2, "directed", [4.69574275, -4.16803071]), (StoufferCombinationTest, _z2, "undirected", [4.87462819, 4.16803071]), - (StoufferCombinationTest, _z2, "concordant", [4.55204117, 4.00717817]), + (StoufferCombinationTest, _z2, "concordant", [4.55204117, -4.00717817]), (FisherCombinationTest, _z1, "directed", [5.22413541]), (FisherCombinationTest, _z1, "undirected", [5.27449962]), (FisherCombinationTest, _z1, "concordant", [5.09434911]), (FisherCombinationTest, _z2, "directed", [5.22413541, -3.30626405]), (FisherCombinationTest, _z2, "undirected", [5.27449962, 4.27572965]), - (FisherCombinationTest, _z2, "concordant", [5.09434911, 4.11869468]), + (FisherCombinationTest, _z2, "concordant", [5.09434911, -4.11869468]), ] @@ -30,8 +30,7 @@ def test_combination_test(Cls, data, mode, expected): """Test CombinationTest Estimators with numpy data.""" results = Cls(mode).fit(data).params_ - z = ss.norm.isf(results["p"]) - assert np.allclose(z, expected, atol=1e-5) + assert np.allclose(results["z"], expected, atol=1e-5) @pytest.mark.parametrize("Cls,data,mode,expected", _params) @@ -40,8 +39,7 @@ def test_combination_test_from_dataset(Cls, data, mode, expected): dset = Dataset(y=data) est = Cls(mode).fit_dataset(dset) results = est.summary() - z = ss.norm.isf(results.p) - assert np.allclose(z, expected, atol=1e-5) + assert np.allclose(results.z, expected, atol=1e-5) def test_stouffer_adjusted(): @@ -61,10 +59,9 @@ def test_stouffer_adjusted(): groups = np.tile(np.array([0, 0, 1, 2, 2, 2]), (data.shape[1], 1)).T results = StoufferCombinationTest("directed").fit(z=data, w=weights, g=groups).params_ - z = ss.norm.isf(results["p"]) z_expected = np.array([5.00088912, 3.70356943, 4.05465924, 5.4633001, 5.18927878]) - assert np.allclose(z, z_expected, atol=1e-5) + assert np.allclose(results["z"], z_expected, atol=1e-5) # Test with weights and no groups. Limiting cases. # Limiting case 1: all correlations are one. @@ -74,11 +71,11 @@ def test_stouffer_adjusted(): groups_l1 = np.tile(np.array([0, 0, 0, 0, 0]), (data_l1.shape[1], 1)).T results_l1 = StoufferCombinationTest("directed").fit(z=data_l1, g=groups_l1).params_ - z_l1 = ss.norm.isf(results_l1["p"]) + # z_l1 = ss.norm.isf(results_l1["p"]) sigma_l1 = n_maps_l1 * (n_maps_l1 - 1) # Expected inflation term z_expected_l1 = n_maps_l1 * common_sample / np.sqrt(n_maps_l1 + sigma_l1) - assert np.allclose(z_l1, z_expected_l1, atol=1e-5) + assert np.allclose(results_l1["z"], z_expected_l1, atol=1e-5) # Test with correlation matrix and groups. data_corr = data - data.mean(0) @@ -86,10 +83,10 @@ def test_stouffer_adjusted(): results_corr = ( StoufferCombinationTest("directed").fit(z=data, w=weights, g=groups, corr=corr).params_ ) - z_corr = ss.norm.isf(results_corr["p"]) + # z_corr = ss.norm.isf(results_corr["p"]) z_corr_expected = np.array([5.00088912, 3.70356943, 4.05465924, 5.4633001, 5.18927878]) - assert np.allclose(z_corr, z_corr_expected, atol=1e-5) + assert np.allclose(results_corr["z"], z_corr_expected, atol=1e-5) # Test with no correlation matrix and groups, but only one feature. with pytest.raises(ValueError): @@ -101,6 +98,6 @@ def test_stouffer_adjusted(): # Test with correlation matrix and no groups. results1 = StoufferCombinationTest("directed").fit(z=_z1, corr=corr).params_ - z1 = ss.norm.isf(results1["p"]) + # z1 = ss.norm.isf(results1["p"]) - assert np.allclose(z1, [4.69574], atol=1e-5) + assert np.allclose(results1["z"], [4.69574], atol=1e-5) From 990910448f487b29950830ac8f628d1b87b83f04 Mon Sep 17 00:00:00 2001 From: James Kent Date: Tue, 10 Sep 2024 19:46:19 -0500 Subject: [PATCH 3/4] remove unused import --- pymare/tests/test_combination_tests.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pymare/tests/test_combination_tests.py b/pymare/tests/test_combination_tests.py index b18fab4..8a0a92f 100644 --- a/pymare/tests/test_combination_tests.py +++ b/pymare/tests/test_combination_tests.py @@ -2,7 +2,6 @@ import numpy as np import pytest -import scipy.stats as ss from pymare import Dataset from pymare.estimators import FisherCombinationTest, StoufferCombinationTest From 046d36c6aa210742a1c5a7233f66efbb4047509c Mon Sep 17 00:00:00 2001 From: James Kent Date: Tue, 10 Sep 2024 19:51:28 -0500 Subject: [PATCH 4/4] remove comments --- pymare/tests/test_combination_tests.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/pymare/tests/test_combination_tests.py b/pymare/tests/test_combination_tests.py index 8a0a92f..c684d8d 100644 --- a/pymare/tests/test_combination_tests.py +++ b/pymare/tests/test_combination_tests.py @@ -70,7 +70,6 @@ def test_stouffer_adjusted(): groups_l1 = np.tile(np.array([0, 0, 0, 0, 0]), (data_l1.shape[1], 1)).T results_l1 = StoufferCombinationTest("directed").fit(z=data_l1, g=groups_l1).params_ - # z_l1 = ss.norm.isf(results_l1["p"]) sigma_l1 = n_maps_l1 * (n_maps_l1 - 1) # Expected inflation term z_expected_l1 = n_maps_l1 * common_sample / np.sqrt(n_maps_l1 + sigma_l1) @@ -82,7 +81,6 @@ def test_stouffer_adjusted(): results_corr = ( StoufferCombinationTest("directed").fit(z=data, w=weights, g=groups, corr=corr).params_ ) - # z_corr = ss.norm.isf(results_corr["p"]) z_corr_expected = np.array([5.00088912, 3.70356943, 4.05465924, 5.4633001, 5.18927878]) assert np.allclose(results_corr["z"], z_corr_expected, atol=1e-5) @@ -97,6 +95,5 @@ def test_stouffer_adjusted(): # Test with correlation matrix and no groups. results1 = StoufferCombinationTest("directed").fit(z=_z1, corr=corr).params_ - # z1 = ss.norm.isf(results1["p"]) assert np.allclose(results1["z"], [4.69574], atol=1e-5)