[med-svn] [python-mne] 205/353: ENH: rework of selection

Yaroslav Halchenko debian at onerussian.com
Fri Nov 27 17:24:58 UTC 2015


This is an automated email from the git hooks/post-receive script.

yoh pushed a commit to tag 0.4
in repository python-mne.

commit 16832ebf65116eff8ad3b1b822ec399a51dd40ec
Author: mluessi at nmr.mgh.harvard.edu <mluessi at nmr.mgh.harvard.edu>
Date:   Wed Jun 20 03:38:37 2012 -0400

    ENH: rework of selection
---
 mne/__init__.py             |  1 +
 mne/fiff/__init__.py        |  2 +-
 mne/fiff/pick.py            | 90 ++++++++-------------------------------------
 mne/fiff/tests/test_pick.py | 16 +-------
 mne/tests/test_selection.py | 28 ++++++++++++++
 5 files changed, 46 insertions(+), 91 deletions(-)

diff --git a/mne/__init__.py b/mne/__init__.py
index 7cfab2e..abdfe55 100644
--- a/mne/__init__.py
+++ b/mne/__init__.py
@@ -22,6 +22,7 @@ from .misc import parse_config, read_reject_parameters
 from .transforms import transform_coordinates
 from .proj import read_proj, write_proj, compute_proj_epochs, \
                   compute_proj_evoked
+from .selection import read_selection
 from . import fiff
 from . import artifacts
 from . import stats
diff --git a/mne/fiff/__init__.py b/mne/fiff/__init__.py
index 1afb60c..3418624 100644
--- a/mne/fiff/__init__.py
+++ b/mne/fiff/__init__.py
@@ -12,7 +12,7 @@ from .raw import Raw, read_raw_segment, read_raw_segment_times, \
                  start_writing_raw, write_raw_buffer, finish_writing_raw
 from .pick import pick_types, pick_channels, pick_types_evoked, \
                   pick_channels_regexp, pick_channels_forward, \
-                  pick_types_forward, pick_channels_cov, pick_selection
+                  pick_types_forward, pick_channels_cov
 
 from .compensator import get_current_comp
 from .proj import compute_spatial_vectors, proj_equal, \
diff --git a/mne/fiff/pick.py b/mne/fiff/pick.py
index 503bacc..2d6dbe6 100644
--- a/mne/fiff/pick.py
+++ b/mne/fiff/pick.py
@@ -1,9 +1,9 @@
 # Authors: Alexandre Gramfort <gramfort at nmr.mgh.harvard.edu>
 #          Matti Hamalainen <msh at nmr.mgh.harvard.edu>
+#          Martin Luessi <mluessi at nmr.mgh.harvard.edu>
 #
 # License: BSD (3-clause)
 
-from os import path
 from copy import deepcopy
 import re
 
@@ -110,80 +110,8 @@ def pick_channels_regexp(ch_names, regexp):
     return [k for k, name in enumerate(ch_names) if r.match(name)]
 
 
