Repository: mne-tools/mne-bids

Issue:
non-eeg channels are dropped from eeg files
I noticed that the EMG channel type wasn't included in the list of types, so I was going to make a nice easy PR to fix it...
**But** then, as I was writing a test I discovered that if I set the type of channel to EMG in an eeg file (was testing edf data since part of the test already modifies a channel so I figured I'd piggyback off that), this channel is dropped.
I narrowed the reason down to the `infer_eeg_placement_scheme` function, which will drop any non-eeg channels because of `raw.pick`.
I can fix this, but I was wondering whether this was in fact the intended behaviour or not?

Now that I think about it, I am pretty sure if there is MEG data which contains EEG data, this function will actually remove all MEG channel info, rendering the data completely wrong, so I guess that this behaviour is indeed unintended.


Discussion:
ok, having more of a test this happens in all data, so the current output from FIF data has only EEG channels listed which is obviously wrong. PR incoming :)

Resolving patch:
diff --git a/mne_bids/mne_bids.py b/mne_bids/mne_bids.py
--- a/mne_bids/mne_bids.py
+++ b/mne_bids/mne_bids.py
@@ -81,7 +81,7 @@ def _channels_tsv(raw, fname, verbose):
                    megrefgradaxial='MEGREFGRADAXIAL',
                    meggradplanar='MEGGRADPLANAR',
                    megmag='MEGMAG', megrefmag='MEGREFMAG',
-                   eeg='EEG', misc='MISC', stim='TRIG',
+                   eeg='EEG', misc='MISC', stim='TRIG', emg='EMG',
                    ecog='ECOG', seeg='SEEG', eog='EOG', ecg='ECG')
     map_desc = defaultdict(lambda: 'Other type of channel')
     map_desc.update(meggradaxial='Axial Gradiometer',
@@ -94,6 +94,7 @@ def _channels_tsv(raw, fname, verbose):
                     seeg='StereoEEG',
                     ecg='ElectroCardioGram',
                     eog='ElectroOculoGram',
+                    emg='ElectroMyoGram',
                     misc='Miscellaneous')
     get_specific = ('mag', 'ref_meg', 'grad')
 
diff --git a/mne_bids/utils.py b/mne_bids/utils.py
--- a/mne_bids/utils.py
+++ b/mne_bids/utils.py
@@ -19,6 +19,7 @@
 from mne import read_events, find_events
 from mne.externals.six import string_types
 from mne.channels import read_montage
+from mne.io.pick import pick_types
 
 from .config import BIDS_VERSION
 from .io import _parse_ext
@@ -535,8 +536,9 @@ def _infer_eeg_placement_scheme(raw):
 
     # How many of the channels in raw are based on the extended 10/20 system
     raw.load_data()
-    raw.pick_types(meg=False, eeg=True)
-    channel_names = [ch.lower() for ch in raw.ch_names]
+    sel = pick_types(raw.info, meg=False, eeg=True)
+    ch_names = [raw.ch_names[i] for i in sel]
+    channel_names = [ch.lower() for ch in ch_names]
     montage1005 = read_montage(kind='standard_1005')
     montage1005_names = [ch.lower() for ch in montage1005.ch_names]
 


Test patch:
diff --git a/mne_bids/tests/test_mne_bids.py b/mne_bids/tests/test_mne_bids.py
--- a/mne_bids/tests/test_mne_bids.py
+++ b/mne_bids/tests/test_mne_bids.py
@@ -11,8 +11,6 @@
 
 import os
 import os.path as op
-import pytest
-import subprocess
 
 import pandas as pd
 import mne
@@ -101,9 +99,8 @@ def test_kit():
         subject=subject_id, session=session_id, task=task, run=run,
         suffix='channels.tsv', acquisition=acq,
         prefix=op.join(output_path, 'sub-01/ses-01/meg'))
-    if op.exists(channels_tsv):
-        df = pd.read_csv(channels_tsv, sep='\t')
-        assert not ('STI 014' in df['name'].values)
+    df = pd.read_csv(channels_tsv, sep='\t')
+    assert not ('STI 014' in df['name'].values)
 
 
 def test_ctf():
@@ -135,12 +132,8 @@ def test_bti():
 
     assert op.exists(op.join(output_path, 'participants.tsv'))
 
-    # FIXME: see these issues for reference:
-    # https://github.com/mne-tools/mne-bids/pull/84
-    # https://github.com/INCF/bids-validator/issues/553
-    with pytest.raises(subprocess.CalledProcessError):
-        cmd = ['bids-validator', output_path]
-        run_subprocess(cmd, shell=shell)
+    cmd = ['bids-validator', output_path]
+    run_subprocess(cmd, shell=shell)
 
 
 # EEG Tests
@@ -168,6 +161,8 @@ def test_edf():
     raw = mne.io.read_raw_edf(raw_fname, preload=True)
     raw.rename_channels({raw.info['ch_names'][0]: 'EOG'})
     raw.info['chs'][0]['coil_type'] = FIFF.FIFFV_COIL_EEG_BIPOLAR
+    raw.rename_channels({raw.info['ch_names'][1]: 'EMG'})
+    raw.set_channel_types({'EMG': 'emg'})
 
     raw_to_bids(subject_id=subject_id, session_id=session_id, run=run,
                 task=task, acquisition=acq, raw_file=raw,
@@ -176,6 +171,14 @@ def test_edf():
     cmd = ['bids-validator', '--bep006', output_path]
     run_subprocess(cmd, shell=shell)
 
+    # ensure there is an EMG channel in the channels.tsv:
+    channels_tsv = make_bids_filename(
+        subject=subject_id, session=session_id, task=task, run=run,
+        suffix='channels.tsv', acquisition=acq,
+        prefix=op.join(output_path, 'sub-01/ses-01/eeg'))
+    df = pd.read_csv(channels_tsv, sep='\t')
+    assert 'ElectroMyoGram' in df['description'].values
+
 
 def test_bdf():
     """Test raw_to_bids conversion for Biosemi data."""