Skip to content

Commit

Permalink
Fix eeg rereferencing error when ch_type is defined (mne-tools#7847)
Browse files Browse the repository at this point in the history
* Fix eeg rereferencing error when ch_type is defined

When calling raw.set_eeg_reference with a pre-defined channel type, the error "local variable 'type_' referenced before assignment" occurs.
eg. raw.set_eeg_reference(ref_channels='average', projection=False, ch_type='seeg')

type_ is set while detecting the channel type that should be referenced. This commit uses the ch_type variable instead of type_, which is guaranteed to be set.

* TST: Add test

Co-authored-by: Eric Larson <[email protected]>
  • Loading branch information
dtxe and larsoner authored Sep 11, 2020
1 parent bd1d122 commit 4d02d02
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
2 changes: 2 additions & 0 deletions doc/changes/latest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ Bug

- Fix bug with `mne.SourceSpaces.export_volume` with ``use_lut=False`` where no values were written by `Eric Larson`_

- Fix bug with logging in :meth:`mne.io.Raw.set_eeg_reference` and related functions by `Simeon Wong`_

- Fix bug with :func:`mne.preprocessing.annotate_movement` where bad data segments, specified in ``raw.annotations``, would be handled incorrectly by `Luke Bloy`_

- Fix bug with :func:`mne.compute_source_morph` when more than one volume source space was present (e.g., when using labels) where only the first label would be interpolated when ``mri_resolution=True`` by `Eric Larson`_
Expand Down
2 changes: 2 additions & 0 deletions doc/changes/names.inc
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,8 @@
.. _Adam Li: http://github.com/adam2392
.. _Simeon Wong: https://github.com/dtxe
.. _Ramiro Gatti: https://github.com/ragatti
.. _Liberty Hamilton: https://github.com/libertyh
Expand Down
13 changes: 11 additions & 2 deletions mne/io/tests/test_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from mne.io.proj import _has_eeg_average_ref_proj, Projection
from mne.io.reference import _apply_reference
from mne.datasets import testing
from mne.utils import run_tests_if_main
from mne.utils import run_tests_if_main, catch_logging

data_dir = op.join(testing.data_path(download=False), 'MEG', 'sample')
fif_fname = op.join(data_dir, 'sample_audvis_trunc_raw.fif')
Expand Down Expand Up @@ -232,13 +232,22 @@ def test_set_eeg_reference():
with pytest.raises(ValueError, match='supported for ref_channels="averag'):
set_eeg_reference(raw, ['EEG 001'], True, True)


@pytest.mark.parametrize('ch_type', ('auto', 'ecog'))
def test_set_eeg_reference_ch_type(ch_type):
"""Test setting EEG reference for ECoG."""
# gh-6454
rng = np.random.RandomState(0)
data = rng.randn(3, 1000)
raw = RawArray(data, create_info(3, 1000., ['ecog'] * 2 + ['misc']))
reref, ref_data = set_eeg_reference(raw.copy())
with catch_logging() as log:
reref, ref_data = set_eeg_reference(raw.copy(), ch_type=ch_type,
verbose=True)
assert 'Applying a custom ECoG' in log.getvalue()
assert reref.info['custom_ref_applied'] # gh-7350
_test_reference(raw, reref, ref_data, ['0', '1'])
with pytest.raises(ValueError, match='No channels supplied'):
set_eeg_reference(raw, ch_type='eeg')


def test_set_eeg_reference_rest():
Expand Down

0 comments on commit 4d02d02

Please sign in to comment.