From 534442b53a0dd01716a0d592f0a2a0892cdd4882 Mon Sep 17 00:00:00 2001 From: Mathieu Scheltienne Date: Thu, 3 Nov 2022 10:16:48 +0100 Subject: [PATCH 1/6] fix splitting by channels --- mne/time_frequency/psd.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mne/time_frequency/psd.py b/mne/time_frequency/psd.py index 84d3cdcc960..da6db59331d 100644 --- a/mne/time_frequency/psd.py +++ b/mne/time_frequency/psd.py @@ -180,7 +180,7 @@ def psd_array_welch(x, sfreq, fmin=0, fmax=np.inf, n_fft=256, n_overlap=0, parallel, my_spect_func, n_jobs = parallel_func(_spect_func, n_jobs=n_jobs) func = partial(spectrogram, noverlap=n_overlap, nperseg=n_per_seg, nfft=n_fft, fs=sfreq, window=window) - x_splits = np.array_split(x, n_jobs) + x_splits = [arr for arr in np.array_split(x, n_jobs) if arr.size != 0] f_spect = parallel(my_spect_func(d, func=func, freq_sl=freq_sl, average=average) for d in x_splits) From bead030ec44b5a60c88e3689c67e003a1e9b1ad6 Mon Sep 17 00:00:00 2001 From: Mathieu Scheltienne Date: Thu, 3 Nov 2022 10:18:09 +0100 Subject: [PATCH 2/6] add entry to changelog --- doc/changes/latest.inc | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/changes/latest.inc b/doc/changes/latest.inc index 38b63ddf7d4..7fde8e15bf2 100644 --- a/doc/changes/latest.inc +++ b/doc/changes/latest.inc @@ -41,6 +41,7 @@ Bugs - Fix bug in the ``.compute_psd()`` methods where the number of unaggregated Welch segments was wrongly computed for some inputs, leading to an assertion error when computing the PSD (:gh:`11248` by `Daniel McCloy`_) - Fix bug in the :func:`~mne.viz.plot_evoked_topo` and :meth:`~mne.Evoked.plot_topo`, where legend colors where shown incorrectly on newer matplotlib versions (:gh:`11258` by `Erkka Heinila`_) - Fix bug where EEGLAB channel positions were read as meters, while they are commonly in millimeters, leading to head outlies of the size of one channel when plotting topomaps. Now ``montage_units`` argument has been added to :func:`~mne.io.read_raw_eeglab` and :func:`~mne.read_epochs_eeglab` to control in what units EEGLAB channel positions are read. The default is millimeters, ``'mm'`` (:gh:`11283` by `MikoĊ‚aj Magnuski`_) +- Fix bug where computing PSD with welch's method with more jobs than channels would fail (:gh:`11298` by `Mathieu Scheltienne`_) API changes ~~~~~~~~~~~ From 484548eb4821b658cf32a6deec1fea8ab1541740 Mon Sep 17 00:00:00 2001 From: Mathieu Scheltienne Date: Thu, 3 Nov 2022 10:24:36 +0100 Subject: [PATCH 3/6] add test --- mne/time_frequency/tests/test_psd.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/mne/time_frequency/tests/test_psd.py b/mne/time_frequency/tests/test_psd.py index f558475faf4..9577831370e 100644 --- a/mne/time_frequency/tests/test_psd.py +++ b/mne/time_frequency/tests/test_psd.py @@ -192,3 +192,10 @@ def test_compares_psd(): assert (np.sum(freqs_scipy < 0) == 0) assert (np.sum(psds_mne < 0) == 0) assert (np.sum(psds_scipy < 0) == 0) + + +def test_psd_array_welch_n_jobs(): + """Test that n_jobs works even with more jobs than channels.""" + data = np.random.randn(1, 2048) + psd_array_welch(data, 1024, n_jobs=1) + psd_array_welch(data, 1024, n_jobs=2) From 52f0d37ef89996ab1a265fc3e999dfff66f4b2bb Mon Sep 17 00:00:00 2001 From: mscheltienne Date: Thu, 3 Nov 2022 11:02:55 +0100 Subject: [PATCH 4/6] fix flake8.. --- mne/time_frequency/psd.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mne/time_frequency/psd.py b/mne/time_frequency/psd.py index da6db59331d..495b6a33688 100644 --- a/mne/time_frequency/psd.py +++ b/mne/time_frequency/psd.py @@ -180,7 +180,7 @@ def psd_array_welch(x, sfreq, fmin=0, fmax=np.inf, n_fft=256, n_overlap=0, parallel, my_spect_func, n_jobs = parallel_func(_spect_func, n_jobs=n_jobs) func = partial(spectrogram, noverlap=n_overlap, nperseg=n_per_seg, nfft=n_fft, fs=sfreq, window=window) - x_splits = [arr for arr in np.array_split(x, n_jobs) if arr.size != 0] + x_splits = [arr for arr in np.array_split(x, n_jobs) if arr.size != 0] f_spect = parallel(my_spect_func(d, func=func, freq_sl=freq_sl, average=average) for d in x_splits) From 163762e9ad62dedb0d37978a34cfbc26f24f1a1f Mon Sep 17 00:00:00 2001 From: Mathieu Scheltienne Date: Thu, 3 Nov 2022 14:52:33 +0100 Subject: [PATCH 5/6] Update mne/time_frequency/tests/test_psd.py Co-authored-by: Alexandre Gramfort --- mne/time_frequency/tests/test_psd.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mne/time_frequency/tests/test_psd.py b/mne/time_frequency/tests/test_psd.py index 9577831370e..7645f5d23f1 100644 --- a/mne/time_frequency/tests/test_psd.py +++ b/mne/time_frequency/tests/test_psd.py @@ -196,6 +196,6 @@ def test_compares_psd(): def test_psd_array_welch_n_jobs(): """Test that n_jobs works even with more jobs than channels.""" - data = np.random.randn(1, 2048) + data = np.empty(1, 2048) psd_array_welch(data, 1024, n_jobs=1) psd_array_welch(data, 1024, n_jobs=2) From e5839b79929813d22f9431c5228fa26b57ef0cbd Mon Sep 17 00:00:00 2001 From: Mathieu Scheltienne Date: Thu, 3 Nov 2022 15:48:49 +0100 Subject: [PATCH 6/6] Update mne/time_frequency/tests/test_psd.py --- mne/time_frequency/tests/test_psd.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mne/time_frequency/tests/test_psd.py b/mne/time_frequency/tests/test_psd.py index 7645f5d23f1..cbc0761eb7d 100644 --- a/mne/time_frequency/tests/test_psd.py +++ b/mne/time_frequency/tests/test_psd.py @@ -196,6 +196,6 @@ def test_compares_psd(): def test_psd_array_welch_n_jobs(): """Test that n_jobs works even with more jobs than channels.""" - data = np.empty(1, 2048) + data = np.empty((1, 2048)) psd_array_welch(data, 1024, n_jobs=1) psd_array_welch(data, 1024, n_jobs=2)