[med-svn] [python-mne] 04/376: first working version of read_cov

Yaroslav Halchenko debian at onerussian.com
Fri Nov 27 17:21:55 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 bcccd7cb7d92a80c765197cb5d3e1155a62c60ab
Author: Alexandre Gramfort <alexandre.gramfort at inria.fr>
Date:   Mon Dec 27 19:37:42 2010 -0500

    first working version of read_cov
---
 examples/read_cov.py |  14 ++++++
 fiff/__init__.py     |   1 +
 fiff/channels.py     |  27 +++++++++++
 fiff/cov.py          | 111 +++++++++++++++++++++++++++++++++++++++++++++
 fiff/evoked.py       | 125 +--------------------------------------------------
 fiff/proj.py         | 104 ++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 259 insertions(+), 123 deletions(-)

diff --git a/examples/read_cov.py b/examples/read_cov.py
new file mode 100644
index 0000000..ee771f1
--- /dev/null
+++ b/examples/read_cov.py
@@ -0,0 +1,14 @@
+import pylab as pl
+import fiff
+
+fname = 'sm02a1-cov.fif'
+
+fid, tree, _ = fiff.fiff_open(fname)
+
+cov_type = 1
+cov = fiff.read_cov(fid, tree, cov_type)
+
+print "cov size: %s x %s" % cov['data'].shape
+
+pl.matshow(cov['data'])
+pl.show()
diff --git a/fiff/__init__.py b/fiff/__init__.py
index 51e92f4..b32fefb 100644
--- a/fiff/__init__.py
+++ b/fiff/__init__.py
@@ -1,3 +1,4 @@
 from .open import fiff_open
 from .evoked import read_evoked
+from .cov import read_cov
 from .constants import FIFF
diff --git a/fiff/channels.py b/fiff/channels.py
new file mode 100644
index 0000000..dcf2838
--- /dev/null
+++ b/fiff/channels.py
@@ -0,0 +1,27 @@
+from .tree import dir_tree_find
+from .tag import find_tag
+from .constants import FIFF
+
+
+def read_bad_channels(fid, node):
+    """
+    %
+    % [bads] = fiff_read_bad_channels(fid,node)
+    %
+    % Reas the bad channel list from a node if it exists
+    %
+    % fid      - The file id
+    % node     - The node of interes
+    %
+    """
+
+    nodes = dir_tree_find(node, FIFF.FIFFB_MNE_BAD_CHANNELS)
+
+    bads = [];
+    if len(nodes) > 0:
+        for node in nodes:
+            tag = find_tag(fid, node, FIFF.FIFF_MNE_CH_NAME_LIST)
+            if tag is not None:
+                bads = tag.data.split(':')
+    return bads
+
diff --git a/fiff/cov.py b/fiff/cov.py
new file mode 100644
index 0000000..3b6dabf
--- /dev/null
+++ b/fiff/cov.py
@@ -0,0 +1,111 @@
+import numpy as np
+
+from .constants import FIFF
+from .tag import find_tag
+from .tree import dir_tree_find
+from .proj import read_proj
+from .channels import read_bad_channels
+
+
+def read_cov(fid, node, cov_kind):
+    """
+    %
+    % [cov] = mne_read_cov(fid, node, kind)
+    %
+    % Reads a covariance matrix from a fiff file
+    %
+    % fid       - an open file descriptor
+    % node      - look for the matrix in here
+    % cov_kind  - what kind of a covariance matrix do we want?
+    %
+    """
+
+    #   Find all covariance matrices
+    covs = dir_tree_find(node, FIFF.FIFFB_MNE_COV)
+    if len(covs) == 0:
+        raise ValueError, 'No covariance matrices found'
+
+    #   Is any of the covariance matrices a noise covariance
+    for p in range(len(covs)):
+        tag = find_tag(fid, covs[p], FIFF.FIFF_MNE_COV_KIND)
+        if tag is not None and tag.data == cov_kind:
+            this = covs[p]
+
+            #   Find all the necessary data
+            tag = find_tag(fid, this, FIFF.FIFF_MNE_COV_DIM)
+            if tag is None:
+                raise ValueError, 'Covariance matrix dimension not found'
+
+            dim = tag.data
+            tag = find_tag(fid, this, FIFF.FIFF_MNE_COV_NFREE)
+            if tag is None:
+                nfree = -1
+            else:
+                nfree = tag.data
+
+            tag = find_tag(fid, this, FIFF.FIFF_MNE_ROW_NAMES)
+            if tag is None:
+                names = []
+            else:
+                names = tag.data.split(':')
+                if len(names) != dim:
+                    raise ValueError, 'Number of names does not match covariance matrix dimension'
+
+            tag = find_tag(fid, this, FIFF.FIFF_MNE_COV)
+            if tag is None:
+                tag = find_tag(fid, this, FIFF.FIFF_MNE_COV_DIAG)
+                if tag is None:
+                    raise ValueError, 'No covariance matrix data found'
+                else:
+                    #   Diagonal is stored
+                    data = tag.data
+                    diagmat = True
+                    print '\t%d x %d diagonal covariance (kind = %d) found.\n' % (dim, dim, cov_kind)
+
+            else:
+                from scipy import sparse
+                if not sparse.issparse(tag.data):
+                    #   Lower diagonal is stored
+                    vals = tag.data
+                    data = np.zeros((dim, dim))
+                    q = 0
+                    for j in range(dim):
+                        for k in range(j):
+                            data[j, k] = vals[q];
+                            q += 1
+                    data = data + data.T
+                    data.flat[::dim+1] /= 2.0
+
+                    diagmat = False;
+                    print '\t%d x %d full covariance (kind = %d) found.\n' % (dim, dim, cov_kind)
+                else:
+                    diagmat = False
+                    data = tag.data
+                    print '\t%d x %d sparse covariance (kind = %d) found.\n' % (dim, dim, cov_kind)
+
+            #   Read the possibly precomputed decomposition
+            tag1 = find_tag(fid, this, FIFF.FIFF_MNE_COV_EIGENVALUES)
+            tag2 = find_tag(fid, this, FIFF.FIFF_MNE_COV_EIGENVECTORS)
+            if tag1 is not None and tag2 is not None:
+                eig = tag1.data
+                eigvec = tag2.data
+            else:
+                eig = None
+                eigvec = None
+
+
+            #   Read the projection operator
+            projs = read_proj(fid, this)
+
+            #   Read the bad channel list
+            bads = read_bad_channels(fid, this)
+
+            #   Put it together
+            cov = dict(kind=cov_kind, diag=diagmat, dim=dim, names=names,
+                       data=data, projs=projs, bads=bads, nfree=nfree, eig=eig,
+                       eigvec=eigvec)
+            return cov
+
+    raise ValueError, 'Did not find the desired covariance matrix'
+
+    return None
diff --git a/fiff/evoked.py b/fiff/evoked.py
index 81f7dca..d9048dd 100644
--- a/fiff/evoked.py
+++ b/fiff/evoked.py
@@ -6,129 +6,8 @@ from .open import fiff_open
 from .ctf import read_ctf_comp
 from .tag import read_tag, find_tag
 from .tree import dir_tree_find
