Repository: mne-tools/mne-python

Issue:
error in find_bad_channels_maxwell
I have an error from find_bad_channels_maxwell that I am not able to fix. 
The file has a BAD_ACQ_SKIP segment at the beginning, but using `skip_by_annotation` did not help. 
Thanks for any hints on how to solve this!

> 
>  File "/home/sh254795/anaconda3/lib/python3.7/site-packages/mne/preprocessing/maxwell.py", line 1950, in find_bad_channels_maxwell
>     ss[-1] = end
> 
> IndexError: index -1 is out of bounds for axis 0 with size 0

You can find the data [here](https://somaxluan.hd.free.fr/nextcloud/index.php/s/FjoscRAJss2LtQG). 

Here is a minimal example:

```
import mne

filename = 'example_badchan_error-raw.fif'

raw = mne.io.read_raw_fif(filename,
                                  allow_maxshield=True,
                                  preload=True, verbose='error')

auto_noisy_chs, auto_flat_chs = mne.preprocessing.find_bad_channels_maxwell(raw,
             limit = 10., verbose=True, skip_by_annotation = 'BAD_ACQ_SKIP')
```


Discussion:
can you share the file so we can debug this?

>

I can see in the code where this can happen if there is some tiny segment of good data, will come up with a test and fix
... actually it looks like the file is shared above already, thanks @SophieHerbst !

Resolving patch:
diff --git a/mne/preprocessing/maxwell.py b/mne/preprocessing/maxwell.py
--- a/mne/preprocessing/maxwell.py
+++ b/mne/preprocessing/maxwell.py
@@ -1946,7 +1946,7 @@ def find_bad_channels_maxwell(
     step = int(round(raw.info['sfreq'] * duration))
     for onset, end in zip(onsets, ends):
         if end - onset >= step:
-            ss = np.arange(onset, end - step, step)
+            ss = np.arange(onset, end - step + 1, step)
             starts.extend(ss)
             ss = ss + step
             ss[-1] = end


Test patch:
diff --git a/mne/preprocessing/tests/test_maxwell.py b/mne/preprocessing/tests/test_maxwell.py
--- a/mne/preprocessing/tests/test_maxwell.py
+++ b/mne/preprocessing/tests/test_maxwell.py
@@ -1059,8 +1059,11 @@ def test_mf_skips():
 
 
 @testing.requires_testing_data
-@pytest.mark.parametrize('bads', [[], ['MEG 0111']])  # just to test picking
-def test_find_bad_channels_maxwell(bads):
+@pytest.mark.parametrize('bads, annot', [
+    ([], False),
+    (['MEG 0111'], True),  # just do 0111 to test picking, add annot to test it
+])
+def test_find_bad_channels_maxwell(bads, annot):
     """Test automatic bad channel detection."""
     raw = mne.io.read_raw_fif(sample_fname, allow_maxshield='yes')
     raw.fix_mag_coil_types().load_data().pick_types(exclude=())
@@ -1068,9 +1071,15 @@ def test_find_bad_channels_maxwell(bads):
     raw.info['bads'] = bads
     raw._data[1] = 0  # MaxFilter didn't have this but doesn't affect results
     # maxfilter -autobad on -v -f test_raw.fif -force -cal off -ctc off -regularize off -list -o test_raw.fif -f ~/mne_data/MNE-testing-data/MEG/sample/sample_audvis_trunc_raw.fif  # noqa: E501
+    if annot:
+        # do a problematic one (gh-7741): exactly one "step" unit
+        step = int(round(raw.info['sfreq'] * 5.))
+        dt = 1. / raw.info['sfreq']
+        assert step == 1502
+        raw.annotations.append(step * dt + raw._first_time, dt, 'BAD')
     got_bads, got_flats = find_bad_channels_maxwell(
         raw, origin=(0., 0., 0.04), regularize=None,
-        bad_condition='ignore', verbose='debug')
+        bad_condition='ignore', skip_by_annotation='BAD', verbose='debug')
     assert got_bads == ['MEG 2443']  # from MaxFilter
     assert got_flats == [raw.ch_names[1]]