[med-svn] [python-mne] 202/353: ENH: pick using named 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 d17d730e4964270688b3e529b880888175f4d532
Author: mluessi at nmr.mgh.harvard.edu <mluessi at nmr.mgh.harvard.edu>
Date:   Tue Jun 19 10:13:04 2012 -0400

    ENH: pick using named selection
---
 mne/fiff/__init__.py        |  2 +-
 mne/fiff/pick.py            | 73 +++++++++++++++++++++++++++++++++++++++++++++
 mne/fiff/tests/test_pick.py | 16 +++++++++-
 setup.py                    |  1 +
 4 files changed, 90 insertions(+), 2 deletions(-)

diff --git a/mne/fiff/__init__.py b/mne/fiff/__init__.py
index 3418624..1afb60c 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_types_forward, pick_channels_cov, pick_selection
 
 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 51ec6c0..503bacc 100644
--- a/mne/fiff/pick.py
+++ b/mne/fiff/pick.py
@@ -3,6 +3,7 @@
 #
 # License: BSD (3-clause)
 
+from os import path
 from copy import deepcopy
 import re
 
@@ -109,6 +110,78 @@ 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=[]):
     """Pick channels by type and names
diff --git a/mne/fiff/tests/test_pick.py b/mne/fiff/tests/test_pick.py
index d3ddb5d..71442a8 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
+from ..pick import pick_channels_regexp, pick_selection
 
 
 def test_pick_channels_regexp():
@@ -8,3 +8,17 @@ 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/setup.py b/setup.py
index d22bf16..3c4e497 100755
--- a/setup.py
+++ b/setup.py
@@ -60,6 +60,7 @@ if __name__ == "__main__":
                    'mne.layouts',
                    'mne.time_frequency', 'mne.time_frequency.tests',
                    'mne.preprocessing', 'mne.preprocessing.tests'],
+         package_data={'mne': ['data/mne_analyze.sel']},
          scripts=['bin/mne_clean_eog_ecg.py', 'bin/mne_flash_bem_model.py',
                   'bin/mne_surf2bem.py', 'bin/mne_compute_proj_ecg.py',
                   'bin/mne_compute_proj_eog.py', 'bin/mne_maxfilter.py'])

-- 
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