From b107d92ecc0526bd8cd90b0a06c13caa9a9ec87c Mon Sep 17 00:00:00 2001 From: Eric Larson Date: Wed, 29 Nov 2023 11:15:19 -0600 Subject: [PATCH] BUG: Fix bug with last item access (#12248) --- doc/changes/devel.rst | 1 + mne/io/base.py | 3 +++ mne/io/tests/test_raw.py | 7 +++++++ 3 files changed, 11 insertions(+) diff --git a/doc/changes/devel.rst b/doc/changes/devel.rst index bd7cd302524..3f3e8036419 100644 --- a/doc/changes/devel.rst +++ b/doc/changes/devel.rst @@ -29,6 +29,7 @@ Enhancements Bugs ~~~~ - Allow :func:`mne.viz.plot_compare_evokeds` to plot eyetracking channels, and improve error handling (:gh:`12190` by `Scott Huberty`_) +- Fix bug with accessing the last data sample using ``raw[:, -1]`` where an empty array was returned (:gh:`12248` by `Eric Larson`_) - Fix bug with type hints in :func:`mne.io.read_raw_neuralynx` (:gh:`12236` by `Richard Höchenberger`_) API changes diff --git a/mne/io/base.py b/mne/io/base.py index de6f3aa589d..fd8dde30258 100644 --- a/mne/io/base.py +++ b/mne/io/base.py @@ -797,6 +797,9 @@ def _parse_get_set_params(self, item): item1 = int(item1) if isinstance(item1, (int, np.integer)): start, stop, step = item1, item1 + 1, 1 + # Need to special case -1, because -1:0 will be empty + if start == -1: + stop = None else: raise ValueError("Must pass int or slice to __getitem__") diff --git a/mne/io/tests/test_raw.py b/mne/io/tests/test_raw.py index bac32f83f65..ce5d111bcbf 100644 --- a/mne/io/tests/test_raw.py +++ b/mne/io/tests/test_raw.py @@ -1022,3 +1022,10 @@ def test_concatenate_raw_dev_head_t(): raw.info["dev_head_t"]["trans"][0, 0] = np.nan raw2 = raw.copy() concatenate_raws([raw, raw2]) + + +def test_last_samp(): + """Test that getting the last sample works.""" + raw = read_raw_fif(raw_fname).crop(0, 0.1).load_data() + last_data = raw._data[:, [-1]] + assert_array_equal(raw[:, -1][0], last_data)