Skip to content

Commit

Permalink
MRG, BUG: Make sure resample return at least one sample (mne-tools#9897)
Browse files Browse the repository at this point in the history
* TST: Expose failure when resampling to 1 sample with sfreq > len(times) [ci skip]

* raise a RuntimeError when resampling yields 0 datapoints.

* Change raised error to match numpy error raised in numpy/fft/_pocketfft:94: in _get_backward_norm

* fix regex match pattern.

* return 1 sample instead of raising.

* FIX: One more max

* Add entry to latest.inc.

* DOC: Add name

* DOC: Update [ci skip]

Co-authored-by: Eric Larson <[email protected]>
  • Loading branch information
mscheltienne and larsoner authored Oct 25, 2021
1 parent 44f4b0f commit 131c676
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
4 changes: 3 additions & 1 deletion doc/changes/latest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ Bugs

- Fix bug where :func:`mne.time_frequency.psd_array_welch` and :func:`mne.time_frequency.psd_array_multitaper` failed to handle negative integer for ``n_jobs`` argument (:gh:`9849` **by new contributor** |Riessarius Stargardsky|_)

- Fix bug where resampling returns empty instances (:gh:`9897` **by new contributor** |Mathieu Scheltienne|_ and `Eric Larson`_)

- Fix bug with :func:`mne.io.read_raw_nihon` where latin-1 annotations could not be read (:gh:`9384` by `Alex Gramfort`_)

- Fix bug when printing a :class:`mne.io.RawArray` in the notebook (:gh:`9404` by `Alex Gramfort`_)
Expand Down Expand Up @@ -347,4 +349,4 @@ API changes

- Deprecate ``mne.viz.utils.center_cmap`` (:gh:`9851` by `Clemens Brunner`_)

- Add depreciation warning for the default partial pathlength factor of :func:`mne.preprocessing.nirs.beer_lambert_law` from 0.1 to 6 (:gh:`9843` by `Robert Luke`_)
- The default partial pathlength factor of :func:`mne.preprocessing.nirs.beer_lambert_law` will change from 0.1 in 0.24 to 6.0 in the next release (:gh:`9843` by `Robert Luke`_)
4 changes: 2 additions & 2 deletions mne/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1380,7 +1380,7 @@ def _check_filterable(x, kind='filtered'):

def _resamp_ratio_len(up, down, n):
ratio = float(up) / down
return ratio, int(round(ratio * n))
return ratio, max(int(round(ratio * n)), 1)


@verbose
Expand Down Expand Up @@ -1469,7 +1469,7 @@ def resample(x, up=1., down=1., npad=100, axis=-1, window='boxcar', n_jobs=1,
# prep for resampling now
x_flat = x.reshape((-1, x_len))
orig_len = x_len + npads.sum() # length after padding
new_len = int(round(ratio * orig_len)) # length after resampling
new_len = max(int(round(ratio * orig_len)), 1) # length after resampling
to_removes = [int(round(ratio * npads[0]))]
to_removes.append(new_len - final_len - to_removes[0])
to_removes = np.array(to_removes)
Expand Down
27 changes: 26 additions & 1 deletion mne/tests/test_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import pytest
from scipy.signal import resample as sp_resample, butter, freqz, sosfreqz

from mne import create_info
from mne import create_info, Epochs
from numpy.fft import fft, fftfreq
from mne.io import RawArray, read_raw_fif
from mne.io.pick import _DATA_CH_TYPES_SPLIT
Expand Down Expand Up @@ -327,6 +327,31 @@ def test_resample_raw():
assert data.shape == (1, 63)


def test_resample_below_1_sample():
"""Test resampling doesn't yield datapoints."""
# Raw
x = np.zeros((1, 100))
sfreq = 1000.
raw = RawArray(x, create_info(1, sfreq, 'eeg'))
raw.resample(5)
assert len(raw.times) == 1
assert raw.get_data().shape[1] == 1

# Epochs
x = np.zeros((1, 10000))
sfreq = 1000.
raw = RawArray(x, create_info(1, sfreq, 'eeg'))
events = np.array([[400, 0, 1],
[2000, 0, 1],
[3000, 0, 1]])
epochs = Epochs(raw, events, {'test': 1}, 0, 0.2, proj=False,
picks='eeg', baseline=None, preload=True,
verbose=False)
epochs.resample(1)
assert len(epochs.times) == 1
assert epochs.get_data().shape[2] == 1


@pytest.mark.slowtest
def test_filters():
"""Test low-, band-, high-pass, and band-stop filters plus resampling."""
Expand Down

0 comments on commit 131c676

Please sign in to comment.