diff --git a/mne/channels/channels.py b/mne/channels/channels.py index 2a772e77491..aec1891d5b6 100644 --- a/mne/channels/channels.py +++ b/mne/channels/channels.py @@ -286,6 +286,10 @@ def set_channel_types(self, mapping): _check_set(self.info['chs'][c_ind], self.info['projs'], ch_type) unit_old = self.info['chs'][c_ind]['unit'] unit_new = _human2unit[ch_type] + if unit_old not in _unit2human: + raise ValueError("Channel '%s' has unknown unit (%s). Please " + "fix the measurement info of your data." + % (ch_name, unit_old)) if unit_old != _human2unit[ch_type]: warn("The unit for channel %s has changed from %s to %s." % (ch_name, _unit2human[unit_old], _unit2human[unit_new])) diff --git a/mne/channels/tests/test_channels.py b/mne/channels/tests/test_channels.py index 32649951deb..660d15204da 100644 --- a/mne/channels/tests/test_channels.py +++ b/mne/channels/tests/test_channels.py @@ -106,6 +106,11 @@ def test_set_channel_types(): assert_true(info['chs'][idx]['unit'] == FIFF.FIFF_UNIT_V) assert_true(info['chs'][idx]['coil_type'] == FIFF.FIFFV_COIL_EEG) + # Test meaningful error when setting channel type with unknown unit + raw.info['chs'][0]['unit'] = 0. + ch_types = {raw.ch_names[0]: 'misc'} + assert_raises(ValueError, raw.set_channel_types, ch_types) + def test_read_ch_connectivity(): "Test reading channel connectivity templates"