Skip to content

Commit

Permalink
Rename variables
Browse files Browse the repository at this point in the history
  • Loading branch information
JulioAPeraza committed May 28, 2024
1 parent 47b05e2 commit 8fc8030
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
36 changes: 21 additions & 15 deletions nimare/meta/ibma.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,8 @@ class Stouffers(IBMAEstimator):
the default is now set to True (two-sided), which differs from previous versions
where only one-sided tests were performed.
* Add correction for multiple contrasts within a study.
* New parameter: ``use_group_size`` to use publication group sizes for weights.
* New parameter: ``normalize_contrast_weights`` to normalized the weights by the
number of contrasts in each study.
.. versionchanged:: 0.2.1
Expand All @@ -320,8 +321,8 @@ class Stouffers(IBMAEstimator):
Whether to use sample sizes for weights (i.e., "weighted Stouffer's") or not,
as described in :footcite:t:`zaykin2011optimally`.
Default is False.
use_group_size : :obj:`bool`, optional
Whether to use publication group sizes for weights or not.
normalize_contrast_weights : :obj:`bool`, optional
Whether to use number of contrast per study to normalized the weights or not.
Default is False.
two_sided : :obj:`bool`, optional
If True, performs an unsigned t-test. Both positive and negative effects are considered;
Expand Down Expand Up @@ -364,13 +365,19 @@ class Stouffers(IBMAEstimator):

_required_inputs = {"z_maps": ("image", "z")}

def __init__(self, use_sample_size=False, use_group_size=False, two_sided=True, **kwargs):
def __init__(
self,
use_sample_size=False,
normalize_contrast_weights=False,
two_sided=True,
**kwargs,
):
super().__init__(**kwargs)
self.use_sample_size = use_sample_size
if self.use_sample_size:
self._required_inputs["sample_sizes"] = ("metadata", "sample_sizes")

self.use_group_size = use_group_size
self.normalize_contrast_weights = normalize_contrast_weights

self.two_sided = two_sided
self._mode = "concordant" if self.two_sided else "directed"
Expand All @@ -380,15 +387,14 @@ def _preprocess_input(self, dataset):
super()._preprocess_input(dataset)

study_mask = dataset.images["id"].isin(self.inputs_["id"])
# self.inputs_["study_mask"] = np.where(study_mask)[0]

# Convert each group to a unique integer value.
# Convert each contrast name to a unique integer value.
labels = dataset.images["study_id"][study_mask].to_list()
label_to_int = {label: i for i, label in enumerate(set(labels))}
label_counts = Counter(labels)

self.inputs_["groups"] = np.array([label_to_int[label] for label in labels])
self.inputs_["group_counts"] = np.array([label_counts[label] for label in labels])
self.inputs_["contrast_names"] = np.array([label_to_int[label] for label in labels])
self.inputs_["num_contrasts"] = np.array([label_counts[label] for label in labels])

def _generate_description(self):
description = (
Expand Down Expand Up @@ -419,15 +425,15 @@ def _fit_model(self, stat_maps, study_mask=None, corr=None):

est = pymare.estimators.StoufferCombinationTest(mode=self._mode)

group_maps, sub_corr = None, None
contrast_maps, sub_corr = None, None
if corr is not None:
group_maps = np.tile(self.inputs_["groups"][study_mask], (n_voxels, 1)).T
contrast_maps = np.tile(self.inputs_["contrast_names"][study_mask], (n_voxels, 1)).T
sub_corr = corr[np.ix_(study_mask, study_mask)]

weights = np.ones(n_studies)

if self.use_group_size:
weights *= 1 / self.inputs_["group_counts"][study_mask]
if self.normalize_contrast_weights:
weights *= 1 / self.inputs_["num_contrasts"][study_mask]

if self.use_sample_size:
sample_sizes = np.array(
Expand All @@ -437,7 +443,7 @@ def _fit_model(self, stat_maps, study_mask=None, corr=None):

weight_maps = np.tile(weights, (n_voxels, 1)).T

pymare_dset = pymare.Dataset(y=stat_maps, n=weight_maps, v=group_maps)
pymare_dset = pymare.Dataset(y=stat_maps, n=weight_maps, v=contrast_maps)
est.fit_dataset(pymare_dset, corr=sub_corr)
est_summary = est.summary()

Expand All @@ -459,7 +465,7 @@ def _fit(self, dataset):
)

corr = None
if self.inputs_["groups"].size != np.unique(self.inputs_["groups"]).size:
if self.inputs_["contrast_names"].size != np.unique(self.inputs_["contrast_names"]).size:
# If all studies are not unique, we will need to correct for multiple contrasts
corr = np.corrcoef(self.inputs_["z_maps"], rowvar=True)

Expand Down
2 changes: 1 addition & 1 deletion nimare/reports/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"alpha": "Alpha",
"prior": "Prior",
"use_sample_size": "Use sample size for weights",
"use_group_size": "Use group size for weights",
"normalize_contrast_weights": "Normalize by the number of contrasts",
"two_sided": "Two-sided test",
"beta": "Parameter estimate",
"se": "Standard error of the parameter estimate",
Expand Down
12 changes: 6 additions & 6 deletions nimare/tests/test_meta_ibma.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,35 @@
),
pytest.param(
ibma.Stouffers,
{"use_sample_size": False, "use_group_size": False},
{"use_sample_size": False, "normalize_contrast_weights": False},
None,
{},
("z", "p", "dof"),
id="Stouffers",
),
pytest.param(
ibma.Stouffers,
{"use_sample_size": True, "use_group_size": False},
{"use_sample_size": True, "normalize_contrast_weights": False},
None,
{},
("z", "p", "dof"),
id="Stouffers_sample_weighted",
),
pytest.param(
ibma.Stouffers,
{"use_sample_size": False, "use_group_size": True},
{"use_sample_size": False, "normalize_contrast_weights": True},
None,
{},
("z", "p", "dof"),
id="Stouffers_group_weighted",
id="Stouffers_contrast_weighted",
),
pytest.param(
ibma.Stouffers,
{"use_sample_size": True, "use_group_size": True},
{"use_sample_size": True, "normalize_contrast_weights": True},
None,
{},
("z", "p", "dof"),
id="Stouffers_sample_group_weighted",
id="Stouffers_sample_contrast_weighted",
),
pytest.param(
ibma.WeightedLeastSquares,
Expand Down

0 comments on commit 8fc8030

Please sign in to comment.