Skip to content

Commit

Permalink
BUG: Fix bug with not short-circuiting n_jobs=1 (#13147)
Browse files Browse the repository at this point in the history
  • Loading branch information
larsoner authored Mar 7, 2025
1 parent 7c531e4 commit be27cf8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
15 changes: 15 additions & 0 deletions mne/parallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,21 @@ def run_verbose(*args, verbose=logger.level, **kwargs):

my_func = delayed(run_verbose)

# if we got that n_jobs=1, we shouldn't bother with any parallelization
if n_jobs == 1:
# TODO: Hack until https://github.com/joblib/joblib/issues/1687 lands
try:
backend_repr = str(parallel._backend)
except Exception:
backend_repr = ""
is_local = any(
f"{x}Backend" in backend_repr
for x in ("Loky", "Threading", "Multiprocessing")
)
if is_local:
my_func = func
parallel = list

if total is not None:

def parallel_progress(op_iter):
Expand Down
13 changes: 10 additions & 3 deletions mne/tests/test_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,14 +418,21 @@ def test_resample_scipy():


@pytest.mark.parametrize("n_jobs", (2, "cuda"))
def test_n_jobs(n_jobs):
def test_n_jobs(n_jobs, capsys):
"""Test resampling against SciPy."""
joblib = pytest.importorskip("joblib")
x = np.random.RandomState(0).randn(4, 100)
y1 = resample(x, 2, 1, n_jobs=None)
y2 = resample(x, 2, 1, n_jobs=n_jobs)
assert_allclose(y1, y2)
y1 = filter_data(x, 100.0, 0, 40, n_jobs=None)
y2 = filter_data(x, 100.0, 0, 40, n_jobs=n_jobs)
capsys.readouterr()
with joblib.parallel_config(backend="loky", n_jobs=1):
y1 = filter_data(x, 100.0, 0, 40, n_jobs=None, verbose=True)
out, err = capsys.readouterr()
# if it's in there, we didn't triage properly
assert "Parallel(" not in out
assert "Parallel(" not in err
y2 = filter_data(x, 100.0, 0, 40, n_jobs=n_jobs, verbose=True)
assert_allclose(y1, y2)


Expand Down

0 comments on commit be27cf8

Please # to comment.