[med-svn] [python-mne] 152/353: ENH : new pick_channels_cov function

Yaroslav Halchenko debian at onerussian.com
Fri Nov 27 17:24:48 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 8786d8a3a51d9cc02309aaefc46fd0bce53e4fcc
Author: Alexandre Gramfort <alexandre.gramfort at inria.fr>
Date:   Tue Apr 17 10:59:56 2012 +0200

    ENH : new pick_channels_cov function
---
 mne/cov.py            |  2 +-
 mne/fiff/__init__.py  |  2 +-
 mne/fiff/pick.py      | 31 +++++++++++++++++++++++++++++++
 mne/tests/test_cov.py |  8 +++++++-
 4 files changed, 40 insertions(+), 3 deletions(-)

diff --git a/mne/cov.py b/mne/cov.py
index 7c0fdb6..aad0d8a 100644
--- a/mne/cov.py
+++ b/mne/cov.py
@@ -81,7 +81,7 @@ class Covariance(dict):
             fiff.write_cov(fid, self)
         except Exception as inst:
             os.remove(fname)
-            raise '%s', inst
+            raise inst
 
         end_file(fid)
 
diff --git a/mne/fiff/__init__.py b/mne/fiff/__init__.py
index fda05f2..138b22a 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_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 9912c79..78ac7bc 100644
--- a/mne/fiff/pick.py
+++ b/mne/fiff/pick.py
@@ -394,3 +394,34 @@ def channel_indices_by_type(info):
                 idx[key].append(k)
 
     return idx
+
+
+def pick_channels_cov(orig, include=[], exclude=[]):
+    """Pick channels from covariance matrix
+
+    Parameters
+    ----------
+    orig : Covariance
+        A covariance
+
+    include : list of string, (optional)
+        List of channels to include. (if None, include all available)
+
+    exclude : list of string, (optional)
+        Channels to exclude (if None, do not exclude any)
+
+    Returns
+    -------
+    res : dict
+        Covariance solution restricted to selected channels. If include and
+        exclude are None it returns orig without copy.
+    """
+    sel = pick_channels(orig['names'], include=include, exclude=exclude)
+    res = deepcopy(orig)
+    res['dim'] = len(sel)
+    res['data'] = orig['data'][sel][:, sel]
+    res['names'] = [orig['names'][k] for k in sel]
+    res['bads'] = [name for name in orig['bads'] if name in res['names']]
+    res['eig'] = None
+    res['eigvec'] = None
+    return res
diff --git a/mne/tests/test_cov.py b/mne/tests/test_cov.py
index 901f7ed..f1fe6aa 100644
--- a/mne/tests/test_cov.py
+++ b/mne/tests/test_cov.py
@@ -7,7 +7,7 @@ from scipy import linalg
 from .. import Covariance, Epochs, merge_events, \
                find_events, compute_raw_data_covariance, \
                compute_covariance
-from ..fiff import Raw
+from ..fiff import Raw, pick_channels_cov
 
 cov_fname = op.join(op.dirname(__file__), '..', 'fiff', 'tests', 'data',
                 'test-cov.fif')
@@ -27,6 +27,12 @@ def test_io_cov():
     cov2 = Covariance('cov.fif')
     assert_array_almost_equal(cov.data, cov2.data)
 
+    cov['bads'] = ['EEG 039']
+    cov_sel = pick_channels_cov(cov, exclude=cov['bads'])
+    assert_true(cov_sel['dim'] == (len(cov['data']) - len(cov['bads'])))
+    assert_true(cov_sel['data'].shape == (cov_sel['dim'], cov_sel['dim']))
+    cov_sel.save('cov.fif')
+
 
 def test_cov_estimation_on_raw_segment():
     """Estimate raw on continuous recordings (typically empty room)

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