-def _load_selections_file(fname):
-    """Load selections from file"""
-    fid = open(fname, 'r')
-
-    selections = {}
-
-    for line in fid:
-        line = line.strip()
-
-        # skip blank lines and comments
-        if len(line) == 0 or line[0] == '#':
-            continue
-
-        # read the channel names into a list
-        pos = line.find(':')
-        if pos < 0:
-            print '":" delimiter not found in selections file, '\
-                  'skipping line'
-            continue
-        selections[line[:pos]] = line[pos + 1:].split('|')
-
-    fid.close()
-
-    return selections
-
-
-def pick_selection(ch_names, sel_name, sel_fname=None):
-    """Pick channels using a named selection
-
-    By default, the selections used in mne_browse_raw are supported*.
-    Additional selections can be added by specifying a selection file (e.g.
-    produced using mne_browse_raw) using the sel_fname parameter.
-
-    * The included selections are: "Vertex", "Left-temporal", "Right-temporal",
-    "Left-parietal", "Right-parietal", "Left-occipital", "Right-occipital",
-    "Left-frontal", and "Right-frontal"
-
-    Parameters
-    ----------
-    ch_names : list of string
-        List of channels
-
-    sel_name : string
-        Name of the selection
-
-    self_fname : string
-        Filename of the selection file (if None, built-in selections are used)
-
-    Returns
-    -------
-    sel : array of int
-        Indices of channels in selection.
-    """
-
-    if sel_fname is None:
-        sel_fname = path.join(path.dirname(__file__), '..', 'data',
-                              'mne_analyze.sel')
-
-    if not path.exists(sel_fname):
-        raise ValueError('The file %s does not exist.' % sel_fname)
-
-    selections = _load_selections_file(sel_fname)
-
-    if sel_name not in selections:
-        raise ValueError('Selection "%s" not in %s' % (sel_name, sel_fname))
-
-    # get the channel indices of the selection
-    sel = pick_channels(ch_names, selections[sel_name])
-
-    return sel
-
-
 def pick_types(info, meg=True, eeg=False, stim=False, eog=False, ecg=False,
-               emg=False, misc=False, include=[], exclude=[]):
+               emg=False, misc=False, include=[], exclude=[], selection=None):
     """Pick channels by type and names
 
     Parameters
@@ -209,9 +137,10 @@ def pick_types(info, meg=True, eeg=False, stim=False, eog=False, ecg=False,
         If True include miscellaneous analog channels
     include : list of string
         List of additional channels to include. If empty do not include any.
-
     exclude : list of string
         List of channels to exclude. If empty do not include any.
+    selection : list of string
+        Restrict sensor channels (MEG, EEG) to this list of channel names.
 
     Returns
     -------
@@ -247,6 +176,17 @@ def pick_types(info, meg=True, eeg=False, stim=False, eog=False, ecg=False,
         elif kind == FIFF.FIFFV_MISC_CH and misc:
             pick[k] = True
 
+    # restrict channels to selection if provided
+    if selection is not None:
+        # the selection only restricts these types of channels
+        sel_kind = [FIFF.FIFFV_MEG_CH, FIFF.FIFFV_REF_MEG_CH,
+                    FIFF.FIFFV_EEG_CH]
+        for k in np.where(pick == True)[0]:
+            if info['chs'][k]['kind'] not in sel_kind:
+                continue
+            if info['ch_names'][k] not in selection:
+                pick[k] = False
+
     myinclude = [info['ch_names'][k] for k in range(nchan) if pick[k]]
     myinclude += include
 
diff --git a/mne/fiff/tests/test_pick.py b/mne/fiff/tests/test_pick.py
index 71442a8..d3ddb5d 100644
--- a/mne/fiff/tests/test_pick.py
+++ b/mne/fiff/tests/test_pick.py
@@ -1,5 +1,5 @@
 from numpy.testing import assert_array_equal
-from ..pick import pick_channels_regexp, pick_selection
+from ..pick import pick_channels_regexp
 
 
 def test_pick_channels_regexp():
@@ -8,17 +8,3 @@ def test_pick_channels_regexp():
     assert_array_equal(pick_channels_regexp(ch_names, 'MEG ...1'), [0])
     assert_array_equal(pick_channels_regexp(ch_names, 'MEG ...[2-3]'), [1, 2])
     assert_array_equal(pick_channels_regexp(ch_names, 'MEG *'), [0, 1, 2])
-
-
-def test_pick_selection():
-    """Test pick using named selection"""
-    # test one channel for each selection
-    ch_names = ['MEG 2211', 'MEG 0223', 'MEG 1312', 'MEG 0412', 'MEG 1043',
-                'MEG 2042', 'MEG 2032', 'MEG 0522', 'MEG 1031']
-    sel_names = ['Vertex', 'Left-temporal', 'Right-temporal', 'Left-parietal',
-                 'Right-parietal', 'Left-occipital', 'Right-occipital',
-                 'Left-frontal', 'Right-frontal']
-
-    for i, sel in enumerate(sel_names):
-        picks = pick_selection(ch_names, sel)
-        assert(i in picks)
diff --git a/mne/tests/test_selection.py b/mne/tests/test_selection.py
new file mode 100644
index 0000000..db8e71c
--- /dev/null
+++ b/mne/tests/test_selection.py
@@ -0,0 +1,28 @@
+from sets import Set
+from .. import read_selection
+
+
+def test_read_selection():
+    """Test reading of selections"""
+    # test one channel for each selection
+    ch_names = ['MEG 2211', 'MEG 0223', 'MEG 1312', 'MEG 0412', 'MEG 1043',
+                'MEG 2042', 'MEG 2032', 'MEG 0522', 'MEG 1031']
+    sel_names = ['Vertex', 'Left-temporal', 'Right-temporal', 'Left-parietal',
+                 'Right-parietal', 'Left-occipital', 'Right-occipital',
+                 'Left-frontal', 'Right-frontal']
+
+    for i, name in enumerate(sel_names):
+        sel = read_selection(name)
+        assert(ch_names[i] in sel)
+
+    # test some combinations
+    all_ch = read_selection(['L', 'R'])
+    left = read_selection('L')
+    right = read_selection('R')
+
+    assert(len(all_ch) == len(left) + len(right))
+    assert(len(Set(left).intersection(Set(right))) == 0)
+
+    frontal = read_selection('frontal')
+    occipital = read_selection('Right-occipital')
+    assert(len(Set(frontal).intersection(Set(occipital))) == 0)

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-med/python-mne.git



More information about the debian-med-commit mailing list