Skip to content

Commit

Permalink
Merge pull request #464 from effigies/test/multiproc-safe
Browse files Browse the repository at this point in the history
TEST: Clear registry consistently to avoid order dependency
  • Loading branch information
effigies authored Oct 3, 2024
2 parents 1350d57 + aee2c92 commit 18e5e95
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 31 deletions.
34 changes: 22 additions & 12 deletions sdcflows/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import nibabel
import pytest
from bids.layout import BIDSLayout
from .fieldmaps import clear_registry

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


@pytest.fixture(autouse=True)
def add_np(doctest_namespace):
doctest_namespace["np"] = numpy
doctest_namespace["nb"] = nibabel
doctest_namespace["os"] = os
doctest_namespace["Path"] = Path
doctest_namespace["layouts"] = layouts
for key, val in list(layouts.items()):
doctest_namespace[key] = Path(val.root)

doctest_namespace["dsA_dir"] = data_dir / "dsA"
doctest_namespace["dsB_dir"] = data_dir / "dsB"
doctest_namespace["dsC_dir"] = data_dir / "dsC"
def doctest_fixture(doctest_namespace, request):
doctest_plugin = request.config.pluginmanager.getplugin("doctest")
if isinstance(request.node, doctest_plugin.DoctestItem):
doctest_namespace.update(
np=numpy,
nb=nibabel,
os=os,
Path=Path,
layouts=layouts,
dsA_dir=data_dir / "dsA",
dsB_dir=data_dir / "dsB",
dsC_dir=data_dir / "dsC",
)
doctest_namespace.update((key, Path(val.root)) for key, val in layouts.items())

# Start every doctest clean, and clean up after ourselves
clear_registry()
yield
clear_registry()
else:
yield


@pytest.fixture
Expand Down
30 changes: 11 additions & 19 deletions sdcflows/tests/test_fieldmaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@
from .. import fieldmaps as fm


@pytest.fixture(autouse=True)
def clear_registry():
fm.clear_registry()
yield
fm.clear_registry()


def test_FieldmapFile(dsA_dir):
"""Test one existing file."""
f1 = fm.FieldmapFile(dsA_dir / "sub-01" / "anat" / "sub-01_T1w.nii.gz")
Expand All @@ -45,13 +52,12 @@ def test_FieldmapFile(dsA_dir):


@pytest.mark.parametrize(
"inputfiles,method,nsources,raises",
"inputfiles,method,nsources",
[
(
("fmap/sub-01_fieldmap.nii.gz", "fmap/sub-01_magnitude.nii.gz"),
fm.EstimatorType.MAPPED,
2,
False,
),
(
(
Expand All @@ -62,51 +68,37 @@ def test_FieldmapFile(dsA_dir):
),
fm.EstimatorType.PHASEDIFF,
4,
False,
),
(
("fmap/sub-01_phase1.nii.gz", "fmap/sub-01_phase2.nii.gz"),
fm.EstimatorType.PHASEDIFF,
4,
True,
),
(("fmap/sub-01_phase2.nii.gz",), fm.EstimatorType.PHASEDIFF, 4, True),
(("fmap/sub-01_phase1.nii.gz",), fm.EstimatorType.PHASEDIFF, 4, True),
(("fmap/sub-01_phase2.nii.gz",), fm.EstimatorType.PHASEDIFF, 4),
(("fmap/sub-01_phase1.nii.gz",), fm.EstimatorType.PHASEDIFF, 4),
(
("fmap/sub-01_dir-LR_epi.nii.gz", "fmap/sub-01_dir-RL_epi.nii.gz"),
fm.EstimatorType.PEPOLAR,
2,
False,
),
(
("fmap/sub-01_dir-LR_epi.nii.gz", "dwi/sub-01_dir-RL_sbref.nii.gz"),
fm.EstimatorType.PEPOLAR,
2,
False,
),
(
("anat/sub-01_T1w.nii.gz", "dwi/sub-01_dir-RL_sbref.nii.gz"),
fm.EstimatorType.ANAT,
2,
False,
),
],
)
def test_FieldmapEstimation(dsA_dir, inputfiles, method, nsources, raises):
def test_FieldmapEstimation(dsA_dir, inputfiles, method, nsources):
"""Test errors."""
sub_dir = dsA_dir / "sub-01"

sources = [sub_dir / f for f in inputfiles]

if raises is True:
# Ensure that _estimators is still holding values from previous
# parameter set of this parametrized execution.
with pytest.raises(ValueError):
fm.FieldmapEstimation(sources)

# Clean up so this parameter set can be tested.
fm.clear_registry()

fe = fm.FieldmapEstimation(sources)
assert fe.method == method
assert len(fe.sources) == nsources
Expand Down

0 comments on commit 18e5e95

Please sign in to comment.