From be27cf8dd32922ba6e15599f7746da635e9856c0 Mon Sep 17 00:00:00 2001 From: Eric Larson Date: Fri, 7 Mar 2025 14:15:48 -0500 Subject: [PATCH] BUG: Fix bug with not short-circuiting n_jobs=1 (#13147) --- mne/parallel.py | 15 +++++++++++++++ mne/tests/test_filter.py | 13 ++++++++++--- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/mne/parallel.py b/mne/parallel.py index 4feb3458227..feacb719bce 100644 --- a/mne/parallel.py +++ b/mne/parallel.py @@ -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): diff --git a/mne/tests/test_filter.py b/mne/tests/test_filter.py index 537f1930f45..24147f202e2 100644 --- a/mne/tests/test_filter.py +++ b/mne/tests/test_filter.py @@ -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)