def test_dataset_init(variables): dataset = Dataset(*variables, X_names=["bork"]) n = len(variables[0]) assert dataset.X.shape == (n, 2) assert dataset.X_names == ["intercept", "bork"] dataset = Dataset(*variables, X_names=["bork"], add_intercept=False) assert dataset.X.shape == (n, 1) assert dataset.X_names == ["bork"]
def test_dataset_init(variables): """Test Dataset creation from numpy arrays.""" dataset = Dataset(*variables, X_names=["bork"]) n = len(variables[0]) assert dataset.X.shape == (n, 2) assert dataset.X_names == ["intercept", "bork"] dataset = Dataset(*variables, X_names=["bork"], add_intercept=False) assert dataset.X.shape == (n, 1) assert dataset.X_names == ["bork"]
def test_combination_test_from_dataset(Cls, data, mode, expected): """Test CombinationTest Estimators with PyMARE Datasets.""" 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)
def dataset_2d(variables): y, v, X = variables y = np.repeat(y, 3, axis=1) y[:, 1] = np.random.randint(-10, 10, size=len(y)) v = np.repeat(v, 3, axis=1) v[:, 1] = np.random.randint(2, 10, size=len(v)) return Dataset(y, v, X)
def dataset_n(): y = np.array( [[-3.0, -0.5, 0.0, -5.01, 0.35, -2.0, -6.0, -4.0, -4.3, -0.1, -1.0]]).T n = (np.array([[ 16, 16, 20.548, 32.597, 14.0, 11.118, 4.444, 12.414, 26.963, 130.556, 126.76 ]]).T / 2) return Dataset(y, n=n)
def dataset_n(): y = np.array([[-3., -0.5, 0., -5.01, 0.35, -2., -6., -4., -4.3, -0.1, -1.]]).T n = np.array([[ 16, 16, 20.548, 32.597, 14., 11.118, 4.444, 12.414, 26.963, 130.556, 126.76 ]]).T / 2 return Dataset(y, sample_sizes=n)
def dataset_2d(variables): """Build a larger Dataset with 2D data.""" y, v, X = variables y = np.repeat(y, 3, axis=1) y[:, 1] = np.random.randint(-10, 10, size=len(y)) v = np.repeat(v, 3, axis=1) v[:, 1] = np.random.randint(2, 10, size=len(v)) return Dataset(y, v, X)
def to_dataset(self, measure='g', **kwargs): y = self.get(measure) v = self.get('v_{}'.format(measure), error=False) try: n = self.get('n') except: n = None return Dataset(y=y, v=v, n=n, **kwargs)
def to_dataset(self, measure, **kwargs): measure = measure.lower() y = self.get(measure) v = self.get("v_{}".format(measure), error=False) try: n = self.get("n") except: n = None return Dataset(y=y, v=v, n=n, **kwargs)
def dataset_n(): """Build a Dataset with sample sizes, but no variances.""" y = np.array( [[-3.0, -0.5, 0.0, -5.01, 0.35, -2.0, -6.0, -4.0, -4.3, -0.1, -1.0]]).T n = (np.array([[ 16, 16, 20.548, 32.597, 14.0, 11.118, 4.444, 12.414, 26.963, 130.556, 126.76 ]]).T / 2) return Dataset(y, n=n)
def test_exact_perm_test_1d_no_mods(): dataset = Dataset([1, 1, 2, 1.3], [1.5, 1, 2, 4]) results = DerSimonianLaird().fit(dataset).summary() pmr = permutation_test(results, 867) assert pmr.n_perm == 16 assert pmr.exact assert isinstance(pmr.results, MetaRegressionResults) assert pmr.fe_p.shape == (1, 1) assert pmr.tau2_p.shape == (1, )
def to_dataset(self, measure, **kwargs): """Convert conversion results to a Dataset.""" measure = measure.lower() y = self.get(measure) v = self.get("v_{}".format(measure), error=False) try: n = self.get("n") except: n = None return Dataset(y=y, v=v, n=n, **kwargs)
def test_2d_loop_warning(dataset_2d): est = VarianceBasedLikelihoodEstimator() y = np.random.normal(size=(10, 100)) v = np.random.randint(1, 50, size=(10, 100)) dataset = Dataset(y, v) # Warning is raised when 2nd dim is > 10 with pytest.warns(UserWarning, match='Input contains'): est.fit(dataset) # But not when it's smaller est.fit(dataset_2d)
def test_stouffers_perm_test_exact(): """Test the exact permutation test on Stouffers Estimator.""" dataset = Dataset([1, 1, 2, 1.3], [1.5, 1, 2, 4]) results = StoufferCombinationTest().fit_dataset(dataset).summary() pmr = results.permutation_test(2000) assert pmr.n_perm == 16 assert pmr.exact assert isinstance(pmr.results, CombinationTestResults) assert pmr.perm_p["fe_p"].shape == (1, ) assert "tau2_p" not in pmr.perm_p
def test_exact_perm_test_1d_no_mods(): """Test the exact permutation test on 1D data.""" dataset = Dataset([1, 1, 2, 1.3], [1.5, 1, 2, 4]) results = DerSimonianLaird().fit_dataset(dataset).summary() pmr = results.permutation_test(867) assert pmr.n_perm == 16 assert pmr.exact assert isinstance(pmr.results, MetaRegressionResults) assert pmr.perm_p["fe_p"].shape == (1, 1) assert pmr.perm_p["tau2_p"].shape == (1, )
def test_stouffers_perm_test_approx(): """Test the approximate permutation test on Stouffers Estimator.""" y = [2.8, -0.2, -1, 4.5, 1.9, 2.38, 0.6, 1.88, -0.4, 1.5, 3.163, 0.7] dataset = Dataset(y) results = StoufferCombinationTest().fit_dataset(dataset).summary() pmr = results.permutation_test(2000) assert not pmr.exact assert pmr.n_perm == 2000 assert isinstance(pmr.results, CombinationTestResults) assert pmr.perm_p["fe_p"].shape == (1, ) assert "tau2_p" not in pmr.perm_p
def test_dataset_init(variables): dataset = Dataset(*variables, names=['bork']) # Convenience accessors assert np.array_equal(dataset.X, dataset.predictors) assert np.array_equal(dataset.y, dataset.estimates) assert np.array_equal(dataset.v, dataset.variances) n = len(variables[0]) assert dataset.X.shape == (n, 2) assert dataset.names == ['intercept', 'bork'] dataset = Dataset(*variables, names=['bork'], add_intercept=False, extra_arg=200) assert dataset.X.shape == (n, 1) assert dataset.names == ['bork'] assert 'extra_arg' in dataset.kwargs assert dataset.extra_arg == 200
def test_dataset_init_from_df(variables): df = pd.DataFrame({ 'y': [2, 4, 6], 'v_alt': [100, 100, 100], 'X1': [5, 2, 1], 'X7': [9, 8, 7] }) dataset = Dataset(v='v_alt', X=['X1', 'X7'], data=df) assert dataset.X.shape == (3, 3) assert dataset.X_names == ['intercept', 'X1', 'X7'] assert np.array_equal(dataset.y, np.array([[2, 4, 6]]).T) assert np.array_equal(dataset.v, np.array([[100, 100, 100]]).T)
def test_dataset_init_from_df(variables): df = pd.DataFrame({ "y": [2, 4, 6], "v_alt": [100, 100, 100], "X1": [5, 2, 1], "X7": [9, 8, 7] }) dataset = Dataset(v="v_alt", X=["X1", "X7"], data=df) assert dataset.X.shape == (3, 3) assert dataset.X_names == ["intercept", "X1", "X7"] assert np.array_equal(dataset.y, np.array([[2, 4, 6]]).T) assert np.array_equal(dataset.v, np.array([[100, 100, 100]]).T)
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)
def dataset(variables): return Dataset(*variables, names=['my_covariate'])
def small_dataset_2d(variables): """Build a small Dataset with 2D data.""" y = np.array([[1.5, 1.9, 2.2], [4, 2, 1]]).T v = np.array([[1, 0.8, 3], [1, 1.5, 1]]).T return Dataset(y, v)
def dataset(variables): """Build a Dataset compiled from the variables fixture.""" return Dataset(*variables, X_names=["my_covariate"])
def dataset(variables): return Dataset(*variables, X_names=["my_covariate"])
def small_dataset_2d(variables): y = np.array([[1.5, 1.9, 2.2], [4, 2, 1]]).T v = np.array([[1, 0.8, 3], [1, 1.5, 1]]).T return Dataset(y, v)