-
-
-def read_proj(fid, node):
-    """
-    [ projdata ] = fiff_read_proj(fid,node)
-
-     Read the SSP data under a given directory node
-
-    """
-
-    projdata = []
-
-    #   Locate the projection data
-    nodes = dir_tree_find(node, FIFF.FIFFB_PROJ)
-    if len(nodes) == 0:
-       return projdata
-
-    tag = find_tag(fid, nodes[0], FIFF.FIFF_NCHAN)
-    if tag is not None:
-        global_nchan = tag.data
-
-    items = dir_tree_find(nodes[0], FIFF.FIFFB_PROJ_ITEM)
-    for i in range(len(items)):
-
-        #   Find all desired tags in one item
-        item = items[i]
-        tag = find_tag(fid, item, FIFF.FIFF_NCHAN)
-        if tag is not None:
-            nchan = tag.data
-        else:
-            nchan = global_nchan
-
-        tag = find_tag(fid, item, FIFF.FIFF_DESCRIPTION)
-        if tag is not None:
-            desc = tag.data
-        else:
-            tag = find_tag(fid, item, FIFF.FIFF_NAME)
-            if tag is not None:
-                desc = tag.data
-            else:
-                raise ValueError, 'Projection item description missing'
-
-        tag = find_tag(fid, item, FIFF.FIFF_PROJ_ITEM_CH_NAME_LIST)
-        if tag is not None:
-            namelist = tag.data;
-        else:
-            raise ValueError, 'Projection item channel list missing'
-
-        tag = find_tag(fid, item,FIFF.FIFF_PROJ_ITEM_KIND);
-        if tag is not None:
-            kind = tag.data;
-        else:
-            raise ValueError, 'Projection item kind missing'
-
-        tag = find_tag(fid, item, FIFF.FIFF_PROJ_ITEM_NVEC)
-        if tag is not None:
-            nvec = tag.data
-        else:
-            raise ValueError, 'Number of projection vectors not specified'
-
-        tag = find_tag(fid, item, FIFF.FIFF_PROJ_ITEM_CH_NAME_LIST)
-        if tag is not None:
-            names = tag.data.split(':')
-        else:
-            raise ValueError, 'Projection item channel list missing'
-
-        tag = find_tag(fid, item, FIFF.FIFF_PROJ_ITEM_VECTORS);
-        if tag is not None:
-            data = tag.data;
-        else:
-            raise ValueError, 'Projection item data missing'
-
-        tag = find_tag(fid, item, FIFF.FIFF_MNE_PROJ_ITEM_ACTIVE);
-        if tag is not None:
-            active = tag.data;
-        else:
-            active = False;
-
-        if data.shape[1] != len(names):
-            raise ValueError, 'Number of channel names does not match the size of data matrix'
-
-        #   Use exactly the same fields in data as in a named matrix
-        one = Bunch(kind=kind, active=active, desc=desc,
-                    data=Bunch(nrow=nvec, ncol=nchan, row_names=None,
-                              col_names=names, data=data))
-
-        projdata.append(one)
-
-    if len(projdata) > 0:
-        print '\tRead a total of %d projection items:\n', len(projdata)
-        for k in range(len(projdata)):
-            print '\t\t%s (%d x %d)' % (projdata[k].desc,
-                                        projdata[k].data.nrow,
-                                        projdata[k].data.ncol)
-            if projdata[k].active:
-                print ' active\n'
-            else:
-                print ' idle\n'
-
-    return projdata
-
-
-def read_bad_channels(fid, node):
-    """
-    %
-    % [bads] = fiff_read_bad_channels(fid,node)
-    %
-    % Reas the bad channel list from a node if it exists
-    %
-    % fid      - The file id
-    % node     - The node of interes
-    %
-    """
-
-    nodes = dir_tree_find(node, FIFF.FIFFB_MNE_BAD_CHANNELS)
-
-    bads = [];
-    if len(nodes) > 0:
-        for node in nodes:
-            tag = find_tag(fid, node, FIFF.FIFF_MNE_CH_NAME_LIST)
-            if tag is not None:
-                bads = tag.data.split(':')
-    return bads
+from .proj import read_proj
+from .channels import read_bad_channels
 
 
 def read_meas_info(source, tree=None):
