Skip to content

Commit 18e5e95

Browse files
authored
Merge pull request #464 from effigies/test/multiproc-safe
TEST: Clear registry consistently to avoid order dependency
2 parents 1350d57 + aee2c92 commit 18e5e95

File tree

2 files changed

+33
-31
lines changed

2 files changed

+33
-31
lines changed

sdcflows/conftest.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import nibabel
2828
import pytest
2929
from bids.layout import BIDSLayout
30+
from .fieldmaps import clear_registry
3031

3132
# disable ET
3233
os.environ['NO_ET'] = '1'
@@ -59,18 +60,27 @@ def pytest_report_header(config):
5960

6061

6162
@pytest.fixture(autouse=True)
62-
def add_np(doctest_namespace):
63-
doctest_namespace["np"] = numpy
64-
doctest_namespace["nb"] = nibabel
65-
doctest_namespace["os"] = os
66-
doctest_namespace["Path"] = Path
67-
doctest_namespace["layouts"] = layouts
68-
for key, val in list(layouts.items()):
69-
doctest_namespace[key] = Path(val.root)
70-
71-
doctest_namespace["dsA_dir"] = data_dir / "dsA"
72-
doctest_namespace["dsB_dir"] = data_dir / "dsB"
73-
doctest_namespace["dsC_dir"] = data_dir / "dsC"
63+
def doctest_fixture(doctest_namespace, request):
64+
doctest_plugin = request.config.pluginmanager.getplugin("doctest")
65+
if isinstance(request.node, doctest_plugin.DoctestItem):
66+
doctest_namespace.update(
67+
np=numpy,
68+
nb=nibabel,
69+
os=os,
70+
Path=Path,
71+
layouts=layouts,
72+
dsA_dir=data_dir / "dsA",
73+
dsB_dir=data_dir / "dsB",
74+
dsC_dir=data_dir / "dsC",
75+
)
76+
doctest_namespace.update((key, Path(val.root)) for key, val in layouts.items())
77+
78+
# Start every doctest clean, and clean up after ourselves
79+
clear_registry()
80+
yield
81+
clear_registry()
82+
else:
83+
yield
7484

7585

7686
@pytest.fixture

sdcflows/tests/test_fieldmaps.py

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@
2828
from .. import fieldmaps as fm
2929

3030

31+
@pytest.fixture(autouse=True)
32+
def clear_registry():
33+
fm.clear_registry()
34+
yield
35+
fm.clear_registry()
36+
37+
3138
def test_FieldmapFile(dsA_dir):
3239
"""Test one existing file."""
3340
f1 = fm.FieldmapFile(dsA_dir / "sub-01" / "anat" / "sub-01_T1w.nii.gz")
@@ -45,13 +52,12 @@ def test_FieldmapFile(dsA_dir):
4552

4653

4754
@pytest.mark.parametrize(
48-
"inputfiles,method,nsources,raises",
55+
"inputfiles,method,nsources",
4956
[
5057
(
5158
("fmap/sub-01_fieldmap.nii.gz", "fmap/sub-01_magnitude.nii.gz"),
5259
fm.EstimatorType.MAPPED,
5360
2,
54-
False,
5561
),
5662
(
5763
(
@@ -62,51 +68,37 @@ def test_FieldmapFile(dsA_dir):
6268
),
6369
fm.EstimatorType.PHASEDIFF,
6470
4,
65-
False,
6671
),
6772
(
6873
("fmap/sub-01_phase1.nii.gz", "fmap/sub-01_phase2.nii.gz"),
6974
fm.EstimatorType.PHASEDIFF,
7075
4,
71-
True,
7276
),
73-
(("fmap/sub-01_phase2.nii.gz",), fm.EstimatorType.PHASEDIFF, 4, True),
74-
(("fmap/sub-01_phase1.nii.gz",), fm.EstimatorType.PHASEDIFF, 4, True),
77+
(("fmap/sub-01_phase2.nii.gz",), fm.EstimatorType.PHASEDIFF, 4),
78+
(("fmap/sub-01_phase1.nii.gz",), fm.EstimatorType.PHASEDIFF, 4),
7579
(
7680
("fmap/sub-01_dir-LR_epi.nii.gz", "fmap/sub-01_dir-RL_epi.nii.gz"),
7781
fm.EstimatorType.PEPOLAR,
7882
2,
79-
False,
8083
),
8184
(
8285
("fmap/sub-01_dir-LR_epi.nii.gz", "dwi/sub-01_dir-RL_sbref.nii.gz"),
8386
fm.EstimatorType.PEPOLAR,
8487
2,
85-
False,
8688
),
8789
(
8890
("anat/sub-01_T1w.nii.gz", "dwi/sub-01_dir-RL_sbref.nii.gz"),
8991
fm.EstimatorType.ANAT,
9092
2,
91-
False,
9293
),
9394
],
9495
)
95-
def test_FieldmapEstimation(dsA_dir, inputfiles, method, nsources, raises):
96+
def test_FieldmapEstimation(dsA_dir, inputfiles, method, nsources):
9697
"""Test errors."""
9798
sub_dir = dsA_dir / "sub-01"
9899

99100
sources = [sub_dir / f for f in inputfiles]
100101

101-
if raises is True:
102-
# Ensure that _estimators is still holding values from previous
103-
# parameter set of this parametrized execution.
104-
with pytest.raises(ValueError):
105-
fm.FieldmapEstimation(sources)
106-
107-
# Clean up so this parameter set can be tested.
108-
fm.clear_registry()
109-
110102
fe = fm.FieldmapEstimation(sources)
111103
assert fe.method == method
112104
assert len(fe.sources) == nsources

0 commit comments

Comments
 (0)