Skip to content

Commit

Permalink
Merge pull request autoreject#115 from jasmainak/fix_picks
Browse files Browse the repository at this point in the history
[MRG] FIX bug in meg picks during interpolation
  • Loading branch information
jasmainak authored Sep 18, 2018
2 parents 71133bb + 096abba commit 32fdbd4
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ before_install:
install:
- conda create -n testenv --yes pip python=${PYTHON_VERSION}
- source activate testenv
- conda install --yes --quiet numpy=1.12 scipy=0.19 scikit-learn=0.18 matplotlib
- conda install --yes --quiet numpy scipy scikit-learn matplotlib
- conda install --yes --quiet nose coverage
- pip install -q flake8 mne check-manifest
- pip install coverage coveralls
Expand Down
17 changes: 17 additions & 0 deletions autoreject/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,20 @@ def test_utils():
_interpolate_bads_eeg(evoked_ar, picks=None)
mne.channels.interpolation._interpolate_bads_eeg(evoked_mne)
assert_array_equal(evoked_ar.data, evoked_mne.data)


def test_interpolate_bads():
"""Test interpolate bads"""
event_id = None
events = mne.find_events(raw)
tmin, tmax = -0.2, 0.5
for ii, ch_name in enumerate(raw.info['ch_names'][:14]):
raw.set_channel_types({ch_name: 'bio'})
raw.rename_channels({ch_name: 'BIO%02d' % ii})

picks = mne.pick_types(raw.info, meg='grad', eeg=False, eog=False)
epochs = mne.Epochs(raw, events, event_id, tmin, tmax,
baseline=(None, 0), decim=10,
reject=None, preload=True)[:10]
epochs.info['bads'] = ['MEG 2212']
interpolate_bads(epochs, picks)
21 changes: 10 additions & 11 deletions autoreject/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import mne
from mne.utils import check_version as version_is_greater_equal
from mne import pick_types, pick_channels, pick_info
from mne import pick_types, pick_info
from mne.channels.interpolation import _do_interp_dots
from mne.externals import six

Expand Down Expand Up @@ -356,22 +356,20 @@ def _interpolate_bads_meg_fast(inst, picks, mode='accurate', verbose=None):
else:
picked_info = inst.info.copy()

def get_picks_bad_good(info):
picks_meg = pick_types(info, meg=True, eeg=False, exclude=[],
stim=False)
ch_names = [info['ch_names'][p] for p in picks_meg]
picks_good = pick_types(info, meg=True, eeg=False, exclude='bads',
stim=False)
def get_picks_bad_good(info, picks_meg):
picks_good = [p for p in picks_meg
if info['ch_names'][p] not in info['bads']]

# select the bad meg channel to be interpolated
if len(info['bads']) == 0:
picks_bad = []
else:
picks_bad = pick_channels(ch_names, info['bads'],
exclude=[])
picks_bad = [p for p in picks_meg
if info['ch_names'][p] in info['bads']]
return picks_meg, picks_good, picks_bad

picks_meg, picks_good, picks_bad = get_picks_bad_good(picked_info)
picks_meg, picks_good, picks_bad = get_picks_bad_good(
picked_info, range(picked_info['nchan']))
# return without doing anything if there are no meg channels
if len(picks_meg) == 0 or len(picks_bad) == 0:
return
Expand All @@ -386,7 +384,8 @@ def get_picks_bad_good(info):
# the unpicked info of the data.
# Since we may have picked the info, we need to double map
# the indices.
_, picks_good_, picks_bad_orig = get_picks_bad_good(inst.info.copy())
_, picks_good_, picks_bad_orig = get_picks_bad_good(
inst.info.copy(), picks)
ch_names_a = [picked_info['ch_names'][pp] for pp in picks_bad]
ch_names_b = [inst.info['ch_names'][pp] for pp in picks_bad_orig]
assert ch_names_a == ch_names_b
Expand Down
1 change: 1 addition & 0 deletions doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@
("Examples", "auto_examples/index"),
("API", "api"),
("FAQ", "faq"),
("What's new", "whats_new"),
("GitHub", "https://github.com/autoreject/autoreject", True)
],
'bootswatch_theme': "united"
Expand Down
27 changes: 27 additions & 0 deletions doc/whats_new.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
:orphan:

.. _whats_new:

What's new?
===========

.. currentmodule:: autoreject

.. _current:

Current
-------

Changelog
~~~~~~~~~

Bug
~~~

- Fixed bug in picking bad channels during interpolation. This bug only affects users who got an assertion
error when running :class:`autoreject.Autoreject`. Fixed by `Mainak Jas`_ in `#115 <https://github.com/autoreject/autoreject/pull/115>`_

API
~~~

.. _Mainak Jas: https://perso.telecom-paristech.fr/mjas/

0 comments on commit 32fdbd4

Please sign in to comment.