Skip to content

Commit

Permalink
[FIX] regular expression for no moderators (#821)
Browse files Browse the repository at this point in the history
* add a test with none moderator variable for CBMRInference

* add a separate test for moderators=None

* fix none moderators

* add file back in from main

---------

Co-authored-by: Yifan Yu <[email protected]>
Co-authored-by: jdkent <[email protected]>
  • Loading branch information
3 people committed Jul 21, 2023
1 parent c4346a2 commit e59a083
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
13 changes: 8 additions & 5 deletions nimare/meta/cbmr.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,11 +513,14 @@ def create_regular_expressions(self):
operator = "(\\ ?(?P<operator>[+-]?)\\ ??)"
for attr in ["groups", "moderators"]:
groups = getattr(self, attr)
first_group, second_group = [
f"(?P<{order}>{'|'.join([re.escape(g) for g in groups])})"
for order in ["first", "second"]
]
reg_expr = re.compile(first_group + "(" + operator + second_group + "?)")
if groups:
first_group, second_group = [
f"(?P<{order}>{'|'.join([re.escape(g) for g in groups])})"
for order in ["first", "second"]
]
reg_expr = re.compile(first_group + "(" + operator + second_group + "?)")
else:
reg_expr = None

setattr(self, "{}_regular_expression".format(attr), reg_expr)

Expand Down
36 changes: 34 additions & 2 deletions nimare/tests/test_meta_cbmr.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def cbmr_result(testdata_cbmr_simulated, model):
cbmr = CBMREstimator(
group_categories=["diagnosis", "drug_status"],
moderators=["standardized_sample_sizes", "standardized_avg_age", "schizophrenia_subtype"],
spline_spacing=200,
spline_spacing=100,
model=model,
penalty=False,
lr=1e-2,
Expand All @@ -56,7 +56,7 @@ def cbmr_result(testdata_cbmr_simulated, model):
@pytest.fixture(scope="session")
def inference_results(testdata_cbmr_simulated, cbmr_result):
"""Test inference results for CBMR estimator."""
inference = CBMRInference(device="cuda")
inference = CBMRInference(device="cpu")
inference.fit(cbmr_result)
t_con_groups = inference.create_contrast(
[
Expand Down Expand Up @@ -121,7 +121,39 @@ def test_firth_penalty(testdata_cbmr_simulated):
)
res = cbmr.fit(dataset=dset)
assert isinstance(res, nimare.results.MetaResult)


def test_moderators_none(testdata_cbmr_simulated):
"""Unit test for Firth penalty."""
dset = StandardizeField(fields=["sample_sizes", "avg_age", "schizophrenia_subtype"]).transform(
testdata_cbmr_simulated
)
cbmr = CBMREstimator(
group_categories=["diagnosis", "drug_status"],
moderators=None,
spline_spacing=100,
model=models.PoissonEstimator,
penalty=False,
lr=1e-2,
tol=1e7,
device="cpu",
)
res = cbmr.fit(dataset=dset)
assert isinstance(res, nimare.results.MetaResult)
inference = CBMRInference(device="cpu")
inference.fit(res)

t_con_groups = inference.create_contrast(
[
"DepressionYes",
],
source="groups",
)
inference_results = inference.transform(
t_con_groups=t_con_groups
)

assert isinstance(inference_results, nimare.results.MetaResult)

def test_CBMREstimator_update(testdata_cbmr_simulated):
"""Unit test for CBMR estimator update function."""
Expand Down

0 comments on commit e59a083

Please sign in to comment.