[med-svn] [python-mne] 50/376: adding Covariance object

Yaroslav Halchenko debian at onerussian.com
Fri Nov 27 17:22:05 UTC 2015


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

yoh pushed a commit to annotated tag v0.1
in repository python-mne.

commit 626003a3fba938024a0543ded51f79c43b81187d
Author: Alexandre Gramfort <alexandre.gramfort at inria.fr>
Date:   Tue Jan 25 10:00:51 2011 -0500

    adding Covariance object
---
 examples/plot_read_noise_covariance_matrix.py | 23 +++++++-----------
 mne/__init__.py                               |  2 +-
 mne/cov.py                                    | 34 +++++++++++++++++++++++++++
 3 files changed, 43 insertions(+), 16 deletions(-)

diff --git a/examples/plot_read_noise_covariance_matrix.py b/examples/plot_read_noise_covariance_matrix.py
index 7a3eee4..e5e1ce6 100644
--- a/examples/plot_read_noise_covariance_matrix.py
+++ b/examples/plot_read_noise_covariance_matrix.py
@@ -11,24 +11,17 @@ print __doc__
 
 import os
 import mne
-from mne import fiff
 
 fname = os.environ['MNE_SAMPLE_DATASET_PATH']
 fname += '/MEG/sample/sample_audvis-cov.fif'
 
-# Reading
-fid, tree, _ = fiff.fiff_open(fname)
-cov_type = 1
-cov = mne.read_cov(fid, tree, cov_type)
-fid.close()
+cov = mne.Covariance(kind='full')
+cov.load(fname)
 
-# Writing
-mne.write_cov_file('cov.fif', cov)
+print cov
 
-# print "covariance matrix size: %s x %s" % cov['data'].shape
-# 
-# ###############################################################################
-# # Show covariance
-# import pylab as pl
-# pl.matshow(cov['data'])
-# pl.show()
+###############################################################################
+# Show covariance
+import pylab as pl
+pl.matshow(cov.data)
+pl.show()
diff --git a/mne/__init__.py b/mne/__init__.py
index 331369d..95ae1fb 100644
--- a/mne/__init__.py
+++ b/mne/__init__.py
@@ -1,6 +1,6 @@
 __version__ = '0.1.git'
 
-from .cov import read_cov, write_cov, write_cov_file
+from .cov import read_cov, write_cov, write_cov_file, Covariance
 from .event import read_events, write_events
 from .forward import read_forward_solution
 from .stc import read_stc, write_stc
diff --git a/mne/cov.py b/mne/cov.py
index 096387c..d230503 100644
--- a/mne/cov.py
+++ b/mne/cov.py
@@ -15,6 +15,40 @@ from .fiff.channels import _read_bad_channels
 from .fiff.write import start_block, end_block, write_int, write_name_list, \
                        write_double, write_float_matrix, start_file, end_file
 from .fiff.proj import write_proj
+from .fiff import fiff_open
+
+
+class Covariance(object):
+    """Noise covariance matrix"""
+
+    _kinds = dict(full=1, sparse=2, diagonal=3) # XXX : check
+
+    def __init__(self, kind):
+        if kind in Covariance._kinds:
+            self.kind = Covariance._kinds[kind]
+        else:
+            raise ValueError, ('Unknown type of covariance. '
+                               'Choose between full, sparse or diagonal.')
+
+    def load(self, fname):
+        """load covariance matrix from FIF file"""
+
+        # Reading
+        fid, tree, _ = fiff_open(fname)
+        cov = read_cov(fid, tree, self.kind)
+        fid.close()
+
+        self._cov = cov
+        self.data = cov['data']
+
+    def save(self, fname):
+        """save covariance matrix in a FIF file"""
+        write_cov_file(fname, self._cov)
+
+    def __repr__(self):
+        s = "kind : %s" % self.kind
+        s += ", size : %s x %s" % self.data.shape
+        return "Covariance (%s)" % s
 
 
 def read_cov(fid, node, cov_kind):

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