[med-svn] [python-mne] 54/353: ENH : add possible to pick channel using regular expression

Yaroslav Halchenko debian at onerussian.com
Fri Nov 27 17:24:30 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 56031141abc2cfe2d1f4a16d0f0c64bd2acb4194
Author: Alexandre Gramfort <alexandre.gramfort at inria.fr>
Date:   Tue Jan 17 20:36:20 2012 +0100

    ENH : add possible to pick channel using regular expression
---
 mne/fiff/__init__.py        |  4 +++-
 mne/fiff/pick.py            | 31 +++++++++++++++++++++++++++++++
 mne/fiff/tests/test_pick.py | 10 ++++++++++
 3 files changed, 44 insertions(+), 1 deletion(-)

diff --git a/mne/fiff/__init__.py b/mne/fiff/__init__.py
index 500c463..cd881ad 100644
--- a/mne/fiff/__init__.py
+++ b/mne/fiff/__init__.py
@@ -10,6 +10,8 @@ from .open import fiff_open
 from .evoked import Evoked, read_evoked, write_evoked
 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
+from .pick import pick_types, pick_channels, pick_types_evoked, \
+                  pick_channels_regexp
+
 from .compensator import get_current_comp
 from .proj import compute_spatial_vectors
diff --git a/mne/fiff/pick.py b/mne/fiff/pick.py
index 05911b4..6d33b48 100644
--- a/mne/fiff/pick.py
+++ b/mne/fiff/pick.py
@@ -4,6 +4,7 @@
 # License: BSD (3-clause)
 
 from copy import deepcopy
+import re
 
 import numpy as np
 from .constants import FIFF
@@ -78,6 +79,36 @@ def pick_channels(ch_names, include, exclude=[]):
     return sel
 
 
+def pick_channels_regexp(ch_names, regexp):
+    """Pick channels using regular expression
+
+    Returns the indices of the good channels in ch_names.
+
+    Parameters
+    ----------
+    ch_names : list of string
+        List of channels
+
+    regexp : string
+        The regular expression. See python standard module for regular
+        expressions.
+
+    Returns
+    -------
+    sel : array of int
+        Indices of good channels.
+
+    Example
+    -------
+    >>> pick_channels_regexp(['MEG 2331', 'MEG 2332', 'MEG 2333'], 'MEG ...1')
+    [0]
+    >>> pick_channels_regexp(['MEG 2331', 'MEG 2332', 'MEG 2333'], 'MEG *')
+    [0, 1, 2]
+    """
+    r = re.compile(regexp)
+    return [k for k, name in enumerate(ch_names) if r.match(name)]
+
+
 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
new file mode 100644
index 0000000..d3ddb5d
--- /dev/null
+++ b/mne/fiff/tests/test_pick.py
@@ -0,0 +1,10 @@
+from numpy.testing import assert_array_equal
+from ..pick import pick_channels_regexp
+
+
+def test_pick_channels_regexp():
+    """Test pick with regular expression"""
+    ch_names = ['MEG 2331', 'MEG 2332', 'MEG 2333']
+    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])

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