diff --git a/fiff/proj.py b/fiff/proj.py
new file mode 100644
index 0000000..089d07b
--- /dev/null
+++ b/fiff/proj.py
@@ -0,0 +1,104 @@
+from .tree import dir_tree_find
+from .constants import FIFF
+from .tag import find_tag
+from .bunch import Bunch
+
+
+def read_proj(fid, node):
+    """
+    [ projdata ] = fiff_read_proj(fid,node)
+
+     Read the SSP data under a given directory node
+
+    """
+
+    projdata = []
+
+    #   Locate the projection data
+    nodes = dir_tree_find(node, FIFF.FIFFB_PROJ)
+    if len(nodes) == 0:
+       return projdata
+
+    tag = find_tag(fid, nodes[0], FIFF.FIFF_NCHAN)
+    if tag is not None:
+        global_nchan = tag.data
+
+    items = dir_tree_find(nodes[0], FIFF.FIFFB_PROJ_ITEM)
+    for i in range(len(items)):
+
+        #   Find all desired tags in one item
+        item = items[i]
+        tag = find_tag(fid, item, FIFF.FIFF_NCHAN)
+        if tag is not None:
+            nchan = tag.data
+        else:
+            nchan = global_nchan
+
+        tag = find_tag(fid, item, FIFF.FIFF_DESCRIPTION)
+        if tag is not None:
+            desc = tag.data
+        else:
+            tag = find_tag(fid, item, FIFF.FIFF_NAME)
+            if tag is not None:
+                desc = tag.data
+            else:
+                raise ValueError, 'Projection item description missing'
+
+        tag = find_tag(fid, item, FIFF.FIFF_PROJ_ITEM_CH_NAME_LIST)
+        if tag is not None:
+            namelist = tag.data;
+        else:
+            raise ValueError, 'Projection item channel list missing'
+
+        tag = find_tag(fid, item,FIFF.FIFF_PROJ_ITEM_KIND);
+        if tag is not None:
+            kind = tag.data;
+        else:
+            raise ValueError, 'Projection item kind missing'
+
+        tag = find_tag(fid, item, FIFF.FIFF_PROJ_ITEM_NVEC)
+        if tag is not None:
+            nvec = tag.data
+        else:
+            raise ValueError, 'Number of projection vectors not specified'
+
+        tag = find_tag(fid, item, FIFF.FIFF_PROJ_ITEM_CH_NAME_LIST)
+        if tag is not None:
+            names = tag.data.split(':')
+        else:
+            raise ValueError, 'Projection item channel list missing'
+
+        tag = find_tag(fid, item, FIFF.FIFF_PROJ_ITEM_VECTORS);
+        if tag is not None:
+            data = tag.data;
+        else:
+            raise ValueError, 'Projection item data missing'
+
+        tag = find_tag(fid, item, FIFF.FIFF_MNE_PROJ_ITEM_ACTIVE);
+        if tag is not None:
+            active = tag.data;
+        else:
+            active = False;
+
+        if data.shape[1] != len(names):
+            raise ValueError, 'Number of channel names does not match the size of data matrix'
+
+        #   Use exactly the same fields in data as in a named matrix
+        one = Bunch(kind=kind, active=active, desc=desc,
+                    data=Bunch(nrow=nvec, ncol=nchan, row_names=None,
+                              col_names=names, data=data))
+
+        projdata.append(one)
+
+    if len(projdata) > 0:
+        print '\tRead a total of %d projection items:\n', len(projdata)
+        for k in range(len(projdata)):
+            print '\t\t%s (%d x %d)' % (projdata[k].desc,
+                                        projdata[k].data.nrow,
+                                        projdata[k].data.ncol)
+            if projdata[k].active:
+                print ' active\n'
+            else:
+                print ' idle\n'
+
+    return projdata

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