Skip to content

Commit e963799

Browse files
authored
Merge pull request #45 from neurostuff/enh/bump_nimare_0.11.1
bump nimare and simplify run.py
2 parents 993ca32 + 0b04293 commit e963799

4 files changed

Lines changed: 168 additions & 21 deletions

File tree

compose_runner/run.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ def __init__(
8080
self.cached_studyset = None
8181
self.cached_annotation = None
8282
self.cached_specification = None
83-
self.first_dataset = None
84-
self.second_dataset = None
83+
self.first_studyset = None
84+
self.second_studyset = None
8585
self.estimator = None
8686
self.corrector = None
8787

@@ -292,13 +292,9 @@ def process_bundle(self, n_cores=None):
292292
studyset = Studyset(self.cached_studyset)
293293
annotation = Annotation(self.cached_annotation, studyset)
294294
first_studyset, second_studyset = self.apply_filter(studyset, annotation)
295-
first_dataset = first_studyset.to_dataset()
296-
second_dataset = (
297-
second_studyset.to_dataset() if second_studyset is not None else None
298-
)
299295
estimator, corrector = self.load_specification(n_cores=n_cores)
300-
self.first_dataset = first_dataset
301-
self.second_dataset = second_dataset
296+
self.first_studyset = first_studyset
297+
self.second_studyset = second_studyset
302298
self.estimator = estimator
303299
self.corrector = corrector
304300

@@ -323,25 +319,28 @@ def create_result_object(self):
323319
raise ValueError(f"Could not create result for {self.meta_analysis_id}")
324320

325321
def run_meta_analysis(self):
326-
if self.second_dataset and isinstance(self.estimator, PairwiseCBMAEstimator):
322+
if self.second_studyset and isinstance(self.estimator, PairwiseCBMAEstimator):
327323
workflow = PairwiseCBMAWorkflow(
328324
estimator=self.estimator,
329325
corrector=self.corrector,
330326
diagnostics="focuscounter",
331327
output_dir=self.result_dir,
332328
)
333-
self.meta_results = workflow.fit(self.first_dataset, self.second_dataset)
334-
elif self.second_dataset is None and isinstance(self.estimator, CBMAEstimator):
329+
self.meta_results = workflow.fit(
330+
self.first_studyset,
331+
self.second_studyset,
332+
)
333+
elif self.second_studyset is None and isinstance(self.estimator, CBMAEstimator):
335334
workflow = CBMAWorkflow(
336335
estimator=self.estimator,
337336
corrector=self.corrector,
338337
diagnostics="focuscounter",
339338
output_dir=self.result_dir,
340339
)
341-
self.meta_results = workflow.fit(self.first_dataset, self.second_dataset)
340+
self.meta_results = workflow.fit(self.first_studyset)
342341
else:
343342
raise ValueError(
344-
f"Estimator {self.estimator} and datasets {self.first_dataset} and {self.second_dataset} are not compatible."
343+
f"Estimator {self.estimator} and studysets {self.first_studyset} and {self.second_studyset} are not compatible."
345344
)
346345
self._persist_meta_results()
347346

compose_runner/tests/test_cli.py

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,47 @@
11
from click.testing import CliRunner
22

3+
from compose_runner import cli as cli_module
34
from compose_runner.cli import cli
45

56

6-
def test_cli():
7+
def test_cli(monkeypatch):
8+
calls = {}
9+
10+
def fake_run(meta_analysis_id, environment, result_dir, nsc_key, nv_key, no_upload, n_cores):
11+
calls["args"] = {
12+
"meta_analysis_id": meta_analysis_id,
13+
"environment": environment,
14+
"result_dir": result_dir,
15+
"nsc_key": nsc_key,
16+
"nv_key": nv_key,
17+
"no_upload": no_upload,
18+
"n_cores": n_cores,
19+
}
20+
return "https://example.org/result", None
21+
22+
monkeypatch.setattr(cli_module, "run", fake_run)
23+
724
runner = CliRunner()
8-
result = runner.invoke(cli, [
9-
"4nBwrGsqVWtt",
10-
'--environment', "staging",
11-
"--n-cores", 1,
12-
"--no-upload"])
25+
result = runner.invoke(
26+
cli,
27+
[
28+
"3opENJpHxRsH",
29+
"--environment",
30+
"staging",
31+
"--n-cores",
32+
1,
33+
"--no-upload",
34+
],
35+
)
36+
1337
assert result.exit_code == 0
38+
assert calls["args"] == {
39+
"meta_analysis_id": "3opENJpHxRsH",
40+
"environment": "staging",
41+
"result_dir": None,
42+
"nsc_key": None,
43+
"nv_key": None,
44+
"no_upload": True,
45+
"n_cores": 1,
46+
}
47+
assert "https://example.org/result" in result.output

compose_runner/tests/test_run.py

Lines changed: 115 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import pytest
22
from requests.exceptions import HTTPError
33

4+
from compose_runner import run as run_module
45
from compose_runner.run import Runner
56

6-
77
@pytest.mark.vcr(record_mode="once")
88
def test_incorrect_id():
99
runner = Runner(
@@ -62,6 +62,120 @@ def test_run_string_group_comparison_workflow():
6262
)
6363
runner.run_workflow()
6464

65+
66+
def test_process_bundle_keeps_studysets(monkeypatch):
67+
first_studyset = object()
68+
second_studyset = object()
69+
estimator = object()
70+
corrector = object()
71+
72+
class FakeStudyset:
73+
def __init__(self, source):
74+
self.source = source
75+
76+
class FakeAnnotation:
77+
def __init__(self, source, studyset):
78+
self.source = source
79+
self.studyset = studyset
80+
81+
def fake_apply_filter(self, studyset, annotation):
82+
assert isinstance(studyset, FakeStudyset)
83+
assert isinstance(annotation, FakeAnnotation)
84+
return first_studyset, second_studyset
85+
86+
def fake_load_specification(self, n_cores=None):
87+
assert n_cores == 3
88+
return estimator, corrector
89+
90+
monkeypatch.setattr(run_module, "Studyset", FakeStudyset)
91+
monkeypatch.setattr(run_module, "Annotation", FakeAnnotation)
92+
monkeypatch.setattr(Runner, "apply_filter", fake_apply_filter)
93+
monkeypatch.setattr(Runner, "load_specification", fake_load_specification)
94+
95+
runner = Runner(meta_analysis_id="made_up_id", environment="staging")
96+
runner.cached_studyset = {"id": "studyset", "studies": []}
97+
runner.cached_annotation = {"note_keys": {}}
98+
99+
runner.process_bundle(n_cores=3)
100+
101+
assert runner.first_studyset is first_studyset
102+
assert runner.second_studyset is second_studyset
103+
assert runner.estimator is estimator
104+
assert runner.corrector is corrector
105+
106+
107+
def test_run_meta_analysis_single_studyset_uses_cbma_workflow(monkeypatch, tmp_path):
108+
calls = {}
109+
110+
class FakeCBMAEstimator:
111+
pass
112+
113+
class FakeWorkflow:
114+
def __init__(self, estimator, corrector, diagnostics, output_dir):
115+
calls["init"] = {
116+
"estimator": estimator,
117+
"corrector": corrector,
118+
"diagnostics": diagnostics,
119+
"output_dir": output_dir,
120+
}
121+
122+
def fit(self, dataset):
123+
calls["fit"] = {"dataset": dataset}
124+
return "meta-results"
125+
126+
monkeypatch.setattr(run_module, "CBMAEstimator", FakeCBMAEstimator)
127+
monkeypatch.setattr(run_module, "CBMAWorkflow", FakeWorkflow)
128+
129+
runner = Runner(meta_analysis_id="made_up_id", environment="staging", result_dir=tmp_path)
130+
runner.first_studyset = object()
131+
runner.second_studyset = None
132+
runner.estimator = FakeCBMAEstimator()
133+
runner.corrector = object()
134+
runner._persist_meta_results = lambda: None
135+
136+
runner.run_meta_analysis()
137+
138+
assert calls["fit"] == {"dataset": runner.first_studyset}
139+
assert runner.meta_results == "meta-results"
140+
141+
142+
def test_run_meta_analysis_pairwise_uses_pairwise_workflow(monkeypatch, tmp_path):
143+
calls = {}
144+
145+
class FakePairwiseEstimator:
146+
pass
147+
148+
class FakeWorkflow:
149+
def __init__(self, estimator, corrector, diagnostics, output_dir):
150+
calls["init"] = {
151+
"estimator": estimator,
152+
"corrector": corrector,
153+
"diagnostics": diagnostics,
154+
"output_dir": output_dir,
155+
}
156+
157+
def fit(self, dataset1, dataset2):
158+
calls["fit"] = {"dataset1": dataset1, "dataset2": dataset2}
159+
return "pairwise-results"
160+
161+
monkeypatch.setattr(run_module, "PairwiseCBMAEstimator", FakePairwiseEstimator)
162+
monkeypatch.setattr(run_module, "PairwiseCBMAWorkflow", FakeWorkflow)
163+
164+
runner = Runner(meta_analysis_id="made_up_id", environment="staging", result_dir=tmp_path)
165+
runner.first_studyset = object()
166+
runner.second_studyset = object()
167+
runner.estimator = FakePairwiseEstimator()
168+
runner.corrector = object()
169+
runner._persist_meta_results = lambda: None
170+
171+
runner.run_meta_analysis()
172+
173+
assert calls["fit"] == {
174+
"dataset1": runner.first_studyset,
175+
"dataset2": runner.second_studyset,
176+
}
177+
assert runner.meta_results == "pairwise-results"
178+
65179
# def test_yifan_workflow():
66180
# runner = Runner(
67181
# meta_analysis_id="4WELjap2yCJm",

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ classifiers = [
1414
"Programming Language :: Python :: 3",
1515
]
1616
dynamic = ["version"]
17-
dependencies = ["nimare>=0.11.0", "click", "sentry-sdk", "numpy"]
17+
dependencies = ["nimare>=0.11.1", "click", "sentry-sdk", "numpy"]
1818

1919
[project.urls]
2020
Repository = "https://github.com/neurostuff/compose-runner"

0 commit comments

Comments
 (0)